编写EJB客户端时取得那个Context对象是比较棘手的一个问题,对于不同的应用服务器,其工厂类和JNDI URL总是不同的,每次都得到处找资料,这次干脆一次性把所有资料都找了一下,写成一个类去获取InitialContext构造函数需要的那个Properties对象。查找的过程比我想象的艰难,这更坚定了我要将它们找出来的决心。目前已找出当今主流J2EE应用服务器的相关资料,有weblogic,websphere,jboss,sun one,oracle application server,JRun,IONA,对于Borland Enterprise Server(BES),大量资料表明它的客户端不需要为InitialContext构造函数提供参数,即Context ctx = new InitialContext()即可,对此我百思不得其解,Borland到底怎么实现获取其它机器上JNDI上下文的呢?所以我将BES对外隐藏了(private)。也许有我未知的东西,有知道的朋友请告知。另外,oracle application server有点特别,有些资料上没有写明它的JNDI端口,并且有时在其JNDI URL中会出现一个附加的Bean类名,这个服务器我没用过,也不知道原因,而JRun不需要提供JNDI服务的协议名,因此为了支持这些情况特地做了一些处理。
以下是这个ContextProperties类的代码:
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 static 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 Properties 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 props;
}
public static Properties newInstance(int serverType){
return newInstance(serverType, DEFAULT_IP);
}
}
本文地址:http://com.8s8s.com/it/it13030.htm