KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > discovery > DiscoveryServiceImpl


1 /**
2 * JOnAS: Java(TM) Open Application Server
3 * Copyright (C) 2004 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 * --------------------------------------------------------------------------
22 * $Id: DiscoveryServiceImpl.java,v 1.10 2005/05/22 03:38:51 vivekl Exp $
23 * --------------------------------------------------------------------------
24 */

25
26 package org.objectweb.jonas.discovery;
27
28 import java.util.ArrayList JavaDoc;
29
30 import javax.management.InstanceNotFoundException JavaDoc;
31 import javax.management.JMException JavaDoc;
32 import javax.management.MBeanRegistrationException JavaDoc;
33 import javax.management.MBeanServer JavaDoc;
34 import javax.management.MalformedObjectNameException JavaDoc;
35 import javax.management.remote.JMXServiceURL JavaDoc;
36 import javax.naming.Context JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38
39 import org.objectweb.jonas.common.Log;
40 import org.objectweb.jonas.jmx.JmxService;
41 import org.objectweb.jonas.jmx.JonasObjectName;
42 import org.objectweb.jonas.service.AbsServiceImpl;
43 import org.objectweb.jonas.service.ServiceException;
44 import org.objectweb.jonas.service.ServiceManager;
45 import org.objectweb.util.monolog.api.BasicLevel;
46 import org.objectweb.util.monolog.api.Logger;
47
48 /**
49  *
50  * @author Adriana Danes
51  *
52  * The discovery service creates and starts at least a DiscoveryManager which is a MBean
53  * that multicasts <code>discovery information</code> to all the servers who joined the
54  * <code>discovery multicast group</code>.
55  * <p>
56  * The discovery service may also create a Enroller and a DiscoveryClient, and in this case
57  * the current server becomes a <code>discovery server</code>.
58  * <p>
59  * <code>Discovery information</code> contains information allowing to remotely manage a server in the group.
60  * <p>
61  * <code>Discovery multicast group</code> is a group of servers which can be managed remotely by a discovery server.
62  * <p>
63  * <code>Discovery server</code> is the server in the group who detains the discovery information concerning all the
64  * servers in the group.
65  */

66 public class DiscoveryServiceImpl extends AbsServiceImpl implements DiscoveryService , DiscoveryServiceImplMBean {
67
68     /**
69      * The default port where responses to discovery events should be sent.
70      */

71     private static final String JavaDoc DISCOVERY_SOURCE_PORT_DEFAULT = "9888";
72     /**
73      * The default port where responses to greeting messages should be sent.
74      */

75     private static final String JavaDoc DISCOVERY_GREETING_PORT_DEFAULT = "9899";
76     /**
77      * The default timeout duration that server should wait to receive
78      * responses to it's greeting message before continuing.
79      */

80     private static final String JavaDoc DISCOVERY_GREETING_TIMEOUT_DEFAULT = "1000";
81     /**
82      * String form of the IP Address where multicast messages are sent.
83      */

84     private String JavaDoc listeningIp = null;
85     /**
86      * The port to listen to for multicast messages.
87      */

88     private int listeningPort;
89     /**
90      * The port to listen to for responses to greeting message.
91      */

92     private int greetingListeningPort;
93     /**
94      * The time period in miliseconds to listen for greeting responses.
95      */

96     private int greetingAckTimeOut;
97     /**
98      * The port where discovery events are sent.
99      */

100     private int sourcePort;
101     /**
102      * set to true if enroller and discoveryClient started
103      */

104     private boolean isDiscoveryMaster = false;
105     /**
106      * Reference to a MBean server.
107      */

108     private MBeanServer JavaDoc mbeanServer = null;
109
110     /**
111      * jmx service reference
112      */

113     private JmxService jmxService = null;
114     /**
115      * Logger for this service.
116      */

117     private static Logger logger = null;
118
119     /**
120      * @return the multicast group IP address used by the discovery service
121      */

122     public String JavaDoc getMulticastAddress() {
123         return listeningIp;
124     }
125
126     /**
127      * @return the multicast group port number used by the discovery service
128      */

129     public String JavaDoc getMulticastPort() {
130         return String.valueOf(listeningPort);
131     }
132
133     /**
134      * @return true if the current server is a discovery server
135      */

136     public Boolean JavaDoc getIsDiscoveryMaster() {
137         return new Boolean JavaDoc(isDiscoveryMaster);
138     }
139     /**
140      * Management operation allowing to make the current server become a
141      * master if its not already.
142      * @throws JMException a JMX exception occured when trying to make current server a discovery master
143      */

144     public void startDiscoveryMaster() throws JMException JavaDoc {
145         if (!isDiscoveryMaster) {
146             createEnroller();
147             createDiscClient();
148             isDiscoveryMaster = true;
149         }
150     }
151
152     /* (non-Javadoc)
153      * @see org.objectweb.jonas.service.AbsServiceImpl#doInit(javax.naming.Context)
154      */

155     protected void doInit(Context JavaDoc ctx) throws ServiceException {
156         //Init the logger
157
logger = Log.getLogger(Log.JONAS_DISCOVERY_PREFIX);
158
159         // Get service configuration
160
try {
161             listeningIp = (String JavaDoc) ctx.lookup("jonas.service.discovery.multicast.address");
162             String JavaDoc sListeningPort = (String JavaDoc) ctx.lookup("jonas.service.discovery.multicast.port");
163             listeningPort = (Integer.valueOf(sListeningPort)).intValue();
164
165         } catch (NamingException JavaDoc ne) {
166             String JavaDoc err = "Cannot read initializations arguments in service context";
167             logger.log(BasicLevel.ERROR, err);
168             throw new ServiceException(err, ne);
169         }
170         // Get info allowing to see if this is a discovery master
171
try {
172             String JavaDoc sMaster = (String JavaDoc) ctx.lookup("jonas.service.discovery.master");
173             if (sMaster != null && sMaster.equals("true")) {
174                 isDiscoveryMaster = true;
175             }
176         } catch (NamingException JavaDoc ne) {
177             // isDiscoveryMaster rests false
178
}
179         // If configuration does not says this ia a master, allow
180
// however to start it as a master if the server name is
181
// identical to the domain name (in the default case this
182
// is "jonas", "jonas")
183
if (!isDiscoveryMaster) {
184             if (getDomainName().equals(getJonasServerName())) {
185                 isDiscoveryMaster = true;
186             }
187         }
188         // Temporary strings to store string forms of ports and timeout.
189
String JavaDoc sSourcePort = null;
190         String JavaDoc greetListeningPort = null;
191         String JavaDoc greetAckTimeOut = null;
192
193         // Check if the source port number has been defined else use default
194
try {
195             sSourcePort = (String JavaDoc) ctx.lookup("jonas.service.discovery.source.port");
196         } catch (NamingException JavaDoc ne) {
197             sSourcePort = DISCOVERY_SOURCE_PORT_DEFAULT;
198         }
199         sourcePort = (Integer.valueOf(sSourcePort)).intValue();
200
201         // Check if the greeting port has been defined else use default
202
try {
203             greetListeningPort = (String JavaDoc) ctx.lookup("jonas.service.discovery.greeting.port");
204         } catch (NamingException JavaDoc e1) {
205             greetListeningPort = DISCOVERY_GREETING_PORT_DEFAULT;
206         }
207         greetingListeningPort = (Integer.valueOf(greetListeningPort)).intValue();
208
209         // Check if the greeting timeout has been defined else use default
210
try {
211             greetAckTimeOut = (String JavaDoc) ctx.lookup("jonas.service.discovery.greeting.timeout");
212         } catch (NamingException JavaDoc e1) {
213             greetAckTimeOut = DISCOVERY_GREETING_TIMEOUT_DEFAULT;
214         }
215         greetingAckTimeOut = (Integer.valueOf(greetAckTimeOut)).intValue();
216
217         // Get JOnAS references allowing to start execution
218
ServiceManager sm = null;
219         try {
220             sm = ServiceManager.getInstance();
221         } catch (Exception JavaDoc e) {
222             String JavaDoc err = "Cannot get ServiceManager instance";
223             logger.log(BasicLevel.ERROR, err);
224             throw new ServiceException(err, e);
225         }
226         jmxService = ((JmxService) sm.getJmxService());
227         try {
228             mbeanServer = jmxService.getJmxServer();
229         } catch (ServiceException e) {
230             // the JMX service may not be started
231
mbeanServer = null;
232             String JavaDoc err = "Cannot get MBeanServer reference";
233             logger.log(BasicLevel.ERROR, err);
234             throw new ServiceException(err, e);
235         }
236     }
237
238     /**
239      * Start the discovery service
240      *
241      * @throws ServiceException
242      * An error occured when starting the service
243      */

244     protected void doStart() throws ServiceException {
245         // Create discovery manager
246
DiscoveryManager dm = new DiscoveryManager(this.getJonasServerName(), listeningPort,
247                 listeningIp, greetingListeningPort, greetingAckTimeOut);
248         dm.setDomainName(jmxService.getDomainName());
249         dm.setJonasName(jmxService.getJonasServerName());
250         JMXServiceURL JavaDoc[] connectorServerURLs = jmxService
251                 .getConnectorServerURLs();
252         ArrayList JavaDoc urlsList = new ArrayList JavaDoc();
253         for (int i = 0; i < connectorServerURLs.length; i++) {
254             // The connectorServerURLs may contain null
255
// if the list of protocols in Carol contain
256
// other protocols than the standard ones (JRMP, JEREMIE, IIOP, CMI)
257
if (connectorServerURLs[i] != null) {
258                 urlsList.add(connectorServerURLs[i].toString());
259             }
260         }
261         String JavaDoc[] urls = new String JavaDoc[urlsList.size()];
262         for (int i = 0; i < urls.length; i++) {
263             urls[i] = (String JavaDoc) urlsList.get(i);
264         }
265         dm.setUrls(urls);
266         try {
267             // Register DiscoveryManager MBean
268
if (mbeanServer != null) {
269                 mbeanServer.registerMBean(dm, JonasObjectName
270                         .discoveryManager());
271             }
272         } catch (JMException JavaDoc e) {
273             throw new ServiceException(
274                     "Problem when starting the Discovery Service: ", e);
275         }
276         // Start discovery manager
277
try {
278             dm.start();
279         } catch (DuplicateServerNameException e) {
280             // Server with the same name already exists in domain.
281
logger.log(BasicLevel.ERROR,
282                     "Discovery manager failed to start due to a pre-existing"
283                             + " server in the domain with the same name.", e);
284             try {
285                 // Discovery manager failed to start so remove the mbean.
286
mbeanServer.unregisterMBean(JonasObjectName.discoveryManager());
287             } catch (JMException JavaDoc e1) {
288                 logger.log(BasicLevel.ERROR, "Problem trying to unregister "
289                         + "DiscoveryManager: ", e);
290             }
291             // Discovery service had a problem.
292
throw new ServiceException(
293                     "Problem when starting the Discovery Service:", e);
294         }
295
296         if (isDiscoveryMaster) {
297             // Create enroller
298
try {
299                 createEnroller();
300             } catch (JMException JavaDoc e) {
301                 throw new ServiceException(
302                         "Problem when starting the Discovery Service: ", e);
303             }
304
305             // Create discovery client
306
try {
307                 createDiscClient();
308             } catch (JMException JavaDoc e) {
309                 throw new ServiceException(
310                         "Problem when starting the Discovery Service: ", e);
311             }
312         }
313
314         // Create and register the service MBean
315
try {
316             if (mbeanServer != null) {
317                 mbeanServer.registerMBean(this, JonasObjectName
318                         .discoveryService());
319             }
320         } catch (JMException JavaDoc e) {
321             throw new ServiceException(
322                     "Problem when starting the Discovery Service: ", e);
323         }
324     }
325
326     private void createEnroller() throws JMException JavaDoc {
327         Enroller enroller = new Enroller(listeningPort, listeningIp);
328         if (mbeanServer != null) {
329             mbeanServer.registerMBean(enroller, JonasObjectName.discoveryEnroller());
330         }
331     }
332
333     private void createDiscClient() throws JMException JavaDoc {
334         DiscoveryClient dc = new DiscoveryClient(listeningPort, listeningIp, sourcePort);
335         // Register MBean
336
if (mbeanServer != null) {
337             mbeanServer.registerMBean(dc, JonasObjectName.discoveryClient());
338         }
339     }
340
341     /* (non-Javadoc)
342      * @see org.objectweb.jonas.service.AbsServiceImpl#doStop()
343      */

344     protected void doStop() throws ServiceException {
345         // TO DO
346
// ....
347

348         // Unegister the service MBean
349
try {
350             if (mbeanServer != null) {
351                 mbeanServer.unregisterMBean(JonasObjectName.discoveryService());
352             }
353         } catch (JMException JavaDoc e) {
354             throw new ServiceException("Problem when stoping the Discovery Service: ", e);
355         }
356     }
357 }
Popular Tags