1 16 17 package org.apache.axis.providers.java; 18 19 import java.lang.reflect.Method ; 20 import java.lang.reflect.InvocationTargetException ; 21 import java.util.Properties ; 22 23 import javax.naming.Context ; 24 import javax.naming.InitialContext ; 25 26 import org.apache.axis.AxisFault; 27 import org.apache.axis.Constants; 28 import org.apache.axis.Handler; 29 import org.apache.axis.MessageContext; 30 import org.apache.axis.components.logger.LogFactory; 31 import org.apache.axis.handlers.soap.SOAPService; 32 import org.apache.axis.utils.ClassUtils; 33 import org.apache.axis.utils.Messages; 34 import org.apache.commons.logging.Log; 35 36 43 public class EJBProvider extends RPCProvider 44 { 45 protected static Log log = 46 LogFactory.getLog(EJBProvider.class.getName()); 47 48 protected static Log entLog = 52 LogFactory.getLog(Constants.ENTERPRISE_LOG_CATEGORY); 53 54 public static final String OPTION_BEANNAME = "beanJndiName"; 55 public static final String OPTION_HOMEINTERFACENAME = "homeInterfaceName"; 56 public static final String OPTION_REMOTEINTERFACENAME = "remoteInterfaceName"; 57 public static final String OPTION_LOCALHOMEINTERFACENAME = "localHomeInterfaceName"; 58 public static final String OPTION_LOCALINTERFACENAME = "localInterfaceName"; 59 60 61 public static final String jndiContextClass = "jndiContextClass"; 62 public static final String jndiURL = "jndiURL"; 63 public static final String jndiUsername = "jndiUser"; 64 public static final String jndiPassword = "jndiPassword"; 65 66 protected static final Class [] empty_class_array = new Class [0]; 67 protected static final Object [] empty_object_array = new Object [0]; 68 69 private static InitialContext cached_context = null; 70 71 78 85 protected Object makeNewServiceObject(MessageContext msgContext, 86 String clsName) 87 throws Exception 88 { 89 String remoteHomeName = getStrOption(OPTION_HOMEINTERFACENAME, 90 msgContext.getService()); 91 String localHomeName = getStrOption(OPTION_LOCALHOMEINTERFACENAME, 92 msgContext.getService()); 93 String homeName = (remoteHomeName != null ? remoteHomeName:localHomeName); 94 95 if (homeName == null) { 96 throw new AxisFault( 98 Messages.getMessage("noOption00", 99 OPTION_HOMEINTERFACENAME, 100 msgContext.getTargetService())); 101 } 102 103 Class homeClass = ClassUtils.forName(homeName, true, msgContext.getClassLoader()); 105 106 if (remoteHomeName != null) 108 return createRemoteEJB(msgContext, clsName, homeClass); 109 else 110 return createLocalEJB(msgContext, clsName, homeClass); 111 } 112 113 121 private Object createRemoteEJB(MessageContext msgContext, 122 String beanJndiName, 123 Class homeClass) 124 throws Exception 125 { 126 Object ejbHome = getEJBHome(msgContext.getService(), 128 msgContext, beanJndiName); 129 Object ehome = javax.rmi.PortableRemoteObject.narrow(ejbHome, homeClass); 130 131 Method createMethod = homeClass.getMethod("create", empty_class_array); 134 Object result = createMethod.invoke(ehome, empty_object_array); 135 136 return result; 137 } 138 139 147 private Object createLocalEJB(MessageContext msgContext, 148 String beanJndiName, 149 Class homeClass) 150 throws Exception 151 { 152 Object ejbHome = getEJBHome(msgContext.getService(), 154 msgContext, beanJndiName); 155 156 Object ehome; 158 if (homeClass.isInstance(ejbHome)) 159 ehome = ejbHome; 160 else 161 throw new ClassCastException ( 162 Messages.getMessage("badEjbHomeType")); 163 164 Method createMethod = homeClass.getMethod("create", empty_class_array); 167 Object result = createMethod.invoke(ehome, empty_object_array); 168 169 return result; 170 } 171 172 176 private boolean isRemoteEjb(SOAPService service) 177 { 178 return getStrOption(OPTION_HOMEINTERFACENAME,service) != null; 179 } 180 181 185 private boolean isLocalEjb(SOAPService service) 186 { 187 return (!isRemoteEjb(service)) && 188 (getStrOption(OPTION_LOCALHOMEINTERFACENAME,service) != null); 189 } 190 191 192 196 protected String getServiceClassNameOptionName() 197 { 198 return OPTION_BEANNAME; 199 } 200 201 211 protected String getStrOption(String optionName, Handler service) 212 { 213 String value = null; 214 if (service != null) 215 value = (String )service.getOption(optionName); 216 if (value == null) 217 value = (String )getOption(optionName); 218 return value; 219 } 220 221 229 private Class getRemoteInterfaceClassFromHome(String beanJndiName, 230 SOAPService service, 231 MessageContext msgContext) 232 throws Exception 233 { 234 Object ejbHome = getEJBHome(service, msgContext, beanJndiName); 236 237 String homeName = getStrOption(OPTION_HOMEINTERFACENAME, 238 service); 239 if (homeName == null) 240 throw new AxisFault( 241 Messages.getMessage("noOption00", 242 OPTION_HOMEINTERFACENAME, 243 service.getName())); 244 245 ClassLoader cl = (msgContext != null) ? 247 msgContext.getClassLoader() : 248 Thread.currentThread().getContextClassLoader(); 249 Class homeClass = ClassUtils.forName(homeName, true, cl); 250 251 252 Object ehome = javax.rmi.PortableRemoteObject.narrow(ejbHome, homeClass); 255 256 261 Method getEJBMetaData = 264 homeClass.getMethod("getEJBMetaData", empty_class_array); 265 Object metaData = getEJBMetaData.invoke(ehome, empty_object_array); 266 Method getRemoteInterfaceClass = 267 metaData.getClass().getMethod("getRemoteInterfaceClass", 268 empty_class_array); 269 return (Class ) getRemoteInterfaceClass.invoke(metaData, 270 empty_object_array); 271 } 272 273 274 282 protected Class getServiceClass(String beanJndiName, 283 SOAPService service, 284 MessageContext msgContext) 285 throws AxisFault 286 { 287 Class interfaceClass = null; 288 289 try { 290 String remoteInterfaceName = 294 getStrOption(OPTION_REMOTEINTERFACENAME, service); 295 String localInterfaceName = 296 getStrOption(OPTION_LOCALINTERFACENAME, service); 297 String interfaceName = (remoteInterfaceName != null ? remoteInterfaceName : localInterfaceName); 298 299 if(interfaceName != null){ 300 ClassLoader cl = (msgContext != null) ? 301 msgContext.getClassLoader() : 302 Thread.currentThread().getContextClassLoader(); 303 interfaceClass = ClassUtils.forName(interfaceName, 304 true, 305 cl); 306 } 307 else 308 { 309 if (isRemoteEjb(service)) { 312 interfaceClass = getRemoteInterfaceClassFromHome(beanJndiName, 313 service, 314 msgContext); 315 } 316 else 317 if (isLocalEjb(service)) { 318 throw new AxisFault( 321 Messages.getMessage("noOption00", 322 OPTION_LOCALINTERFACENAME, 323 service.getName())); 324 } 325 else 326 { 327 throw new AxisFault(Messages.getMessage("noOption00", 329 OPTION_HOMEINTERFACENAME, 330 service.getName())); 331 } 332 } 333 } catch (Exception e) { 334 throw AxisFault.makeFault(e); 335 } 336 337 return interfaceClass; 339 } 340 341 346 private Object getEJBHome(SOAPService serviceHandler, 347 MessageContext msgContext, 348 String beanJndiName) 349 throws AxisFault 350 { 351 Object ejbHome = null; 352 353 try { 355 Properties properties = null; 356 357 360 String username = getStrOption(jndiUsername, serviceHandler); 362 if ((username == null) && (msgContext != null)) 363 username = msgContext.getUsername(); 364 if (username != null) { 365 if (properties == null) 366 properties = new Properties (); 367 properties.setProperty(Context.SECURITY_PRINCIPAL, username); 368 } 369 370 String password = getStrOption(jndiPassword, serviceHandler); 372 if ((password == null) && (msgContext != null)) 373 password = msgContext.getPassword(); 374 if (password != null) { 375 if (properties == null) 376 properties = new Properties (); 377 properties.setProperty(Context.SECURITY_CREDENTIALS, password); 378 } 379 380 String factoryClass = getStrOption(jndiContextClass, serviceHandler); 382 if (factoryClass != null) { 383 if (properties == null) 384 properties = new Properties (); 385 properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, factoryClass); 386 } 387 388 String contextUrl = getStrOption(jndiURL, serviceHandler); 390 if (contextUrl != null) { 391 if (properties == null) 392 properties = new Properties (); 393 properties.setProperty(Context.PROVIDER_URL, contextUrl); 394 } 395 396 InitialContext context = getContext(properties); 398 399 if (context == null) 401 throw new AxisFault( Messages.getMessage("cannotCreateInitialContext00")); 402 403 ejbHome = getEJBHome(context, beanJndiName); 404 405 if (ejbHome == null) 406 throw new AxisFault( Messages.getMessage("cannotFindJNDIHome00",beanJndiName)); 407 } 408 catch (Exception exception) { 410 entLog.info(Messages.getMessage("toAxisFault00"), exception); 411 throw AxisFault.makeFault(exception); 412 } 413 414 return ejbHome; 415 } 416 417 protected InitialContext getCachedContext() 418 throws javax.naming.NamingException 419 { 420 if (cached_context == null) 421 cached_context = new InitialContext (); 422 return cached_context; 423 } 424 425 426 protected InitialContext getContext(Properties properties) 427 throws AxisFault, javax.naming.NamingException 428 { 429 return ((properties == null) 433 ? getCachedContext() 434 : new InitialContext (properties)); 435 } 436 437 protected Object getEJBHome(InitialContext context, String beanJndiName) 438 throws AxisFault, javax.naming.NamingException 439 { 440 return context.lookup(beanJndiName); 442 } 443 444 455 protected Object invokeMethod(MessageContext msgContext, Method method, 456 Object obj, Object [] argValues) 457 throws Exception { 458 try { 459 return super.invokeMethod(msgContext, method, obj, argValues); 460 } catch (InvocationTargetException ite) { 461 Throwable cause = getCause(ite); 462 if (cause instanceof java.rmi.ServerException ) { 463 throw new InvocationTargetException (getCause(cause)); 464 } 465 throw ite; 466 } 467 } 468 469 476 private Throwable getCause(Throwable original) { 477 try { 478 Method method = original.getClass().getMethod("getCause", null); 479 Throwable cause = (Throwable ) method.invoke(original, null); 480 if (cause != null) { 481 return cause; 482 } 483 } catch (NoSuchMethodException nsme) { 484 } catch (Throwable t) { 486 } 487 return original; 488 } 489 } 490 | Popular Tags |