KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > routing > SEIImplHandler


1 package org.objectweb.celtix.routing;
2
3 import java.lang.reflect.InvocationHandler JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.lang.reflect.Proxy JavaDoc;
6 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
7 import java.net.MalformedURLException JavaDoc;
8 import java.net.URL JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.logging.Level JavaDoc;
13 import java.util.logging.Logger JavaDoc;
14
15 import javax.annotation.Resource;
16 import javax.wsdl.Definition;
17 import javax.wsdl.Port;
18 import javax.xml.namespace.QName JavaDoc;
19 import javax.xml.ws.BindingProvider;
20 import javax.xml.ws.ProtocolException;
21 import javax.xml.ws.Service;
22 import javax.xml.ws.WebServiceContext;
23 import javax.xml.ws.WebServiceException;
24 import javax.xml.ws.handler.MessageContext;
25
26 import org.objectweb.celtix.common.i18n.Message;
27 import org.objectweb.celtix.common.logging.LogUtils;
28 import org.objectweb.celtix.routing.configuration.DestinationType;
29 import org.objectweb.celtix.routing.configuration.RouteType;
30
31 public class SEIImplHandler implements InvocationHandler JavaDoc {
32     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(SEIImplHandler.class);
33     protected List JavaDoc<Object JavaDoc> proxyList;
34     private final Definition wsdlModel;
35     private final RouteType route;
36     private final URL JavaDoc wsdlLocation;
37     private boolean doInit;
38     /**
39      * Injectable context.
40      */

41     @Resource
42     private WebServiceContext wsCtx;
43
44     public SEIImplHandler(Definition model, RouteType rt) {
45         wsdlModel = model;
46         route = rt;
47         try {
48             wsdlLocation = new URL JavaDoc(wsdlModel.getDocumentBaseURI());
49         } catch (MalformedURLException JavaDoc mue) {
50             throw new WebServiceException("Invalid wsdl url", mue);
51         }
52         doInit = true;
53     }
54
55     @Resource
56     public void setContext(WebServiceContext ctx) {
57         wsCtx = ctx;
58     }
59
60     public WebServiceContext getContext() {
61         return wsCtx;
62     }
63     
64     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Exception JavaDoc {
65         if (doInit) {
66             init(method.getDeclaringClass());
67         }
68         
69         //TODO Iterate over the list for fanin/fanout
70
Object JavaDoc clientProxy = proxyList.get(0);
71         Object JavaDoc ret = null;
72         if (Proxy.isProxyClass(clientProxy.getClass())
73             && BindingProvider.class.isAssignableFrom(clientProxy.getClass())) {
74             //Proxy instance inherits out of BindingProvider Interface
75
//as per JAXWS spec 4.2.3
76
BindingProvider bp = (BindingProvider) clientProxy;
77             Exception JavaDoc ex = null;
78             
79             //Synchrnization will not be required if the client proxies are thread safe.
80
synchronized (this) {
81                 updateRequestContext(bp.getRequestContext());
82                 
83                 InvocationHandler JavaDoc proxyHandler = Proxy.getInvocationHandler(clientProxy);
84                 try {
85                     ret = proxyHandler.invoke(clientProxy, method, args);
86                 } catch (UndeclaredThrowableException JavaDoc ute) {
87                     LOG.log(Level.SEVERE, "PROXY_INVOKE_UNDECLEXCEPTION", method.toString());
88                     ex = new ProtocolException(new Message("PROXY_INVOKE_UNDECLEXCEPTION",
89                                                            LOG,
90                                                            method.toString()).toString(),
91                                                ute.getCause());
92                 } catch (Error JavaDoc error) {
93                     LOG.log(Level.SEVERE, "PROXY_INVOKE_ERROR", method.toString());
94                     ex = new ProtocolException(new Message("PROXY_INVOKE_UNDECLEXCEPTION",
95                                                            LOG,
96                                                            method.toString()).toString(),
97                                                error);
98                 } catch (Throwable JavaDoc t) {
99                     LOG.log(Level.WARNING, "PROXY_INVOKE_EXCEPTION", method.toString());
100                     ex = (Exception JavaDoc) t;
101                 }
102                 
103                 updateWebServiceContext(bp.getResponseContext());
104             }
105                 
106             if (null != ex) {
107                 throw ex;
108             }
109         }
110         
111         return ret;
112     }
113     
114     protected synchronized void init(Class JavaDoc<?> seiClass) {
115         if (doInit) {
116             List JavaDoc<DestinationType> dtList = route.getDestination();
117             if (null == proxyList) {
118                 proxyList = new ArrayList JavaDoc<Object JavaDoc>(dtList.size());
119             }
120
121             for (DestinationType dt : dtList) {
122                 Service dtService = createService(wsdlLocation, dt.getService());
123                 QName JavaDoc portName;
124                 if (dt.isSetPort()) {
125                     portName = new QName JavaDoc(dt.getService().getNamespaceURI(), dt.getPort());
126                 } else {
127                     javax.wsdl.Service destService =
128                         wsdlModel.getService(dt.getService());
129                     String JavaDoc name = ((Port)destService.getPorts().values().iterator()).getName();
130                     portName = new QName JavaDoc(dt.getService().getNamespaceURI(), name);
131                 }
132                 
133                 Object JavaDoc proxy = dtService.getPort(portName, seiClass);
134                 if (null == proxy) {
135                     LOG.log(Level.SEVERE,
136                             "GETPORT_FAILURE",
137                             new Object JavaDoc[] {dt.getService(), portName.toString()});
138                     throw new WebServiceException(new Message("GETPORT_FAILURE",
139                                                               LOG,
140                                                               dt.getService(),
141                                                               portName).toString());
142                 }
143                 proxyList.add(proxy);
144             }
145             doInit = false;
146         }
147     }
148     
149     protected Service createService(URL JavaDoc wsdlUrl, QName JavaDoc serviceName) {
150         //TODO Set Executor used by the Source Endpoint onto Service
151
//Currently destination service uses bus workqueue.
152
return Service.create(wsdlUrl, serviceName);
153     }
154     
155     private void updateRequestContext(Map JavaDoc<String JavaDoc, Object JavaDoc> reqCtx) {
156         if (null != getContext()) {
157             MessageContext sourceMsgCtx = getContext().getMessageContext();
158             reqCtx.put(BindingProvider.USERNAME_PROPERTY,
159                        sourceMsgCtx.get(BindingProvider.USERNAME_PROPERTY));
160             reqCtx.put(BindingProvider.PASSWORD_PROPERTY,
161                        sourceMsgCtx.get(BindingProvider.PASSWORD_PROPERTY));
162         }
163     }
164     
165     private void updateWebServiceContext(Map JavaDoc<String JavaDoc, Object JavaDoc> respCtx) {
166         //TODO
167
}
168 }
169
Popular Tags