📄 ejbprovider.java
字号:
ClassLoader cl = (msgContext != null) ? msgContext.getClassLoader() : Thread.currentThread().getContextClassLoader(); Class homeClass = ClassUtils.forName(homeName, true, cl); // Make sure the object we got back from JNDI is the same type // as the what is specified in the config file Object ehome = javax.rmi.PortableRemoteObject.narrow(ejbHome, homeClass); // This code requires the use of ejb.jar, so we do the stuff below // EJBHome ejbHome = (EJBHome) ehome; // EJBMetaData meta = ejbHome.getEJBMetaData(); // Class interfaceClass = meta.getRemoteInterfaceClass(); // Invoke the getEJBMetaData method of the ejbHome class without // actually touching any EJB classes (i.e. no cast to EJBHome) Method getEJBMetaData = homeClass.getMethod("getEJBMetaData", empty_class_array); Object metaData = getEJBMetaData.invoke(ehome, empty_object_array); Method getRemoteInterfaceClass = metaData.getClass().getMethod("getRemoteInterfaceClass", empty_class_array); return (Class) getRemoteInterfaceClass.invoke(metaData, empty_object_array); } /** * Get the class description for the EJB Remote or Local Interface, * which is what we are interested in exposing to the world (i.e. in WSDL). * * @param msgContext the message context (can be null) * @param beanJndiName the JNDI name of the EJB * @return the class info of the EJB remote or local interface */ protected Class getServiceClass(String beanJndiName, SOAPService service, MessageContext msgContext) throws AxisFault { Class interfaceClass = null; try { // First try to get the interface class from the configuation // Note that we don't verify that remote remoteInterfaceName is used for // remote ejb and localInterfaceName for local ejb. Should we ? String remoteInterfaceName = getStrOption(OPTION_REMOTEINTERFACENAME, service); String localInterfaceName = getStrOption(OPTION_LOCALINTERFACENAME, service); String interfaceName = (remoteInterfaceName != null ? remoteInterfaceName : localInterfaceName); if(interfaceName != null){ ClassLoader cl = (msgContext != null) ? msgContext.getClassLoader() : Thread.currentThread().getContextClassLoader(); interfaceClass = ClassUtils.forName(interfaceName, true, cl); } else { // cannot get the interface name from the configuration, we get // it from the EJB Home (if remote) if (isRemoteEjb(service)) { interfaceClass = getRemoteInterfaceClassFromHome(beanJndiName, service, msgContext); } else if (isLocalEjb(service)) { // we cannot get the local interface from the local ejb home // localInterfaceName is mandatory for local ejbs throw new AxisFault( Messages.getMessage("noOption00", OPTION_LOCALINTERFACENAME, service.getName())); } else { // neither a local ejb or a remote one ... throw new AxisFault(Messages.getMessage("noOption00", OPTION_HOMEINTERFACENAME, service.getName())); } } } catch (Exception e) { throw AxisFault.makeFault(e); } // got it, return it return interfaceClass; } /** * Common routine to do the JNDI lookup on the Home interface object * username and password for jndi lookup are got from the configuration or from * the messageContext if not found in the configuration */ private Object getEJBHome(SOAPService serviceHandler, MessageContext msgContext, String beanJndiName) throws AxisFault { Object ejbHome = null; // Set up an InitialContext and use it get the beanJndiName from JNDI try { Properties properties = null; // collect all the properties we need to access JNDI: // username, password, factoryclass, contextUrl // username String username = getStrOption(jndiUsername, serviceHandler); if ((username == null) && (msgContext != null)) username = msgContext.getUsername(); if (username != null) { if (properties == null) properties = new Properties(); properties.setProperty(Context.SECURITY_PRINCIPAL, username); } // password String password = getStrOption(jndiPassword, serviceHandler); if ((password == null) && (msgContext != null)) password = msgContext.getPassword(); if (password != null) { if (properties == null) properties = new Properties(); properties.setProperty(Context.SECURITY_CREDENTIALS, password); } // factory class String factoryClass = getStrOption(jndiContextClass, serviceHandler); if (factoryClass != null) { if (properties == null) properties = new Properties(); properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, factoryClass); } // contextUrl String contextUrl = getStrOption(jndiURL, serviceHandler); if (contextUrl != null) { if (properties == null) properties = new Properties(); properties.setProperty(Context.PROVIDER_URL, contextUrl); } // get context using these properties InitialContext context = getContext(properties); // if we didn't get a context, fail if (context == null) throw new AxisFault( Messages.getMessage("cannotCreateInitialContext00")); ejbHome = getEJBHome(context, beanJndiName); if (ejbHome == null) throw new AxisFault( Messages.getMessage("cannotFindJNDIHome00",beanJndiName)); } // Should probably catch javax.naming.NameNotFoundException here catch (Exception exception) { entLog.info(Messages.getMessage("toAxisFault00"), exception); throw AxisFault.makeFault(exception); } return ejbHome; } protected InitialContext getCachedContext() throws javax.naming.NamingException { if (cached_context == null) cached_context = new InitialContext(); return cached_context; } protected InitialContext getContext(Properties properties) throws AxisFault, javax.naming.NamingException { // if we got any stuff from the configuration file // create a new context using these properties // otherwise, we get a default context and cache it for next time return ((properties == null) ? getCachedContext() : new InitialContext(properties)); } protected Object getEJBHome(InitialContext context, String beanJndiName) throws AxisFault, javax.naming.NamingException { // Do the JNDI lookup return context.lookup(beanJndiName); } /** * Override the default implementation such that we can include * special handling for {@link java.rmi.ServerException}. * <p/> * Converts {@link java.rmi.ServerException} exceptions to * {@link InvocationTargetException} exceptions with the same cause. * This allows the axis framework to create a SOAP fault. * </p> * * @see org.apache.axis.providers.java.RPCProvider#invokeMethod(org.apache.axis.MessageContext, java.lang.reflect.Method, java.lang.Object, java.lang.Object[]) */ protected Object invokeMethod(MessageContext msgContext, Method method, Object obj, Object[] argValues) throws Exception { try { return super.invokeMethod(msgContext, method, obj, argValues); } catch (InvocationTargetException ite) { Throwable cause = getCause(ite); if (cause instanceof java.rmi.ServerException) { throw new InvocationTargetException(getCause(cause)); } throw ite; } } /** * Get the cause of an exception, using reflection so that * it still works under JDK 1.3 * * @param original the original exception * @return the cause of the exception, or the given exception if the cause cannot be discovered. */ private Throwable getCause(Throwable original) { try { Method method = original.getClass().getMethod("getCause", null); Throwable cause = (Throwable) method.invoke(original, null); if (cause != null) { return cause; } } catch (NoSuchMethodException nsme) { // ignore, this occurs under JDK 1.3 } catch (Throwable t) { } return original; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -