001 package com.bea.medrec.utils;
002
003 import java.lang.reflect.InvocationTargetException;
004 import java.lang.reflect.Method;
005 import javax.ejb.EJBException;
006 import javax.ejb.EJBHome;
007 import javax.ejb.EJBLocalHome;
008 import javax.naming.InitialContext;
009 import javax.naming.NamingException;
010 import javax.rmi.PortableRemoteObject;
011 import org.apache.log4j.Logger;
012
013 /**
014 * <p>Encapsulates service lookup and creation processes for EJBs.</p>
015 *
016 * @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
017 */
018 public class EJBHomeFactory {
019
020 static Logger logger = MedRecLog4jFactory.getLogger(EJBHomeFactory.class.getName());
021
022 private InitialContext ic = null;
023 private static EJBHomeFactory instance;
024
025 /**
026 * Construtors
027 */
028 private EJBHomeFactory() throws NamingException
029 {
030 try {
031 ic = new InitialContext();
032 }
033 catch(NamingException ne) {
034 logger.error(ne.getMessage());
035 throw ne;
036 }
037 }
038
039 private EJBHomeFactory(InitialContext iCtx) { ic = iCtx; }
040
041 /**
042 * <p>Get instance of EJB factory.</p>
043 *
044 * @return EJBHomeFactory
045 */
046 public static EJBHomeFactory getFactory() throws NamingException {
047 if (instance == null) instance = new EJBHomeFactory();
048 return instance;
049 }
050
051 /**
052 * <p>Get instance of EJB factory.</p>
053 *
054 * @param iCtx
055 *
056 * @return EJBHomeFactory
057 */
058 public static EJBHomeFactory getFactory(InitialContext iCtx) {
059 if (instance == null) instance = new EJBHomeFactory(iCtx);
060 return instance;
061 }
062
063 /**
064 * <p>Lookup EJB local home.</p>
065 *
066 * @param name
067 * @param homeClass
068 *
069 * @return EJBLocalHome
070 */
071 public EJBLocalHome lookupLocalHome(String name, Class homeClass)
072 throws NamingException, ClassCastException
073 {
074 logger.debug("Getting local home: "+name+", "+homeClass.getName());
075
076 EJBLocalHome home = null;
077 try {
078 home = (EJBLocalHome)ic.lookup(name);
079 }
080 catch(NamingException ne) {
081 logger.error(ne.getMessage());
082 throw ne;
083 }
084 catch(ClassCastException cce) {
085 logger.error(cce.getMessage());
086 throw cce;
087 }
088 return home;
089 }
090
091 /**
092 * <p>Lookup EJB remote home.</p>
093 *
094 * @param name
095 * @param homeClass
096 *
097 * @return EJBHome
098 */
099 public EJBHome lookupHome(String name, Class homeClass)
100 throws NamingException, ClassCastException
101 {
102 logger.debug("Getting home: "+name+", "+homeClass.getName());
103
104 EJBHome home = null;
105
106 try {
107 Object objRef = ic.lookup(name);
108 logger.debug("objRef: "+objRef);
109 home = (EJBHome)PortableRemoteObject.narrow(objRef, homeClass);
110 }
111 catch(NamingException ne) {
112 logger.error(ne.getMessage());
113 throw ne;
114 }
115 catch(ClassCastException cce) {
116 logger.error(cce.getMessage());
117 throw cce;
118 }
119 return home;
120 }
121
122 /**
123 * <p>Get local object.</p>
124 *
125 * @param pJndiLookupName
126 * @param homeClass
127 *
128 * @return Object
129 */
130 public Object getLocalObj(String pJndiLookupName, Class homeClass)
131 throws EJBException, NamingException
132 {
133 try
134 {
135 EJBLocalHome objLocalHome = lookupLocalHome(pJndiLookupName, homeClass);
136 //get the method of create
137 Method m =
138 objLocalHome.getClass().getDeclaredMethod("create", new Class[0]);
139 //invoke the create method
140 Object obj = m.invoke (objLocalHome, new Object[0]);
141 return obj;
142 }
143 catch (NoSuchMethodException ne) {
144 throw new EJBException (ne);
145 }
146 catch (InvocationTargetException ie) {
147 throw new EJBException (ie);
148 }
149 catch (IllegalAccessException iae) {
150 throw new EJBException (iae);
151 }
152 }
153
154 /**
155 * <p>Get object.</p>
156 *
157 * @param pJndiLookupName
158 * @param homeClass
159 *
160 * @return Object
161 */
162 public Object getObj(String pJndiLookupName, Class homeClass)
163 throws EJBException, NamingException
164 {
165 try
166 {
167 EJBHome objLocalHome = lookupHome(pJndiLookupName, homeClass);
168 //get the method of create
169 Method m = objLocalHome.getClass().getDeclaredMethod("create", new Class[0]);
170 //invoke the create method
171 Object obj = m.invoke (objLocalHome, new Object[0]);
172 return obj;
173 }
174 catch (NoSuchMethodException ne) {
175 throw new EJBException (ne);
176 }
177 catch (InvocationTargetException ie) {
178 throw new EJBException (ie);
179 }
180 catch (IllegalAccessException iae) {
181 throw new EJBException (iae);
182 }
183 }
184 }
|