001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): Guillaume Sauthier
022: * --------------------------------------------------------------------------
023: * $Id: JAXRServiceImpl.java 6661 2005-04-28 08:43:27Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.jaxr;
026:
027: import java.util.Hashtable;
028: import java.util.Iterator;
029: import java.util.Map;
030: import java.util.Properties;
031: import java.util.Vector;
032:
033: import javax.naming.Context;
034: import javax.naming.InitialContext;
035: import javax.naming.NamingException;
036:
037: import org.objectweb.jonas.common.JProp;
038: import org.objectweb.jonas.common.Log;
039: import org.objectweb.jonas.common.PropDump;
040: import org.objectweb.jonas.service.AbsServiceImpl;
041: import org.objectweb.jonas.service.ServiceException;
042:
043: import org.objectweb.util.monolog.api.BasicLevel;
044: import org.objectweb.util.monolog.api.Logger;
045:
046: /**
047: * <code>JAXRService</code> Implementation.
048: * @author Guillaume Sauthier
049: */
050: public class JAXRServiceImpl extends AbsServiceImpl implements
051: JAXRService {
052:
053: /**
054: * Initial Context for Naming
055: */
056: private Context ictx = null;
057: /**
058: * Logger for this service.
059: */
060: private static Logger logger = null;
061:
062: /**
063: * List of the factories names to load when starting the MailService
064: */
065: private Vector factoryNames = new Vector();
066:
067: /**
068: * List of the binded factories (jndi name -> factory name)
069: */
070: private Map bindedFactories = new Hashtable();
071: /**
072: * JAXR service configuration parameters (factories)
073: */
074: public static final String FACTORIES = "jonas.service.jaxr.factories";
075:
076: /**
077: * JAXR service configuration parameters (class)
078: */
079: public static final String CLASS = "jonas.service.jaxr.class";
080:
081: /**
082: * JAXR ConnectionFactory JNDI name
083: */
084: private static final String JNDI_NAME = "jaxr.jndi.name";
085:
086: /**
087: * @see org.objectweb.jonas.service.AbsServiceImpl#doInit(javax.naming.Context)
088: */
089: protected void doInit(Context ctx) throws ServiceException {
090: // get logger for this service
091: logger = Log.getLogger(Log.JONAS_JAXR_PREFIX);
092:
093: //Get the inital Context
094: try {
095: ictx = new InitialContext();
096: } catch (NamingException e) {
097: logger
098: .log(BasicLevel.ERROR,
099: "Cannot create initial context during the jaxr service initializing");
100: throw new ServiceException(
101: "Cannot create initial context during the jaxr service initializing",
102: e);
103: }
104:
105: // Get the list of the factory names
106: String factories = null;
107: try {
108: factories = (String) ctx.lookup(FACTORIES);
109: } catch (NamingException e) {
110: ; // No problem if there is no value for 'factories'
111: }
112: if (factories != null) {
113: String[] names = factories.split(", ");
114: for (int i = 0; i < names.length; i++) {
115: factoryNames.add(names[i].trim());
116: }
117: }
118: if (logger.isLoggable(BasicLevel.DEBUG)) {
119: logger.log(BasicLevel.DEBUG, "jaxr service initialized");
120: }
121:
122: }
123:
124: /**
125: * @see org.objectweb.jonas.service.AbsServiceImpl#doStart()
126: */
127: protected void doStart() throws ServiceException {
128: // creates each factory
129: String factoryName = null;
130: for (Iterator i = factoryNames.iterator(); i.hasNext();) {
131: factoryName = (String) i.next();
132: try {
133: JProp prop = JProp.getInstance(factoryName);
134: if (logger.isLoggable(BasicLevel.DEBUG)) {
135: logger.log(BasicLevel.DEBUG,
136: "Creating JAXR Connection Factory "
137: + factoryName);
138: }
139: createJAXRConnection(prop.getConfigFileEnv());
140: } catch (Exception e) {
141: if (logger.isLoggable(BasicLevel.ERROR)) {
142: logger.log(BasicLevel.ERROR,
143: "JOnAS: Cannot create jaxr factory "
144: + factoryName + " : " + e);
145: logger.log(BasicLevel.ERROR, "Please check the "
146: + factoryName + ".properties file");
147: }
148: }
149: }
150: }
151:
152: /**
153: * @see org.objectweb.jonas.service.AbsServiceImpl#doStop()
154: */
155: protected void doStop() throws ServiceException {
156: removeAllJAXRConnections();
157: if (logger.isLoggable(BasicLevel.DEBUG)) {
158: logger.log(BasicLevel.DEBUG, "jaxr service stopped");
159: }
160: }
161:
162: /**
163: * Remove all Binded JAXRConnections
164: */
165: private void removeAllJAXRConnections() {
166:
167: for (Iterator i = bindedFactories.keySet().iterator(); i
168: .hasNext();) {
169: String name = (String) i.next();
170: removeJAXRConnection(name);
171: }
172:
173: }
174:
175: /**
176: * @see org.objectweb.jonas.jaxr.JAXRService#createJAXRConnection(java.util.Properties)
177: */
178: public void createJAXRConnection(Properties props)
179: throws ServiceException {
180: if (logger.isLoggable(BasicLevel.DEBUG)) {
181: PropDump
182: .print(
183: "These are the properties from which the jaxrService picks to construct jaxr Factories",
184: props, logger, BasicLevel.DEBUG);
185: }
186:
187: //Factory type/jndi name must be non null
188: String jndiName = props.getProperty(JNDI_NAME);
189:
190: if (jndiName == null) {
191: String err = "The property '" + JNDI_NAME
192: + "' is a required property.";
193: logger.log(BasicLevel.ERROR, err);
194: throw new ServiceException(err);
195: }
196:
197: // Verify that jndi name not already used
198: if (bindedFactories.containsKey(jndiName)) {
199: String err = "There is already a factory bound with the name "
200: + jndiName
201: + ", please correct the provided configuration properties";
202: logger.log(BasicLevel.ERROR, err);
203: throw new ServiceException(err);
204: }
205:
206: // Create the JAXRConnection Object
207: JAXRConnection jaxrConnection = new JAXRConnection(props);
208:
209: // Bind the factory object in the naming context
210: bindJAXRFactory(jndiName, jaxrConnection);
211:
212: logger.log(BasicLevel.INFO,
213: "Mapping JAXR Connection Factory on " + jndiName);
214:
215: }
216:
217: /**
218: * Bind JAXRConnection into Registry and inside local Map.
219: * @param jndiName jndiName of the connection
220: * @param jaxrConnection the JAXRConnection to be bound
221: */
222: private void bindJAXRFactory(String jndiName,
223: JAXRConnection jaxrConnection) {
224: try {
225: ictx.rebind(jndiName, jaxrConnection.getReference());
226: bindedFactories.put(jndiName, jaxrConnection);
227: } catch (NamingException e) {
228: String err = "Cannot bind jaxr factory '" + jndiName + "'";
229: logger.log(BasicLevel.ERROR, err);
230: throw new ServiceException(err, e);
231: }
232: }
233:
234: /**
235: * @see org.objectweb.jonas.jaxr.JAXRService#modifyJAXRConnection(java.lang.String, org.objectweb.jonas.jaxr.JAXRConnection)
236: */
237: public void modifyJAXRConnection(String name, JAXRConnection jaxrc)
238: throws ServiceException {
239: // remove old Connection
240: removeJAXRConnection(name);
241: // create new one
242: bindJAXRFactory(name, jaxrc);
243: if (logger.isLoggable(BasicLevel.DEBUG)) {
244: logger.log(BasicLevel.DEBUG, "JAXRConnection modified");
245: }
246: }
247:
248: /**
249: * @see org.objectweb.jonas.jaxr.JAXRService#removeJAXRConnection(java.lang.String)
250: */
251: public void removeJAXRConnection(String name)
252: throws ServiceException {
253:
254: // test if factory is registered
255: if (!bindedFactories.containsKey(name)) {
256: String err = "Unknown JAXRConnection '" + name + "'";
257: logger.log(BasicLevel.ERROR, err);
258: throw new ServiceException(err);
259: }
260:
261: // remove from the local list
262: bindedFactories.remove(name);
263:
264: // remove from the registry
265: try {
266: ictx.unbind(name);
267: } catch (NamingException e) {
268: String err = "Cannot unbind JAXR Connection '" + name + "'";
269: logger.log(BasicLevel.ERROR, err);
270: throw new ServiceException(err, e);
271: }
272:
273: if (logger.isLoggable(BasicLevel.DEBUG)) {
274: logger.log(BasicLevel.DEBUG, "JAXRConnection '" + name
275: + "' removed");
276: }
277: }
278:
279: }
|