KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > kernel > registry > RegistryServer


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: DistributedJNDIServer.java 6:42:03 PM ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.kernel.registry;
23
24 import java.net.InetAddress JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.TreeMap JavaDoc;
28
29 import javax.naming.NamingException JavaDoc;
30
31 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
32 import org.objectweb.fractal.api.control.LifeCycleController;
33 import org.objectweb.fractal.fraclet.annotation.FractalComponent;
34 import org.objectweb.fractal.fraclet.annotation.LifeCycle;
35 import org.objectweb.fractal.fraclet.annotation.LifeCycleType;
36 import org.objectweb.fractal.fraclet.annotation.Monolog;
37 import org.objectweb.petals.kernel.configuration.ConfigurationService;
38 import org.objectweb.petals.kernel.registry.msg.request.RegistryRequest;
39 import org.objectweb.petals.kernel.registry.msg.response.RegistryResponse;
40 import org.objectweb.petals.kernel.registry.thread.FullUpdateThread;
41 import org.objectweb.petals.kernel.registry.thread.SocketServerThread;
42 import org.objectweb.petals.kernel.registry.thread.TribeThread;
43 import org.objectweb.petals.util.LoggingUtil;
44 import org.objectweb.petals.util.SystemUtil;
45 import org.objectweb.tribe.channel.AbstractReliableFifoChannel;
46 import org.objectweb.tribe.channel.ReliableGroupChannelWithGms;
47 import org.objectweb.tribe.channel.tcp.TcpChannelPool;
48 import org.objectweb.tribe.common.GroupIdentifier;
49 import org.objectweb.tribe.common.IpAddress;
50 import org.objectweb.tribe.gms.GroupMembershipService;
51 import org.objectweb.tribe.gms.discovery.UdpDiscoveryService;
52 import org.objectweb.util.monolog.api.Logger;
53
54 /**
55  * JNDI Server
56  *
57  * At startup :
58  * <ol>
59  * <li>The JNDI server will start the socket server and the tribe listener.</li>
60  * <li>The JNDI server will search for other members in the group by sending a
61  * electMaster request</li>
62  * <li>
63  * <ul>
64  * <li>If the server is alone then it will be its own master (by keeping its
65  * master field at null)</li>
66  * <li>If there is a master, it will send back its member id and ask for a full
67  * update of its data</li>
68  * </ul>
69  * </li>
70  * </ol>
71  *
72  * At Shutdown :
73  * <ul>
74  * <li>If the server is not the master, the server does nothing</li>
75  * <li>If the server is the master :
76  * <ol>
77  * <li>The server a newMaster request to all the servers</li>
78  * <li>The others servers reset their master field</li>
79  * <li>The others servers sends a electNewMaster message with their starttime</li>
80  * <li>Each server checks if it is the master (the one with the smallest start
81  * time)</li>
82  * </ol>
83  * </li>
84  * </ul>
85  *
86  * @author ddesjardins - eBMWebsourcing
87  */

