KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jaxr > JAXRServiceImpl


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Guillaume Sauthier
22  * --------------------------------------------------------------------------
23  * $Id: JAXRServiceImpl.java,v 1.3 2005/04/28 08:43:26 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26 package org.objectweb.jonas.jaxr;
27
28 import java.util.Hashtable JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Properties JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 import javax.naming.Context JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37
38 import org.objectweb.jonas.common.JProp;
39 import org.objectweb.jonas.common.Log;
40 import org.objectweb.jonas.common.PropDump;
41 import org.objectweb.jonas.service.AbsServiceImpl;
42 import org.objectweb.jonas.service.ServiceException;
43
44 import org.objectweb.util.monolog.api.BasicLevel;
45 import org.objectweb.util.monolog.api.Logger;
46
47
48 /**
49  * <code>JAXRService</code> Implementation.
50  * @author Guillaume Sauthier
51  */

52 public class JAXRServiceImpl extends AbsServiceImpl implements JAXRService {
53
54     /**
55      * Initial Context for Naming
56      */

57     private Context JavaDoc ictx = null;
58     /**
59      * Logger for this service.
60      */

61     private static Logger logger = null;
62
63     /**
64      * List of the factories names to load when starting the MailService
65      */

66     private Vector JavaDoc factoryNames = new Vector JavaDoc();
67
68     /**
69      * List of the binded factories (jndi name -> factory name)
70      */

71     private Map JavaDoc bindedFactories = new Hashtable JavaDoc();
72     /**
73      * JAXR service configuration parameters (factories)
74      */

75     public static final String JavaDoc FACTORIES = "jonas.service.jaxr.factories";
76
77     /**
78      * JAXR service configuration parameters (class)
79      */

80     public static final String JavaDoc CLASS = "jonas.service.jaxr.class";
81
82     /**
83      * JAXR ConnectionFactory JNDI name
84      */

85     private static final String JavaDoc JNDI_NAME = "jaxr.jndi.name";
86
87     /**
88      * @see org.objectweb.jonas.service.AbsServiceImpl#doInit(javax.naming.Context)
89      */

90     protected void doInit(Context JavaDoc ctx) throws ServiceException {
91         // get logger for this service
92
logger = Log.getLogger(Log.JONAS_JAXR_PREFIX);
93
94         //Get the inital Context
95
try {
96             ictx = new InitialContext JavaDoc();
97         } catch (NamingException JavaDoc e) {
98             logger.log(BasicLevel.ERROR, "Cannot create initial context during the jaxr service initializing");
99             throw new ServiceException("Cannot create initial context during the jaxr service initializing", e);
100         }
101
102         // Get the list of the factory names
103
String JavaDoc factories = null;
104         try {
105             factories = (String JavaDoc) ctx.lookup(FACTORIES);
106         } catch (NamingException JavaDoc e) {
107             ; // No problem if there is no value for 'factories'
108
}
109         if (factories != null) {
110             String JavaDoc[] names = factories.split(", ");
111             for (int i = 0; i < names.length; i++) {
112                 factoryNames.add(names[i].trim());
113             }
114         }
115         if (logger.isLoggable(BasicLevel.DEBUG)) {
116             logger.log(BasicLevel.DEBUG, "jaxr service initialized");
117         }
118
119     }
120
121     /**
122      * @see org.objectweb.jonas.service.AbsServiceImpl#doStart()
123      */

124     protected void doStart() throws ServiceException {
125         // creates each factory
126
String JavaDoc factoryName = null;
127         for (Iterator JavaDoc i = factoryNames.iterator(); i.hasNext();) {
128             factoryName = (String JavaDoc) i.next();
129             try {
130                 JProp prop = JProp.getInstance(factoryName);
131                 if (logger.isLoggable(BasicLevel.DEBUG)) {
132                     logger.log(BasicLevel.DEBUG, "Creating JAXR Connection Factory " + factoryName);
133                 }
134                 createJAXRConnection(prop.getConfigFileEnv());
135             } catch (Exception JavaDoc e) {
136                 if (logger.isLoggable(BasicLevel.ERROR)) {
137                     logger.log(BasicLevel.ERROR, "JOnAS: Cannot create jaxr factory " + factoryName + " : " + e);
138                     logger.log(BasicLevel.ERROR, "Please check the " + factoryName + ".properties file");
139                 }
140             }
141         }
142     }
143
144     /**
145      * @see org.objectweb.jonas.service.AbsServiceImpl#doStop()
146      */

147     protected void doStop() throws ServiceException {
148         removeAllJAXRConnections();
149         if (logger.isLoggable(BasicLevel.DEBUG)) {
150             logger.log(BasicLevel.DEBUG, "jaxr service stopped");
151         }
152     }
153
154     /**
155      * Remove all Binded JAXRConnections
156      */

157     private void removeAllJAXRConnections() {
158
159         for (Iterator JavaDoc i = bindedFactories.keySet().iterator(); i.hasNext();) {
160             String JavaDoc name = (String JavaDoc) i.next();
161             removeJAXRConnection(name);
162         }
163
164     }
165
166     /**
167      * @see org.objectweb.jonas.jaxr.JAXRService#createJAXRConnection(java.util.Properties)
168      */

169     public void createJAXRConnection(Properties JavaDoc props) throws ServiceException {
170         if (logger.isLoggable(BasicLevel.DEBUG)) {
171             PropDump.print("These are the properties from which the jaxrService picks to construct jaxr Factories", props, logger, BasicLevel.DEBUG);
172         }
173
174         //Factory type/jndi name must be non null
175
String JavaDoc jndiName = props.getProperty(JNDI_NAME);
176
177         if (jndiName == null) {
178             String JavaDoc err = "The property '" + JNDI_NAME + "' is a required property.";
179             logger.log(BasicLevel.ERROR, err);
180             throw new ServiceException(err);
181         }
182
183         // Verify that jndi name not already used
184
if (bindedFactories.containsKey(jndiName)) {
185             String JavaDoc err = "There is already a factory bound with the name " + jndiName + ", please correct the provided configuration properties";
186             logger.log(BasicLevel.ERROR, err);
187             throw new ServiceException(err);
188         }
189
190         // Create the JAXRConnection Object
191
JAXRConnection jaxrConnection = new JAXRConnection(props);
192
193         // Bind the factory object in the naming context
194
bindJAXRFactory(jndiName, jaxrConnection);
195
196         logger.log(BasicLevel.INFO, "Mapping JAXR Connection Factory on " + jndiName);
197
198     }
199
200     /**
201      * Bind JAXRConnection into Registry and inside local Map.
202      * @param jndiName jndiName of the connection
203      * @param jaxrConnection the JAXRConnection to be bound
204      */

205     private void bindJAXRFactory(String JavaDoc jndiName, JAXRConnection jaxrConnection) {
206         try {
207             ictx.rebind(jndiName, jaxrConnection.getReference());
208             bindedFactories.put(jndiName, jaxrConnection);
209         } catch (NamingException JavaDoc e) {
210             String JavaDoc err = "Cannot bind jaxr factory '" + jndiName + "'";
211             logger.log(BasicLevel.ERROR, err);
212             throw new ServiceException(err, e);
213         }
214     }
215
216     /**
217      * @see org.objectweb.jonas.jaxr.JAXRService#modifyJAXRConnection(java.lang.String, org.objectweb.jonas.jaxr.JAXRConnection)
218      */

219     public void modifyJAXRConnection(String JavaDoc name, JAXRConnection jaxrc) throws ServiceException {
220         // remove old Connection
221
removeJAXRConnection(name);
222         // create new one
223
bindJAXRFactory(name, jaxrc);
224         if (logger.isLoggable(BasicLevel.DEBUG)) {
225             logger.log(BasicLevel.DEBUG, "JAXRConnection modified");
226         }
227     }
228
229     /**
230      * @see org.objectweb.jonas.jaxr.JAXRService#removeJAXRConnection(java.lang.String)
231      */

232     public void removeJAXRConnection(String JavaDoc name) throws ServiceException {
233
234         // test if factory is registered
235
if (!bindedFactories.containsKey(name)) {
236             String JavaDoc err = "Unknown JAXRConnection '" + name + "'";
237             logger.log(BasicLevel.ERROR, err);
238             throw new ServiceException(err);
239         }
240
241         // remove from the local list
242
bindedFactories.remove(name);
243
244         // remove from the registry
245
try {
246             ictx.unbind(name);
247         } catch (NamingException JavaDoc e) {
248             String JavaDoc err = "Cannot unbind JAXR Connection '" + name + "'";
249             logger.log(BasicLevel.ERROR, err);
250             throw new ServiceException(err, e);
251         }
252
253         if (logger.isLoggable(BasicLevel.DEBUG)) {
254             logger.log(BasicLevel.DEBUG, "JAXRConnection '" + name + "' removed");
255         }
256     }
257
258 }
259
Popular Tags