KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > server > ServerEngine


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.webservice.server;
8
9 // $Id: ServerEngine.java,v 1.8.2.3 2005/04/22 11:28:31 tdiesler Exp $
10

11 import org.jboss.axis.AxisFault;
12 import org.jboss.axis.Constants;
13 import org.jboss.axis.EngineConfiguration;
14 import org.jboss.axis.Handler;
15 import org.jboss.axis.MessageContext;
16 import org.jboss.axis.SimpleTargetedChain;
17 import org.jboss.axis.description.OperationDesc;
18 import org.jboss.axis.server.AxisServer;
19 import org.jboss.axis.soap.SOAPConstants;
20 import org.jboss.axis.utils.Messages;
21 import org.jboss.logging.Logger;
22
23 import javax.xml.soap.SOAPException JavaDoc;
24
25 /**
26  * The ws4ee server engine.
27  * <p/>
28  * Note, this instance is shared by all deployed web services.
29  *
30  * @author Thomas.Diesler@jboss.org
31  * @since 15-April-2004
32  */

33 public class ServerEngine extends AxisServer
34 {
35    /** @since 4.0.2 */
36    static final long serialVersionUID = 5160872757245559530L;
37    // provide logging
38
private static final Logger log = Logger.getLogger(ServerEngine.class);
39
40    /**
41     * Construct an AxisServer with a given configuration.
42     */

43    public ServerEngine(EngineConfiguration config)
44    {
45       super(config);
46       log.debug("new ServerEngine [config=" + config + "]");
47    }
48
49    /**
50     * Main routine of the AXIS server. In short we locate the appropriate
51     * handler for the desired service and invoke() it.
52     */

53    public void invoke(final MessageContext msgContext) throws AxisFault
54    {
55       try
56       {
57          // Force deserialization
58
long t1 = System.currentTimeMillis();
59          msgContext.getMessage().getSOAPPart().getEnvelope();
60          long t2 = System.currentTimeMillis();
61          Logger.getLogger(Constants.TIME_LOG_CATEGORY).debug("Get SOAPEnvelope in " + (t2 - t1) + "ms");
62
63          OperationDesc currOperation = msgContext.getOperation();
64          if (currOperation != null && currOperation.isOneWay())
65          {
66             Thread JavaDoc th = new Thread JavaDoc()
67             {
68                public void run()
69                {
70                   try
71                   {
72                      invokeInternal(msgContext);
73                   }
74                   catch (AxisFault fault)
75                   {
76                      log.error("Asyncronous Server error: " + fault.dumpToString());
77                   }
78                }
79             };
80             th.start();
81
82             msgContext.setResponseMessage(null);
83
84             log.debug("Returning immediately from one-way operation: " + currOperation.getName());
85             return;
86          }
87
88          // invoke the service endpoint
89
invokeInternal(msgContext);
90       }
91       catch (AxisFault af)
92       {
93          log.error("Server error: " + af.dumpToString());
94          throw af;
95       }
96       catch (SOAPException JavaDoc ex)
97       {
98          log.error("Server error", ex);
99          throw AxisFault.makeFault(ex);
100       }
101       catch (RuntimeException JavaDoc ex)
102       {
103          log.error("Server error", ex);
104          throw ex;
105       }
106    }
107
108    /**
109     * Do the server side handler stuff and invoke the service endpoint
110     */

111    private void invokeInternal(MessageContext msgContext) throws AxisFault
112    {
113       log.debug("invoke: " + msgContext);
114
115       long t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0;
116
117       if (!isRunning())
118       {
119          throw new AxisFault("Server.disabled",
120                  Messages.getMessage("serverDisabled00"),
121                  null, null);
122       }
123
124       String JavaDoc hName = null;
125       Handler handler = null;
126
127       // save previous context
128
MessageContext previousContext = getCurrentMessageContext();
129
130       try
131       {
132          // set active context
133
setCurrentMessageContext(msgContext);
134
135          // Now we do the 'real' work. The flow is basically:
136
// Transport Specific Request Handler/Chain
137
// Global Request Handler/Chain
138
// Protocol Specific-Handler(ie. SOAP, XP)
139
// ie. For SOAP Handler:
140
// - Service Specific Request Handler/Chain
141
// - SOAP Semantic Checks
142
// - Service Specific Response Handler/Chain
143
// Global Response Handler/Chain
144
// Transport Specific Response Handler/Chain
145
/**************************************************************/
146
147          /* Process the Transport Specific Request Chain */
148          /**********************************************/
149          hName = msgContext.getTransportName();
150          SimpleTargetedChain transportChain = null;
151
152          log.debug("TransportHandler: " + hName);
153
154          // time before transport handler
155
t1 = System.currentTimeMillis();
156
157          if (hName != null && (handler = getTransport(hName)) != null)
158          {
159             if (handler instanceof SimpleTargetedChain)
160             {
161                transportChain = (SimpleTargetedChain)handler;
162                handler = transportChain.getRequestHandler();
163                if (handler != null)
164                   handler.invoke(msgContext);
165             }
166          }
167
168          // time after transport handler
169
t2 = System.currentTimeMillis();
170
171          /* Process the Global Request Chain */
172          /**********************************/
173          if ((handler = getGlobalRequest()) != null)
174             handler.invoke(msgContext);
175
176          // time after optional parse
177
t3 = System.currentTimeMillis();
178
179          /**
180           * At this point, the service should have been set by someone
181           * (either the originator of the MessageContext, or one of the
182           * transport or global Handlers). If it hasn't been set, we
183           * fault.
184           */

185          handler = msgContext.getService();
186          if (handler == null)
187          {
188             throw new AxisFault("Server.NoService",
189                     Messages.getMessage("noService05", msgContext.getTargetService()),
190                     null, null);
191          }
192
193          // Ensure that if we get SOAP1.2, then reply using SOAP1.2
194
if (msgContext.getSOAPConstants() != null)
195          {
196             SOAPConstants soapConstants = msgContext.getSOAPConstants();
197             msgContext.setSOAPConstants(soapConstants);
198          }
199
200          handler.invoke(msgContext);
201
202          // time after endpoint invocation
203
t4 = System.currentTimeMillis();
204
205          // Process the Global Response Chain
206
if ((handler = getGlobalResponse()) != null)
207             handler.invoke(msgContext);
208
209          // time after global handlers
210
t5 = System.currentTimeMillis();
211
212          // Process the Transport Specific Response Chain
213
if (transportChain != null)
214          {
215             handler = transportChain.getResponseHandler();
216             if (handler != null)
217                handler.invoke(msgContext);
218          }
219
220          // time after transport handlers
221
t6 = System.currentTimeMillis();
222
223          if (log.isTraceEnabled())
224          {
225             String JavaDoc serviceName = msgContext.getTargetService();
226             if (msgContext.getOperation() != null)
227                serviceName += "." + msgContext.getOperation().getName();
228
229             log.trace(serviceName + " [" +
230                     "trIn=" + (t2 - t1) +
231                     ",glIn=" + (t3 - t2) +
232                     ",invoke=" + (t4 - t3) +
233                     ",glOut=" + (t5 - t4) +
234                     ",trOut=" + (t6 - t5) +
235                     "]");
236          }
237       }
238       catch (AxisFault e)
239       {
240          throw e;
241       }
242       catch (Exception JavaDoc e)
243       {
244          // Should we even bother catching it ?
245
throw AxisFault.makeFault(e);
246
247       }
248       finally
249       {
250          // restore previous state
251
setCurrentMessageContext(previousContext);
252       }
253    }
254 }
255
Popular Tags