1 7 8 10 package org.jboss.net.axis; 11 12 import org.jboss.axis.ConfigurationException; 13 import org.jboss.axis.EngineConfiguration; 14 import org.jboss.axis.EngineConfigurationFactory; 15 import org.jboss.axis.client.Call; 16 import org.jboss.axis.client.Service; 17 import org.jboss.axis.configuration.EngineConfigurationFactoryFinder; 18 19 import javax.management.MalformedObjectNameException ; 20 import javax.management.ObjectName ; 21 import java.io.IOException ; 22 import java.io.ObjectInputStream ; 23 import java.io.Serializable ; 24 import java.lang.reflect.InvocationHandler ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.Proxy ; 27 import java.net.URL ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 31 47 48 public class AxisInvocationHandler implements InvocationHandler , Serializable 49 { 50 static final long serialVersionUID = -8250523938712824229L; 51 52 56 57 protected Map methodToInterface; 58 59 protected Map methodToName; 60 61 transient protected Call call; 62 63 protected String rootContext; 64 65 protected String endPoint; 66 67 71 79 public AxisInvocationHandler(Call call, Map methodMap, Map interfaceMap) 80 { 81 this.call = call; 82 this.methodToInterface = interfaceMap; 83 this.methodToName = methodMap; 84 EngineConfiguration myEngineConfig = call.getService().getEngine().getConfig(); 86 87 try 88 { 89 rootContext = (String )myEngineConfig.getGlobalOptions().get(Constants.CONFIGURATION_CONTEXT); 90 } 91 catch (ConfigurationException e) 92 { 93 } 95 catch (NullPointerException e) 96 { 97 } 99 100 if (call.getTargetEndpointAddress() != null) 103 { 104 endPoint = call.getTargetEndpointAddress().toString(); 105 } 106 } 107 108 115 public AxisInvocationHandler(URL endpoint, 116 String soapAction, 117 Service service, 118 Map methodMap, 119 Map interfaceMap) 120 { 121 this(new Call(service), methodMap, interfaceMap); 122 call.setTargetEndpointAddress(endpoint); 123 call.setSOAPActionURI(soapAction); 124 setBasicAuthentication(endpoint); 125 endPoint = endpoint.toString(); 126 } 127 128 136 public AxisInvocationHandler(URL endPoint, 137 String soapAction, 138 Map methodMap, 139 Map interfaceMap, 140 boolean maintainSession) 141 { 142 this(endPoint, soapAction, new Service(), methodMap, interfaceMap); 143 call.setMaintainSession(maintainSession); 144 } 145 146 153 public AxisInvocationHandler(URL endPoint, String soapAction, Map methodMap, Map interfaceMap) 154 { 155 this(endPoint, soapAction, methodMap, interfaceMap, true); 156 } 157 158 165 public AxisInvocationHandler(URL endPoint, String soapAction, Map methodMap) 166 { 167 this(endPoint, soapAction, methodMap, new DefaultInterfaceMap()); 168 } 169 170 178 public AxisInvocationHandler(URL endPoint, String soapAction) 179 { 180 this(endPoint, soapAction, new DefaultMethodMap()); 181 } 182 183 187 188 protected void setBasicAuthentication(URL target) 189 { 190 String userInfo = target.getUserInfo(); 191 if (userInfo != null) 192 { 193 java.util.StringTokenizer tok = new java.util.StringTokenizer (userInfo, ":"); 194 if (tok.hasMoreTokens()) 195 { 196 call.setUsername(tok.nextToken()); 197 if (tok.hasMoreTokens()) 198 { 199 call.setPassword(tok.nextToken()); 200 } 201 } 202 } 203 } 204 205 209 210 public Object invoke(String serviceName, String methodName, Object [] args) 211 throws java.rmi.RemoteException 212 { 213 try 214 { 215 return call.invoke(serviceName, methodName, args); 216 } 217 finally 218 { 219 call.setReturnType(null); 220 call.removeAllParameters(); 221 } 222 } 223 224 225 public Object invoke(String serviceName, 226 String methodName, 227 Object [] args, 228 Class [] parameters) 229 throws java.rmi.RemoteException 230 { 231 return invoke(serviceName, methodName, args); 233 } 234 235 236 public java.lang.Object invoke(java.lang.Object target, 237 java.lang.reflect.Method method, 238 java.lang.Object [] args) 239 throws java.lang.Throwable 240 { 241 return invoke((String )methodToInterface.get(method), 242 (String )methodToName.get(method), 243 args, 244 method.getParameterTypes()); 245 } 246 247 248 252 253 public static Object createAxisService(Class _interface, URL endpoint, String soapAction) 254 { 255 return createAxisService(_interface, new AxisInvocationHandler(endpoint, soapAction)); 256 } 257 258 259 public static Object createAxisService(Class _interface, 260 URL endpoint, String soapAction, Service service) 261 { 262 return createAxisService(_interface, 263 new AxisInvocationHandler(endpoint, soapAction, service, new DefaultMethodMap(), new DefaultInterfaceMap())); 264 } 265 266 267 public static Object createAxisService(Class _interface, 268 Call call) 269 { 270 return createAxisService(_interface, 271 new AxisInvocationHandler(call, new DefaultMethodMap(), new DefaultInterfaceMap())); 272 } 273 274 275 public static Object createAxisService(Class _interface, 276 AxisInvocationHandler handler) 277 { 278 return Proxy.newProxyInstance(_interface.getClassLoader(), 279 new Class []{_interface}, 280 handler); 281 } 282 283 289 290 public static class DefaultInterfaceMap extends HashMap 291 { 292 293 294 public DefaultInterfaceMap() 295 { 296 super(0); 297 } 298 299 300 public Object get(Object key) 301 { 302 303 Object result = super.get(((Method )key).getName()); 305 306 if (result == null) 307 { 308 result = super.get(((Method )key).getDeclaringClass().getName()); 311 312 if (result == null) 315 { 316 String sresult = ((Method )key).getDeclaringClass().getName(); 317 if (sresult.indexOf(".") != -1) 318 sresult = sresult.substring(sresult.lastIndexOf(".") + 1); 319 result = sresult; 320 } 321 } 322 323 return result; 324 } 325 326 327 public Object put(Object key, Object value) 328 { 329 if (key instanceof Method ) 330 { 331 return super.put(((Method )key).getName(), value); 332 } 333 else if (key instanceof Class ) 334 { 335 return super.put(((Class )key).getName(), value); 336 } 337 else 338 { 339 return super.put(key, value); 340 } 341 } 342 343 } 344 345 351 352 public static class DefaultMethodMap extends HashMap 353 { 354 355 356 public DefaultMethodMap() 357 { 358 super(0); 359 } 360 361 362 public Object get(Object key) 363 { 364 365 Object result = super.get(((Method )key).getName()); 366 367 if (result == null) 368 { 369 result = ((Method )key).getName(); 370 } 371 372 return result; 373 } 374 375 376 public Object put(Object key, Object value) 377 { 378 if (key instanceof Method ) 379 { 380 return super.put(((Method )key).getName(), value); 381 } 382 else 383 { 384 return super.put(key, value); 385 } 386 } 387 } 388 389 393 private void readObject(ObjectInputStream stream) throws IOException , ClassNotFoundException 394 { 395 stream.defaultReadObject(); 396 397 try 399 { 400 EngineConfigurationFactory factory = EngineConfigurationFactoryFinder. 401 newFactory(new ObjectName (rootContext)); 402 403 EngineConfiguration engine = null; 404 405 if (factory != null) 406 { 407 engine = factory.getClientEngineConfig(); 408 } 409 410 if (engine != null) 411 { 412 call = new Call(new Service(engine)); 413 } 414 else 415 { 416 call = new Call(new Service()); 418 } 419 } 420 catch (MalformedObjectNameException e) 421 { 422 throw new IOException ("Could not contact jmx configuration factory." + e); 423 } 424 425 URL endpoint = new URL (endPoint); 426 call.setTargetEndpointAddress(endpoint); 427 setBasicAuthentication(endpoint); 428 } 429 430 } 431 | Popular Tags |