1 55 56 package org.jboss.axis.providers.java; 57 58 import org.jboss.axis.AxisFault; 59 import org.jboss.axis.Handler; 60 import org.jboss.axis.MessageContext; 61 import org.jboss.axis.handlers.soap.SOAPService; 62 import org.jboss.axis.utils.ClassUtils; 63 import org.jboss.axis.utils.Messages; 64 import org.jboss.logging.Logger; 65 66 import javax.naming.Context ; 67 import javax.naming.InitialContext ; 68 import java.lang.reflect.Method ; 69 import java.util.Properties ; 70 71 78 public class EJBProvider extends RPCProvider 79 { 80 private static Logger log = Logger.getLogger(EJBProvider.class.getName()); 81 82 public static final String OPTION_BEANNAME = "beanJndiName"; 83 public static final String OPTION_HOMEINTERFACENAME = "homeInterfaceName"; 84 public static final String OPTION_REMOTEINTERFACENAME = "remoteInterfaceName"; 85 public static final String OPTION_LOCALHOMEINTERFACENAME = "localHomeInterfaceName"; 86 public static final String OPTION_LOCALINTERFACENAME = "localInterfaceName"; 87 88 89 public static final String jndiContextClass = "jndiContextClass"; 90 public static final String jndiURL = "jndiURL"; 91 public static final String jndiUsername = "jndiUser"; 92 public static final String jndiPassword = "jndiPassword"; 93 94 protected static final Class [] empty_class_array = new Class [0]; 95 protected static final Object [] empty_object_array = new Object [0]; 96 97 private static InitialContext cached_context = null; 98 99 106 113 protected Object makeNewServiceObject(MessageContext msgContext, 114 String clsName) 115 throws Exception 116 { 117 String remoteHomeName = getStrOption(OPTION_HOMEINTERFACENAME, 118 msgContext.getService()); 119 String localHomeName = getStrOption(OPTION_LOCALHOMEINTERFACENAME, 120 msgContext.getService()); 121 String homeName = (remoteHomeName != null ? remoteHomeName : localHomeName); 122 123 if (homeName == null) 124 { 125 throw new AxisFault(Messages.getMessage("noOption00", 127 OPTION_HOMEINTERFACENAME, 128 msgContext.getTargetService())); 129 } 130 131 Class homeClass = ClassUtils.forName(homeName, true, msgContext.getClassLoader()); 133 134 if (remoteHomeName != null) 136 return createRemoteEJB(msgContext, clsName, homeClass); 137 else 138 return createLocalEJB(msgContext, clsName, homeClass); 139 } 140 141 149 private Object createRemoteEJB(MessageContext msgContext, 150 String beanJndiName, 151 Class homeClass) 152 throws Exception 153 { 154 Object ejbHome = getEJBHome(msgContext.getService(), 156 msgContext, beanJndiName); 157 Object ehome = javax.rmi.PortableRemoteObject.narrow(ejbHome, homeClass); 158 159 Method createMethod = homeClass.getMethod("create", empty_class_array); 162 Object result = createMethod.invoke(ehome, empty_object_array); 163 164 return result; 165 } 166 167 175 private Object createLocalEJB(MessageContext msgContext, 176 String beanJndiName, 177 Class homeClass) 178 throws Exception 179 { 180 Object ejbHome = getEJBHome(msgContext.getService(), 182 msgContext, beanJndiName); 183 184 Object ehome; 186 if (homeClass.isInstance(ejbHome)) 187 ehome = ejbHome; 188 else 189 throw new ClassCastException (Messages.getMessage("badEjbHomeType")); 190 191 Method createMethod = homeClass.getMethod("create", empty_class_array); 194 Object result = createMethod.invoke(ehome, empty_object_array); 195 196 return result; 197 } 198 199 203 private boolean isRemoteEjb(SOAPService service) 204 { 205 return getStrOption(OPTION_HOMEINTERFACENAME, service) != null; 206 } 207 208 212 private boolean isLocalEjb(SOAPService service) 213 { 214 return (!isRemoteEjb(service)) && 215 (getStrOption(OPTION_LOCALHOMEINTERFACENAME, service) != null); 216 } 217 218 219 223 protected String getServiceClassNameOptionName() 224 { 225 return OPTION_BEANNAME; 226 } 227 228 238 protected String getStrOption(String optionName, Handler service) 239 { 240 String value = null; 241 if (service != null) 242 value = (String )service.getOption(optionName); 243 if (value == null) 244 value = (String )getOption(optionName); 245 return value; 246 } 247 248 256 private Class getRemoteInterfaceClassFromHome(String beanJndiName, 257 SOAPService service, 258 MessageContext msgContext) 259 throws Exception 260 { 261 Object ejbHome = getEJBHome(service, msgContext, beanJndiName); 263 264 String homeName = getStrOption(OPTION_HOMEINTERFACENAME, 265 service); 266 if (homeName == null) 267 throw new AxisFault(Messages.getMessage("noOption00", 268 OPTION_HOMEINTERFACENAME, 269 service.getName())); 270 271 ClassLoader cl = (msgContext != null) ? 273 msgContext.getClassLoader() : 274 Thread.currentThread().getContextClassLoader(); 275 Class homeClass = ClassUtils.forName(homeName, true, cl); 276 277 278 Object ehome = javax.rmi.PortableRemoteObject.narrow(ejbHome, homeClass); 281 282 287 Method getEJBMetaData = 290 homeClass.getMethod("getEJBMetaData", empty_class_array); 291 Object metaData = getEJBMetaData.invoke(ehome, empty_object_array); 292 Method getRemoteInterfaceClass = 293 metaData.getClass().getMethod("getRemoteInterfaceClass", 294 empty_class_array); 295 return (Class )getRemoteInterfaceClass.invoke(metaData, 296 empty_object_array); 297 } 298 299 300 308 protected Class getServiceClass(String beanJndiName, 309 SOAPService service, 310 MessageContext msgContext) 311 throws AxisFault 312 { 313 Class interfaceClass = null; 314 315 try 316 { 317 String remoteInterfaceName = 321 getStrOption(OPTION_REMOTEINTERFACENAME, service); 322 String localInterfaceName = 323 getStrOption(OPTION_LOCALINTERFACENAME, service); 324 String interfaceName = (remoteInterfaceName != null ? remoteInterfaceName : localInterfaceName); 325 326 if (interfaceName != null) 327 { 328 ClassLoader cl = (msgContext != null) ? 329 msgContext.getClassLoader() : 330 Thread.currentThread().getContextClassLoader(); 331 interfaceClass = ClassUtils.forName(interfaceName, 332 true, 333 cl); 334 } 335 else 336 { 337 if (isRemoteEjb(service)) 340 { 341 String remoteHomeName = getStrOption(OPTION_HOMEINTERFACENAME, 342 service); 343 interfaceClass = getRemoteInterfaceClassFromHome(beanJndiName, 344 service, 345 msgContext); 346 } 347 else if (isLocalEjb(service)) 348 { 349 throw new AxisFault(Messages.getMessage("noOption00", 352 OPTION_LOCALINTERFACENAME, 353 service.getName())); 354 } 355 else 356 { 357 throw new AxisFault(Messages.getMessage("noOption00", 359 OPTION_HOMEINTERFACENAME, 360 service.getName())); 361 } 362 } 363 } 364 catch (Exception e) 365 { 366 throw AxisFault.makeFault(e); 367 } 368 369 return interfaceClass; 371 } 372 373 378 private Object getEJBHome(SOAPService serviceHandler, 379 MessageContext msgContext, 380 String beanJndiName) 381 throws AxisFault 382 { 383 Object ejbHome = null; 384 385 try 387 { 388 Properties properties = null; 389 390 393 String username = getStrOption(jndiUsername, serviceHandler); 395 if ((username == null) && (msgContext != null)) 396 username = msgContext.getUsername(); 397 if (username != null) 398 { 399 if (properties == null) 400 properties = new Properties (); 401 properties.setProperty(Context.SECURITY_PRINCIPAL, username); 402 } 403 404 String password = getStrOption(jndiPassword, serviceHandler); 406 if ((password == null) && (msgContext != null)) 407 password = msgContext.getPassword(); 408 if (password != null) 409 { 410 if (properties == null) 411 properties = new Properties (); 412 properties.setProperty(Context.SECURITY_CREDENTIALS, password); 413 } 414 415 String factoryClass = getStrOption(jndiContextClass, serviceHandler); 417 if (factoryClass != null) 418 { 419 if (properties == null) 420 properties = new Properties (); 421 properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, factoryClass); 422 } 423 424 String contextUrl = getStrOption(jndiURL, serviceHandler); 426 if (contextUrl != null) 427 { 428 if (properties == null) 429 properties = new Properties (); 430 properties.setProperty(Context.PROVIDER_URL, contextUrl); 431 } 432 433 InitialContext context = getContext(properties); 435 436 if (context == null) 438 throw new AxisFault(Messages.getMessage("cannotCreateInitialContext00")); 439 440 ejbHome = getEJBHome(context, beanJndiName); 441 442 if (ejbHome == null) 443 throw new AxisFault(Messages.getMessage("cannotFindJNDIHome00", beanJndiName)); 444 } 445 catch (Exception exception) 447 { 448 log.info(Messages.getMessage("toAxisFault00"), exception); 449 throw AxisFault.makeFault(exception); 450 } 451 452 return ejbHome; 453 } 454 455 protected InitialContext getCachedContext() 456 throws javax.naming.NamingException 457 { 458 if (cached_context == null) 459 cached_context = new InitialContext (); 460 return cached_context; 461 } 462 463 464 protected InitialContext getContext(Properties properties) 465 throws AxisFault, javax.naming.NamingException 466 { 467 return ((properties == null) 471 ? getCachedContext() 472 : new InitialContext (properties)); 473 } 474 475 protected Object getEJBHome(InitialContext context, String beanJndiName) 476 throws AxisFault, javax.naming.NamingException 477 { 478 return context.lookup(beanJndiName); 480 } 481 482 486 502 } 503 | Popular Tags |