1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Com.Elect.GLEP.Clients.Locator ;
/**
*
*
*/
import Com.Elect.GLEP.clients.Locator.ServiceLocatorException;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import Com.Elect.GLEP.Util.Constants;
/**
* This class enables Swing client to look for resources (like home, remote interfaces...).
* It follows the singleton pattern
*/
public class Service {
// ======================================
// = Attributes =
// ======================================
private final String cname = this.getClass().getName() ;
private Logger logger = Logger.getLogger(Constants.LOGGER_CLIENT) ;
private Context initalContext ;
private Map<String, Object> cache ;
// ======================================
// = Singleton =
// ======================================
private static final Service instance = new Service();
public static Service getInstance() {
return instance;
}
private Service() throws ServiceLocatorException {
try {
initalContext = new InitialContext();
cache = new HashMap<String, Object>();
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
}
// ======================================
// = Business methods =
// ======================================
/**
* will get the ejb Local home factory. If this ejb home factory has already been
* clients need to cast to the type of EJBHome they desire
*
* @param jndiName JNDI name of the EJB that we are looking for
* @return the EJB Home corresponding to the homeName
* @throws ServiceLocatorException thrown if lookup problems
*/
public Object getRemoteInterface(String jndiName) throws ServiceLocatorException {
String methodName = "getRemoteInterface" ;
logger.entering(cname, methodName, jndiName) ;
Object remoteInterface = getRemoteObject(jndiName) ;
return remoteInterface ;
}
/**
* @param connFactoryName Name of the connection factory that we are looking for
* @return the factory for the factory to get queue connections from
* @throws ServiceLocatorException thrown if lookup problems
*/
public ConnectionFactory getConnectionFactory(String connFactoryName) throws ServiceLocatorException {
String methodName = "getConnectionFactory";
logger.entering(cname, methodName, connFactoryName);
ConnectionFactory factory = (ConnectionFactory) getRemoteObject(connFactoryName);
return factory;
}
public Destination getDestination(String destinationName) {
String methodName = "getDestination" ;
logger.entering(cname, methodName, destinationName) ;
Destination destination = (Destination) getRemoteObject(destinationName) ;
return destination;
}
// ======================================
// = Private methods =
// ======================================
private synchronized Object getRemoteObject(String jndiName) throws ServiceLocatorException {
String methodName = "getObject" ;
logger.entering(cname, methodName, jndiName) ;
Object remoteObject = cache.get(jndiName);
if (remoteObject == null) {
try {
remoteObject = initalContext.lookup(jndiName);
cache.put(jndiName, remoteObject);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
}
return remoteObject;
}
} |
Partager