001 package com.bea.medrec.utils;
002
003 import java.util.Collections;
004 import java.util.HashMap;
005 import java.util.Hashtable;
006 import java.util.Map;
007 import javax.ejb.EJBHome;
008 import javax.ejb.EJBLocalHome;
009 import javax.jms.Queue;
010 import javax.jms.QueueConnectionFactory;
011 import javax.naming.Context;
012 import javax.naming.InitialContext;
013 import javax.naming.NamingException;
014 import org.apache.log4j.Logger;
015
016 /**
017 * <p>Central service locator that encapsulates all service lookup and
018 * creation processes for EJBs, JMS, Transactions, Datasources, etc.</p>
019 *
020 * @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
021 */
022 public class ServiceLocator {
023 static Logger logger = MedRecLog4jFactory.getLogger(ServiceLocator.class.getName());
024
025 private InitialContext ic = null;
026 private Map<Object, Object> cache = null;
027 private static ServiceLocator instance;
028 private final static String JNDI_FACTORY =
029 "weblogic.jndi.WLInitialContextFactory";
030
031 /**
032 * Constructors
033 */
034 private ServiceLocator() throws NamingException {
035 try {
036 ic = new InitialContext();
037 cache = Collections.synchronizedMap(new HashMap<Object, Object>());
038 } catch (NamingException ne) {
039 logger.error(ne.getMessage());
040 throw ne;
041 }
042 }
043
044 private ServiceLocator(String pUrl)
045 throws NamingException {
046 try {
047 ic = getInitialContext(pUrl);
048 } catch (NamingException ne) {
049 logger.error(ne.getMessage());
050 throw ne;
051 }
052 }
053
054 private ServiceLocator(InitialContext iCtx) {
055 ic = iCtx;
056 }
057
058 /**
059 * <p>Get instance of ServiceLocator.</p>
060 *
061 * @return ServiceLocator
062 */
063 public static ServiceLocator getInstance() throws NamingException {
064 if (instance == null) instance = new ServiceLocator();
065 return instance;
066 }
067
068 /**
069 * <p>Get instance of ServiceLocator.</p>
070 *
071 * @param pUrl
072 * @return ServiceLocator
073 */
074 public static ServiceLocator getInstance(String pUrl) throws NamingException {
075 if (instance == null) instance = new ServiceLocator(pUrl);
076 return instance;
077 }
078
079 /**
080 * <p>Lookup EJB local home.</p>
081 *
082 * @param name
083 * @param homeClass
084 * @return EJBLocalHome
085 */
086 public EJBLocalHome lookupLocalHome(String name, Class homeClass)
087 throws NamingException, ClassCastException {
088 EJBLocalHome home = null;
089
090 if (cache.containsKey(name)) {
091 logger.debug("Getting cached local home for " + name);
092 home = (EJBLocalHome) cache.get(name);
093 } else {
094 logger.debug("Getting new local home for " + name);
095 home = EJBHomeFactory.getFactory(ic).lookupLocalHome(name, homeClass);
096 cache.put(name, home);
097 }
098 return home;
099 }
100
101 /**
102 * <p>Lookup EJB remote home.</p>
103 *
104 * @param name
105 * @param homeClass
106 * @return EJBLocalHome
107 */
108 public EJBHome lookupHome(String name, Class homeClass)
109 throws NamingException, ClassCastException {
110 EJBHome home = null;
111
112 if (cache.containsKey(name)) {
113 logger.debug("Getting cached remote home for " + name);
114 home = (EJBHome) cache.get(name);
115 } else {
116 logger.debug("Getting new remote home for " + name);
117 home = EJBHomeFactory.getFactory(ic).lookupHome(name, homeClass);
118 cache.put(name, home);
119 }
120 return home;
121 }
122
123 /**
124 * <p>Get local object.</p>
125 *
126 * @param name
127 * @param homeClass
128 * @return Object
129 */
130 public Object getLocalObj(String name, Class homeClass)
131 throws NamingException, ClassCastException {
132 return EJBHomeFactory.getFactory(ic).getLocalObj(name, homeClass);
133 }
134
135 /**
136 * <p>Get local object.</p>
137 *
138 * @param name
139 * @param homeClass
140 * @return Object
141 */
142 public Object getObj(String name, Class homeClass)
143 throws NamingException, ClassCastException {
144 return EJBHomeFactory.getFactory(ic).getObj(name, homeClass);
145 }
146
147 /**
148 * <p>Lookup QueueConnectionFactory.</p>
149 *
150 * @param name
151 * @return QueueConnectionFactory
152 */
153 public QueueConnectionFactory lookupQCFactory(String name)
154 throws NamingException {
155 QueueConnectionFactory queConnFactory = null;
156
157 if (cache.containsKey(name)) {
158 queConnFactory = (QueueConnectionFactory) cache.get(name);
159 } else {
160 queConnFactory = JMSFactory.getFactory(ic).lookupQCFactory(name);
161 cache.put(name, queConnFactory);
162 }
163 return queConnFactory;
164 }
165
166 /**
167 * <p>Lookup queue.</p>
168 *
169 * @param name
170 * @return Queue
171 */
172 public Queue lookupQueue(String name) throws NamingException {
173 Queue que = null;
174
175 if (cache.containsKey(name)) {
176 que = (Queue) cache.get(name);
177 } else {
178 que = JMSFactory.getFactory(ic).lookupQueue(name);
179 cache.put(name, que);
180 }
181 return que;
182 }
183
184 /**
185 * <p>Utility to get the InitialContext of a given server.</p>
186 *
187 * @param url URL of the server.
188 * @throws NamingException Thrown on naming errors.
189 */
190 public static InitialContext getInitialContext(String url)
191 throws NamingException {
192 Hashtable<String,String> env = new Hashtable<String,String>();
193 env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
194 env.put(Context.PROVIDER_URL, url);
195 return new InitialContext(env);
196 }
197 }
|