KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.configuration;
57
58 import org.jboss.axis.AxisProperties;
59 import org.jboss.axis.ConfigurationException;
60 import org.jboss.axis.EngineConfiguration;
61 import org.jboss.axis.EngineConfigurationFactory;
62 import org.jboss.axis.server.AxisServer;
63 import org.jboss.axis.utils.ClassUtils;
64 import org.jboss.axis.utils.Messages;
65 import org.jboss.logging.Logger;
66
67 import javax.servlet.ServletContext JavaDoc;
68 import java.io.File JavaDoc;
69 import java.io.InputStream JavaDoc;
70
71 /**
72  * This is a default implementation of ServletEngineConfigurationFactory.
73  * It is user-overrideable by a system property without affecting
74  * the caller. If you decide to override it, use delegation if
75  * you want to inherit the behaviour of this class as using
76  * class extension will result in tight loops. That is, your
77  * class should implement EngineConfigurationFactory and keep
78  * an instance of this class in a member field and delegate
79  * methods to that instance when the default behaviour is
80  * required.
81  *
82  * @author Richard A. Sitze
83  * @author Davanum Srinivas (dims@apache.org)
84  */

85 public class EngineConfigurationFactoryServlet
86         extends EngineConfigurationFactoryDefault
87 {
88    private static Logger log = Logger.getLogger(EngineConfigurationFactoryServlet.class.getName());
89
90    private ServletContext JavaDoc ctx;
91
92    /**
93     * Creates and returns a new EngineConfigurationFactory.
94     * If a factory cannot be created, return 'null'.
95     * <p/>
96     * The factory may return non-NULL only if:
97     * - it knows what to do with the param (param instanceof ServletContext)
98     * - it can find it's configuration information
99     *
100     * @see org.jboss.axis.configuration.EngineConfigurationFactoryFinder
101     */

102    public static EngineConfigurationFactory newFactory(Object JavaDoc param)
103    {
104       /**
105        * Default, let this one go through if we find a ServletContext.
106        *
107        * The REAL reason we are not trying to make any
108        * decision here is because it's impossible
109        * (without refactoring FileProvider) to determine
110        * if a *.wsdd file is present or not until the configuration
111        * is bound to an engine.
112        *
113        * FileProvider/EngineConfiguration pretend to be independent,
114        * but they are tightly bound to an engine instance...
115        */

116       return (param instanceof ServletContext JavaDoc)
117               ? new EngineConfigurationFactoryServlet((ServletContext JavaDoc)param)
118               : null;
119    }
120
121    /**
122     * Create the default engine configuration and detect whether the user
123     * has overridden this with their own.
124     */

125    protected EngineConfigurationFactoryServlet(ServletContext JavaDoc ctx)
126    {
127       super();
128       this.ctx = ctx;
129    }
130
131    /**
132     * Get a default server engine configuration.
133     *
134     * @return a server EngineConfiguration
135     */

136    public EngineConfiguration getServerEngineConfig()
137    {
138       return getServerEngineConfig(ctx);
139    }
140
141    /**
142     * Get a default server engine configuration in a servlet environment.
143     *
144     * @param ctx a ServletContext
145     * @return a server EngineConfiguration
146     */

147    private static
148    EngineConfiguration getServerEngineConfig(ServletContext JavaDoc ctx)
149    {
150       // Respect the system property setting for a different config file
151
String JavaDoc configFile =
152               AxisProperties.getProperty(OPTION_SERVER_CONFIG_FILE);
153       if (configFile == null)
154       {
155          configFile = SERVER_CONFIG_FILE;
156       }
157
158       /**
159        * Flow can be confusing. Here is the logic:
160        * 1) Make all attempts to open resource IF it exists
161        * - If it exists as a file, open as file (r/w)
162        * - If not a file, it may still be accessable as a stream (r)
163        * (env will handle security checks).
164        * 2) If it doesn't exist, allow it to be opened/created
165        *
166        * Now, the way this is done below is:
167        * a) If the file does NOT exist, attempt to open as a stream (r)
168        * b) Open named file (opens existing file, creates if not avail).
169        */

170
171       /*
172        * Use the WEB-INF directory
173        * (so the config files can't get snooped by a browser)
174        */

175       String JavaDoc appWebInfPath = "/WEB-INF";
176
177       FileProvider config = null;
178
179       String JavaDoc realWebInfPath = ctx.getRealPath(appWebInfPath);
180
181       /**
182        * If path/file doesn't exist, it may still be accessible
183        * as a resource-stream (i.e. it may be packaged in a JAR
184        * or WAR file).
185        */

186       if (realWebInfPath == null ||
187               !(new File JavaDoc(realWebInfPath, configFile)).exists())
188       {
189          String JavaDoc name = appWebInfPath + "/" + configFile;
190          InputStream JavaDoc is = ctx.getResourceAsStream(name);
191          if (is != null)
192          {
193             // FileProvider assumes responsibility for 'is':
194
// do NOT call is.close().
195
config = new FileProvider(is);
196          }
197
198          if (config == null)
199          {
200             log.error(Messages.getMessage("servletEngineWebInfError03",
201                     name));
202          }
203       }
204
205       /**
206        * Couldn't get data OR file does exist.
207        * If we have a path, then attempt to either open
208        * the existing file, or create an (empty) file.
209        */

210       if (config == null && realWebInfPath != null)
211       {
212          try
213          {
214             config = new FileProvider(realWebInfPath, configFile);
215          }
216          catch (ConfigurationException e)
217          {
218             log.error(Messages.getMessage("servletEngineWebInfError00"), e);
219          }
220       }
221
222       /**
223        * Fall back to config file packaged with AxisEngine
224        */

225       if (config == null)
226       {
227          log.warn(Messages.getMessage("servletEngineWebInfWarn00"));
228          try
229          {
230             InputStream JavaDoc is =
231                     ClassUtils.getResourceAsStream(AxisServer.class,
232                             SERVER_CONFIG_FILE);
233             config = new FileProvider(is);
234          }
235          catch (Exception JavaDoc e)
236          {
237             log.error(Messages.getMessage("servletEngineWebInfError02"), e);
238          }
239       }
240
241       return config;
242    }
243 }
244
Popular Tags