001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JServletEngineConfigurationFactory.java 5607 2004-10-13 14:04:22Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.ws.axis;
025:
026: import java.io.File;
027: import java.io.FileInputStream;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.StringWriter;
031:
032: import javax.servlet.ServletConfig;
033: import javax.servlet.ServletContext;
034:
035: import org.w3c.dom.Document;
036:
037: import org.apache.axis.EngineConfiguration;
038: import org.apache.axis.EngineConfigurationFactory;
039: import org.apache.axis.configuration.EngineConfigurationFactoryServlet;
040: import org.apache.axis.deployment.wsdd.WSDDDeployment;
041: import org.apache.axis.encoding.SerializationContext;
042: import org.apache.axis.utils.XMLUtils;
043:
044: import org.objectweb.jonas.common.Log;
045:
046: import org.objectweb.util.monolog.api.BasicLevel;
047: import org.objectweb.util.monolog.api.Logger;
048:
049: /**
050: * This is the JOnAS implementation of EngineConfigurationFactory for Servlet.
051: * It override Axis default one (EngineConfigurationFactoryServlet).
052: *
053: * @author Guillaume Sauthier
054: * @author Xavier Delplanque
055: */
056: public class JServletEngineConfigurationFactory implements
057: EngineConfigurationFactory {
058:
059: /** server config parameter name in init-param */
060: public static final String AXIS_SERVER_CONFIG_PARAM = "axis.serverConfigFile";
061:
062: /** server-config.wsdd base */
063: public static final String SERVER_CONFIG_WSDD = "org/objectweb/jonas/ws/axis/server-config.wsdd";
064:
065: /** The logger to use */
066: private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
067:
068: /** The file name to use for research */
069: private String serverConfigFile;
070:
071: /** The EngineConfigurationFactoryServlet for delegation */
072: private EngineConfigurationFactory delegate;
073:
074: /** The context used for this JServletEngineConfigurationFactory */
075: private ServletContext ctx;
076:
077: /** The config used for this JServletEngineConfigurationFactory */
078: private ServletConfig cfg;
079:
080: /**
081: * Create the default engine configuration and detect whether the user has
082: * overridden this with their own.
083: *
084: * @param conf a ServletConfig
085: */
086: protected JServletEngineConfigurationFactory(ServletConfig conf) {
087: // get the axis delegate
088: cfg = conf;
089: ctx = conf.getServletContext();
090: delegate = EngineConfigurationFactoryServlet.newFactory(conf);
091: }
092:
093: /**
094: * Creates and returns a new JServletEngineConfigurationFactory. If a factory
095: * cannot be created, return 'null'. The factory may return non-NULL only
096: * if: <br>
097: * - it knows what to do with the param (param != null) <br>
098: * - it can find it's configuration information
099: *
100: * @param param The object used to retrieved the right Factory instance
101: *
102: * @return null if param is not a ServletContext, or return the JOnAS
103: * EngineConfigurationFactory used for a service-endpoint.
104: */
105: public static EngineConfigurationFactory newFactory(Object param) {
106:
107: if (param == null) {
108: return null; // not for us.
109: }
110:
111: if (param instanceof ServletConfig) {
112: return new JServletEngineConfigurationFactory(
113: (ServletConfig) param);
114: } else {
115: return null;
116: }
117: }
118:
119: /**
120: * Get a server engine configuration. Try to load it from a File or as a
121: * ServletContext Resource. Delegate to EngineConfigurationFactoryServlet
122: * for default behavior.
123: *
124: * @return a server side EngineConfiguration
125: */
126: public EngineConfiguration getServerEngineConfig() {
127:
128: logger.log(BasicLevel.DEBUG,
129: "Entering getServerEngineConfig for servlet "
130: + cfg.getServletName());
131:
132: try {
133:
134: // retrieve init param specifying server-config.wsdd filename to be loaded
135: serverConfigFile = cfg
136: .getInitParameter(AXIS_SERVER_CONFIG_PARAM);
137:
138: logger.log(BasicLevel.DEBUG, "serverConfigFile="
139: + serverConfigFile);
140:
141: // if no config file specified, delegate to Axis Servlet EngineConfigurationFactory
142: if (serverConfigFile == null) {
143: return delegate.getServerEngineConfig();
144: }
145:
146: if (logger.isLoggable(BasicLevel.DEBUG)) {
147: logger.log(BasicLevel.DEBUG,
148: "Loading server-config file '"
149: + serverConfigFile + "'");
150: }
151:
152: /*
153: * Use the WEB-INF directory
154: * (so the config files can't get snooped by a browser)
155: */
156: String appWebInfPath = "/WEB-INF";
157:
158: Document deploy = null;
159:
160: String realWebInfPath = ctx.getRealPath(appWebInfPath);
161:
162: /**
163: * If path/file doesn't exist, it may still be accessible
164: * as a resource-stream (i.e. it may be packaged in a JAR
165: * or WAR file).
166: */
167: if (realWebInfPath == null
168: || !(new File(realWebInfPath, serverConfigFile))
169: .exists()) {
170:
171: String name = appWebInfPath + "/" + serverConfigFile;
172: InputStream is = ctx.getResourceAsStream(name);
173:
174: if (is != null) {
175: deploy = XMLUtils.newDocument(is);
176: }
177:
178: if (deploy == null) {
179: String err = "Cannot get config file '"
180: + serverConfigFile + "' as Resource.";
181: logger.log(BasicLevel.ERROR, err);
182: }
183: }
184:
185: /**
186: * Couldn't get data OR file does exist.
187: * If we have a path, then attempt to either open
188: * the existing file, or create an (empty) file.
189: */
190: if (deploy == null && realWebInfPath != null) {
191: try {
192: InputStream is = new FileInputStream(new File(
193: realWebInfPath, serverConfigFile));
194: deploy = XMLUtils.newDocument(is);
195: } catch (IOException e) {
196: String err = "Cannot get config file '"
197: + serverConfigFile + "' as File Resource.";
198: logger.log(BasicLevel.ERROR, err);
199: }
200: }
201:
202: // if all attempts fails, delegate to Axis...
203: if (deploy == null) {
204: return delegate.getServerEngineConfig();
205: }
206:
207: // get Base Deployment desc as Resource of ClassLoader
208: ClassLoader cl = Thread.currentThread()
209: .getContextClassLoader();
210: InputStream is = cl.getResourceAsStream(SERVER_CONFIG_WSDD);
211: Document base = XMLUtils.newDocument(is);
212:
213: // Combine the 2 DD
214: WSDDDeployment deployWSDD = new WSDDDeployment(deploy
215: .getDocumentElement());
216: WSDDDeployment configWSDD = new WSDDDeployment(base
217: .getDocumentElement());
218:
219: deployWSDD.deployToRegistry(configWSDD);
220:
221: if (logger.isLoggable(BasicLevel.DEBUG)) {
222: StringWriter sw = new StringWriter();
223: SerializationContext ctx = new SerializationContext(sw);
224:
225: try {
226: configWSDD.writeToContext(ctx);
227: } catch (Exception ioe) {
228: logger.log(BasicLevel.DEBUG,
229: "Cannot serialize Axis wsdd for servlet "
230: + cfg.getServletName());
231: }
232: logger.log(BasicLevel.DEBUG, sw.getBuffer().toString());
233: }
234:
235: return configWSDD;
236: } catch (Exception e) {
237: // if exception occurs, set a default server-config.wsdd
238: String err = "Cannot configure axis server from '"
239: + serverConfigFile
240: + "'. Use axis default. Caused by : "
241: + e.getMessage();
242: e.printStackTrace();
243: logger.log(BasicLevel.ERROR, err);
244: return delegate.getServerEngineConfig();
245: }
246: }
247:
248: /**
249: * Return null. (Not used for WebService server-side)
250: *
251: * @return null
252: */
253: public EngineConfiguration getClientEngineConfig() {
254: return null;
255: }
256: }
|