Jay,
I have this (utility) class that just does that for you.
(see code below). It does cache ejb lookups in a hashtable.
Warning: do not use the same (jndi/link) name for two
different EJBs types.
----------------------------------------------------------------------------
----
package com.foliquest.ejb.util;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Hashtable;
/**
* The LookupFactory utility class makes it easy to locate remote objects.
* The remote objects once looked up are stored in a <code>Hashtable</code>.
* @author Krishnan Subramanian
* @version 0.1
**/
public final class LookupFactory {
private static Context context_ = null;
// A Hashtable capable of storing 150 entries without growing
// since the load factor is 0.75 by default (200 * 0.75 = 150)
private static Hashtable homeInterfaces = new Hashtable(200);
// prevent instantiation of this class
private LookupFactory() {
}
private synchronized static Context getContext() throws NamingException {
if (context_ == null) {
context_ = new InitialContext();
}
return context_;
}
/**
* This method looks up a remote object given the object name and
* the class to which it belongs. The method below can be used
* by client side applications and by other EJBs. Caching the
<code>Context</code>
* will speed up remote object lookups dramatically.
* @param objectName The remote object's name which should be used to
* perform the lookup
* @param objectClass The class to which this remote object belongs. This
* is necessary to narrow down the remote object to the desired class.
* @return object The remote object which has to be cast (normal java
* cast) to the required type usually the home interface of the EJB.
* @exception NamingException The object with the given name could not
* be found
*/
public static Object lookup(String objectName, Class objectClass) throws
NamingException {
Object aRemoteObject = homeInterfaces.get(objectName);
if (aRemoteObject == null) { // i.e. not present in the hashtable, then
look it up
aRemoteObject =
javax.rmi.PortableRemoteObject.narrow(getContext().lookup(objectName),
objectClass);
homeInterfaces.put(objectName, aRemoteObject);
}
return aRemoteObject;
}
/**
* This method is used to lookup an (environment) entry in the JNDI.
* e.g.: An Integer in the "java:comp/env/SomeVar"
* @param environmentEntry The JNDI location to perform the lookup
* @return Object The object which has to be case to the required type
* @exception NamingException The object with the given name could not be
* found in the JNDI
*/
public static Object jndiLookup(String environmentEntry) throws
NamingException {
return getContext().lookup(environmentEntry);
}
}
----------------------------------------------------------------------------
-------------
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".