A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Engineering
»
OO, Patterns, UML and Refactoring
Author
must the service locator be a singleton
zb cong
Ranch Hand
Joined: Jan 14, 2002
Posts: 403
posted
Feb 23, 2003 07:04:00
0
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;
}
...............
...............
...............
I agree. Here's the link:
http://aspose.com/file-tools
subject: must the service locator be a singleton
Similar Threads
Service Locator
Service Locator Pattern
caching the jndi call
static code?
Proble with Relationship in Entity Bean
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter