001: /**
002: *
003: * Bonita
004: * Copyright (C) 1999 Bull S.A.
005: * Bull 68 route de versailles 78434 Louveciennes Cedex France
006: * Further information: bonita@objectweb.org
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public
019: * License along with this library; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
021: * USA
022: *
023: *
024: --------------------------------------------------------------------------
025: * $Id: BonitaServiceLocator.java,v 1.5 2005/09/11 09:41:58 brice Exp $
026: *
027: --------------------------------------------------------------------------
028: */package hero.util;
029:
030: import javax.naming.Context;
031: import javax.naming.InitialContext;
032: import javax.naming.NamingException;
033: import java.util.Map;
034: import java.util.HashMap;
035:
036: import javax.rmi.PortableRemoteObject;
037: import javax.mail.Session;
038:
039: public class BonitaServiceLocator {
040: private static BonitaServiceLocator msl;
041: private Map cache;
042: private static Context initial;
043:
044: private BonitaServiceLocator() throws BonitaServiceException {
045: try {
046: initial = new InitialContext();
047: cache = new HashMap();
048: } catch (javax.naming.NamingException ne) {
049: throw new BonitaServiceException(ne.getMessage());
050: }
051: }
052:
053: static public BonitaServiceLocator getInstance()
054: throws BonitaServiceException {
055:
056: if (msl == null) {
057: msl = new BonitaServiceLocator();
058: }
059: return msl;
060: }
061:
062: // Services constant inner class - service objects
063:
064: public class Services {
065: final public static int TOPIC_CONNECTION_FACTORY = 0;
066: final public static int TOPIC = 1;
067: final public static int MAIL_SERVICE = 2;
068: }
069:
070: // JMS related constants
071: // NB: It was decided not to change the name of the factory in
072: // JBoss configuration to match JOnAS name, hence the test.
073: final static String TOPIC_CONNECTION_FACTORY_NAME = ServerType
074: .isJonas() ? "TCF" : "java:/XAConnectionFactory";
075: final static String TOPIC_NAME = "testTopic";
076:
077: // Mail configuration
078: final static String MAIL_SERVICE_NAME = "Mail";
079:
080: // Returns the JNDI name for the required service
081:
082: static private String getServiceName(int service) {
083: switch (service) {
084: case Services.TOPIC_CONNECTION_FACTORY:
085: return TOPIC_CONNECTION_FACTORY_NAME;
086: case Services.TOPIC:
087: return TOPIC_NAME;
088: case Services.MAIL_SERVICE:
089: return MAIL_SERVICE_NAME;
090: }
091: return null;
092: }
093:
094: // returns a EJBHome for the given service, using the JNDI name
095: // and the Class for the EJBHome
096:
097: public Object getResource(int s) throws BonitaServiceException {
098: Object resource = null;
099: try {
100: String serv = getServiceName(s);
101: if (cache.containsKey(serv)) {
102: resource = (Object) cache.get(serv);
103: } else {
104: Object objref = initial.lookup(serv);
105: resource = objref;
106: cache.put(serv, resource);
107: }
108: } catch (NamingException ex) {
109: throw new BonitaServiceException("ServiceException");
110: } catch (Exception ex) {
111: System.out.println("ServiceLocator Exception");
112: ex.printStackTrace();
113: }
114:
115: return resource;
116: }
117:
118: public Object getMailSession(int s) throws BonitaServiceException {
119: Object mail = null;
120: try {
121: String serv = getServiceName(s);
122: if (cache.containsKey(serv)) {
123: mail = (Object) cache.get(serv);
124: } else {
125: Object objref = initial.lookup(serv);
126: mail = PortableRemoteObject.narrow(objref,
127: Session.class);
128: cache.put(serv, mail);
129: }
130: } catch (NamingException ex) {
131: throw new BonitaServiceException("ServiceException");
132: } catch (Exception ex) {
133: System.out.println("ServiceLocator Exception");
134: ex.printStackTrace();
135: }
136:
137: return mail;
138: }
139:
140: }
|