KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > client > ClientEngine


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

7
8 // $Id: ClientEngine.java,v 1.8.2.3 2005/04/12 16:07:31 starksm Exp $
9
package org.jboss.webservice.client;
10
11 // $Id: ClientEngine.java,v 1.8.2.3 2005/04/12 16:07:31 starksm Exp $
12

13 import org.jboss.axis.AxisFault;
14 import org.jboss.axis.ConfigurationException;
15 import org.jboss.axis.EngineConfiguration;
16 import org.jboss.axis.Handler;
17 import org.jboss.axis.MessageContext;
18 import org.jboss.axis.client.AxisClient;
19 import org.jboss.axis.client.Call;
20 import org.jboss.axis.utils.Messages;
21 import org.jboss.logging.Logger;
22 import org.jboss.webservice.handler.ClientHandlerChain;
23
24 import javax.xml.rpc.JAXRPCException JavaDoc;
25 import javax.xml.rpc.handler.HandlerChain JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30
31 /**
32  * The ws4ee client engine.
33  * <p/>
34  * Note, every service-ref client maintains an instance of this client engine.
35  *
36  * @author Thomas.Diesler@jboss.org
37  * @since 11-May-2004
38  */

39 public class ClientEngine extends AxisClient
40 {
41    /** @since 4.0.2 */
42    static final long serialVersionUID = 53844570311989118L;
43    protected static Logger log = Logger.getLogger(ClientEngine.class);
44
45    // Maps the port binding QName to the client HandlerChain
46
private Map JavaDoc handlerChainMap = new HashMap JavaDoc();
47
48    /**
49     * Constructs the client engine with a given configuration.
50     */

51    public ClientEngine(EngineConfiguration config)
52    {
53       super(config);
54    }
55
56    /**
57     * Register a handler chain for the given port name
58     */

59    public void registerHandlerChain(String JavaDoc portName, List JavaDoc infos, Set JavaDoc roles)
60    {
61       ClientHandlerChain chain = new ClientHandlerChain(infos, roles);
62       handlerChainMap.put(portName, chain);
63    }
64
65    /**
66     * Main routine of the AXIS engine. In short we locate the appropriate
67     * handler for the desired service and invoke() it.
68     */

69    public void invoke(MessageContext msgContext) throws AxisFault
70    {
71       log.debug("invoke: " + msgContext);
72
73       String JavaDoc hName = null;
74       Handler handler = null;
75
76       // save previous context
77
MessageContext previousContext = getCurrentMessageContext();
78
79       try
80       {
81          // set active context
82
setCurrentMessageContext(msgContext);
83
84          // Now we do the 'real' work. The flow is basically:
85
//
86
// Global Request Chain
87
// Service Ref Request Handler Chain
88
// Transport Request Chain - must have a send at the end
89
// Transport Response Chain
90
// Service Ref Response Handler Chain
91
// Global Response Chain
92
//************************************************************
93

94          msgContext.setPastPivot(false);
95
96          ClientHandlerChain handlerChain = (ClientHandlerChain)getHandlerChain(msgContext);
97
98          // The CTS resets its HanderTracker after the JNDI lookup
99
// So if we init the handlers during lookup, that won't be tracked
100
if (handlerChain.getState() == ClientHandlerChain.STATE_CREATED)
101          {
102             // what is the config for a handler chain?
103
handlerChain.init(null);
104          }
105
106          // Process the Global Request Chain
107
// ********************************
108
if ((handler = getGlobalRequest()) != null)
109             handler.invoke(msgContext);
110
111          // Process the Request Handlers
112
if (handlerChain.handleRequest(msgContext) == false)
113          {
114             // revert direction and change into a reply message
115
log.warn("FIXME: handlerChain.handleRequest() returned false");
116             return;
117          }
118
119          /** Process the Transport Specific stuff
120           *
121           * NOTE: Somewhere in here there is a handler which actually
122           * sends the message and receives a response. Generally
123           * this is the pivot point in the Transport chain.
124           */

125          hName = msgContext.getTransportName();
126          if (hName != null && (handler = getTransport(hName)) != null)
127          {
128             handler.invoke(msgContext);
129          }
130          else
131          {
132             throw new AxisFault(Messages.getMessage("noTransport00", hName));
133          }
134
135          // [todo] detect if we got a fault msg
136
boolean isFaultMessage = false;
137
138          // Process the Fault Handlers
139
if (isFaultMessage && handlerChain.handleFault(msgContext) == false)
140          {
141             // skip over the remaining message handlers in the chain and go strait to the client
142
log.warn("FIXME: handlerChain.handleFault() returned false");
143             return;
144          }
145
146          // Process the Response Handlers
147
else if (handlerChain.handleResponse(msgContext) == false)
148          {
149             // skip over the remaining message handlers in the chain and go strait to the client
150
log.warn("FIXME: handlerChain.handleResponse() returned false");
151             return;
152          }
153
154          /* Process the Global Response Chain */
155          /***********************************/
156          if ((handler = getGlobalResponse()) != null)
157          {
158             handler.invoke(msgContext);
159          }
160       }
161       catch (ConfigurationException e)
162       {
163          throw new IllegalStateException JavaDoc(e.toString());
164       }
165       finally
166       {
167          // restore previous state
168
setCurrentMessageContext(previousContext);
169       }
170    }
171
172    /**
173     * Returns the handler chain for the current message context
174     *
175     * @return
176     */

177    private HandlerChain getHandlerChain(MessageContext msgContext)
178    {
179       HandlerChain handlerChain = null;
180
181       // Return an empty handler chain
182
if (handlerChainMap.size() == 0)
183          handlerChain = new ClientHandlerChain(null, null);
184
185       // We only have one handler chain associated to one port, return it
186
if (handlerChainMap.size() == 1)
187       {
188          String JavaDoc portName = (String JavaDoc)handlerChainMap.keySet().iterator().next();
189          handlerChain = (HandlerChain)handlerChainMap.get(portName);
190          log.debug("Using handler chain for port: " + portName);
191       }
192
193       Call call = (Call)msgContext.getProperty(MessageContext.CALL);
194       if (call == null)
195          throw new JAXRPCException JavaDoc("Cannot obtain current call");
196
197       if (call.getPortName() != null)
198       {
199          String JavaDoc portName = call.getPortName().getLocalPart();
200          handlerChain = (HandlerChain)handlerChainMap.get(portName);
201          log.debug("Using handler chain for port: " + portName);
202       }
203
204       // return an empty handler chain
205
if (handlerChain == null)
206       {
207          handlerChain = new ClientHandlerChain(null, null);
208          log.debug("Using empty handler chain");
209       }
210
211       return handlerChain;
212    }
213 }
214
215
Popular Tags