KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > kernel > registry > thread > TribeThread


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: TribeThread.java 11:51:28 AM ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.kernel.registry.thread;
23
24 import java.util.Map JavaDoc;
25
26 import javax.naming.NamingException JavaDoc;
27
28 import org.objectweb.petals.kernel.registry.RegistryServer;
29 import org.objectweb.petals.kernel.registry.msg.request.NewMasterRequest;
30 import org.objectweb.petals.kernel.registry.msg.request.RegistryRequest;
31 import org.objectweb.petals.kernel.registry.msg.response.ExceptionResponse;
32 import org.objectweb.petals.kernel.registry.msg.response.FullUpdateResponse;
33 import org.objectweb.petals.kernel.registry.msg.response.RegistryResponse;
34 import org.objectweb.petals.util.LoggingUtil;
35
36 /**
37  * This class is the Tribe listener for the JNDI directory
38  *
39  * Each time we received a message, we need to check if the received message is
40  * not a message that this server sent
41  *
42  * Else we need to check if the recipient is :
43  * <ul>
44  * <li>0 for everybody except the sender of the message</li>
45  * <li>a null zero value for only one recipient</li>
46  * </ul>
47  *
48  * @author ddesjardins - eBMWebsourcing
49  */

50 public class TribeThread extends Thread JavaDoc {
51
52     /**
53      * Last request date
54      */

55     protected static long lastRequest;
56
57     /**
58      * Last response date
59      */

60     protected static long lastResponse;
61
62     /**
63      * Logger
64      */

65     protected LoggingUtil log;
66
67     /**
68      * Boolean used to control the lifecycle of the Tribe listener
69      */

70     protected boolean run = true;
71
72     /**
73      * Id of the sender
74      */

75     protected long sender;
76
77     /**
78      * JNDI server
79      */

80     protected RegistryServer server;
81
82     public TribeThread(RegistryServer server) {
83         super("TribeThread" + server.getStartTime());
84         this.server = server;
85         this.log = server.getLog();
86         this.sender = server.getStartTime();
87     }
88
89     /**
90      * Run the Tribe listener
91      */

92     public void run() {
93         log.call();
94
95         while (run) {
96             Object JavaDoc msg = null;
97             RegistryRequest request = null;
98             RegistryResponse response = null;
99             try {
100                 msg = server.getChannel().receive();
101                 //log.debug(server.getStartTime() + " " + msg);
102
if (msg instanceof RegistryRequest) {
103                     request = (RegistryRequest) msg;
104                     if (request.getSender() != sender
105                         && (request.getRecipient() == sender || request
106                             .getRecipient() == 0)
107                         && lastRequest != request.getRequestDate()) {
108                         /**
109                          * WE PROCESS THE REQUEST
110                          */

111                         lastRequest = request.getRequestDate();
112                         processRequest(request);
113                     }
114                 } else if (msg instanceof RegistryResponse) {
115                     response = (RegistryResponse) msg;
116                     if (response.getSender() != sender
117                         && (response.getRecipient() == sender || response
118                             .getRecipient() == 0)
119                         && lastResponse != response.getResponseDate()) {
120                         /**
121                          * WE PROCESS THE RESPONSE
122                          */

123                         lastResponse = response.getResponseDate();
124                         processResponse(response);
125                     }
126                 } else {
127                     if (msg != null) {
128                         log.warning("Message is not a registry message "
129                             + msg.getClass());
130                     } else {
131                         log.warning("Message is null");
132                     }
133                     continue;
134                 }
135             } catch (Exception JavaDoc e) {
136                 log.error(e.getMessage(), e);
137                 continue;
138             }
139         }
140     }
141
142     /**
143      * Terminate the Tribe listener
144      *
145      */

146     public void terminate() {
147         log.start();
148         run = false;
149         log.end();
150     }
151
152     /**
153      * Process a request
154      *
155      * @param request
156      * request to process
157      */

158     protected void processRequest(RegistryRequest request) {
159         RegistryResponse response = null;
160         try {
161             Object JavaDoc object = server.processRequest(request);
162             switch (request.getType()) {
163                 /**
164                  * BIND
165                  */

166                 case bind:
167                     break;
168                 /**
169                  * CREATE SUBCONTEXT
170                  */

171                 case createSubcontext:
172                     break;
173                 /**
174                  * DESTROY SUBCONTEXT
175                  */

176                 case destroySubcontext:
177                     break;
178                 /**
179                  * FULL UPDATE
180                  */

181                 case fullUpdate:
182                     response = new FullUpdateResponse((Map JavaDoc<?, ?>) object,
183                         sender, request.getSender());
184                     break;
185                 /**
186                  * REBIND
187                  */

188                 case rebind:
189                     break;
190                 /**
191                  * RENAME
192                  */

193                 case rename:
194                     break;
195                 /**
196                  * UNBIND
197                  */

198                 case unbind:
199                     break;
200                 default:
201                     break;
202             }
203         } catch (NamingException JavaDoc e) {
204             response = new ExceptionResponse(e, sender, request.getSender());
205         }
206         if (response != null) {
207             // We send back the response
208
try {
209                 server.getChannel().send(response);
210             } catch (Exception JavaDoc e) {
211                 log.error("Problem while sending response", e);
212             }
213         }
214     }
215
216     /**
217      * Process a response
218      *
219      * @param response
220      * response to process
221      */

222     protected void processResponse(RegistryResponse response) {
223         RegistryRequest request = null;
224         Object JavaDoc outObject = server.processResponse(response);
225         switch (response.getType()) {
226             /**
227              * MY START TIME
228              */

229             case myStartTime:
230                 if (outObject != null) {
231                     // This server can be a master
232
request = new NewMasterRequest(sender, response.getSender());
233                 }
234                 break;
235             default:
236                 break;
237         }
238         if (request != null) {
239             // We send back the request
240
try {
241                 server.getChannel().send(request);
242             } catch (Exception JavaDoc e) {
243                 log.error("Problem while sending request", e);
244             }
245         }
246     }
247
248 }
249
Popular Tags