GeeCON Prague 2014*
The moose likes OO, Patterns, UML and Refactoring and the fly likes must the service locator be a singleton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "must the service locator be a singleton" Watch "must the service locator be a singleton" New topic
Author

must the service locator be a singleton

zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
from the "java core pattern" book,i have learned that the service locator is a singleton,but when i downloaded the petstore1.3.1_01 and opened the source code,i find the web tier locator is a singleton,but the ejb tier locator is not a singleton,why?
the ejb tier locator as:

package com.sun.j2ee.blueprints.servicelocator.ejb;
import java.net.URL;
import javax.ejb.EJBHome;
import javax.ejb.EJBLocalHome;
import javax.jms.QueueConnectionFactory;
import javax.jms.Queue;
import javax.jms.TopicConnectionFactory;
import javax.jms.Topic;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.sql.DataSource;
import com.sun.j2ee.blueprints.servicelocator.ServiceLocatorException;
public class ServiceLocator {
private InitialContext ic;
private static ServiceLocator me;
public ServiceLocator() throws ServiceLocatorException {
try {
ic = new InitialContext();
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
}
................
................
................
................

the web tier locator as:

package com.sun.j2ee.blueprints.servicelocator.web;
import java.util.HashMap;
import java.util.Map;
import java.util.Collections;
import java.net.URL;

import javax.ejb.EJBHome;
import javax.ejb.EJBLocalHome;
import javax.jms.QueueConnectionFactory;
import javax.jms.Queue;
import javax.jms.TopicConnectionFactory;
import javax.jms.Topic;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.sql.DataSource;
import com.sun.j2ee.blueprints.servicelocator.ServiceLocatorException;
public class ServiceLocator {
private InitialContext ic;
private Map cache; //used to hold references to EJBHomes/JMS Resources for re-use
private static ServiceLocator me;
static {
try {
me = new ServiceLocator();
} catch(ServiceLocatorException se) {
System.err.println(se);
se.printStackTrace(System.err);
}
}
private ServiceLocator() throws ServiceLocatorException {
try {
ic = new InitialContext();
cache = Collections.synchronizedMap(new HashMap());
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
}
static public ServiceLocator getInstance() {
return me;
}
...............
...............
...............
 
GeeCON Prague 2014
 
subject: must the service locator be a singleton