/Users/lyon/j4p/src/classUtils/pack/util/pool/EjbFactory.java

1    package classUtils.pack.util.pool; 
2     
3    import javax.ejb.EJBHome; 
4    import javax.naming.Context; 
5    import javax.naming.InitialContext; 
6    import javax.naming.NamingException; 
7    import javax.rmi.PortableRemoteObject; 
8    import java.lang.reflect.Method; 
9    import classUtils.pack.util.Setup; 
10    
11     /** 
12      * An  implementation which uses reflection 
13      * to create instances of enterprise java beans, looking up the home interface and invoking a 
14      * proper <tt>create()</tt> method. 
15      * <p> 
16      * The JNDI name and the home interface class of the EJB to pool must be provided at construction. 
17      * <p> 
18      * After creation, objects can be optionally setup by an user-provided class implementing 
19      * the {@link classUtils.pack.util.Setup Setup} interface. 
20      * 
21      * @author Cristiano Sadun 
22      * @version 1.0 
23      */ 
24     public class EjbFactory extends ObjectPool.BaseFactory { 
25    
26       private Method createMethod; 
27       private String jndiName; 
28       private EJBHome home; 
29    
30       /** 
31        * Create a factory which makes use of a <tt>create()</tt> method on the home interface of 
32        * an EJB with the given JNDI name and expecting the given parameters; after creation, 
33        * the given {@link classUtils.pack.util.Setup Setup} object will be used for post initialization. 
34        * @param jndiName the JNDI name of the EJB's home interface 
35        * @param remoteInterface the remote interface of the EJB 
36        * @param params the types of the parameters expected by the EJB's create() method, or <b>null</b> 
37        * @param ps the {@link classUtils.pack.util.Setup Setup} object to be used for post-construction setup, or <b>null</b> 
38        */ 
39       public EjbFactory(String jndiName, Class remoteInterface, Object []params, Setup ps) { 
40           super(remoteInterface, params, ps); 
41           this.jndiName=jndiName; 
42       } 
43    
44       /* 
45        * Create a factory which makes use of a <tt>create()</tt> method on the home interface of 
46        * an EJB with the given JNDI name and expecting the given parameters 
47        * @param jndiName the JNDI name of the EJB's home interface 
48        * @param remoteInterface the remote interface of the EJB 
49        * @param params the types of the parameters expected by the EJB's create() method 
50        */ 
51       public EjbFactory(String jndiName, Class remoteInterface, Object []params) { 
52           this(jndiName, remoteInterface, params, null); 
53       } 
54    
55       /* 
56        * Create a factory which makes use of a <tt>create()</tt> method on the home interface of 
57        * an EJB with the given JNDI name with no parameters 
58        * @param jndiName the JNDI name of the EJB's home interface 
59        * @param remoteInterface the remote interface of the EJB 
60        */ 
61       public EjbFactory(String jndiName, Class remoteInterface) { 
62           this(jndiName, remoteInterface, null, null); 
63       } 
64    
65       public Object create() throws ObjectPool.ObjectPoolException { 
66           try { 
67               if (home==null) { 
68                   Context ic = new InitialContext(); 
69                   Object obj = ic.lookup(jndiName); 
70                   home = (EJBHome)PortableRemoteObject.narrow(obj, EJBHome.class); 
71                   home = (EJBHome)PortableRemoteObject.narrow(obj, home.getEJBMetaData().getHomeInterfaceClass()); 
72               } 
73               Object newObject = createMethod.invoke(home, params); 
74               if (ps != null) ps.setup(newObject); 
75               return newObject; 
76           } catch(Exception e) { 
77               throw new ObjectPool.ObjectPoolException(e); 
78           } 
79       } 
80    
81       /** 
82        * Return the JNDI name of the Enterprise Java Bean produced by this factory 
83        * @return the JNDI name of the Enterprise Java Bean produced by this factory 
84        */ 
85       public String getJNDIName() { return jndiName; } 
86    
87       /** 
88        * Get the home interface used by this factory to produce Enterprise Java Beans 
89        * @return the home interface used by this factory to produce Enterprise Java Beans 
90        */ 
91       public EJBHome getEJBHome() { return home; } 
92    
93     } 
94