There may be many methods for each bean, and in general we call the method in the Sessionbean through a delegate instead of calling Sessionbean directly, Delegate is simply a simple encapsulation of the public method for each corresponding Sessionbean, eliminating every lookup of the home and the create of the EJB object at the time of invocation, but there may be many ways for our bean to If each bean writes such a delegate, the workload will be very large, and it is not easy to migrate the system later, for example, the original use of the EJB implementation, now to JDO direct operation of the database, and through the use of Java reflect technology, you can better achieve these requirements. First, a facadedelegate abstract class is defined that implements the lookup of the Sessionbean's home, as follows:
import javax.ejb.*;
import testejb.util.common.*;
import testejb.util.resource.*;
public abstract class FacadeDelegate{
private static String type = Resource.RemoteType;
public FacadeDelegate() {
}
public EJBHome getHome(String jindiName,Class className)
{
EJBHome home = null;
ServerLocatorAdapter adapter = ServerLocatorAdapter.getInstance();
try
{
home = (EJBHome)adapter.getHome(type, jindiName, className);
}
catch(Exception e)
{
System.err.println(e.getMessage() + jindiName + className.toString());
}
return home;
}
}
Where Serverlocatoradapter is a class that uses different methods to find home by invoking an EJB object as local or remote, and if type is local, calls the method in Localserverlocate. If type is remote, the method in Remoteserverlocate is called and the home is obtained. The code is as follows:
import java.util.*;
import java.lang.reflect.*;
Import testejb.util.resource.*;
public class Serverlocatoradapter {
private Map cache;//to cache home
private static Serverlocatoradapter me;
public static Serverlocatoradapter getinstance ()
{
if (me = = null)
me = new Serverlocatoradapter ();
return me;
}
//Get home
public Object gethome (String type,string jndihomename,class className) throws Exception
{
Object home = null;
if (Cache.containskey (jndihomename))
return Cache.get (jndihomename);
if (Resource.LocalType.equals (type))
{
home = Getlocalhome (jndihomename,classname);
Cache.put (jndihomename,home);
return home;
}
if (Resource.RemoteType.equals (type))
{
home = Getremotehome (jndihomename,classname);
Cache.put (jndihomename,home);
return home;
}
return home;
}
//Get local home
private Object getlocalhome (String jndihomename,class className) throws Exception
{
Class myClass = Class.forName (Resource.localclass);
//Resource. Localclass = "Testejb.util.common." Localserverlocator
method = Myclass.getmethod (Resource.localconstractmethod,null);
//Resource. Localconstractmethod = "GetInstance"
Localserverlocator local = null;
local = (localserverlocator) method.invoke (myclass,null);
return Local.getlocalhome (jndihomename,classname);
}
//Get remote home
private Object getremotehome (String jndihomename,class className) throws Exception
{
Class myClass = Class.forName (Resource.remoteclass);
//Resource.remoteclass = "Testejb.util.common.RemoteServerLocator"
method = Myclass.getmethod (Resource.remoteconstractmethod,null);
//resource.remoteconstractmethod= "getinstance"
Remoteserverlocator remote = null;
remote = (remoteserverlocator) method.invoke (myclass,null);
return Remote.gethome (jndihomename,classname);
}
Private Serverlocatoradapter () {
//provides thread-safe assurance for Cache
cache = Collections.synchronizedmap (new HashMap ());
}
}