1 7 package org.jboss.webservice.client; 8 9 11 import org.jboss.axis.client.AxisClientProxy; 12 import org.jboss.logging.Logger; 13 14 import javax.xml.rpc.JAXRPCException ; 15 import java.lang.reflect.InvocationHandler ; 16 import java.lang.reflect.InvocationTargetException ; 17 import java.lang.reflect.Method ; 18 import java.lang.reflect.Proxy ; 19 import java.rmi.Remote ; 20 import java.rmi.RemoteException ; 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import java.util.List ; 24 25 33 public class PortProxy implements InvocationHandler 34 { 35 private static final Logger log = Logger.getLogger(PortProxy.class); 37 38 private Remote port; 40 private CallImpl call; 41 42 private List seiMethods = new ArrayList (); 44 private List objectMethods = new ArrayList (); 46 private List stubMethods = new ArrayList (); 48 49 private Method getPropertyMethod; 51 private Method setPropertyMethod; 52 53 62 public PortProxy(Remote port, Class seiClass) 63 { 64 this.port = port; 65 66 AxisClientProxy axisClientProxy = (AxisClientProxy)Proxy.getInvocationHandler(port); 68 this.call = (CallImpl)axisClientProxy.getCall(); 69 70 objectMethods.addAll(Arrays.asList(Object .class.getMethods())); 72 73 seiMethods.addAll(Arrays.asList(seiClass.getMethods())); 75 76 stubMethods.addAll(Arrays.asList(org.jboss.webservice.client.Stub.class.getMethods())); 78 79 try 81 { 82 getPropertyMethod = Stub.class.getMethod("_getProperty", new Class []{String .class}); 83 setPropertyMethod = Stub.class.getMethod("_setProperty", new Class []{String .class, Object .class}); 84 } 85 catch (NoSuchMethodException ignore) 86 { 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 (seiMethods.contains(method)) 102 { 103 log.debug("Invoke on service endpoint interface: " + methodName); 104 105 retObj = method.invoke(port, args); 106 return retObj; 107 } 108 if (stubMethods.contains(method)) 109 { 110 log.debug("Invoke on stub interface: " + methodName); 111 112 if ("getUsername".equals(methodName)) 114 retObj = getPropertyMethod.invoke(port, new Object []{Stub.USERNAME_PROPERTY}); 115 else if ("setUsername".equals(methodName)) 116 retObj = setPropertyMethod.invoke(port, new Object []{Stub.USERNAME_PROPERTY, args[0]}); 117 else if ("getPassword".equals(methodName)) 118 retObj = getPropertyMethod.invoke(port, new Object []{Stub.PASSWORD_PROPERTY}); 119 else if ("setPassword".equals(methodName)) 120 retObj = setPropertyMethod.invoke(port, new Object []{Stub.PASSWORD_PROPERTY, args[0]}); 121 else if ("getEndpointAddress".equals(methodName)) 122 retObj = getPropertyMethod.invoke(port, new Object []{Stub.ENDPOINT_ADDRESS_PROPERTY}); 123 else if ("setEndpointAddress".equals(methodName)) 124 retObj = setPropertyMethod.invoke(port, new Object []{Stub.ENDPOINT_ADDRESS_PROPERTY, args[0]}); 125 else if ("getSessionMaintain".equals(methodName)) 126 retObj = getPropertyMethod.invoke(port, new Object []{Stub.SESSION_MAINTAIN_PROPERTY}); 127 else if ("setSessionMaintain".equals(methodName)) 128 retObj = setPropertyMethod.invoke(port, new Object []{Stub.SESSION_MAINTAIN_PROPERTY, args[0]}); 129 130 else if ("getTimeout".equals(methodName)) 132 retObj = call.getTimeout(); 133 else if ("setTimeout".equals(methodName)) 134 call.setTimeout((Integer )args[0]); 135 else if (getPropertyMethod.equals(method) && Stub.CLIENT_TIMEOUT_PROPERTY.equals(args[0])) 136 retObj = call.getTimeout(); 137 else if (setPropertyMethod.equals(method) && Stub.CLIENT_TIMEOUT_PROPERTY.equals(args[0])) 138 call.setTimeout((Integer )args[1]); 139 140 else if ("addAttachment".equals(methodName)) 142 call.addAttachment((String )args[0], args[1]); 143 else if ("removeAttachment".equals(methodName)) 144 call.removeAttachment((String )args[0]); 145 else if ("getAttachments".equals(methodName)) 146 retObj = call.getAttachmentIdentifiers(); 147 else if ("getAttachment".equals(methodName)) 148 retObj = call.getAttachment((String )args[0]); 149 else if ("getTransportOption".equals(methodName)) 150 retObj = call.getTransportOption((String )args[0]); 151 else if ("setTransportOption".equals(methodName)) 152 call.setTransportOption((String )args[0], args[1]); 153 154 else 156 retObj = method.invoke(port, args); 157 158 return retObj; 159 } 160 if (objectMethods.contains(method)) 161 { 162 log.debug("Invoke on object: " + methodName); 163 164 retObj = method.invoke(port, args); 165 return retObj; 166 } 167 168 throw new JAXRPCException ("Don't know how to invoke: " + method); 169 } 170 catch (Exception e) 171 { 172 Throwable th = processException(e); 173 174 if (seiMethods.contains(method)) 177 { 178 if (th instanceof JAXRPCException || th instanceof RuntimeException ) 179 { 180 RemoteException re = new RemoteException (th.getMessage(), th); 181 th = re; 182 } 183 } 184 185 throw th; 186 } 187 } 188 189 192 private Throwable processException(Exception ex) 193 { 194 Throwable th = ex; 195 if (ex instanceof InvocationTargetException ) 196 th = ((InvocationTargetException )ex).getTargetException(); 197 198 log.error("Port error", th); 199 return th; 200 } 201 } | Popular Tags |