1 7 8 package org.jboss.webservice.client; 10 11 13 import org.jboss.logging.Logger; 14 import org.jboss.webservice.metadata.jaxrpcmapping.JavaWsdlMapping; 15 import org.jboss.webservice.util.WSDLUtilities; 16 17 import javax.wsdl.Definition; 18 import javax.xml.rpc.JAXRPCException ; 19 import javax.xml.rpc.Service ; 20 import javax.xml.rpc.ServiceException ; 21 import java.lang.reflect.InvocationHandler ; 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 import java.rmi.Remote ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.List ; 28 29 37 public class ServiceProxy implements InvocationHandler 38 { 39 private static final Logger log = Logger.getLogger(ServiceProxy.class); 41 42 private ServiceImpl jaxrpcService; 44 45 private List objectMethods = new ArrayList (); 47 private List jaxrpcServiceMethods = new ArrayList (); 49 private List serviceInterfaceMethods = new ArrayList (); 51 private List endorsedServiceEndpointClasses = new ArrayList (); 53 54 private Method getPortMethod; 56 57 65 public ServiceProxy(ServiceImpl service, Class siClass) 66 { 67 this.jaxrpcService = service; 68 69 objectMethods.addAll(Arrays.asList(java.lang.Object .class.getMethods())); 71 72 jaxrpcServiceMethods.addAll(Arrays.asList(javax.xml.rpc.Service .class.getMethods())); 74 75 if (siClass.getName().equals("javax.xml.rpc.Service") == false) 77 serviceInterfaceMethods.addAll(Arrays.asList(siClass.getDeclaredMethods())); 78 79 try 81 { 82 getPortMethod = Service .class.getMethod("getPort", new Class []{Class .class}); 83 } 84 catch (NoSuchMethodException e) 85 { 86 throw new JAXRPCException (e.toString()); 87 } 88 } 89 90 94 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable 95 { 96 String methodName = method.getName(); 97 98 try 99 { 100 Object retObj = null; 101 if (jaxrpcServiceMethods.contains(method)) 102 { 103 log.debug("Invoke on jaxrpc service: " + methodName); 104 105 if (method.getName().equals("getPort")) 106 { 107 Class seiClass = (Class )(args.length == 1 ? args[0] : args[1]); 108 endorseServiceEndpointClass(seiClass); 109 110 Remote port = (Remote )method.invoke(jaxrpcService, args); 111 return port; 112 } 113 else 114 { 115 retObj = method.invoke(jaxrpcService, args); 116 return retObj; 117 } 118 } 119 if (serviceInterfaceMethods.contains(method)) 120 { 121 log.debug("Invoke on service interface: " + methodName); 122 123 Class seiClass = method.getReturnType(); 124 retObj = getPortMethod.invoke(jaxrpcService, new Object []{seiClass}); 125 return retObj; 126 } 127 if (objectMethods.contains(method)) 128 { 129 log.debug("Invoke on object: " + methodName); 130 131 retObj = method.invoke(jaxrpcService, args); 132 return retObj; 133 } 134 135 throw new JAXRPCException ("Don't know how to invoke: " + method); 136 } 137 catch (Exception e) 138 { 139 handleException(e); 140 return null; 141 } 142 } 143 144 147 private void handleException(Exception ex) throws Throwable 148 { 149 Throwable th = ex; 150 if (ex instanceof InvocationTargetException ) 151 th = ((InvocationTargetException )ex).getTargetException(); 152 153 log.error("Service error", th); 154 155 throw th; 156 } 157 158 161 private void endorseServiceEndpointClass(Class seiClass) throws ServiceException 162 { 163 if (endorsedServiceEndpointClasses.contains(seiClass)) 165 return; 166 167 Definition wsdlDefinition = jaxrpcService.getWsdlDefinition(); 168 JavaWsdlMapping jaxrpcMapping = jaxrpcService.getJavaWsdlMapping(); 169 WSDLUtilities.endorseServiceEndpointInterface(wsdlDefinition, seiClass, jaxrpcMapping); 170 171 endorsedServiceEndpointClasses.add(seiClass); 172 } 173 } | Popular Tags |