KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ws > axis > JServletEngineConfigurationFactory


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JServletEngineConfigurationFactory.java,v 1.11 2004/10/13 14:04:22 sauthieg Exp $
23  * --------------------------------------------------------------------------
24 */

25
26 package org.objectweb.jonas.ws.axis;
27
28 import java.io.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.StringWriter JavaDoc;
33
34 import javax.servlet.ServletConfig JavaDoc;
35 import javax.servlet.ServletContext JavaDoc;
36
37 import org.w3c.dom.Document JavaDoc;
38
39 import org.apache.axis.EngineConfiguration;
40 import org.apache.axis.EngineConfigurationFactory;
41 import org.apache.axis.configuration.EngineConfigurationFactoryServlet;
42 import org.apache.axis.deployment.wsdd.WSDDDeployment;
43 import org.apache.axis.encoding.SerializationContext;
44 import org.apache.axis.utils.XMLUtils;
45
46 import org.objectweb.jonas.common.Log;
47
48 import org.objectweb.util.monolog.api.BasicLevel;
49 import org.objectweb.util.monolog.api.Logger;
50
51
52 /**
53  * This is the JOnAS implementation of EngineConfigurationFactory for Servlet.
54  * It override Axis default one (EngineConfigurationFactoryServlet).
55  *
56  * @author Guillaume Sauthier
57  * @author Xavier Delplanque
58  */

