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: package org.apache.axis2.deployment;
020:
021: import org.apache.axis2.AxisFault;
022: import org.apache.axis2.Constants;
023: import org.apache.axis2.util.Loader;
024: import org.apache.axis2.context.ConfigurationContext;
025: import org.apache.axis2.deployment.repository.util.ArchiveReader;
026: import org.apache.axis2.description.AxisServiceGroup;
027: import org.apache.axis2.description.Parameter;
028: import org.apache.axis2.engine.AxisConfiguration;
029: import org.apache.axis2.engine.AxisConfigurator;
030: import org.apache.axis2.transport.http.HTTPConstants;
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: import javax.servlet.ServletConfig;
035: import javax.xml.stream.XMLStreamException;
036: import java.io.File;
037: import java.io.FileInputStream;
038: import java.io.FileNotFoundException;
039: import java.io.IOException;
040: import java.io.InputStream;
041: import java.net.MalformedURLException;
042: import java.net.URL;
043: import java.util.HashMap;
044:
045: /**
046: * Processes the init parameters for the AxisServlet.
047: * This allows the location of the axis2.xml and the module repository to be different from the default locations.
048: * The init parameters support alternate file, or URL values for both of these.
049: */
050: public class WarBasedAxisConfigurator extends DeploymentEngine
051: implements AxisConfigurator {
052:
053: private static final Log log = LogFactory
054: .getLog(WarBasedAxisConfigurator.class);
055: private ServletConfig config;
056:
057: /**
058: * The name of the init parameter (axis2.xml.path) that can be used to override the default location for the axis2.xml file. When both this init parameter, and the axis2.xml.url init parameters are not specified in the axis servlet init-parameter, the default location of ${app}/WEB-INF/conf/axis2.xml is used.
059: * The value of this path is interpreted as a file system absolute path.
060: * This parameter takes precedence over the axis2.xml.url init parameter.
061: */
062: public static final String PARAM_AXIS2_XML_PATH = "axis2.xml.path";
063:
064: /**
065: * The name of the init parameter (axis2.xml.url) that when specified indicates the axis2.xml should be loaded using the URL specified as the value of this init parameter. If the axis2.xml.path init parameter is present, this init parameter has no effect.
066: */
067: public static final String PARAM_AXIS2_XML_URL = "axis2.xml.url";
068:
069: /**
070: * The name of the init parameter (axis2.repository.path) that when specified indicates the path to the
071: */
072: public static final String PARAM_AXIS2_REPOSITORY_PATH = "axis2.repository.path";
073:
074: /**
075: * The name of the init parameter (axis2.repository.url) that when specified indicates the url to be used
076: */
077: public static final String PARAM_AXIS2_REPOSITORY_URL = "axis2.repository.url";
078:
079: /**
080: * Default constructor for configurator.
081: * This determines the axis2.xml file to be used from the init parameters for the AxisServlet in the web.xml.
082: * The order of initialization is according the the following precedence:
083: * <ul>
084: * <li>If the parameter axis2.xml.path is present, the value is webapp relative path to be used as the location to the axis2.xml file.
085: * <li>Otherwise, if the parameter axis2.xml.url is present, the URL is used as the location to the axis2.xml file.
086: * <li>Otherwise, when both of the above init parameters are not present, file is attempted to be loaded from <repo>/WEB-INF/axis2.xml.
087: * <li> When none of the above could be found, the axis2.xml is loaded from the classpath resource, the value of DeploymenConstants.AXIS2_CONFIGURATION_RESOURCE.
088: * </ul>
089: *
090: * @param servletConfig the ServletConfig object from the AxisServlet. This method is called from the init() of the AxisServlet.
091: */
092: public WarBasedAxisConfigurator(ServletConfig servletConfig)
093: throws DeploymentException {
094: try {
095: this .config = servletConfig;
096: InputStream axis2Stream = null;
097: try {
098:
099: if (axis2Stream == null) {
100: String axis2xmlpath = config
101: .getInitParameter(PARAM_AXIS2_XML_PATH);
102: if (axis2xmlpath != null) {
103: // when init parameter was present.
104: axis2Stream = new FileInputStream(axis2xmlpath);
105: log.debug("using axis2.xml from path: "
106: + axis2xmlpath);
107: }
108: }
109:
110: if (axis2Stream == null) {
111: String axisurl = config
112: .getInitParameter(PARAM_AXIS2_XML_URL);
113: if (axisurl != null) {
114: axis2Stream = new URL(axisurl).openStream();
115: axisConfig = populateAxisConfiguration(axis2Stream);
116: log.debug("loading axis2.xml from URL: "
117: + axisurl);
118: }
119: }
120:
121: if (axis2Stream == null) {
122: // both the axis2.xml.path and axis2.xml.url init parameters were not present
123: // try to find the default /WEB-INF/conf/axis2.xml
124: axis2Stream = config.getServletContext()
125: .getResourceAsStream(
126: "/WEB-INF/conf/axis2.xml");
127: log
128: .debug("trying to load axis2.xml from module: /WEB-INF/conf/axis2.xml");
129: }
130:
131: if (axis2Stream == null) {
132: // Simple deployment, no need for conf directory either
133: axis2Stream = config.getServletContext()
134: .getResourceAsStream("/WEB-INF/axis2.xml");
135: log
136: .debug("trying to load axis2.xml from module: /WEB-INF/conf/axis2.xml");
137: }
138: } // try
139: catch (Exception e) {
140: log.error(e, e);
141: log
142: .warn("Using default configuration: "
143: + DeploymentConstants.AXIS2_CONFIGURATION_RESOURCE);
144: // not there, use default configuration from class path resource.
145: } // catch
146:
147: if (axis2Stream == null) {
148: log
149: .info("Could not find axis2.xml, loading default "
150: + DeploymentConstants.AXIS2_CONFIGURATION_RESOURCE
151: + " from classpath");
152: axis2Stream = Loader
153: .getResourceAsStream(DeploymentConstants.AXIS2_CONFIGURATION_RESOURCE);
154: }
155: axisConfig = populateAxisConfiguration(axis2Stream);
156:
157: if (axis2Stream != null) {
158: axis2Stream.close();
159: }
160: Parameter param = new Parameter();
161: param.setName(Constants.Configuration.ARTIFACTS_TEMP_DIR);
162: param.setValue(config.getServletContext().getAttribute(
163: "javax.servlet.context.tempdir"));
164: try {
165: axisConfig.addParameter(param);
166: } catch (AxisFault axisFault) {
167: log.error(axisFault.getMessage(), axisFault);
168: }
169:
170: // when the module is an unpacked war file,
171: // we can set the web location path in the deployment engine.
172: // This will let us
173: String webpath = config.getServletContext().getRealPath("");
174: if (webpath != null && !"".equals(webpath)) {
175: log.debug("setting web location string: " + webpath);
176: File weblocation = new File(webpath);
177: setWebLocationString(weblocation.getAbsolutePath());
178: } // if webpath not null
179:
180: } catch (DeploymentException e) {
181: log.error(e.getMessage(), e);
182: throw e;
183: } catch (IOException e) {
184: log.error(e.getMessage(), e);
185: }
186: }
187:
188: /**
189: * Gets the axis configuration object by loading the repository.
190: * The order of initialization is according the the following precedence:
191: * <ul>
192: * <li>If the parameter axis2.repository.path is present, this folder is used as the location to the repository.
193: * <li>Otherwise, if the parameter axis2.repository.url is present, the URL is used as the location to the repository.
194: * <li>Otherwise, when both of the above init parameters are not present, the web applications WEB-INF folder is used as the folder for the repository.
195: * </ul>
196: *
197: * @return the instance of the AxisConfiguration object that reflects the repository according to the rules above.
198: * @throws AxisFault when an error occurred in the initialization of the AxisConfiguration.
199: */
200: public AxisConfiguration getAxisConfiguration() throws AxisFault {
201: try {
202: String repository = null;
203:
204: if (repository == null) {
205: repository = config
206: .getInitParameter(PARAM_AXIS2_REPOSITORY_PATH);
207: if (repository != null) {
208: loadRepository(repository);
209: log.debug("loaded repository from path: "
210: + repository);
211: }
212: }
213:
214: if (repository == null) {
215: repository = config
216: .getInitParameter(PARAM_AXIS2_REPOSITORY_URL);
217: if (repository != null) {
218: loadRepositoryFromURL(new URL(repository));
219: log.debug("loaded repository from url: "
220: + repository);
221: }
222: }
223:
224: if (repository == null) {
225: if (config.getServletContext().getRealPath("") != null) {
226: // this is an unpacked war file
227: repository = config.getServletContext()
228: .getRealPath("/WEB-INF");
229: }
230: if (repository != null) {
231: loadRepository(repository);
232: log
233: .debug("loaded repository from /WEB-INF folder (unpacked war)");
234: }
235: }
236:
237: if (repository == null) {
238: URL url = config.getServletContext().getResource(
239: "/WEB-INF/");
240: if (url != null) {
241: repository = url.toString();
242: loadRepositoryFromURL(url);
243: log
244: .debug("loaded repository from /WEB-INF/ folder (URL)");
245: }
246: }
247:
248: if (repository == null) {
249: loadFromClassPath();
250: log.debug("loaded repository from classpath");
251: }
252:
253: } catch (Exception ex) {
254: log.error(ex + ": loading repository from classpath", ex);
255: loadFromClassPath();
256: }
257: axisConfig.setConfigurator(this );
258: return axisConfig;
259: }
260:
261: //to load services
262:
263: /**
264: * Loads the services within the repository.
265: * When the axis2.repository.path init parameter was present, we just call loadServices() in the deployment engine.<br/>
266: * When the axis2.repository.url init parameter was present we load services from the respective URL value of the init parameter.<br/>
267: * Otherwise, try to load the services from the /WEB-INF folder within the web application.
268: */
269: public void loadServices() {
270: try {
271: String repository;
272:
273: repository = config
274: .getInitParameter(PARAM_AXIS2_REPOSITORY_PATH);
275: if (repository != null) {
276: super .loadServices();
277: log.debug("loaded services from path: " + repository);
278: return;
279: }
280:
281: repository = config
282: .getInitParameter(PARAM_AXIS2_REPOSITORY_URL);
283: if (repository != null) {
284: loadServicesFromUrl(new URL(repository));
285: log.debug("loaded services from URL: " + repository);
286: return;
287: }
288: loadServicesFromWebInf();
289: if (config.getServletContext().getRealPath("") != null) {
290: super .loadServices();
291: log.debug("loaded services from webapp");
292: return;
293: }
294:
295: URL url = config.getServletContext().getResource(
296: "/WEB-INF/");
297: if (url != null) {
298: loadServicesFromUrl(url);
299: log
300: .debug("loaded services from /WEB-INF/ folder (URL)");
301: }
302: } catch (MalformedURLException e) {
303: log.info(e.getMessage());
304: }
305: }
306:
307: //To engage globally listed modules
308: public void engageGlobalModules() throws AxisFault {
309: engageModules();
310: }
311:
312: /**
313: * This method will look inside the web-inf directory to find services.xml
314: * inside that , if it is there will load that and creat service group out
315: * of that and add into axisConfig. User can drop corresponding class files
316: * into class directory.
317: */
318: private void loadServicesFromWebInf() {
319: try {
320: InputStream servicexml = config.getServletContext()
321: .getResourceAsStream("/WEB-INF/services.xml");
322: if (servicexml != null) {
323: HashMap wsdlServices = new HashMap();
324: ArchiveReader archiveReader = new ArchiveReader();
325: String path = config.getServletContext().getRealPath(
326: "/WEB-INF");
327: if (path != null) {
328: archiveReader.processFilesInFolder(new File(path),
329: wsdlServices);
330: }
331: AxisServiceGroup serviceGroup = DeploymentEngine
332: .buildServiceGroup(servicexml, Thread
333: .currentThread()
334: .getContextClassLoader(),
335: "annonServiceGroup", configContext,
336: archiveReader, wsdlServices);
337: axisConfig.addServiceGroup(serviceGroup);
338: }
339: } catch (AxisFault axisFault) {
340: log.info(axisFault);
341: } catch (FileNotFoundException e) {
342: log.info(e);
343: } catch (XMLStreamException e) {
344: log.info(e);
345: }
346: }
347:
348: public void setConfigContext(ConfigurationContext configContext) {
349: super .setConfigContext(configContext);
350:
351: // setting ServletContext into configctx
352: configContext.setProperty(HTTPConstants.MC_HTTP_SERVLETCONTEXT,
353: config.getServletContext());
354: // setting ServletContext into configctx
355: configContext.setProperty(HTTPConstants.MC_HTTP_SERVLETCONTEXT,
356: config.getServletContext());
357: Parameter servletConfigParam = new Parameter();
358: servletConfigParam.setName(HTTPConstants.HTTP_SERVLETCONFIG);
359: servletConfigParam.setValue(config);
360: try {
361: configContext.getAxisConfiguration().addParameter(
362: servletConfigParam);
363: } catch (AxisFault axisFault) {
364: log.error(axisFault.getMessage(), axisFault);
365: }
366: }
367: }
|