KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > configuration > EngineConfigurationFactoryServlet


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.configuration;
18
19 import org.apache.axis.AxisProperties;
20 import org.apache.axis.ConfigurationException;
21 import org.apache.axis.EngineConfiguration;
22 import org.apache.axis.EngineConfigurationFactory;
23 import org.apache.axis.components.logger.LogFactory;
24 import org.apache.axis.server.AxisServer;
25 import org.apache.axis.utils.ClassUtils;
26 import org.apache.axis.utils.Messages;
27 import org.apache.commons.logging.Log;
28
29 import javax.servlet.ServletConfig JavaDoc;
30 import javax.servlet.ServletContext JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.InputStream JavaDoc;
33
34 /**
35  * This is a default implementation of ServletEngineConfigurationFactory.
36  * It is user-overrideable by a system property without affecting
37  * the caller. If you decide to override it, use delegation if
38  * you want to inherit the behaviour of this class as using
39  * class extension will result in tight loops. That is, your
40  * class should implement EngineConfigurationFactory and keep
41  * an instance of this class in a member field and delegate
42  * methods to that instance when the default behaviour is
43  * required.
44  *
45  * @author Richard A. Sitze
46  * @author Davanum Srinivas (dims@apache.org)
47  */

48 public class EngineConfigurationFactoryServlet
49     extends EngineConfigurationFactoryDefault
50 {
51     protected static Log log =
52         LogFactory.getLog(EngineConfigurationFactoryServlet.class.getName());
53
54     private ServletConfig JavaDoc cfg;
55     
56     /**
57      * Creates and returns a new EngineConfigurationFactory.
58      * If a factory cannot be created, return 'null'.
59      *
60      * The factory may return non-NULL only if:
61      * - it knows what to do with the param (param instanceof ServletContext)
62      * - it can find it's configuration information
63      *
64      * @see org.apache.axis.configuration.EngineConfigurationFactoryFinder
65      */

66     public static EngineConfigurationFactory newFactory(Object JavaDoc param) {
67         /**
68          * Default, let this one go through if we find a ServletContext.
69          *
70          * The REAL reason we are not trying to make any
71          * decision here is because it's impossible
72          * (without refactoring FileProvider) to determine
73          * if a *.wsdd file is present or not until the configuration
74          * is bound to an engine.
75          *
76          * FileProvider/EngineConfiguration pretend to be independent,
77          * but they are tightly bound to an engine instance...
78          */

79         return (param instanceof ServletConfig JavaDoc)
80                ? new EngineConfigurationFactoryServlet((ServletConfig JavaDoc)param)
81                : null;
82     }
83
84     /**
85      * Create the default engine configuration and detect whether the user
86      * has overridden this with their own.
87      */

88     protected EngineConfigurationFactoryServlet(ServletConfig JavaDoc conf) {
89         super();
90         this.cfg = conf;
91     }
92
93     /**
94      * Get a default server engine configuration.
95      *
96      * @return a server EngineConfiguration
97      */

98     public EngineConfiguration getServerEngineConfig() {
99         return getServerEngineConfig(cfg);
100     }
101
102     /**
103      * Get a default server engine configuration in a servlet environment.
104      *
105      * @param ctx a ServletContext
106      * @return a server EngineConfiguration
107      */

108     private static
109             EngineConfiguration getServerEngineConfig(ServletConfig JavaDoc cfg) {
110         
111         ServletContext JavaDoc ctx = cfg.getServletContext();
112         
113         // Respect the system property setting for a different config file
114
String JavaDoc configFile = cfg.getInitParameter(OPTION_SERVER_CONFIG_FILE);
115         if (configFile == null)
116                 configFile =
117                         AxisProperties.getProperty(OPTION_SERVER_CONFIG_FILE);
118         if (configFile == null) {
119             configFile = SERVER_CONFIG_FILE;
120         }
121         
122         /**
123          * Flow can be confusing. Here is the logic:
124          * 1) Make all attempts to open resource IF it exists
125          * - If it exists as a file, open as file (r/w)
126          * - If not a file, it may still be accessable as a stream (r)
127          * (env will handle security checks).
128          * 2) If it doesn't exist, allow it to be opened/created
129          *
130          * Now, the way this is done below is:
131          * a) If the file does NOT exist, attempt to open as a stream (r)
132          * b) Open named file (opens existing file, creates if not avail).
133          */

134
135         /*
136          * Use the WEB-INF directory
137          * (so the config files can't get snooped by a browser)
138          */

139         String JavaDoc appWebInfPath = "/WEB-INF";
140
141         FileProvider config = null;
142
143         String JavaDoc realWebInfPath = ctx.getRealPath(appWebInfPath);
144
145         /**
146          * If path/file doesn't exist, it may still be accessible
147          * as a resource-stream (i.e. it may be packaged in a JAR
148          * or WAR file).
149          */

150         if (realWebInfPath == null ||
151             !(new File JavaDoc(realWebInfPath, configFile)).exists())
152         {
153             String JavaDoc name = appWebInfPath + "/" + configFile;
154             InputStream JavaDoc is = ctx.getResourceAsStream(name);
155             if (is != null) {
156                 // FileProvider assumes responsibility for 'is':
157
// do NOT call is.close().
158
config = new FileProvider(is);
159             }
160
161             if (config == null) {
162                 log.error(Messages.getMessage("servletEngineWebInfError03",
163                                                name));
164             }
165         }
166         
167         /**
168          * Couldn't get data OR file does exist.
169          * If we have a path, then attempt to either open
170          * the existing file, or create an (empty) file.
171          */

172         if (config == null && realWebInfPath != null) {
173             try {
174                 config = new FileProvider(realWebInfPath, configFile);
175             } catch (ConfigurationException e) {
176                 log.error(Messages.getMessage("servletEngineWebInfError00"), e);
177             }
178         }
179
180         /**
181          * Fall back to config file packaged with AxisEngine
182          */

183         if (config == null) {
184             log.warn(Messages.getMessage("servletEngineWebInfWarn00"));
185             try {
186                 InputStream JavaDoc is =
187                         ClassUtils.getResourceAsStream(AxisServer.class,
188                                                        SERVER_CONFIG_FILE);
189                 config = new FileProvider(is);
190             } catch (Exception JavaDoc e) {
191                 log.error(Messages.getMessage("servletEngineWebInfError02"), e);
192             }
193         }
194
195         return config;
196     }
197 }
198
Popular Tags