Initializing the Context of the EJB client

xiaoxiao2021-03-06  44

When writing EJB clients, it is a tricky problem. For different application servers, its factory classes and JNDI URLs are always different. Every time they have to find information, this time you simply put all the information. Looking out, write into a class to get the Properties object that the initialContext constructor is required. The process of finding is more hard than I imagined, which is more determined to find the determination to find them. Information on today's mainstream J2EE application servement has been found, there is WebLogic, WebSphere, JBoss, Sun One, Oracle Application Server, JRun, Iona, for Borland Enterprise Server (BES), a large amount of information indicates that its client does not need to be InitialContext The constructor provides parameters, namely CONTEXT CTX = New InitialContext (), I can't understand this, how to get the JNDI context on other machines? So I hide BES. Maybe there is something I unknown, I know the friends, please let us know. In addition, Oracle Application Server is a bit special, and some information has not been written in its JNDI port, and sometimes an additional bean class name will appear in its JNDI URL. I haven't used it, I don't know why, and JRUN is not A protocol name of JNDI service is required, so some processing is made to support these situations. The following is the code for this ContextProperties class: import java.util.properties; import javax.naming.initialcontext;

public class ContextProperties {public static final int WEBLOGIC = 0; public static final int WEBSPHERE = 1; public static final int JBOSS = 2; public static final int SUNONE = 3; public static final int ORACLE = 4; public static final int JRUN = 5; public static final int IONA = 6; private static final int BES = 7; // public static final int NULL_PORT = Integer.MAX_VALUE; private static final String WEBLOGIC_FACTORY = "weblogic.jndi.WLInitialContextFactory"; private static final String WEBLOGIC_PROTOCOL = "t3"; private static final int WEBLOGIC_PORT = 7001; private static final String WEBSPHERE_FACTORY = "com.ibm.websphere.naming.WsnInitialContextFactory"; private static final String WEBSPHERE_PROTOCOL = "iiop"; private static final int WEBSPHERE_PORT = 2809; // May Be 900 private static final string jboss_factory = "org.jnp.interfaces.namingContextFactory"; private static final string jboss_protocol = "jnp"; private stat ic final int JBOSS_PORT = 1099; private static final String SUNONE_FACTORY = "com.sun.jndi.cosnaming.CNCtxFactory"; private static final String SUNONE_PROTOCOL = "iiop"; private static final int SUNONE_PORT = 3700; private static final String BES_FACTORY = " "; private static final String BES_PROTOCOL =" "; private static final int BES_PORT = NULL_PORT; private static final String ORACLE_FACTORY =" com.evermind.server.rmi.RMIInitialContextFactory "; private static final String ORACLE_PROTOCOL =" ormi ";

private static final int ORACLE_PORT = 23791; // may be no private static final String JRUN_FACTORY = "jrun.naming.JRunContextFactory"; private static final String JRUN_PROTOCOL = ""; private static final int JRUN_PORT = 2908; private static final String IONA_FACTORY = "com.ejbhome.naming.spi.rmi.RMIInitCtxFactory"; private static final String IONA_PROTOCOL = "iiop"; private static final int IONA_PORT = 9070; private static final String DEFAULT_IP = "localhost"; private ContextProperties () {}

public static Properties newInstance (int serverType, String ip, int port, String addIn) {String factory = null; String protocol = null; switch (serverType) {case WEBLOGIC: factory = WEBLOGIC_FACTORY; protocol = WEBLOGIC_PROTOCOL; break; case WEBSPHERE: factory = WEBSPHERE_FACTORY; protocol = WEBSPHERE_PROTOCOL; break; case JBOSS: factory = JBOSS_FACTORY; protocol = JBOSS_PROTOCOL; break; case SUNONE: factory = SUNONE_FACTORY; protocol = SUNONE_PROTOCOL; break; case ORACLE: factory = ORACLE_FACTORY; protocol = ORACLE_PROTOCOL; break; case JRUN : Factory = JRUN_FACTORY; Protocol = JRUN_PROTOCOL; BREAK; Case Iona: factory = IONA_FACTORY; protocol = IONA_PROTOCOL; break; case BES: factory = BES_FACTORY; protocol = BES_PROTOCOL; break;} Properties props = new Properties (); if (! factory = null) {String slash = ""; if (protocol. Length ()> 0) {slash = ": //";} String portstring = ""; if (port! = null_port) {portstring = ":" port;} String additivePath = "

if (addIn.length ()> 0) {additivePath = "/" addIn;} props.put (InitialContext.INITIAL_CONTEXT_FACTORY, factory); props.put (InitialContext.PROVIDER_URL, protocol slash ip portString additivePath); } Return Props;} public static property newinstance (int serverType, String IP, INT port) {Return NewInstance (Servertype, IP, Port, ");

public static Properties newInstance (int serverType, String ip) {int port = Integer.MIN_VALUE; switch (serverType) {case WEBLOGIC: port = WEBLOGIC_PORT; break; case WEBSPHERE: port = WEBSPHERE_PORT; break; case JBOSS: port = JBOSS_PORT; break ; case SUNONE: port = SUNONE_PORT; break; case ORACLE: port = ORACLE_PORT; break; case JRUN: port = JRUN_PORT; break; case IONA: port = IONA_PORT; break; case BES: port = BES_PORT; break;} Properties props = NEW Properties (); if (port! = integer.min_value) {prOPs = newinstance (serverType, IP, port);} return.com

Public Static Properties NEWINSTANCE (INT ServerType) {Return NewInstance (Servertype, Default_IP);

}

转载请注明原文地址:https://www.9cbs.com/read-86847.html

New Post(0)