1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.axis2.context;
21
22 import org.apache.axis2.AxisFault;
23 import org.apache.axis2.Constants;
24 import org.apache.axis2.deployment;
25 import org.apache.axis2.deployment.util.Utils;
26 import org.apache.axis2.description.AxisModule;
27 import org.apache.axis2.description.AxisServiceGroup;
28 import org.apache.axis2.description.Parameter;
29 import org.apache.axis2.description.TransportOutDescription;
30 import org.apache.axis2.engine.AxisConfiguration;
31 import org.apache.axis2.engine.AxisConfigurator;
32 import org.apache.axis2.engine.DependencyManager;
33 import org.apache.axis2.i18n.Messages;
34 import org.apache.axis2.modules.Module;
35 import org.apache.axis2.transport.TransportSender;
36 import org.apache.axis2.util.Loader;
37 import org.apache.axis2.util.SessionUtils;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41 import java.io.InputStream;
42 import java.net.URL;
43 import java.util.Collection;
44 import java.util.HashMap;
45 import java.util.Iterator;
46 import java.util.Map;
47
48 public class ConfigurationContextFactory {
49
50 protected static final Log log = LogFactory.getLog(ConfigurationContextFactory.class);
51
52 /**
53 * Creates a AxisConfiguration depending on the user requirement.
54 * First creates an AxisConfigurator object with appropriate parameters.
55 * Depending on the implementation getAxisConfiguration(), gets
56 * the AxisConfiguration and uses it to create the ConfigurationContext.
57 *
58 * @param axisConfigurator : AxisConfigurator
59 * @return Returns ConfigurationContext.
60 * @throws AxisFault : If somthing goes wrong
61 */
62 public static ConfigurationContext createConfigurationContext(
63 AxisConfigurator axisConfigurator) throws AxisFault {
64 AxisConfiguration axisConfig = axisConfigurator.getAxisConfiguration();
65 // call to the deployment listners
66 Parameter param = axisConfig.getParameter(Constants.Configuration.DEPLOYMENT_LIFE_CYCLE_LISTENER);
67 DeploymentLifeCycleListener deploymentLifeCycleListener = null;
68 if (param != null){
69 String className = (String) param.getValue();
70 try {
71 deploymentLifeCycleListener = (DeploymentLifeCycleListener) Class.forName(className).newInstance();
72 } catch (InstantiationException e) {
73 log.error("Can not instantiate deployment Listener " + className, e);
74 throw new AxisFault("Can not instantiate deployment Listener " + className);
75 } catch (IllegalAccessException e) {
76 log.error("Illegal Access deployment Listener " + className, e);
77 throw new AxisFault("Illegal Access deployment Listener " + className);
78 } catch (ClassNotFoundException e) {
79 log.error("Class not found deployment Listener " + className, e);
80 throw new AxisFault("Class not found deployment Listener " + className);
81 }
82 }
83 if (deploymentLifeCycleListener != null){
84 deploymentLifeCycleListener.preDeploy(axisConfig);
85 }
86 ConfigurationContext configContext = new ConfigurationContext(axisConfig);
87
88 if (axisConfigurator instanceof DeploymentEngine) {
89 ((DeploymentEngine) axisConfigurator).setConfigContext(configContext);
90 }
91 //To override context path
92 setContextPaths(axisConfig, configContext);
93 init(configContext);
94 axisConfigurator.engageGlobalModules();
95 axisConfigurator.loadServices();
96 addModuleService(configContext);
97
98 // TODO: THIS NEEDS A TEST CASE!
99 initApplicationScopeServices(configContext);
100
101 axisConfig.setStart(true);
102 if (deploymentLifeCycleListener != null){
103 deploymentLifeCycleListener.postDeploy(configContext);
104 }
105
106 // Finally initialize the cluster
107 if (axisConfig.getClusterManager() != null) {
108 configContext.initCluster();
109 }
110
111 return configContext;
112 }
113
114 private static void initApplicationScopeServices(ConfigurationContext configCtx)
115 throws AxisFault {
116 Iterator serviceGroups = configCtx.getAxisConfiguration().getServiceGroups();
117 while (serviceGroups.hasNext()) {
118 AxisServiceGroup axisServiceGroup = (AxisServiceGroup) serviceGroups.next();
119 String maxScope = SessionUtils.calculateMaxScopeForServiceGroup(axisServiceGroup);
120 if (Constants.SCOPE_APPLICATION.equals(maxScope)) {
121 ServiceGroupContext serviceGroupContext =
122 configCtx.createServiceGroupContext(axisServiceGroup);
123 configCtx.addServiceGroupContextIntoApplicationScopeTable(serviceGroupContext);
124 DependencyManager.initService(serviceGroupContext);
125 }
126 }
127 }
128
129 private static void addModuleService(ConfigurationContext configCtx) throws AxisFault {
130 AxisConfiguration axisConfig = configCtx.getAxisConfiguration();
131 HashMap modules = axisConfig.getModules();
132 if (modules != null && modules.size() > 0) {
133 Iterator mpduleItr = modules.values().iterator();
134 while (mpduleItr.hasNext()) {
135 AxisModule axisModule = (AxisModule) mpduleItr.next();
136 Utils.deployModuleServices(axisModule, configCtx);
137 }
138 }
139 }
140
141 private static void setContextPaths(AxisConfiguration axisConfig,
142 ConfigurationContext configContext) {
143 // Checking for context path
144 Parameter servicePath = axisConfig.getParameter(Constants.PARAM_SERVICE_PATH);
145 if (servicePath != null) {
146 String spath = ((String) servicePath.getValue()).trim();
147 if (spath.length() > 0) {
148 configContext.setServicePath(spath);
149 }
150 } else {
151 configContext.setServicePath(Constants.DEFAULT_SERVICES_PATH);
152 }
153
154 Parameter contextPath = axisConfig.getParameter(Constants.PARAM_CONTEXT_ROOT);
155 if (contextPath != null) {
156 String cpath = ((String) contextPath.getValue()).trim();
157 if (cpath.length() > 0) {
158 configContext.setContextRoot(cpath);
159 }
160 } else {
161 configContext.setContextRoot("axis2");
162 }
163 }
164
165 /**
166 * To get a ConfigurationContext for given data , and underline implementation
167 * is Axis2 default impl which is file system based deployment model to create
168 * an AxisConfiguration.
169 * <p/>
170 * Here either or both parameter can be null. So that boil down to following
171 * scenarios and it should note that parameter value should be full path ,
172 * you are not allowed to give one relative to other. And these two can be located
173 * in completely different locations.
174 * <ul>
175 * <li>If none of them are null , then AxisConfiguration will be based on the
176 * value of axis2xml , and the repository will be the value specified by the
177 * path parameter and there will not be any assumptions.</li>
178 * <li>If axis2xml is null , then the repository will be the value specfied by
179 * path parameter and AxisConfiguration will be created using default_axis2.xml</li>
180 * <li>If path parameter is null , then AxisConfiguration will be created using
181 * that axis2.xml. And after creating AxisConfiguration system will try to
182 * find user has specified repository parameter in axis2.xml
183 * (<parameter name="repository">location of the repo</parameter>) , if it
184 * find that then repository will be the value specified by that parameter.</li>
185 * <li>If both are null , then it is simple , AixsConfiguration will be created
186 * using default_axis2.xml and thats it.</li>
187 * </ul>
188 * <p/>
189 * Note : rather than passing any parameters you can give them as System
190 * properties. Simple you can add following system properties before
191 * you call this.
192 * <ul>
193 * <li>axis2.repo : same as path parameter</li>
194 * <li>axis2.xml : same as axis2xml</li>
195 * </ul>
196 *
197 * @param path : location of the repository
198 * @param axis2xml : location of the axis2.xml (configuration) , you can not give
199 * axis2xml relative to repository.
200 * @return Returns the built ConfigurationContext.
201 * @throws AxisFault in case of problems
202 */
203 public static ConfigurationContext createConfigurationContextFromFileSystem(
204 String path,
205 String axis2xml) throws AxisFault {
206 return createConfigurationContext(new FileSystemConfigurator(path, axis2xml));
207 }
208
209 public static ConfigurationContext createConfigurationContextFromFileSystem(String path)
210 throws AxisFault {
211 return createConfigurationContextFromFileSystem(path, null);
212 }
213
214 public static ConfigurationContext createConfigurationContextFromURIs(
215 URL axis2xml, URL repositoy) throws AxisFault {
216 return createConfigurationContext(new URLBasedAxisConfigurator(axis2xml, repositoy));
217 }
218
219 /**
220 * Initializes modules and creates Transports.
221 *
222 * @param configContext ConfigurationContext
223 */
224
225 private static void init(ConfigurationContext configContext) {
226 initModules(configContext);
227 initTransportSenders(configContext);
228 }
229
230 /**
231 * Initializes the modules. If the module needs to perform some recovery process
232 * it can do so in init and this is different from module.engage().
233 *
234 * @param context : ConfigurationContext
235 */
236 private static void initModules(ConfigurationContext context) {
237 AxisConfiguration configuration = context.getAxisConfiguration();
238 HashMap modules = configuration.getModules();
239 Collection col = modules.values();
240 Map faultyModule = new HashMap();
241
242 for (Iterator iterator = col.iterator(); iterator.hasNext();) {
243 AxisModule axismodule = (AxisModule) iterator.next();
244 Module module = axismodule.getModule();
245
246 if (module != null) {
247 try {
248 module.init(context, axismodule);
249 } catch (AxisFault axisFault) {
250 log.info(axisFault.getMessage());
251 faultyModule.put(axismodule, axisFault);
252 }
253 }
254 }
255
256 //Checking whether we have found any faulty services during the module initilization ,
257 // if so we need to mark them as fautyModule and need to remove from the modules list
258 if (faultyModule.size() > 0) {
259 Iterator axisModules = faultyModule.keySet().iterator();
260 while (axisModules.hasNext()) {
261 AxisModule axisModule = (AxisModule) axisModules.next();
262 String fileName;
263 if (axisModule.getFileName() != null) {
264 fileName = axisModule.getFileName().toString();
265 } else {
266 fileName = axisModule.getName();
267 }
268 configuration.getFaultyModules().put(fileName, faultyModule.get(axisModule).toString());
269 //removing from original list
270 configuration.removeModule(axisModule.getName(), axisModule.getName());
271 }
272 }
273
274
275 }
276
277 /**
278 * Initializes TransportSenders and TransportListeners with appropriate configuration information
279 *
280 * @param configContext : ConfigurationContext
281 */
282 private static void initTransportSenders(ConfigurationContext configContext) {
283 AxisConfiguration axisConf = configContext.getAxisConfiguration();
284
285 // Initialize Transport Outs
286 HashMap transportOuts = axisConf.getTransportsOut();
287
288 Iterator values = transportOuts.values().iterator();
289
290 while (values.hasNext()) {
291 TransportOutDescription transportOut = (TransportOutDescription) values.next();
292 TransportSender sender = transportOut.getSender();
293
294 if (sender != null) {
295 try {
296 sender.init(configContext, transportOut);
297 } catch (AxisFault axisFault) {
298 log.info(Messages.getMessage("transportiniterror", transportOut.getName()));
299 }
300 }
301 }
302 }
303
304 /**
305 * creates an empty configuration context.
306 *
307 * @return Returns ConfigurationContext.
308 */
309 public static ConfigurationContext createEmptyConfigurationContext() throws AxisFault {
310 AxisConfiguration axisConfiguration = new AxisConfiguration();
311 ConfigurationContext configContext = new ConfigurationContext(axisConfiguration);
312 if (axisConfiguration.getClusterManager() != null) {
313 configContext.initCluster();
314 }
315
316 setContextPaths(axisConfiguration, configContext);
317 return configContext;
318 }
319
320 /**
321 * Gets the default configuration context by using Axis2.xml in the classpath
322 *
323 * @return Returns ConfigurationContext.
324 */
325 public static ConfigurationContext createDefaultConfigurationContext() throws Exception {
326 return createBasicConfigurationContext(DeploymentConstants.AXIS2_CONFIGURATION_RESOURCE);
327 }
328
329 /**
330 * Creates configuration context using resource file found in the classpath.
331 *
332 * @return Returns ConfigurationContext.
333 */
334 public static ConfigurationContext createBasicConfigurationContext(String resourceName) throws Exception {
335 InputStream in = Loader.getResourceAsStream(resourceName);
336
337 AxisConfiguration axisConfig = new AxisConfiguration();
338 AxisConfigBuilder builder = new AxisConfigBuilder(in, axisConfig, null);
339 builder.populateConfig();
340 axisConfig.validateSystemPredefinedPhases();
341 ConfigurationContext configContext = new ConfigurationContext(axisConfig);
342
343 if (axisConfig.getClusterManager() != null) {
344 configContext.initCluster();
345 }
346
347 setContextPaths(axisConfig, configContext);
348 return configContext;
349 }
350 }