1 package org.objectweb.celtix.routing; 2 3 import java.lang.reflect.InvocationHandler ; 4 import java.lang.reflect.Method ; 5 import java.lang.reflect.Proxy ; 6 import java.lang.reflect.UndeclaredThrowableException ; 7 import java.net.MalformedURLException ; 8 import java.net.URL ; 9 import java.util.ArrayList ; 10 import java.util.List ; 11 import java.util.Map ; 12 import java.util.logging.Level ; 13 import java.util.logging.Logger ; 14 15 import javax.annotation.Resource; 16 import javax.wsdl.Definition; 17 import javax.wsdl.Port; 18 import javax.xml.namespace.QName ; 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 { 32 private static final Logger LOG = LogUtils.getL7dLogger(SEIImplHandler.class); 33 protected List <Object > proxyList; 34 private final Definition wsdlModel; 35 private final RouteType route; 36 private final URL wsdlLocation; 37 private boolean doInit; 38 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 (wsdlModel.getDocumentBaseURI()); 49 } catch (MalformedURLException 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 invoke(Object proxy, Method method, Object [] args) throws Exception { 65 if (doInit) { 66 init(method.getDeclaringClass()); 67 } 68 69 Object clientProxy = proxyList.get(0); 71 Object ret = null; 72 if (Proxy.isProxyClass(clientProxy.getClass()) 73 && BindingProvider.class.isAssignableFrom(clientProxy.getClass())) { 74 BindingProvider bp = (BindingProvider) clientProxy; 77 Exception ex = null; 78 79 synchronized (this) { 81 updateRequestContext(bp.getRequestContext()); 82 83 InvocationHandler proxyHandler = Proxy.getInvocationHandler(clientProxy); 84 try { 85 ret = proxyHandler.invoke(clientProxy, method, args); 86 } catch (UndeclaredThrowableException 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 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 t) { 99 LOG.log(Level.WARNING, "PROXY_INVOKE_EXCEPTION", method.toString()); 100 ex = (Exception ) 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 <?> seiClass) { 115 if (doInit) { 116 List <DestinationType> dtList = route.getDestination(); 117 if (null == proxyList) { 118 proxyList = new ArrayList <Object >(dtList.size()); 119 } 120 121 for (DestinationType dt : dtList) { 122 Service dtService = createService(wsdlLocation, dt.getService()); 123 QName portName; 124 if (dt.isSetPort()) { 125 portName = new QName (dt.getService().getNamespaceURI(), dt.getPort()); 126 } else { 127 javax.wsdl.Service destService = 128 wsdlModel.getService(dt.getService()); 129 String name = ((Port)destService.getPorts().values().iterator()).getName(); 130 portName = new QName (dt.getService().getNamespaceURI(), name); 131 } 132 133 Object proxy = dtService.getPort(portName, seiClass); 134 if (null == proxy) { 135 LOG.log(Level.SEVERE, 136 "GETPORT_FAILURE", 137 new Object [] {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 wsdlUrl, QName serviceName) { 150 return Service.create(wsdlUrl, serviceName); 153 } 154 155 private void updateRequestContext(Map <String , Object > 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 <String , Object > respCtx) { 166 } 168 } 169 | Popular Tags |