KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > server > AxisServer


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-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.server;
57
58 import org.jboss.axis.AxisEngine;
59 import org.jboss.axis.AxisFault;
60 import org.jboss.axis.AxisProperties;
61 import org.jboss.axis.Constants;
62 import org.jboss.axis.EngineConfiguration;
63 import org.jboss.axis.Handler;
64 import org.jboss.axis.Message;
65 import org.jboss.axis.MessageContext;
66 import org.jboss.axis.SimpleTargetedChain;
67 import org.jboss.axis.client.AxisClient;
68 import org.jboss.axis.configuration.EngineConfigurationFactoryFinder;
69 import org.jboss.axis.soap.SOAPConstants;
70 import org.jboss.axis.utils.ClassUtils;
71 import org.jboss.axis.utils.Messages;
72 import org.jboss.logging.Logger;
73
74 import java.util.Map JavaDoc;
75
76 /**
77  * @author Doug Davis (dug@us.ibm.com)
78  * @author Glen Daniels (gdaniels@allaire.com)
79  */

80 public class AxisServer extends AxisEngine
81 {
82    private static Logger log = Logger.getLogger(AxisServer.class.getName());
83    private static Logger tlog = Logger.getLogger("org.jboss.axis.TIME");
84
85    private static AxisServerFactory factory = null;
86
87    public static AxisServer getServer(Map JavaDoc environment) throws AxisFault
88    {
89       if (factory == null)
90       {
91          String JavaDoc factoryClassName = AxisProperties.getProperty("axis.ServerFactory");
92          if (factoryClassName != null)
93          {
94             try
95             {
96                Class JavaDoc factoryClass = ClassUtils.forName(factoryClassName);
97                if (AxisServerFactory.class.isAssignableFrom(factoryClass))
98                   factory = (AxisServerFactory)factoryClass.newInstance();
99             }
100             catch (Exception JavaDoc e)
101             {
102                // If something goes wrong here, should we just fall
103
// through and use the default one?
104
log.error(Messages.getMessage("exception00"), e);
105             }
106          }
107
108          if (factory == null)
109          {
110             factory = new DefaultAxisServerFactory();
111          }
112       }
113
114       return factory.getServer(environment);
115    }
116
117    /**
118     * the AxisClient to be used by outcalling Services
119     */

120    private AxisEngine clientEngine;
121
122    public AxisServer()
123    {
124       this(EngineConfigurationFactoryFinder.newFactory().getServerEngineConfig());
125    }
126
127    public AxisServer(EngineConfiguration config)
128    {
129       super(config);
130       // Server defaults to persisting configuration
131
setShouldSaveConfig(true);
132    }
133
134    /**
135     * Is this server active? If this is false, any requests will
136     * cause a SOAP Server fault to be generated.
137     */

138    private boolean running = true;
139
140    public boolean isRunning()
141    {
142       return running;
143    }
144
145    /**
146     * Start the server.
147     */

148    public void start()
149    {
150       // re-init...
151
init();
152       running = true;
153    }
154
155    /**
156     * Stop the server.
157     */

158    public void stop()
159    {
160       running = false;
161    }
162
163    /**
164     * Get this server's client engine. Create it if it does
165     * not yet exist.
166     */

167    public synchronized AxisEngine getClientEngine()
168    {
169       if (clientEngine == null)
170       {
171          clientEngine = new AxisClient(); // !!!!
172
}
173       return clientEngine;
174    }
175
176    /**
177     * Main routine of the AXIS server. In short we locate the appropriate
178     * handler for the desired service and invoke() it.
179     */

180    public void invoke(MessageContext msgContext) throws AxisFault
181    {
182       long t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0;
183       if (tlog.isDebugEnabled())
184       {
185          t0 = System.currentTimeMillis();
186       }
187
188       if (log.isDebugEnabled())
189       {
190          log.debug("Enter: AxisServer::invoke");
191       }
192
193       if (!isRunning())
194       {
195          throw new AxisFault("Server.disabled",
196                  Messages.getMessage("serverDisabled00"),
197                  null, null);
198       }
199
200       String JavaDoc hName = null;
201       Handler h = null;
202
203       // save previous context
204
MessageContext previousContext = getCurrentMessageContext();
205
206       try
207       {
208          // set active context
209
setCurrentMessageContext(msgContext);
210
211          hName = msgContext.getStrProp(MessageContext.ENGINE_HANDLER);
212          if (hName != null)
213          {
214             if ((h = getHandler(hName)) == null)
215             {
216                ClassLoader JavaDoc cl = msgContext.getClassLoader();
217                try
218                {
219                   log.debug(Messages.getMessage("tryingLoad00", hName));
220                   Class JavaDoc cls = ClassUtils.forName(hName, true, cl);
221                   h = (Handler)cls.newInstance();
222                }
223                catch (Exception JavaDoc e)
224                {
225                   h = null;
226                }
227             }
228             if (tlog.isDebugEnabled())
229             {
230                t1 = System.currentTimeMillis();
231             }
232             if (h != null)
233                h.invoke(msgContext);
234             else
235                throw new AxisFault("Server.error",
236                        Messages.getMessage("noHandler00", hName),
237                        null, null);
238             if (tlog.isDebugEnabled())
239             {
240                t2 = System.currentTimeMillis();
241                tlog.debug("AxisServer.invoke " + hName + " invoke=" +
242                        (t2 - t1) + " pre=" + (t1 - t0));
243             }
244
245          }
246          else
247          {
248             // This really should be in a handler - but we need to discuss it
249
// first - to make sure that's what we want.
250
/* Now we do the 'real' work. The flow is basically: */
251             /* Transport Specific Request Handler/Chain */
252             /* Global Request Handler/Chain */
253             /* Protocol Specific-Handler(ie. SOAP, XP) */
254             /* ie. For SOAP Handler: */
255             /* - Service Specific Request Handler/Chain */
256             /* - SOAP Semantic Checks */
257             /* - Service Specific Response Handler/Chain */
258             /* Global Response Handler/Chain */
259             /* Transport Specific Response Handler/Chain */
260             /**************************************************************/
261
262             // When do we call init/cleanup??
263
if (log.isDebugEnabled())
264             {
265                log.debug(Messages.getMessage("defaultLogic00"));
266             }
267
268             /* This is what the entirety of this logic might evolve to:
269
270             hName = msgContext.getStrProp(MessageContext.TRANSPORT);
271             if ( hName != null ) {
272             if ((h = hr.find( hName )) != null ) {
273             h.invoke(msgContext);
274             } else {
275             log.error(Messages.getMessage("noTransport02", hName));
276             }
277             } else {
278             // No transport set, so use the default (probably just
279             // calls the global->service handlers)
280             defaultTransport.invoke(msgContext);
281             }
282
283             */

284
285             /* Process the Transport Specific Request Chain */
286             /**********************************************/
287             hName = msgContext.getTransportName();
288             SimpleTargetedChain transportChain = null;
289
290             if (log.isDebugEnabled())
291                log.debug(Messages.getMessage("transport01", "AxisServer.invoke", hName));
292
293             if (tlog.isDebugEnabled())
294             {
295                t1 = System.currentTimeMillis();
296             }
297             if (hName != null && (h = getTransport(hName)) != null)
298             {
299                if (h instanceof SimpleTargetedChain)
300                {
301                   transportChain = (SimpleTargetedChain)h;
302                   h = transportChain.getRequestHandler();
303                   if (h != null)
304                      h.invoke(msgContext);
305                }
306             }
307
308             if (tlog.isDebugEnabled())
309             {
310                t2 = System.currentTimeMillis();
311             }
312             /* Process the Global Request Chain */
313             /**********************************/
314             if ((h = getGlobalRequest()) != null)
315             {
316                h.invoke(msgContext);
317             }
318
319             /**
320              * At this point, the service should have been set by someone
321              * (either the originator of the MessageContext, or one of the
322              * transport or global Handlers). If it hasn't been set, we
323              * fault.
324              */

325             h = msgContext.getService();
326             if (h == null)
327             {
328                // It's possible that we haven't yet parsed the
329
// message at this point. This is a kludge to
330
// make sure we have. There probably wants to be
331
// some kind of declarative "parse point" on the handler
332
// chain instead....
333
Message rm = msgContext.getRequestMessage();
334                rm.getSOAPEnvelope().getFirstBody();
335
336                h = msgContext.getService();
337                if (h == null)
338                   throw new AxisFault("Server.NoService",
339                           Messages.getMessage("noService05",
340                                   "" + msgContext.getTargetService()),
341                           null, null);
342             }
343             if (tlog.isDebugEnabled())
344             {
345                t3 = System.currentTimeMillis();
346             }
347
348             // Ensure that if we get SOAP1.2, then reply using SOAP1.2
349
if (msgContext.getRequestMessage().getSOAPEnvelope().getSOAPConstants() != null)
350             {
351                SOAPConstants soapConstants = msgContext.getRequestMessage().getSOAPEnvelope().getSOAPConstants();
352                msgContext.setSOAPConstants(soapConstants);
353             }
354
355             h.invoke(msgContext);
356
357             if (tlog.isDebugEnabled())
358             {
359                t4 = System.currentTimeMillis();
360             }
361
362             /* Process the Global Response Chain */
363             /***********************************/
364             if ((h = getGlobalResponse()) != null)
365                h.invoke(msgContext);
366
367             /* Process the Transport Specific Response Chain */
368             /***********************************************/
369             if (transportChain != null)
370             {
371                h = transportChain.getResponseHandler();
372                if (h != null)
373                   h.invoke(msgContext);
374             }
375
376             if (tlog.isDebugEnabled())
377             {
378                t5 = System.currentTimeMillis();
379                tlog.debug("AxisServer.invoke2 " +
380                        " preTr=" +
381                        (t1 - t0) + " tr=" + (t2 - t1) +
382                        " preInvoke=" + (t3 - t2) +
383                        " invoke=" + (t4 - t3) +
384                        " postInvoke=" + (t5 - t4) +
385                        " " + msgContext.getTargetService() + "." +
386                        ((msgContext.getOperation() == null) ?
387                        "" : msgContext.getOperation().getName()));
388             }
389
390          }
391       }
392       catch (AxisFault e)
393       {
394          throw e;
395       }
396       catch (Exception JavaDoc e)
397       {
398          // Should we even bother catching it ?
399
throw AxisFault.makeFault(e);
400
401       }
402       finally
403       {
404          // restore previous state
405
setCurrentMessageContext(previousContext);
406       }
407
408       if (log.isDebugEnabled())
409       {
410          log.debug("Exit: AxisServer::invoke");
411       }
412    }
413
414    /**
415     *
416     */

417    public void generateWSDL(MessageContext msgContext) throws AxisFault
418    {
419       if (log.isDebugEnabled())
420       {
421          log.debug("Enter: AxisServer::generateWSDL");
422       }
423
424       if (!isRunning())
425       {
426          throw new AxisFault("Server.disabled",
427                  Messages.getMessage("serverDisabled00"),
428                  null, null);
429       }
430
431       String JavaDoc hName = null;
432       Handler h = null;
433
434       // save previous context
435
MessageContext previousContext = getCurrentMessageContext();
436
437       try
438       {
439          // set active context
440
setCurrentMessageContext(msgContext);
441
442          hName = msgContext.getStrProp(MessageContext.ENGINE_HANDLER);
443          if (hName != null)
444          {
445             if ((h = getHandler(hName)) == null)
446             {
447                ClassLoader JavaDoc cl = msgContext.getClassLoader();
448                try
449                {
450                   log.debug(Messages.getMessage("tryingLoad00", hName));
451                   Class JavaDoc cls = ClassUtils.forName(hName, true, cl);
452                   h = (Handler)cls.newInstance();
453                }
454                catch (Exception JavaDoc e)
455                {
456                   throw new AxisFault("Server.error",
457                           Messages.getMessage("noHandler00", hName),
458                           null, null);
459                }
460             }
461             h.generateWSDL(msgContext);
462          }
463          else
464          {
465             // This really should be in a handler - but we need to discuss it
466
// first - to make sure that's what we want.
467
/* Now we do the 'real' work. The flow is basically: */
468             /* Transport Specific Request Handler/Chain */
469             /* Global Request Handler/Chain */
470             /* Protocol Specific-Handler(ie. SOAP, XP) */
471             /* ie. For SOAP Handler: */
472             /* - Service Specific Request Handler/Chain */
473             /* - SOAP Semantic Checks */
474             /* - Service Specific Response Handler/Chain */
475             /* Global Response Handler/Chain */
476             /* Transport Specific Response Handler/Chain */
477             /**************************************************************/
478
479             // When do we call init/cleanup??
480
log.debug(Messages.getMessage("defaultLogic00"));
481
482             /* This is what the entirety of this logic might evolve to:
483
484             hName = msgContext.getStrProp(MessageContext.TRANSPORT);
485             if ( hName != null ) {
486             if ((h = hr.find( hName )) != null ) {
487             h.generateWSDL(msgContext);
488             } else {
489             log.error(Messages.getMessage("noTransport02", hName));
490             }
491             } else {
492             // No transport set, so use the default (probably just
493             // calls the global->service handlers)
494             defaultTransport.generateWSDL(msgContext);
495             }
496
497             */

498
499             /* Process the Transport Specific Request Chain */
500             /**********************************************/
501             hName = msgContext.getTransportName();
502             SimpleTargetedChain transportChain = null;
503
504             if (log.isDebugEnabled())
505                log.debug(Messages.getMessage("transport01",
506                        "AxisServer.generateWSDL",
507                        hName));
508             if (hName != null && (h = getTransport(hName)) != null)
509             {
510                if (h instanceof SimpleTargetedChain)
511                {
512                   transportChain = (SimpleTargetedChain)h;
513                   h = transportChain.getRequestHandler();
514                   if (h != null)
515                   {
516                      h.generateWSDL(msgContext);
517                   }
518                }
519             }
520
521             /* Process the Global Request Chain */
522             /**********************************/
523             if ((h = getGlobalRequest()) != null)
524                h.generateWSDL(msgContext);
525
526             /**
527              * At this point, the service should have been set by someone
528              * (either the originator of the MessageContext, or one of the
529              * transport or global Handlers). If it hasn't been set, we
530              * fault.
531              */

532             h = msgContext.getService();
533             if (h == null)
534             {
535                // It's possible that we haven't yet parsed the
536
// message at this point. This is a kludge to
537
// make sure we have. There probably wants to be
538
// some kind of declarative "parse point" on the handler
539
// chain instead....
540
Message rm = msgContext.getRequestMessage();
541                if (rm != null)
542                {
543                   rm.getSOAPEnvelope().getFirstBody();
544                   h = msgContext.getService();
545                }
546                if (h == null)
547                {
548                   throw new AxisFault(Constants.QNAME_NO_SERVICE_FAULT_CODE,
549                           Messages.getMessage("noService05",
550                                   "" + msgContext.getTargetService()),
551                           null, null);
552                }
553             }
554
555             h.generateWSDL(msgContext);
556
557             /* Process the Global Response Chain */
558             /***********************************/
559             if ((h = getGlobalResponse()) != null)
560                h.generateWSDL(msgContext);
561
562             /* Process the Transport Specific Response Chain */
563             /***********************************************/
564             if (transportChain != null)
565             {
566                h = transportChain.getResponseHandler();
567                if (h != null)
568                {
569                   h.generateWSDL(msgContext);
570                }
571             }
572          }
573       }
574       catch (AxisFault e)
575       {
576          throw e;
577       }
578       catch (Exception JavaDoc e)
579       {
580          // Should we even bother catching it ?
581
throw AxisFault.makeFault(e);
582       }
583       finally
584       {
585          // restore previous state
586
setCurrentMessageContext(previousContext);
587       }
588
589       if (log.isDebugEnabled())
590       {
591          log.debug("Exit: AxisServer::generateWSDL");
592       }
593    }
594 }
595
Popular Tags