88 @FractalComponent
89 public class RegistryServer {
90
91     /**
92      * Group channel
93      *
94      * The send method is broadcast thus we have to take care when we send a
95      * message because we receive our own messages if we use the send method
96      * without parameters
97      */

98     protected ReliableGroupChannelWithGms channel;
99
100     /**
101      * Configuration service
102      */

103     protected ConfigurationService configurationService;
104
105     /**
106      * Data stored in this registry
107      */

108     protected Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Object JavaDoc>> data = Collections
109             .synchronizedMap(new TreeMap JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Object JavaDoc>>());
110
111     /**
112      * Discovery service
113      */

114     protected UdpDiscoveryService discovery;
115
116     /**
117      * Configuration initializer thread
118      */

119     protected FullUpdateThread electMasterThread;
120
121     /**
122      * Boolean to know if the server has been updated
123      */

124     protected boolean fullyUpdated;
125
126     /**
127      * Group membership service
128      */

129     protected GroupMembershipService gms;
130
131     /**
132      * Name of the Tribe group
133      */

134     protected String JavaDoc groupName = SystemUtil.getJoramDomain();
135
136     /**
137      * Boolean to know if a server is started
138      */

139     protected boolean isStarted;
140
141     /**
142      * Logger
143      */

144     protected LoggingUtil log;
145
146     /**
147      * Logger
148      */

149     @Monolog(name = "logger")
150     protected Logger logger;
151
152     /**
153      * Registry Util
154      */

155     protected RegistryUtil registryUtil;
156
157     /**
158      * Socket server thread
159      */

160     protected SocketServerThread socketServerThread;
161
162     /**
163      * Start time (to find who is the master)
164      */

165     protected long startTime = System.currentTimeMillis();
166
167     /**
168      * Tribe thread
169      */

170     protected TribeThread tribeThread;
171
172     public RegistryServer() {
173         super();
174         try {
175             InetAddress JavaDoc multicastAddress = InetAddress.getByName(SystemUtil
176                     .getTribeMulticastAddress());
177             final int multicastPort = 2288;
178             final IpAddress multicastIP = new IpAddress(multicastAddress,
179                     multicastPort);
180             final InetAddress JavaDoc repplyAddress = InetAddress.getByName(SystemUtil
181                     .getHost());
182             int repplyPort = 0;
183             final IpAddress repplyIP = new IpAddress(repplyAddress, repplyPort);
184
185             discovery = new UdpDiscoveryService(multicastIP, repplyIP);
186             gms = new GroupMembershipService(repplyIP, TcpChannelPool
187                     .getChannelPool(), discovery);
188
189             channel = new ReliableGroupChannelWithGms(gms);
190
191             data.put("/", new TreeMap JavaDoc<String JavaDoc, Object JavaDoc>());
192
193         } catch (Exception JavaDoc e) {
194             // Do nothing
195
}
196         registryUtil = new RegistryUtil(this);
197     }
198
199     public ReliableGroupChannelWithGms getChannel() {
200         return channel;
201     }
202
203     public ConfigurationService getConfigurationService() {
204         return configurationService;
205     }
206
207     public Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Object JavaDoc>> getData() {
208         return data;
209     }
210
211     public LoggingUtil getLog() {
212         return log;
213     }
214
215     public long getStartTime() {
216         return startTime;
217     }
218
219     public boolean isFullyUpdated() {
220         return fullyUpdated;
221     }
222
223     /**
224      * Process a request
225      *
226      * @param request
227      * request to process
228      * @return output Object of the operation
229      * @throws NamingException
230      */

231     public Object JavaDoc processRequest(RegistryRequest request)
232             throws NamingException JavaDoc {
233         // log.call();
234
// log.debug(startTime + " : " + request.getType());
235
Object JavaDoc output = null;
236         switch (request.getType()) {
237         /**
238          * BIND
239          */

240         case bind:
241             registryUtil.bind(request.getArg1(), request.getArg2(), request
242                     .getArg3());
243             break;
244         /**
245          * CREATE SUBCONTEXT
246          */

247         case createSubcontext:
248             output = registryUtil.createSubcontext(request.getArg1(), request
249                     .getArg2());
250             break;
251         /**
252          * DESTROY SUBCONTEXT
253          */

254         case destroySubcontext:
255             registryUtil
256                     .destroySubcontext(request.getArg1(), request.getArg2());
257             break;
258         /**
259          * FULL UPDATE
260          */

261         case fullUpdate:
262             output = data;
263             break;
264         /**
265          * LIST
266          */

267         case list:
268             output = registryUtil.list(request.getArg1(), request.getArg2());
269             break;
270         /**
271          * LIST BINDINGS
272          */

273         case listBindings:
274             output = registryUtil.listBindings(request.getArg1(), request
275                     .getArg2());
276             break;
277         /**
278          * LOOKUP
279          */

280         case lookup:
281             output = registryUtil.lookup(request.getArg1(), request.getArg2());
282             break;
283         /**
284          * LOOKUP LINK
285          */

286         case lookupLink:
287             output = registryUtil.lookupLink(request.getArg1(), request
288                     .getArg2());
289             break;
290         /**
291          * REBIND
292          */

293         case rebind:
294             registryUtil.rebind(request.getArg1(), request.getArg2(), request
295                     .getArg3());
296             break;
297         /**
298          * RENAME
299          */

300         case rename:
301             registryUtil.rename(request.getArg1(), request.getArg2(), request
302                     .getArg3());
303             break;
304         /**
305          * UNBIND
306          */

307         case unbind:
308             registryUtil.unbind(request.getArg1(), request.getArg2());
309             break;
310         default:
311             break;
312         }
313         // log.end();
314
return output;
315     }
316
317     /**
318      * Process a response
319      *
320      * @param response
321      * Reponse to process
322      * @return output of the corresponding operation
323      */

324     @SuppressWarnings JavaDoc("unchecked")
325     public Object JavaDoc processResponse(RegistryResponse response) {
326         // log.call();
327
// log.debug(startTime + " " + response.getType());
328
Object JavaDoc output = null;
329         switch (response.getType()) {
330         /**
331          * FULL UPDATE
332          */

333         case fullUpdate:
334             if (!fullyUpdated) {
335                 fullyUpdated = true;
336                 data = (Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Object JavaDoc>>) response.getArg1();
337             }
338             break;
339         /**
340          * MY STARTIME
341          */

342         case myStartTime:
343             if ((Long JavaDoc) response.getArg1() > startTime) {
344                 output = "i can be a master";
345             }
346             break;
347         /**
348          * EXCEPTION
349          */

350         case exception:
351             // log.debug(this.startTime + " EXCEPTION : " +
352
// response.getArg1());
353
break;
354         default:
355             break;
356         }
357         // log.end();
358
return output;
359     }
360
361     public void setData(Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Object JavaDoc>> data) {
362         this.data = data;
363     }
364
365     public void setFullyUpdated(boolean fullyUpdated) {
366         this.fullyUpdated = fullyUpdated;
367     }
368
369     public void setGroupName(String JavaDoc groupName) {
370         this.groupName = groupName;
371     }
372
373     /**
374      * @see LifeCycleController#startFc()
375      */

376     @LifeCycle(on = LifeCycleType.START)
377     public void start() throws IllegalLifeCycleException {
378         log = new LoggingUtil(null);
379         log.call();
380         configurationService = new ConfigurationService(log);
381         startServer();
382     }
383
384     /**
385      * Start the registry server
386      *
387      */

388     public void startServer() {
389         isStarted = true;
390         // Set up the threads
391
socketServerThread = new SocketServerThread(this);
392         tribeThread = new TribeThread(this);
393         electMasterThread = new FullUpdateThread(this);
394         try {
395             channel.join(new GroupIdentifier(groupName));
396             // Wait the group membership to connect the other peer
397
Thread.sleep(1000);
398             electMasterThread.start();
399             tribeThread.start();
400             // Wait for the electMasterThread to set up the configuration
401
electMasterThread.join();
402             socketServerThread.start();
403             // TODO find another way // wait for the Socket to start
404
Thread.sleep(1000);
405         } catch (Exception JavaDoc e) {
406             log.error("Error starting Registry server", e);
407         }
408     }
409
410     /**
411      * @see LifeCycleController#stopFc()
412      */

413     @LifeCycle(on = LifeCycleType.STOP)
414     public void stop() throws IllegalLifeCycleException {
415         log.start();
416         stopServer();
417         log.end();
418     }
419
420     /**
421      * Stop the Registry server
422      *
423      */

424     public void stopServer() {
425         isStarted = false;
426
427         tribeThread.terminate();
428         socketServerThread.terminate();
429         try {
430             AbstractReliableFifoChannel abstractReliableFifoChannel = TcpChannelPool
431                     .getChannelPool().getChannel(
432                             channel.getLocalMembership().getAddress());
433             TcpChannelPool.getChannelPool().removeChannelFromPool(
434                     abstractReliableFifoChannel);
435
436             channel.quit();
437             gms.stop();
438             discovery.kill();
439
440             discovery.join();
441             tribeThread.join();
442             socketServerThread.join();
443             gms = null;
444             discovery = null;
445             channel = null;
446         } catch (Exception JavaDoc e) {
447             // Do nothing
448
}
449     }
450
451 }
452
Popular Tags