Access methods to server resources through JNDI
·If you are in a managed enviroment such as J2EE ‘s EJB container, then you can simply access the jndi object by injecting it via the annotations as follows:
EJB Container makes the resource jou’ve injected ready in a managed pool. When you need one, it pulls the session bean or resource from the pool.
@EJB
Private MyRemoteInterface myRemoteInterface;
If it is a resource like timer then you should use @Resource annotation
·@Resouce
private EJBContext context;
public void myRemoteMethod(){
MyLocalBean myBean=(MyLocalBean) context.lookup(“com.ozay.MyLocalBean”);
myBean..
}
·If your object is not managed that means can not inject server resource directly(it does not have @Stateless, @Statefull etc. bean annotation like your domain class), this time you can access the object via the jndi lookup as follows:
Context context = null;
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLIni tialContextFactory");
ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
ht.put(Context.SECURITY_PRINCIPAL, pUsername);
ht.put(Context.SECURITY_CREDENTIALS, pPassword);
try {
context = new InitialContext(ht);
MyLocalBean myBean=(MyLocalBean) context.lookup(“com.ozay.MyLocalBean”);
myBean.callMethod()
}catch (NamingException e) {
// an exception occurred
}finally {
try { context.close();}
catch (Exception e) {
// an exception occurred
}
}
And if your class running on a different jvm and it wants to access a resource that is kept securely, you must pass credantials.
If your server environment is a managed server or cluster architecture as shown in the figure below then you can make resource lookup via environment object without specifying credantials:
//MACHINE-B wants to access MACHINE-C
Environment env = new Environment();
env.setProviderURL("t3://machine-c:7001");
Context ctx = env.getInitialContext()
