EJBfactoryの設計方法.
一、EJBfactory設計類は以下の通りである.
二、EJBインスタンスを取得する方法:
三、EJBfactoryの設計方法のみを参考にする.
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.EJBHome;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
/**
* EJB Home Factory, maintains a simple hashmap cache of EJBHomes
* For a production implementations, exceptions such as NamingException
* can be wrapped with a factory exception to futher simplify
* the client.
*/
public class EJBHomeFactory
{
private Map ejbHomes;
private static EJBHomeFactory aFactorySingleton;
Context ctx;
private Log4j log = new Log4j(Constant.ModuleType.SETTLEMENT, this);
/**
* EJBHomeFactory private constructor.
*/
private EJBHomeFactory() throws NamingException
{
try
{
ctx = new InitialContext();
this.ejbHomes = Collections.synchronizedMap(new HashMap());
}
catch (Exception e)
{
//can't do anything about this
//client will catch errors upon trying to
//do a lookup
}
}
/**
* Returns the singleton instance of the EJBHomeFactory
* The sychronized keyword is intentionally left out the
* as I don't think the potential to intialize the singleton
* twice at startup time (which is not a destructive event)
* is worth creating a sychronization bottleneck on this
* VERY frequently used class, for the lifetime of the
* client application.
*/
public static EJBHomeFactory getFactory() throws IException
{
try
{
if (EJBHomeFactory.aFactorySingleton == null)
{
EJBHomeFactory.aFactorySingleton = new EJBHomeFactory();
}
}
catch (NamingException e)
{
throw new IException("EJBHomeFactory NamingException", e);
}
return EJBHomeFactory.aFactorySingleton;
}
/**
* Lookup and cache an EJBHome object using a home class.
* Assumes that the JNDI name of the EJB Home being looked for
* is the same as the fully qualified class name of the
* same EJB Home.
* If EJB-REF tags are being used externally, then the classname
* of the EJB Home can be mapped to the actual JNDI name of the
* deployed bean transaprently at deployment time.
* If EJB-REF tags are not used, then the EJB's must be deployed
* with JNDI names equal to their fully qualified home interfaces.
*/
public EJBHome lookUpHome(Class homeClass) throws IException
{
EJBHome anEJBHome;
String jndiName = "";
anEJBHome = (EJBHome) this.ejbHomes.get(homeClass);
try
{
String ResourceReference = homeClass.getName().substring(homeClass.getName().lastIndexOf(".") + 1, homeClass.getName().length());
if (anEJBHome == null)
{
anEJBHome = EJBObject.getEJBHome(ResourceReference);
//anEJBHome = (EJBHome) PortableRemoteObject.narrow(ctx.lookup(jndiName), AccountHome.class);
this.ejbHomes.put(homeClass, anEJBHome);
log.info(ResourceReference + " lookup successfully.");
}
}
catch (ClassCastException e)
{
throw new IException("EJBHomeFactory ClassCastException", e);
}
catch (NamingException e)
{
throw new IException("EJBHomeFactory ClassCastException", e);
}
catch (Exception e)
{
Log.print(jndiName + " home not found - " + "Is bean registered with JNDI?: " + e.toString());
}
return anEJBHome;
}
}
二、EJBインスタンスを取得する方法:
try
{
TransCurrentDepositHome home =
(TransCurrentDepositHome) EJBHomeFactory.getFactory().lookUpHome(TransCurrentDepositHome.class);
transCurrentDepositFacade = (TransCurrentDeposit) home.create();
}
catch (RemoteException e)
{
throw e;
}
catch (IException e)
{
e.printStackTrace();
throw new RemoteException();
}
catch (CreateException e)
{
e.printStackTrace();
throw new RemoteException();
}
三、EJBfactoryの設計方法のみを参考にする.