59 public class JServletEngineConfigurationFactory
60     implements EngineConfigurationFactory {
61
62     /** server config parameter name in init-param */
63     public static final String JavaDoc AXIS_SERVER_CONFIG_PARAM = "axis.serverConfigFile";
64
65     /** server-config.wsdd base */
66     public static final String JavaDoc SERVER_CONFIG_WSDD = "org/objectweb/jonas/ws/axis/server-config.wsdd";
67
68     /** The logger to use */
69     private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
70
71     /** The file name to use for research */
72     private String JavaDoc serverConfigFile;
73
74     /** The EngineConfigurationFactoryServlet for delegation */
75     private EngineConfigurationFactory delegate;
76
77     /** The context used for this JServletEngineConfigurationFactory */
78     private ServletContext JavaDoc ctx;
79
80     /** The config used for this JServletEngineConfigurationFactory */
81     private ServletConfig JavaDoc cfg;
82
83
84     /**
85      * Create the default engine configuration and detect whether the user has
86      * overridden this with their own.
87      *
88      * @param conf a ServletConfig
89      */

90     protected JServletEngineConfigurationFactory(ServletConfig JavaDoc conf) {
91         // get the axis delegate
92
cfg = conf;
93         ctx = conf.getServletContext();
94         delegate = EngineConfigurationFactoryServlet.newFactory(conf);
95     }
96
97     /**
98      * Creates and returns a new JServletEngineConfigurationFactory. If a factory
99      * cannot be created, return 'null'. The factory may return non-NULL only
100      * if: <br>
101      * - it knows what to do with the param (param != null) <br>
102      * - it can find it's configuration information
103      *
104      * @param param The object used to retrieved the right Factory instance
105      *
106      * @return null if param is not a ServletContext, or return the JOnAS
107      * EngineConfigurationFactory used for a service-endpoint.
108      */

109     public static EngineConfigurationFactory newFactory(Object JavaDoc param) {
110
111         if (param == null) {
112             return null; // not for us.
113
}
114
115         if (param instanceof ServletConfig JavaDoc) {
116             return new JServletEngineConfigurationFactory((ServletConfig JavaDoc) param);
117         } else {
118             return null;
119         }
120     }
121
122     /**
123      * Get a server engine configuration. Try to load it from a File or as a
124      * ServletContext Resource. Delegate to EngineConfigurationFactoryServlet
125      * for default behavior.
126      *
127      * @return a server side EngineConfiguration
128      */

129     public EngineConfiguration getServerEngineConfig() {
130
131         logger.log(BasicLevel.DEBUG, "Entering getServerEngineConfig for servlet " + cfg.getServletName());
132
133         try {
134
135             // retrieve init param specifying server-config.wsdd filename to be loaded
136
serverConfigFile = cfg.getInitParameter(AXIS_SERVER_CONFIG_PARAM);
137
138             logger.log(BasicLevel.DEBUG, "serverConfigFile=" + serverConfigFile);
139
140             // if no config file specified, delegate to Axis Servlet EngineConfigurationFactory
141
if (serverConfigFile == null) {
142                 return delegate.getServerEngineConfig();
143             }
144
145             if (logger.isLoggable(BasicLevel.DEBUG)) {
146                 logger.log(BasicLevel.DEBUG,
147                            "Loading server-config file '" + serverConfigFile + "'");
148             }
149
150             /*
151              * Use the WEB-INF directory
152              * (so the config files can't get snooped by a browser)
153              */

154             String JavaDoc appWebInfPath = "/WEB-INF";
155
156             Document JavaDoc deploy = null;
157
158             String JavaDoc realWebInfPath = ctx.getRealPath(appWebInfPath);
159
160             /**
161              * If path/file doesn't exist, it may still be accessible
162              * as a resource-stream (i.e. it may be packaged in a JAR
163              * or WAR file).
164              */

165             if (realWebInfPath == null
166                 || !(new File JavaDoc(realWebInfPath, serverConfigFile)).exists()) {
167
168                 String JavaDoc name = appWebInfPath + "/" + serverConfigFile;
169                 InputStream JavaDoc is = ctx.getResourceAsStream(name);
170
171                 if (is != null) {
172                     deploy = XMLUtils.newDocument(is);
173                 }
174
175                 if (deploy == null) {
176                     String JavaDoc err = "Cannot get config file '" + serverConfigFile + "' as Resource.";
177                     logger.log(BasicLevel.ERROR, err);
178                 }
179             }
180
181             /**
182              * Couldn't get data OR file does exist.
183              * If we have a path, then attempt to either open
184              * the existing file, or create an (empty) file.
185              */

186             if (deploy == null && realWebInfPath != null) {
187                 try {
188                     InputStream JavaDoc is = new FileInputStream JavaDoc(new File JavaDoc(realWebInfPath, serverConfigFile));
189                     deploy = XMLUtils.newDocument(is);
190                 } catch (IOException JavaDoc e) {
191                     String JavaDoc err = "Cannot get config file '" + serverConfigFile + "' as File Resource.";
192                     logger.log(BasicLevel.ERROR, err);
193                 }
194             }
195
196             // if all attempts fails, delegate to Axis...
197
if (deploy == null) {
198                 return delegate.getServerEngineConfig();
199             }
200
201             // get Base Deployment desc as Resource of ClassLoader
202
ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
203             InputStream JavaDoc is = cl.getResourceAsStream(SERVER_CONFIG_WSDD);
204             Document JavaDoc base = XMLUtils.newDocument(is);
205
206             // Combine the 2 DD
207
WSDDDeployment deployWSDD = new WSDDDeployment(deploy.getDocumentElement());
208             WSDDDeployment configWSDD = new WSDDDeployment(base.getDocumentElement());
209
210             deployWSDD.deployToRegistry(configWSDD);
211
212             if (logger.isLoggable(BasicLevel.DEBUG)) {
213                 StringWriter JavaDoc sw = new StringWriter JavaDoc();
214                 SerializationContext ctx = new SerializationContext(sw);
215
216                 try {
217                     configWSDD.writeToContext(ctx);
218                 } catch (Exception JavaDoc ioe) {
219                     logger.log(BasicLevel.DEBUG, "Cannot serialize Axis wsdd for servlet " + cfg.getServletName());
220                 }
221                 logger.log(BasicLevel.DEBUG, sw.getBuffer().toString());
222             }
223
224             return configWSDD;
225         } catch (Exception JavaDoc e) {
226             // if exception occurs, set a default server-config.wsdd
227
String JavaDoc err = "Cannot configure axis server from '" + serverConfigFile + "'. Use axis default. Caused by : " + e.getMessage();
228             e.printStackTrace();
229             logger.log(BasicLevel.ERROR, err);
230             return delegate.getServerEngineConfig();
231         }
232     }
233
234     /**
235      * Return null. (Not used for WebService server-side)
236      *
237      * @return null
238      */

239     public EngineConfiguration getClientEngineConfig() {
240         return null;
241     }
242 }
243
Popular Tags