KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > util > BonitaServiceLocator


1 /**
2  *
3  * Bonita
4  * Copyright (C) 1999 Bull S.A.
5  * Bull 68 route de versailles 78434 Louveciennes Cedex France
6  * Further information: bonita@objectweb.org
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21  * USA
22  *
23  *
24 --------------------------------------------------------------------------
25  * $Id: BonitaServiceLocator.java,v 1.4 2005/03/18 14:55:05 mvaldes Exp $
26  *
27 --------------------------------------------------------------------------
28  */

29
30 package hero.util;
31
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.HashMap JavaDoc;
37
38 import javax.rmi.PortableRemoteObject JavaDoc;
39 import javax.mail.Session JavaDoc;
40
41 public class BonitaServiceLocator {
42     private static BonitaServiceLocator msl;
43     private Map JavaDoc cache;
44     private static Context JavaDoc initial;
45
46    
47     private BonitaServiceLocator() throws BonitaServiceException {
48     try {
49         initial=new InitialContext JavaDoc();
50         cache = new HashMap JavaDoc();
51     } catch (javax.naming.NamingException JavaDoc ne) {
52         throw new BonitaServiceException(ne.getMessage());
53     }
54     }
55
56     static public BonitaServiceLocator getInstance() throws BonitaServiceException {
57
58     if (msl==null) {
59         msl=new BonitaServiceLocator();
60     }
61     return msl;
62     }
63
64     // Services constant inner class - service objects
65

66     public class Services {
67        final public static int TOPIC_CONNECTION_FACTORY=0;
68        final public static int TOPIC=1;
69        final public static int MAIL_SERVICE=2;
70     }
71
72     // JMS related constants
73

74     final static String JavaDoc TOPIC_CONNECTION_FACTORY_NAME = "TCF";
75     final static String JavaDoc TOPIC_NAME = "testTopic";
76
77     // Mail configuration
78
final static String JavaDoc MAIL_SERVICE_NAME = "Mail";
79
80     // Returns the JNDI name for the required service
81

82
83     static private String JavaDoc getServiceName(int service) {
84       switch (service) {
85         case Services.TOPIC_CONNECTION_FACTORY: return TOPIC_CONNECTION_FACTORY_NAME;
86         case Services.TOPIC: return TOPIC_NAME;
87         case Services.MAIL_SERVICE: return MAIL_SERVICE_NAME;
88       }
89     return null;
90     }
91
92     // returns a EJBHome for the given service, using the JNDI name
93
// and the Class for the EJBHome
94

95     public Object JavaDoc getResource(int s) throws BonitaServiceException {
96     Object JavaDoc resource=null;
97     try {
98         String JavaDoc serv = getServiceName(s);
99         if (cache.containsKey(serv)) {
100           resource = (Object JavaDoc) cache.get(serv);
101         } else {
102            Object JavaDoc objref = initial.lookup(serv);
103            resource=objref;
104            cache.put(serv, resource);
105         }
106     } catch (NamingException JavaDoc ex) {
107         throw new BonitaServiceException("ServiceException");
108     } catch (Exception JavaDoc ex) {
109         System.out.println("ServiceLocator Exception");
110         ex.printStackTrace();
111     }
112
113     return resource;
114     }
115
116     public Object JavaDoc getMailSession(int s) throws BonitaServiceException {
117     Object JavaDoc mail=null;
118     try {
119         String JavaDoc serv = getServiceName(s);
120         if (cache.containsKey(serv)) {
121           mail = (Object JavaDoc) cache.get(serv);
122         } else {
123            Object JavaDoc objref = initial.lookup(serv);
124            mail= PortableRemoteObject.narrow(objref,Session JavaDoc.class);
125            cache.put(serv, mail);
126         }
127     } catch (NamingException JavaDoc ex) {
128         throw new BonitaServiceException("ServiceException");
129     } catch (Exception JavaDoc ex) {
130         System.out.println("ServiceLocator Exception");
131         ex.printStackTrace();
132     }
133
134     return mail;
135     }
136   
137 }
138
Popular Tags