EJBHomeFactory.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:4k
源码类别:
Java编程
开发平台:
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;
- }
- }