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.deployment;
21
22 import org.apache.axis2.AxisFault;
23 import org.apache.axis2.Constants;
24 import org.apache.axis2.context.ConfigurationContext;
25 import org.apache.axis2.deployment.repository.util.ArchiveReader;
26 import org.apache.axis2.description.AxisServiceGroup;
27 import org.apache.axis2.description.Parameter;
28 import org.apache.axis2.engine.AxisConfiguration;
29 import org.apache.axis2.engine.AxisConfigurator;
30 import org.apache.axis2.transport.http.HTTPConstants;
31 import org.apache.axis2.util.Loader;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import javax.servlet.ServletConfig;
36 import javax.xml.stream.XMLStreamException;
37 import java.io.File;
38 import java.io.FileInputStream;
39 import java.io.FileNotFoundException;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.net.MalformedURLException;
43 import java.net.URL;
44 import java.util.HashMap;
45
46 /**
47 * Processes the init parameters for the AxisServlet.
48 * This allows the location of the axis2.xml and the module repository to be different from the default locations.
49 * The init parameters support alternate file, or URL values for both of these.
50 */
51 public class WarBasedAxisConfigurator extends DeploymentEngine implements AxisConfigurator {
52
53 private static final Log log = LogFactory.getLog(WarBasedAxisConfigurator.class);
54 private ServletConfig config;
55
56 /**
57 * 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.
58 * The value of this path is interpreted as a file system absolute path.
59 * This parameter takes precedence over the axis2.xml.url init parameter.
60 */
61 public static final String PARAM_AXIS2_XML_PATH = "axis2.xml.path";
62
63
64 /**
65 * 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.
66 */
67 public static final String PARAM_AXIS2_XML_URL = "axis2.xml.url";
68
69
70 /**
71 * The name of the init parameter (axis2.repository.path) that when specified indicates the path to the
72 */
73 public static final String PARAM_AXIS2_REPOSITORY_PATH = "axis2.repository.path";
74
75
76 /**
77 * The name of the init parameter (axis2.repository.url) that when specified indicates the url to be used
78 */
79 public static final String PARAM_AXIS2_REPOSITORY_URL = "axis2.repository.url";
80
81
82 /**
83 * Default constructor for configurator.
84 * This determines the axis2.xml file to be used from the init parameters for the AxisServlet in the web.xml.
85 * The order of initialization is according the the following precedence:
86 * <ul>
87 * <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.
88 * <li>Otherwise, if the parameter axis2.xml.url is present, the URL is used as the location to the axis2.xml file.
89 * <li>Otherwise, when both of the above init parameters are not present, file is attempted to be loaded from <repo>/WEB-INF/axis2.xml.
90 * <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.
91 * </ul>
92 *
93 * @param servletConfig the ServletConfig object from the AxisServlet. This method is called from the init() of the AxisServlet.
94 */
95 public WarBasedAxisConfigurator(ServletConfig servletConfig) throws DeploymentException {
96 try {
97 this.config = servletConfig;
98 InputStream axis2Stream = null;
99 try {
100 // when the module is an unpacked war file,
101 // we can set the web location path in the deployment engine.
102 // This will let us
103 String webpath = config.getServletContext().getRealPath("");
104 if (webpath == null || webpath.length() == 0) {
105 webpath = config.getServletContext().getRealPath("/");
106 }
107 if (webpath != null && !"".equals(webpath)) {
108 log.debug("setting web location string: " + webpath);
109 File weblocation = new File(webpath);
110 setWebLocationString(weblocation.getAbsolutePath());
111 } // if webpath not null
112
113 String axis2xmlpath = config.getInitParameter(PARAM_AXIS2_XML_PATH);
114 if (axis2xmlpath != null) {
115 // when init parameter was present.
116 axis2Stream = new FileInputStream(axis2xmlpath);
117 log.debug("using axis2.xml from path: " + axis2xmlpath);
118 }
119
120 if (axis2Stream == null) {
121 String axisurl = config.getInitParameter(PARAM_AXIS2_XML_URL);
122 if (axisurl != null) {
123 axis2Stream = new URL(axisurl).openStream();
124 axisConfig = populateAxisConfiguration(axis2Stream);
125 log.debug("loading axis2.xml from URL: " + axisurl);
126 }
127 }
128
129 if (axis2Stream == null) {
130 // both the axis2.xml.path and axis2.xml.url init parameters were not present
131 // try to find the default /WEB-INF/conf/axis2.xml
132 axis2Stream = config.getServletContext()
133 .getResourceAsStream("/WEB-INF/conf/axis2.xml");
134 log.debug("trying to load axis2.xml from module: /WEB-INF/conf/axis2.xml");
135 }
136
137 if (axis2Stream == null) {
138 // Simple deployment, no need for conf directory either
139 axis2Stream = config.getServletContext()
140 .getResourceAsStream("/WEB-INF/axis2.xml");
141 log.debug("trying to load axis2.xml from module: /WEB-INF/conf/axis2.xml");
142 }
143 } // try
144 catch (Exception e) {
145 log.error(e, e);
146 log.warn("Using default configuration: " + DeploymentConstants
147 .AXIS2_CONFIGURATION_RESOURCE);
148 // not there, use default configuration from class path resource.
149 } // catch
150
151 if (axis2Stream == null) {
152 log.info("Could not find axis2.xml, loading default "
153 + DeploymentConstants.AXIS2_CONFIGURATION_RESOURCE + " from classpath");
154 axis2Stream =
155 Loader.getResourceAsStream(DeploymentConstants.AXIS2_CONFIGURATION_RESOURCE);
156 }
157 axisConfig = populateAxisConfiguration(axis2Stream);
158
159 if(axis2Stream != null){
160 axis2Stream.close();
161 }
162 Parameter param = new Parameter();
163 param.setName(Constants.Configuration.ARTIFACTS_TEMP_DIR);
164 File f = new File((File) config.getServletContext().getAttribute("javax.servlet.context.tempdir"), "_axis2");
165 if (f.exists() || f.mkdirs()) {
166 param.setValue(f);
167 } else {
168 f = new File(System.getProperty("java.io.tmpdir"), "_axis2");
169 if (f.exists() || f.mkdirs()) {
170 param.setValue(f);
171 } else {
172 throw new DeploymentException("Unable to create a temporary working directory");
173 }
174 }
175 try {
176 axisConfig.addParameter(param);
177 } catch (AxisFault axisFault) {
178 log.error(axisFault.getMessage(), axisFault);
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 /**
190 * Gets the axis configuration object by loading the repository.
191 * The order of initialization is according the the following precedence:
192 * <ul>
193 * <li>If the parameter axis2.repository.path is present, this folder is used as the location to the repository.
194 * <li>Otherwise, if the parameter axis2.repository.url is present, the URL is used as the location to the repository.
195 * <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.
196 * </ul>
197 *
198 * @return the instance of the AxisConfiguration object that reflects the repository according to the rules above.
199 * @throws AxisFault when an error occurred in the initialization of the AxisConfiguration.
200 */
201 public AxisConfiguration getAxisConfiguration() throws AxisFault {
202 try {
203 String repository = config.getInitParameter(PARAM_AXIS2_REPOSITORY_PATH);
204 if (repository != null) {
205 loadRepository(repository);
206 log.debug("loaded repository from path: " + repository);
207 }
208
209 if (repository == null) {
210 repository = config.getInitParameter(PARAM_AXIS2_REPOSITORY_URL);
211 if (repository != null) {
212 loadRepositoryFromURL(new URL(repository));
213 log.debug("loaded repository from url: " + repository);
214 }
215 }
216
217 if (repository == null) {
218 if (config.getServletContext().getRealPath("") != null) {
219 // this is an unpacked war file
220 repository = config.getServletContext().getRealPath("/WEB-INF");
221 }
222 if (repository == null) {
223 if (config.getServletContext().getRealPath("/") != null) {
224 // this is an unpacked war file
225 repository = config.getServletContext().getRealPath("/WEB-INF");
226 }
227 }
228 if (repository != null) {
229 loadRepository(repository);
230 log.debug("loaded repository from /WEB-INF folder (unpacked war)");
231 }
232 }
233
234 if (repository == null) {
235 URL url = config.getServletContext().getResource("/WEB-INF/");
236 if (url != null) {
237 repository = url.toString();
238 loadRepositoryFromURL(url);
239 log.debug("loaded repository from /WEB-INF/ folder (URL)");
240 }
241 }
242
243 if (repository == null) {
244 loadFromClassPath();
245 log.debug("loaded repository from classpath");
246 }
247
248 } catch (Exception ex) {
249 log.error(ex + ": loading repository from classpath", ex);
250 loadFromClassPath();
251 }
252 axisConfig.setConfigurator(this);
253 return axisConfig;
254 }
255
256 //to load services
257
258 /**
259 * Loads the services within the repository.
260 * When the axis2.repository.path init parameter was present, we just call loadServices() in the deployment engine.<br/>
261 * When the axis2.repository.url init parameter was present we load services from the respective URL value of the init parameter.<br/>
262 * Otherwise, try to load the services from the /WEB-INF folder within the web application.
263 */
264 public void loadServices() {
265 try {
266 String repository;
267
268 repository = config.getInitParameter(PARAM_AXIS2_REPOSITORY_PATH);
269 if (repository != null) {
270 super.loadServices();
271 log.debug("loaded services from path: " + repository);
272 return;
273 }
274
275 repository = config.getInitParameter(PARAM_AXIS2_REPOSITORY_URL);
276 if (repository != null) {
277 loadServicesFromUrl(new URL(repository));
278 log.debug("loaded services from URL: " + repository);
279 return;
280 }
281 loadServicesFromWebInf();
282 if (config.getServletContext().getRealPath("") != null ||
283 config.getServletContext().getRealPath("/") != null) {
284 super.loadServices();
285 log.debug("loaded services from webapp");
286 return;
287 }
288
289 URL url = config.getServletContext().getResource("/WEB-INF/");
290 if (url != null) {
291 loadServicesFromUrl(url);
292 log.debug("loaded services from /WEB-INF/ folder (URL)");
293 }
294 } catch (MalformedURLException e) {
295 log.info(e.getMessage());
296 }
297 }
298
299 //To engage globally listed modules
300 public void engageGlobalModules() throws AxisFault {
301 engageModules();
302 }
303
304 /**
305 * This method will look inside the web-inf directory to find services.xml
306 * inside that , if it is there will load that and creat service group out
307 * of that and add into axisConfig. User can drop corresponding class files
308 * into class directory.
309 */
310 private void loadServicesFromWebInf() {
311 try {
312 InputStream servicexml = config.getServletContext().
313 getResourceAsStream("/WEB-INF/services.xml");
314 if (servicexml != null) {
315 HashMap wsdlServices = new HashMap();
316 ArchiveReader archiveReader = new ArchiveReader();
317 String path = config.getServletContext().getRealPath("/WEB-INF");
318 if (path != null) {
319 archiveReader.processFilesInFolder(new File(path), wsdlServices);
320 }
321 AxisServiceGroup serviceGroup = DeploymentEngine.buildServiceGroup(servicexml,
322 Thread.currentThread().getContextClassLoader(),
323 "annonServiceGroup",
324 configContext,
325 archiveReader,
326 wsdlServices);
327 axisConfig.addServiceGroup(serviceGroup);
328 }
329 } catch (AxisFault axisFault) {
330 log.info(axisFault);
331 } catch (FileNotFoundException e) {
332 log.info(e);
333 } catch (XMLStreamException e) {
334 log.info(e);
335 }
336 }
337
338 public void setConfigContext(ConfigurationContext configContext) {
339 // setting ServletContext into configctx
340 configContext.setProperty(HTTPConstants.MC_HTTP_SERVLETCONTEXT,
341 config.getServletContext());
342 Parameter servletConfigParam = new Parameter();
343 servletConfigParam.setName(HTTPConstants.HTTP_SERVLETCONFIG);
344 servletConfigParam.setValue(config);
345 try {
346 configContext.getAxisConfiguration().addParameter(servletConfigParam);
347 } catch (AxisFault axisFault) {
348 log.error(axisFault.getMessage(), axisFault);
349 }
350 super.setConfigContext(configContext);
351 }
352 }