KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > AxisEngine


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the docs/licenses/apache-1.1.txt file.
7  */

8
9 package org.jboss.axis;
10
11 import org.jboss.axis.encoding.TypeMappingRegistry;
12 import org.jboss.axis.handlers.BasicHandler;
13 import org.jboss.axis.handlers.soap.SOAPService;
14 import org.jboss.axis.session.Session;
15 import org.jboss.axis.session.SimpleSession;
16 import org.jboss.axis.utils.JavaUtils;
17 import org.jboss.axis.utils.Messages;
18 import org.jboss.axis.utils.cache.ClassCache;
19 import org.jboss.logging.Logger;
20
21 import javax.xml.namespace.QName JavaDoc;
22 import javax.xml.rpc.server.ServiceLifecycle JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Hashtable JavaDoc;
26
27
28 /**
29  * An <code>AxisEngine</code> is the base class for AxisClient and
30  * AxisServer. Handles common functionality like dealing with the
31  * handler/service registries and loading properties.
32  *
33  * @author Glen Daniels (gdaniels@macromedia.com)
34  * @author Glyn Normington (glyn@apache.org)
35  */

36 public abstract class AxisEngine extends BasicHandler
37 {
38    private static Logger log = Logger.getLogger(AxisEngine.class.getName());
39
40    // Engine property names
41
public static final String JavaDoc PROP_XML_DECL = "sendXMLDeclaration";
42    public static final String JavaDoc PROP_DEBUG_LEVEL = "debugLevel";
43    public static final String JavaDoc PROP_DEBUG_FILE = "debugFile";
44    public static final String JavaDoc PROP_DOMULTIREFS = "sendMultiRefs";
45    public static final String JavaDoc PROP_PASSWORD = "adminPassword";
46    public static final String JavaDoc PROP_SYNC_CONFIG = "syncConfiguration";
47    public static final String JavaDoc PROP_SEND_XSI = "sendXsiTypes";
48    public static final String JavaDoc PROP_ATTACHMENT_DIR = "attachments.Directory";
49    public static final String JavaDoc PROP_ATTACHMENT_IMPLEMENTATION = "attachments.implementation";
50    public static final String JavaDoc PROP_ATTACHMENT_CLEANUP = "attachment.DirectoryCleanUp";
51    public static final String JavaDoc PROP_DEFAULT_CONFIG_CLASS = "axis.engineConfigClass";
52    public static final String JavaDoc PROP_SOAP_VERSION = "defaultSOAPVersion";
53    public static final String JavaDoc PROP_SOAP_ALLOWED_VERSION = "singleSOAPVersion";
54    public static final String JavaDoc PROP_TWOD_ARRAY_ENCODING = "enable2DArrayEncoding";
55    public static final String JavaDoc PROP_SEND_MINIMIZED_ELEMENTS = "axis.sendMinimizedElements";
56    public static final String JavaDoc PROP_XML_ENCODING = "axis.xmlEncoding";
57
58    public static final String JavaDoc DEFAULT_ATTACHMENT_IMPL = "org.jboss.axis.attachments.AttachmentsImpl";
59
60    public static final String JavaDoc ENV_ATTACHMENT_DIR = "axis.attachments.Directory";
61    public static final String JavaDoc ENV_SERVLET_REALPATH = "servlet.realpath";
62    public static final String JavaDoc ENV_SERVLET_CONTEXT = "servletContext";
63
64    // Default admin. password
65
private static final String JavaDoc DEFAULT_ADMIN_PASSWORD = "admin";
66
67
68    /**
69     * Our go-to guy for configuration...
70     */

71    protected EngineConfiguration config;
72
73    /**
74     * Has the user changed the password yet?
75     */

76    protected boolean _hasSafePassword = false;
77
78    /**
79     * Should we save the engine config each time we modify it?
80     */

81    protected boolean shouldSaveConfig = false;
82
83    /** Java class cache */
84    //protected ClassCache classCache = new ClassCache();
85

86    /**
87     * This engine's Session. This Session supports "application scope"
88     * in the Apache SOAP sense... if you have a service with "application
89     * scope", have it store things in this Session.
90     */

91    private Session session = new SimpleSession();
92
93    /**
94     * What actor URIs hold for the entire engine?
95     */

96    private ArrayList JavaDoc actorURIs = new ArrayList JavaDoc();
97
98    /**
99     * Thread local storage used for locating the active message context.
100     * This information is only valid for the lifetime of this request.
101     */

102    private static ThreadLocal JavaDoc currentMessageContext = new ThreadLocal JavaDoc();
103
104    /**
105     * Set the active message context.
106     *
107     * @param mc - the new active message context.
108     */

109    public static void setCurrentMessageContext(MessageContext mc)
110    {
111       currentMessageContext.set(mc);
112    }
113
114    /**
115     * Get the active message context.
116     *
117     * @return the current active message context
118     */

119    public static MessageContext getCurrentMessageContext()
120    {
121       return (MessageContext)currentMessageContext.get();
122    }
123
124    /**
125     * Construct an AxisEngine using the specified engine configuration.
126     *
127     * @param config the EngineConfiguration for this engine
128     */

129    public AxisEngine(EngineConfiguration config)
130    {
131       this.config = config;
132       init();
133    }
134
135    /**
136     * (re)initialize - What should really go in here???
137     */

138    public void init()
139    {
140       if (log.isDebugEnabled())
141       {
142          log.debug("Enter: AxisEngine::init");
143       }
144
145       // The SOAP/XSD stuff is in the default TypeMapping of the TypeMappingRegistry.
146
//getTypeMappingRegistry().setParent(SOAPTypeMappingRegistry.getSingleton());
147

148       try
149       {
150          config.configureEngine(this);
151       }
152       catch (Exception JavaDoc e)
153       {
154          throw new InternalException(e);
155       }
156
157       /*Set the default attachment implementation */
158       setOptionDefault(PROP_ATTACHMENT_IMPLEMENTATION,
159               AxisProperties.getProperty("axis." + PROP_ATTACHMENT_IMPLEMENTATION));
160
161       setOptionDefault(PROP_ATTACHMENT_IMPLEMENTATION, DEFAULT_ATTACHMENT_IMPL);
162
163       if (log.isDebugEnabled())
164       {
165          log.debug("Exit: AxisEngine::init");
166       }
167
168    }
169
170    /**
171     * cleanup routine removes application scoped objects
172     * There is a small risk of this being called more than once
173     * so the cleanup should be designed to resist that event
174     */

175    public void cleanup()
176    {
177       super.cleanup();
178
179       // Let any application-scoped service objects know that we're going
180
// away...
181
Enumeration JavaDoc keys = session.getKeys();
182       if (keys != null)
183       {
184          while (keys.hasMoreElements())
185          {
186             String JavaDoc key = (String JavaDoc)keys.nextElement();
187             Object JavaDoc obj = session.get(key);
188             if (obj != null && obj instanceof ServiceLifecycle JavaDoc)
189             {
190                ((ServiceLifecycle JavaDoc)obj).destroy();
191             }
192             session.remove(key);
193          }
194       }
195    }
196
197    /**
198     * Write out our engine configuration.
199     */

200    public void saveConfiguration()
201    {
202       if (!shouldSaveConfig)
203          return;
204
205       try
206       {
207          config.writeEngineConfig(this);
208       }
209       catch (Exception JavaDoc e)
210       {
211          log.error(Messages.getMessage("saveConfigFail00"), e);
212       }
213    }
214
215    public EngineConfiguration getConfig()
216    {
217       return config;
218    }
219
220    public boolean hasSafePassword()
221    {
222       return _hasSafePassword;
223    }
224
225    public void setAdminPassword(String JavaDoc pw)
226    {
227       setOption(PROP_PASSWORD, pw);
228       _hasSafePassword = true;
229       saveConfiguration();
230    }
231
232    public void setShouldSaveConfig(boolean shouldSaveConfig)
233    {
234       this.shouldSaveConfig = shouldSaveConfig;
235    }
236
237    public Handler getHandler(String JavaDoc name) throws AxisFault
238    {
239       try
240       {
241          return config.getHandler(new QName JavaDoc(null, name));
242       }
243       catch (ConfigurationException e)
244       {
245          throw AxisFault.makeFault(e);
246       }
247    }
248
249    public SOAPService getService(String JavaDoc name) throws AxisFault
250    {
251       try
252       {
253          return config.getService(new QName JavaDoc(null, name));
254       }
255       catch (ConfigurationException e)
256       {
257          throw AxisFault.makeFault(e);
258       }
259    }
260
261    public Handler getTransport(String JavaDoc name) throws AxisFault
262    {
263       try
264       {
265          return config.getTransport(new QName JavaDoc(null, name));
266       }
267       catch (ConfigurationException e)
268       {
269          throw AxisFault.makeFault(e);
270       }
271    }
272
273    public TypeMappingRegistry getTypeMappingRegistry()
274    {
275       TypeMappingRegistry tmr = null;
276       try
277       {
278          tmr = config.getTypeMappingRegistry();
279       }
280       catch (ConfigurationException e)
281       {
282          log.error(Messages.getMessage("axisConfigurationException00"), e);
283       }
284
285       return tmr;
286    }
287
288    public Handler getGlobalRequest()
289            throws ConfigurationException
290    {
291       return config.getGlobalRequest();
292    }
293
294    public Handler getGlobalResponse()
295            throws ConfigurationException
296    {
297       return config.getGlobalResponse();
298    }
299
300    public ArrayList JavaDoc getActorURIs()
301    {
302       return actorURIs;
303    }
304
305    public void addActorURI(String JavaDoc uri)
306    {
307       actorURIs.add(uri);
308    }
309
310    public void removeActorURI(String JavaDoc uri)
311    {
312       actorURIs.remove(uri);
313    }
314
315    /**
316     * ******************************************************************
317     * Client engine access
318     * <p/>
319     * An AxisEngine may define another specific AxisEngine to be used
320     * by newly created Clients. For instance, a server may
321     * create an AxisClient and allow deployment to it. Then
322     * the server's services may access the AxisClient's deployed
323     * handlers and transports.
324     * ********************************************************************
325     */

326
327    public abstract AxisEngine getClientEngine();
328
329    /*********************************************************************
330     * Administration and management APIs
331     *
332     * These can get called by various admin adapters, such as JMX MBeans,
333     * our own Admin client, web applications, etc...
334     *
335     *********************************************************************
336     */

337
338    /**
339     * List of options which should be converted from Strings to Booleans
340     * automatically. Note that these options are common to all XML
341     * web services.
342     */

343    private static final String JavaDoc[] BOOLEAN_OPTIONS = new String JavaDoc[]{
344       PROP_DOMULTIREFS, PROP_SEND_XSI, PROP_XML_DECL, PROP_SEND_MINIMIZED_ELEMENTS
345    };
346
347    /**
348     * Normalise the engine's options.
349     * <p/>
350     * Convert boolean options from String to Boolean and default
351     * any ommitted boolean options to TRUE. Default the admin.
352     * password.
353     */

354    public static void normaliseOptions(Handler handler)
355    {
356       // Convert boolean options to Booleans so we don't need to use
357
// string comparisons. Default is "true".
358

359       for (int i = 0; i < BOOLEAN_OPTIONS.length; i++)
360       {
361          Object JavaDoc val = handler.getOption(BOOLEAN_OPTIONS[i]);
362          if (val != null)
363          {
364             if (val instanceof Boolean JavaDoc)
365                continue;
366             if (JavaUtils.isFalse(val))
367             {
368                handler.setOption(BOOLEAN_OPTIONS[i], Boolean.FALSE);
369                continue;
370             }
371          }
372          else
373          {
374             if (!(handler instanceof AxisEngine))
375                continue;
376          }
377          // If it was null or not "false"...
378
handler.setOption(BOOLEAN_OPTIONS[i], Boolean.TRUE);
379       }
380
381       // Deal with admin password's default value.
382
if (handler instanceof AxisEngine)
383       {
384          AxisEngine engine = (AxisEngine)handler;
385          if (!engine.setOptionDefault(PROP_PASSWORD,
386                  DEFAULT_ADMIN_PASSWORD))
387          {
388             engine.setAdminPassword((String JavaDoc)engine.getOption(PROP_PASSWORD));
389          }
390       }
391    }
392
393    /**
394     * (Re-)load the global options from the registry.
395     */

396    public void refreshGlobalOptions() throws ConfigurationException
397    {
398       Hashtable JavaDoc globalOptions = config.getGlobalOptions();
399       if (globalOptions != null)
400          setOptions(globalOptions);
401
402       normaliseOptions(this);
403    }
404
405    /**
406     * accessor only, for application session
407     * (could call it "engine session" instead, but named with reference
408     * to Apache SOAP's notion of "application scope")
409     */

410    public Session getApplicationSession()
411    {
412       return session;
413    }
414
415    public ClassCache getClassCache()
416    {
417       // disabled, until I know how to do this in jboss
418
// TDI 09-June-2004
419
return null;
420    }
421
422 }
423
Popular Tags