KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > server > ConnectorService


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: ConnectorService.java,v 1.1 2005/06/07 14:36:03 tanderson Exp $
44  */

45
46 package org.exolab.jms.server;
47
48 import java.lang.reflect.Constructor JavaDoc;
49 import javax.naming.NamingException JavaDoc;
50 import javax.naming.Context JavaDoc;
51
52 import org.apache.commons.logging.Log;
53 import org.apache.commons.logging.LogFactory;
54
55 import org.exolab.jms.config.Configuration;
56 import org.exolab.jms.config.Connector;
57 import org.exolab.jms.config.ConnectorResource;
58 import org.exolab.jms.config.ConnectorHelper;
59 import org.exolab.jms.config.types.SchemeType;
60 import org.exolab.jms.service.Service;
61 import org.exolab.jms.service.ServiceException;
62
63
64 /**
65  * Service that manages the connectors configured for the server.
66  *
67  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
68  * @version $Revision: 1.1 $ $Date: 2005/06/07 14:36:03 $
69  */

70 class ConnectorService extends Service {
71
72     /**
73      * The configuration.
74      */

75     private final Configuration _config;
76
77     /**
78      * The interfaces to this server. One interface is constructed for
79      * each configured connector.
80      */

81     private ServerConnector[] _interfaces = null;
82
83     /**
84      * The logger.
85      */

86     private static final Log _log = LogFactory.getLog(ConnectorService.class);
87
88
89     /**
90      * Construct a new <code>ConnectorService</code>.
91      *
92      * @param config the configuration to use.
93      */

94     public ConnectorService(Configuration config) {
95         super("ConnectorService");
96         _config = config;
97     }
98
99     /**
100      * Start the service.
101      *
102      * @throws ServiceException if the service fails to start, or is already
103      * running
104      */

105     public synchronized void start() throws ServiceException {
106         super.start();
107         try {
108             Context JavaDoc context = NamingHelper.getInitialContext(_config);
109             initConnectors(context);
110         } catch (NamingException JavaDoc exception) {
111             throw new ServiceException(exception.getMessage(), exception);
112         }
113     }
114
115     /**
116      * Stop the service.
117      *
118      * @throws ServiceException if the service fails to stop, or is already
119      * stopped
120      */

121     public synchronized void stop() throws ServiceException {
122         super.stop();
123         for (int i = 0; i < _interfaces.length; ++i) {
124             _interfaces[i].close();
125         }
126     }
127
128     /**
129      * Creates an interface to the server for each configured connector.
130      *
131      * @param context the initial context
132      * @throws NamingException if administered objects cannot be bound in JNDI
133      * @throws ServiceException if an interface can't be created
134      */

135     protected void initConnectors(Context JavaDoc context)
136         throws NamingException JavaDoc, ServiceException {
137
138         Connector[] connectors = _config.getConnectors().getConnector();
139         _interfaces = new ServerConnector[connectors.length];
140
141         for (int i = 0; i < connectors.length; ++i) {
142             Connector connector = connectors[i];
143             _interfaces[i] = initConnector(connector, context);
144         }
145     }
146
147     /**
148      * Create an interface to the server for the specified connector.
149      *
150      * @param connector the connector
151      * @param context the initial context
152      * @return the interface corresponding to <code>connector</code>
153      * @throws NamingException if administered objects cannot be bound in JNDI
154      * @throws ServiceException if the interface can't be created
155      */

156     protected ServerConnector initConnector(Connector connector, Context JavaDoc context)
157         throws NamingException JavaDoc, ServiceException {
158
159         _log.info("Creating server interface for the " + connector.getScheme()
160             + " connector");
161
162         ServerConnector server;
163         ConnectorResource resource = ConnectorHelper.getConnectorResource(
164             connector.getScheme());
165
166         String JavaDoc className = resource.getServer().getImplementationClass();
167         Class JavaDoc clazz;
168         try {
169             clazz = Class.forName(className);
170         } catch (ClassNotFoundException JavaDoc exception) {
171             throw new ServiceException(
172                 "Failed to load class " + className);
173         }
174
175         if (!ServerConnector.class.isAssignableFrom(clazz)) {
176             throw new ServiceException(
177                 "Class " + className + " does not implement ServerConnector");
178         }
179         try {
180             SchemeType scheme = connector.getScheme();
181             Constructor JavaDoc ctor = clazz.getConstructor(
182                 new Class JavaDoc[]{SchemeType.class, Configuration.class});
183             server = (ServerConnector) ctor.newInstance(
184                 new Object JavaDoc[]{scheme, _config});
185         } catch (NoSuchMethodError JavaDoc ignore) {
186             // fall back to the default constructor
187
try {
188                 server = (ServerConnector) clazz.newInstance();
189             } catch (Exception JavaDoc exception) {
190                 throw new ServiceException(
191                     exception.getMessage(), exception);
192             }
193         } catch (Exception JavaDoc exception) {
194             throw new ServiceException(
195                 exception.getMessage(), exception);
196         }
197
198         _log.debug("Created an instance of " + className
199                    + " as a server interface");
200
201         // initialise the interface
202
server.init();
203
204         // bind any configured connection factories
205
server.bindConnectionFactories(context);
206
207         return server;
208     }
209
210
211
212 }
213
Popular Tags