• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

must the service locator be a singleton

 
zb cong
Ranch Hand
Posts: 416
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
...............
...............
...............
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic