1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
public class InitialServerContextFactory implements InitialContextFactory, InitialContextFactoryBuilder
{
private static Hashtable contexts = new Hashtable();
public InitialContextFactory createInitialContextFactory(Hashtable environment) throws NamingException
{
if (environment != null)
{
String factory = (String)environment.get(InitialContext.INITIAL_CONTEXT_FACTORY);
if (factory != null)
{
if (!InitialServerContext.class.getName().equals(factory))
{
try
{
return (InitialContextFactory)Class.forName(factory).newInstance();
}
catch (Throwable throwable)
{
throw new NamingException(throwable.getMessage());
}
}
}
}
return this;
}
public Context getInitialContext(Hashtable environment) throws NamingException
{
if (environment == null)
{
environment = new Hashtable(2);
}
Object server = environment.get(InitialServerContext.ENV_SERVER_NAME);
Object port = environment.get(InitialServerContext.ENV_SERVER_PORT);
String contextid = null;
if (server == null || port == null)
{
contextid = "default_context";
}
else
{
contextid = server.toString() + "_" + port.toString();
}
Context context = (Context)contexts.get(contextid);
if (context == null)
{
String type = (String)environment.get(InitialServerContext.ENV_CONTEXT_TYPE);
if (type != null && type.equals(InitialServerContext.LOCAL_CONTEXT_TYPE))
{
// Server context
context = new InitialServerContext("java:comp/env", environment);
try
{
RemoteContext remote = new RemoteContextRMI(environment);
context.bind("java:comp/env/"+RemoteContext.BINDING, remote);
}
catch (RemoteException remoteException)
{
throw new NamingException(remoteException.getMessage());
}
contexts.put("default_context", context);
}
else
{
// Remote client context
context = new RMIContext(environment);
}
contexts.put(contextid, context);
}
return context;
}
} |
Partager