📄 ejbhomefactory.java
字号:
package com.portal.util;
import javax.ejb.*;
import java.rmi.*;
import javax.rmi.*;
import java.util.*;
import javax.naming.*;
/**
* 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;
/**
* 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 HomeFactoryException
{
try
{
if ( EJBHomeFactory.aFactorySingleton == null )
{
EJBHomeFactory.aFactorySingleton = new EJBHomeFactory();
}
} catch (NamingException e)
{
throw new HomeFactoryException(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 HomeFactoryException
{
EJBHome anEJBHome;
anEJBHome = (EJBHome) this.ejbHomes.get(homeClass);
try
{
if(anEJBHome == null)
{
anEJBHome = (EJBHome) PortableRemoteObject.narrow
(ctx.lookup(homeClass.getName()), homeClass);
this.ejbHomes.put(homeClass, anEJBHome);
}
}
catch (ClassCastException e)
{
throw new HomeFactoryException(e);
}
catch (NamingException e)
{
throw new HomeFactoryException(e);
}
return anEJBHome;
}
/**
* Lookup and cache an EJBHome object.
* This 'alternate' implementation delegates JNDI name knowledge
* to the client. It is included here for example only.
*/
public EJBHome lookUpHome(Class homeClass, String jndiName)
throws HomeFactoryException
{
EJBHome anEJBHome;
anEJBHome = (EJBHome) this.ejbHomes.get(homeClass);
try
{
if(anEJBHome == null)
{
System.out.println("finding HOME for first time");
anEJBHome = (EJBHome) PortableRemoteObject.narrow
( ctx.lookup (jndiName), homeClass);
this.ejbHomes.put(homeClass, anEJBHome);
}
}
catch (ClassCastException e)
{
throw new HomeFactoryException(e);
}
catch (NamingException e)
{
throw new HomeFactoryException(e);
}
return anEJBHome;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -