KickJava   Java API By Example, From Geeks To Geeks.

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


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 modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or any later
9  * version.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19  * --------------------------------------------------------------------------
20  * $Id: DiscoveryManager.java,v 1.7 2005/05/25 22:45:32 vivekl Exp $
21  * --------------------------------------------------------------------------
22  */

23
24 package org.objectweb.jonas.discovery;
25
26 /**
27  * @author Takoua Abdellatif
28  */

29 import javax.management.JMException JavaDoc;
30 import javax.management.MBeanRegistration JavaDoc;
31 import javax.management.MBeanServer JavaDoc;
32 import javax.management.MBeanServerNotification JavaDoc;
33 import javax.management.Notification JavaDoc;
34 import javax.management.NotificationListener JavaDoc;
35 import javax.management.ObjectName JavaDoc;
36
37 import org.objectweb.jonas.common.Log;
38 import org.objectweb.jonas.jmx.J2eeObjectName;
39 import org.objectweb.util.monolog.api.BasicLevel;
40 import org.objectweb.util.monolog.api.Logger;
41
42 /**
43  * DiscoveryManager goal is to listen to a well known and reconfigurable
44  * IpAddress and to give appropriate JMXURL to manage remotely Jonas server.
45  *
46  * @author <a HREF="mailto:Takoua.Abdellatif@inria.fr">Takoua Abdellatif </a>
47  * @author Vivek Lakshmanan
48  * @version 1.0
49  *
50  */

51 public class DiscoveryManager implements DiscoveryManagerMBean,
52         MBeanRegistration JavaDoc, NotificationListener JavaDoc {
53
54     private static Logger logger = Log.getLogger(Log.JONAS_DISCOVERY_PREFIX);
55
56     /**
57      * <code>greetingListeningPort</code> unicast port to listen to for
58      * responses to greeting message
59      */

60     private int greetingListeningPort;
61
62     /**
63      * <code>listeningPort</code> for multicast socket creation
64      */

65     private int listeningPort;
66
67     /**
68      * <code>listeningIp</code> for multicast socket creation
69      */

70     private String JavaDoc listeningIp;
71
72     /**
73      * Runnable object that implements multicast communication with the servers
74      * in the management domain
75      */

76     private DiscoveryComm dc;
77
78     private DiscoveryGreetingResponder dgr;
79
80     private DiscoveryGreetingListener dgl;
81
82     /**
83      * Thread associated to DiscoveryManager
84      */

85     private Thread JavaDoc commDaemon;
86
87     /**
88      * Thread associated with DiscoveryGreetingListener. This will continue
89      * listening on the multicast port throughout the life of the server for
90      * greeting messages from new servers.
91      */

92     private Thread JavaDoc discoveryGreetingListenerThread;
93
94     /**
95      * Current MBean Server
96      */

97     private MBeanServer JavaDoc mbeanServer;
98
99     private ObjectName JavaDoc myOn;
100
101     private int ttl = 1;
102
103     private int greetingAckTimeOut;
104
105     private String JavaDoc jonasName = null;
106
107     private String JavaDoc domainName = null;
108
109     private String JavaDoc serverId = null;
110
111     private String JavaDoc[] urls = null;
112
113     /**
114      * @param serverId TODO
115      * @param listeningPort
116      * Port to listen to for multicast messages.
117      * @param listeningIp
118      * IP where the multicast group will listen.
119      * @param greetingListeningPort
120      * Port to listen to greeting replies on. A message on this port
121      * signifies that a server already in the group has the same
122      * server name as used by this instance.
123      * @param greetingAckTimeOut
124      * The amount of time the server will listen to
125      * greetingListeningPort for replies.
126      */

127     public DiscoveryManager(String JavaDoc serverId, int listeningPort,
128             String JavaDoc listeningIp, int greetingListeningPort, int greetingAckTimeOut) {
129         this.serverId = serverId;
130         this.listeningIp = listeningIp;
131         this.listeningPort = listeningPort;
132         this.greetingListeningPort = greetingListeningPort;
133         this.greetingAckTimeOut = greetingAckTimeOut;
134     }
135
136     /*
137      * (non-Javadoc)
138      *
139      * @see org.objectweb.jonas.server.discovery.DiscoveryManagerMBean#start()
140      */

141     public void start() {
142
143         dgl = new DiscoveryGreetingListener(this);
144         dgr = new DiscoveryGreetingResponder(this);
145
146         // Start greeting listener and greeting responder in this order.
147
if (discoveryGreetingListenerThread == null)
148             discoveryGreetingListenerThread = new Thread JavaDoc(dgl,
149                     "GreetingListener");
150
151         discoveryGreetingListenerThread.start();
152         try {
153             // We dont need this to happen inside a thread since it is a
154
// serial operation, this will only listen for greetingAckTimeOut
155
// and listen for rejection messages.
156
dgr.handleGreeting();
157         } catch (DuplicateServerNameException e) {
158             // A server with the same servername already exists.
159
logger.log(BasicLevel.ERROR,
160                     "Discovery manager failed to start due to a pre-existing"
161                             + " server in the domain with the same ID.", e);
162             throw e;
163         }
164
165         // creates an instance of the Discovery communication class
166
dc = new DiscoveryComm(this);
167         if (commDaemon == null) {
168             commDaemon = new Thread JavaDoc(dc, "commDaemon");
169         }
170         commDaemon.start();
171     }
172
173     /**
174      * @see org.objectweb.jonas.server.discovery.DiscoveryManagerMBean#getGreetingAckTimeOut()
175      */

176     public int getGreetingAckTimeOut() {
177         return greetingAckTimeOut;
178
179     }
180
181     /**
182      * (non-Javadoc)
183      *
184      * @see org.objectweb.jonas.server.discovery.DiscoveryManagerMBean#setGreetingAckTimeOut()
185      */

186     public void setGreetingAckTimeOut(int greetingAckTimeOut) {
187         this.greetingAckTimeOut = greetingAckTimeOut;
188
189     }
190
191     /**
192      * @see org.objectweb.jonas.server.discovery.DiscoveryManagerMBean#getGreetingListeningPort()
193      */

194     public int getGreetingListeningPort() {
195         return greetingListeningPort;
196
197     }
198
199     /**
200      * (non-Javadoc)
201      *
202      * @see org.objectweb.jonas.server.discovery.DiscoveryManagerMBean#setGreetingListeningPort()
203      */

204     public void setGreetingListeningPort(int greetingListeningPort) {
205         this.greetingListeningPort = greetingListeningPort;
206
207     }
208
209     /**
210      * @see org.objectweb.jonas.server.discovery.DiscoveryManagerMBean#getListeningPort()
211      */

212     public int getListeningPort() {
213         return listeningPort;
214     }
215
216     /**
217      * (non-Javadoc)
218      *
219      * @see org.objectweb.jonas.server.discovery.DiscoveryManagerMBean#setListeningPort()
220      */

221     public void setListeningPort(int listeningPort) {
222         this.listeningPort = listeningPort;
223
224     }
225
226     /**
227      * @see org.objectweb.jonas.server.discovery.DiscoveryManagerMBean#getListeningIp()
228      */

229     public String JavaDoc getListeningIp() {
230         return listeningIp;
231     }
232
233     /**
234      * @see org.objectweb.jonas.server.discovery.DiscoveryManagerMBean#setListeningIp()
235      */

236     public void setListeningIp(String JavaDoc listeningIp) {
237         this.listeningIp = listeningIp;
238
239     }
240
241     /**
242      * @see org.objectweb.jonas.server.discovery.DiscoveryManagerMBean#setTimeToLive()
243      */

244     public void setTimeToLive(int ttl) {
245         this.ttl = ttl;
246     }
247
248     /**
249      * @see org.objectweb.jonas.server.discovery.DiscoveryManagerMBean#getTimeToLive()
250      */

251     public int getTimeToLive() {
252         return this.ttl;
253     }
254
255     /**
256      * @see javax.management.MBeanRegistration#preRegister(javax.management.MBeanServer,
257      * javax.management.ObjectName)
258      */

259     public ObjectName JavaDoc preRegister(MBeanServer JavaDoc mbeanServer, ObjectName JavaDoc on)
260             throws Exception JavaDoc {
261         // set mbeanServer
262
this.mbeanServer = mbeanServer;
263         this.myOn = on;
264
265         return myOn;
266     }
267
268     /**
269      * @see javax.management.MBeanRegistration#postRegister(java.lang.Boolean)
270      */

271     public void postRegister(Boolean JavaDoc arg0) {
272         // Add discovery manager as a listener of the delegate object to receive
273
// unregistration notification of the j2EEServer.
274
ObjectName JavaDoc delegate;
275         try {
276             delegate = new ObjectName JavaDoc(
277                     "JMImplementation:type=MBeanServerDelegate");
278             mbeanServer.addNotificationListener(delegate, this, null, null);
279         } catch (JMException JavaDoc e) {
280             e.printStackTrace();
281         }
282     }
283
284     /**
285      * @see javax.management.MBeanRegistration#preDeregister()
286      */

287     public void preDeregister() throws Exception JavaDoc {
288         // stop the Discovery Manager
289
stop();
290     }
291
292     /**
293      * @see javax.management.MBeanRegistration#postDeregister()
294      */

295     public void postDeregister() {
296
297     }
298
299     /**
300      * @see javax.management.NotificationListener#handleNotification(javax.management.Notification,
301      * java.lang.Object)
302      */

303     public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback) {
304         if (notification.getType().equals(
305                 MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) {
306             ObjectName JavaDoc notificationSender = ((MBeanServerNotification JavaDoc) notification)
307                     .getMBeanName();
308             ObjectName JavaDoc j2eeServerName = J2eeObjectName.J2EEServer(domainName,
309                     jonasName);
310             if (notificationSender.equals(j2eeServerName)) {
311                 // If DiscoveryComm has been created then stop it.
312
// If the discovery greeting checks passed then dc != null.
313
if(dc != null) {
314                     dc.stop();
315                 }
316
317             }
318         }
319     }
320
321     /**
322      * @return jonasName The jonasName.
323      */

324     public String JavaDoc getJonasName() {
325         return jonasName;
326     }
327
328     /**
329      * @param jonasName
330      * The jonasName to set.
331      */

332     protected void setJonasName(String JavaDoc jonasName) {
333         this.jonasName = jonasName;
334     }
335
336     /**
337      * @return jonasName The domain name.
338      */

339     public String JavaDoc getDomainName() {
340         return domainName;
341     }
342
343     /**
344      * @param domainNamle
345      * The domainName to set.
346      */

347     protected void setDomainName(String JavaDoc domainNamle) {
348         this.domainName = domainNamle;
349     }
350
351     /**
352      * @param urls
353      * The urls.
354      */

355     protected String JavaDoc[] getUrls() {
356         return urls;
357     }
358
359     /**
360      * @param urls
361      * The urls to set.
362      */

363     protected void setUrls(String JavaDoc[] urls) {
364         this.urls = urls;
365     }
366
367     /**
368      * Stop
369      */

370     public void stop() {
371         // dc is null if a DuplicateServerNameException is thrown
372
// at start up time, so do not send a notification.
373
if (dc != null) {
374             dc.stop();
375         }
376         dgl.stop();
377     }
378
379     public String JavaDoc getServerId() {
380         return serverId;
381     }
382
383     public void setServerId(String JavaDoc serverId) {
384         this.serverId = serverId;
385     }
386 }
Popular Tags