001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.context;
021:
022: import org.apache.axis2.AxisFault;
023: import org.apache.axis2.Constants;
024: import org.apache.axis2.deployment.*;
025: import org.apache.axis2.deployment.util.Utils;
026: import org.apache.axis2.description.AxisModule;
027: import org.apache.axis2.description.AxisServiceGroup;
028: import org.apache.axis2.description.Parameter;
029: import org.apache.axis2.description.TransportOutDescription;
030: import org.apache.axis2.engine.AxisConfiguration;
031: import org.apache.axis2.engine.AxisConfigurator;
032: import org.apache.axis2.engine.DependencyManager;
033: import org.apache.axis2.i18n.Messages;
034: import org.apache.axis2.modules.Module;
035: import org.apache.axis2.transport.TransportSender;
036: import org.apache.axis2.util.SessionUtils;
037: import org.apache.axis2.util.Loader;
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040:
041: import java.io.InputStream;
042: import java.net.URL;
043: import java.util.Collection;
044: import java.util.HashMap;
045: import java.util.Iterator;
046: import java.util.Map;
047:
048: public class ConfigurationContextFactory {
049:
050: protected static final Log log = LogFactory
051: .getLog(ConfigurationContextFactory.class);
052:
053: /**
054: * Creates a AxisConfiguration depending on the user requirement.
055: * First creates an AxisConfigurator object with appropriate parameters.
056: * Depending on the implementation getAxisConfiguration(), gets
057: * the AxisConfiguration and uses it to create the ConfigurationContext.
058: *
059: * @param axisConfigurator : AxisConfigurator
060: * @return Returns ConfigurationContext.
061: * @throws AxisFault : If somthing goes wrong
062: */
063: public static ConfigurationContext createConfigurationContext(
064: AxisConfigurator axisConfigurator) throws AxisFault {
065: AxisConfiguration axisConfig = axisConfigurator
066: .getAxisConfiguration();
067: ConfigurationContext configContext = new ConfigurationContext(
068: axisConfig);
069:
070: if (axisConfig.getClusterManager() != null) {
071: configContext.initCluster();
072: }
073:
074: if (axisConfigurator instanceof DeploymentEngine) {
075: ((DeploymentEngine) axisConfigurator)
076: .setConfigContext(configContext);
077: }
078: //To override context path
079: setContextPaths(axisConfig, configContext);
080: init(configContext);
081: axisConfigurator.engageGlobalModules();
082: axisConfigurator.loadServices();
083: addModuleService(configContext);
084:
085: // TODO: THIS NEEDS A TEST CASE!
086: initApplicationScopeServices(configContext);
087:
088: axisConfig.setStart(true);
089: return configContext;
090: }
091:
092: private static void initApplicationScopeServices(
093: ConfigurationContext configCtx) throws AxisFault {
094: Iterator serviceGroups = configCtx.getAxisConfiguration()
095: .getServiceGroups();
096: while (serviceGroups.hasNext()) {
097: AxisServiceGroup axisServiceGroup = (AxisServiceGroup) serviceGroups
098: .next();
099: String maxScope = SessionUtils
100: .calculateMaxScopeForServiceGroup(axisServiceGroup);
101: if (Constants.SCOPE_APPLICATION.equals(maxScope)) {
102: ServiceGroupContext serviceGroupContext = configCtx
103: .createServiceGroupContext(axisServiceGroup);
104: configCtx
105: .addServiceGroupContextIntoApplicationScopeTable(serviceGroupContext);
106: DependencyManager.initService(serviceGroupContext);
107: }
108: }
109: }
110:
111: private static void addModuleService(ConfigurationContext configCtx)
112: throws AxisFault {
113: AxisConfiguration axisConfig = configCtx.getAxisConfiguration();
114: HashMap modules = axisConfig.getModules();
115: if (modules != null && modules.size() > 0) {
116: Iterator mpduleItr = modules.values().iterator();
117: while (mpduleItr.hasNext()) {
118: AxisModule axisModule = (AxisModule) mpduleItr.next();
119: Utils.deployModuleServices(axisModule, configCtx);
120: }
121: }
122: }
123:
124: private static void setContextPaths(AxisConfiguration axisConfig,
125: ConfigurationContext configContext) {
126: // Checking for context path
127: Parameter servicePath = axisConfig
128: .getParameter(Constants.PARAM_SERVICE_PATH);
129: if (servicePath != null) {
130: String spath = ((String) servicePath.getValue()).trim();
131: if (spath.length() > 0) {
132: configContext.setServicePath(spath);
133: }
134: } else {
135: configContext
136: .setServicePath(Constants.DEFAULT_SERVICES_PATH);
137: }
138:
139: Parameter contextPath = axisConfig
140: .getParameter(Constants.PARAM_CONTEXT_ROOT);
141: if (contextPath != null) {
142: String cpath = ((String) contextPath.getValue()).trim();
143: if (cpath.length() > 0) {
144: configContext.setContextRoot(cpath);
145: }
146: } else {
147: configContext.setContextRoot("axis2");
148: }
149: }
150:
151: /**
152: * To get a ConfigurationContext for given data , and underline implementation
153: * is Axis2 default impl which is file system based deployment model to create
154: * an AxisConfiguration.
155: * <p/>
156: * Here either or both parameter can be null. So that boil down to following
157: * scenarios and it should note that parameter value should be full path ,
158: * you are not allowed to give one relative to other. And these two can be located
159: * in completely different locations.
160: * <ul>
161: * <li>If none of them are null , then AxisConfiguration will be based on the
162: * value of axis2xml , and the repository will be the value specified by the
163: * path parameter and there will not be any assumptions.</li>
164: * <li>If axis2xml is null , then the repository will be the value specfied by
165: * path parameter and AxisConfiguration will be created using default_axis2.xml</li>
166: * <li>If path parameter is null , then AxisConfiguration will be created using
167: * that axis2.xml. And after creating AxisConfiguration system will try to
168: * find user has specified repository parameter in axis2.xml
169: * (<parameter name="repository">location of the repo</parameter>) , if it
170: * find that then repository will be the value specified by that parameter.</li>
171: * <li>If both are null , then it is simple , AixsConfiguration will be created
172: * using default_axis2.xml and thats it.</li>
173: * </ul>
174: * <p/>
175: * Note : rather than passing any parameters you can give them as System
176: * properties. Simple you can add following system properties before
177: * you call this.
178: * <ul>
179: * <li>axis2.repo : same as path parameter</li>
180: * <li>axis2.xml : same as axis2xml</li>
181: * </ul>
182: *
183: * @param path : location of the repository
184: * @param axis2xml : location of the axis2.xml (configuration) , you can not give
185: * axis2xml relative to repository.
186: * @return Returns the built ConfigurationContext.
187: * @throws AxisFault in case of problems
188: */
189: public static ConfigurationContext createConfigurationContextFromFileSystem(
190: String path, String axis2xml) throws AxisFault {
191: return createConfigurationContext(new FileSystemConfigurator(
192: path, axis2xml));
193: }
194:
195: public static ConfigurationContext createConfigurationContextFromFileSystem(
196: String path) throws AxisFault {
197: return createConfigurationContextFromFileSystem(path, null);
198: }
199:
200: public static ConfigurationContext createConfigurationContextFromURIs(
201: URL axis2xml, URL repositoy) throws AxisFault {
202: return createConfigurationContext(new URLBasedAxisConfigurator(
203: axis2xml, repositoy));
204: }
205:
206: /**
207: * Initializes modules and creates Transports.
208: *
209: * @param configContext ConfigurationContext
210: */
211:
212: private static void init(ConfigurationContext configContext) {
213: initModules(configContext);
214: initTransportSenders(configContext);
215: }
216:
217: /**
218: * Initializes the modules. If the module needs to perform some recovery process
219: * it can do so in init and this is different from module.engage().
220: *
221: * @param context : ConfigurationContext
222: */
223: private static void initModules(ConfigurationContext context) {
224: AxisConfiguration configuration = context
225: .getAxisConfiguration();
226: HashMap modules = configuration.getModules();
227: Collection col = modules.values();
228: Map faultyModule = new HashMap();
229:
230: for (Iterator iterator = col.iterator(); iterator.hasNext();) {
231: AxisModule axismodule = (AxisModule) iterator.next();
232: Module module = axismodule.getModule();
233:
234: if (module != null) {
235: try {
236: module.init(context, axismodule);
237: } catch (AxisFault axisFault) {
238: log.info(axisFault.getMessage());
239: faultyModule.put(axismodule, axisFault);
240: }
241: }
242: }
243:
244: //Checking whether we have found any faulty services during the module initilization ,
245: // if so we need to mark them as fautyModule and need to remove from the modules list
246: if (faultyModule.size() > 0) {
247: Iterator axisModules = faultyModule.keySet().iterator();
248: while (axisModules.hasNext()) {
249: AxisModule axisModule = (AxisModule) axisModules.next();
250: String fileName;
251: if (axisModule.getFileName() != null) {
252: fileName = axisModule.getFileName().toString();
253: } else {
254: fileName = axisModule.getName();
255: }
256: configuration.getFaultyModules().put(fileName,
257: faultyModule.get(axisModule));
258: //removing from original list
259: configuration.removeModule(axisModule.getName(),
260: axisModule.getName());
261: }
262: }
263:
264: }
265:
266: /**
267: * Initializes TransportSenders and TransportListeners with appropriate configuration information
268: *
269: * @param configContext : ConfigurationContext
270: */
271: private static void initTransportSenders(
272: ConfigurationContext configContext) {
273: AxisConfiguration axisConf = configContext
274: .getAxisConfiguration();
275:
276: // Initialize Transport Outs
277: HashMap transportOuts = axisConf.getTransportsOut();
278:
279: Iterator values = transportOuts.values().iterator();
280:
281: while (values.hasNext()) {
282: TransportOutDescription transportOut = (TransportOutDescription) values
283: .next();
284: TransportSender sender = transportOut.getSender();
285:
286: if (sender != null) {
287: try {
288: sender.init(configContext, transportOut);
289: } catch (AxisFault axisFault) {
290: log.info(Messages.getMessage("transportiniterror",
291: transportOut.getName()));
292: }
293: }
294: }
295: }
296:
297: /**
298: * creates an empty configuration context.
299: *
300: * @return Returns ConfigurationContext.
301: */
302: public static ConfigurationContext createEmptyConfigurationContext()
303: throws AxisFault {
304: AxisConfiguration axisConfiguration = new AxisConfiguration();
305: ConfigurationContext configContext = new ConfigurationContext(
306: axisConfiguration);
307: if (axisConfiguration.getClusterManager() != null) {
308: configContext.initCluster();
309: }
310:
311: setContextPaths(axisConfiguration, configContext);
312: return configContext;
313: }
314:
315: /**
316: * Gets the default configuration context by using Axis2.xml in the classpath
317: *
318: * @return Returns ConfigurationContext.
319: */
320: public static ConfigurationContext createDefaultConfigurationContext()
321: throws Exception {
322: return createBasicConfigurationContext(DeploymentConstants.AXIS2_CONFIGURATION_RESOURCE);
323: }
324:
325: /**
326: * Creates configuration context using resource file found in the classpath.
327: *
328: * @return Returns ConfigurationContext.
329: */
330: public static ConfigurationContext createBasicConfigurationContext(
331: String resourceName) throws Exception {
332: InputStream in = Loader.getResourceAsStream(resourceName);
333:
334: AxisConfiguration axisConfig = new AxisConfiguration();
335: AxisConfigBuilder builder = new AxisConfigBuilder(in,
336: axisConfig, null);
337: builder.populateConfig();
338: axisConfig.validateSystemPredefinedPhases();
339: ConfigurationContext configContext = new ConfigurationContext(
340: axisConfig);
341:
342: if (axisConfig.getClusterManager() != null) {
343: configContext.initCluster();
344: }
345:
346: setContextPaths(axisConfig, configContext);
347: return configContext;
348: }
349: }
|