KickJava   Java API By Example, From Geeks To Geeks.

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


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: SocketServerThread.java 11:55:50 AM ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.kernel.registry.thread;
23
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.net.InetSocketAddress JavaDoc;
28 import java.net.ServerSocket JavaDoc;
29 import java.net.Socket JavaDoc;
30 import java.net.SocketException JavaDoc;
31
32 import javax.naming.Binding JavaDoc;
33 import javax.naming.NameClassPair JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35
36 import org.objectweb.petals.kernel.registry.RegistryServer;
37 import org.objectweb.petals.kernel.registry.msg.request.RegistryRequest;
38 import org.objectweb.petals.kernel.registry.msg.response.AckResponse;
39 import org.objectweb.petals.kernel.registry.msg.response.CreateSubcontextResponse;
40 import org.objectweb.petals.kernel.registry.msg.response.ExceptionResponse;
41 import org.objectweb.petals.kernel.registry.msg.response.ListBindingsResponse;
42 import org.objectweb.petals.kernel.registry.msg.response.ListResponse;
43 import org.objectweb.petals.kernel.registry.msg.response.LookupLinkResponse;
44 import org.objectweb.petals.kernel.registry.msg.response.LookupResponse;
45 import org.objectweb.petals.kernel.registry.msg.response.RegistryResponse;
46 import org.objectweb.petals.util.LoggingUtil;
47 import org.objectweb.petals.util.SystemUtil;
48
49 /**
50  * This class is the socket server for the distributed JNDI directory
51  *
52  * @author ddesjardins - eBMWebsourcing
53  */

54 public class SocketServerThread extends Thread JavaDoc {
55
56     /**
57      * Logger
58      */

59     protected LoggingUtil log;
60
61     /**
62      * Boolean used to control the lifecycle of the socket server
63      */

64     protected boolean run = true;
65
66     /**
67      * Start time of this server
68      */

69     protected long sender;
70
71     /**
72      * JNDI server
73      */

74     protected RegistryServer server;
75
76     /**
77      * Socket server
78      */

79     protected ServerSocket JavaDoc serverSocket;
80
81     public SocketServerThread(RegistryServer server) {
82         super("SocketServerThread" + server.getStartTime());
83         this.server = server;
84         this.log = server.getLog();
85         this.sender = server.getStartTime();
86     }
87
88     @SuppressWarnings JavaDoc("unchecked")
89     public void run() {
90         log.call();
91         try {
92             serverSocket = new ServerSocket JavaDoc();
93             serverSocket.bind(new InetSocketAddress JavaDoc(SystemUtil.getHost(),
94                 Integer.parseInt(SystemUtil.getJndiPort())));
95
96             log.info("Start JNDI server on " + SystemUtil.getHost() + ":"
97                 + SystemUtil.getJndiPort());
98
99             RegistryRequest request = null;
100             RegistryResponse response = null;
101             ObjectInputStream JavaDoc objectInputStream = null;
102             ObjectOutputStream JavaDoc objectOutputStream = null;
103
104             Socket JavaDoc socket = null;
105             while (run) {
106                 socket = serverSocket.accept();
107                 // log.debug(server.getStartTime() + " Accept a socket");
108

109                 try {
110                     objectInputStream = new ObjectInputStream JavaDoc(socket
111                         .getInputStream());
112                     request = (RegistryRequest) objectInputStream.readObject();
113                 } catch (Exception JavaDoc e) {
114                     log.error("Unable to read JNDIRequest", e);
115                 }
116
117                 if (request != null) {
118                     try {
119                         // log.debug(server.getStartTime() + " Request type : "
120
// + request.getType());
121
Object JavaDoc object = server.processRequest(request);
122                         switch (request.getType()) {
123                             /**
124                              * BIND
125                              */

126                             case bind:
127                                 response = new AckResponse(sender, 0);
128                                 request.setSender(sender);
129                                 break;
130                             /**
131                              * CREATE SUBCONTEXT
132                              */

133                             case createSubcontext:
134                                 response = new CreateSubcontextResponse(
135                                     (String JavaDoc) object, sender, 0);
136                                 request.setSender(sender);
137                                 break;
138                             /**
139                              * DESTROY SUBCONTEXT
140                              */

141                             case destroySubcontext:
142                                 response = new AckResponse(sender, 0);
143                                 request.setSender(sender);
144                                 break;
145                             /**
146                              * LIST
147                              */

148                             case list:
149                                 response = new ListResponse(
150                                     (NameClassPair JavaDoc[]) object, sender, 0);
151                                 break;
152                             /**
153                              * LIST BINDINGS
154                              */

155                             case listBindings:
156                                 response = new ListBindingsResponse(
157                                     (Binding JavaDoc[]) object, sender, 0);
158                                 break;
159                             /**
160                              * LOOKUP
161                              */

162                             case lookup:
163                                 response = new LookupResponse(object, sender,
164                                     request.getSender());
165                                 break;
166                             /**
167                              * LOOKUP LINK
168                              */

169                             case lookupLink:
170                                 response = new LookupLinkResponse(object,
171                                     sender, request.getSender());
172                                 break;
173                             /**
174                              * PING
175                              */

176                             case ping:
177                                 response = new AckResponse(sender, request
178                                     .getSender());
179                                 break;
180                             /**
181                              * REBIND
182                              */

183                             case rebind:
184                                 response = new AckResponse(sender, 0);
185                                 request.setSender(sender);
186                                 break;
187                             /**
188                              * RENAME
189                              */

190                             case rename:
191                                 response = new AckResponse(sender, 0);
192                                 request.setSender(sender);
193                                 break;
194                             /**
195                              * UNBIND
196                              */

197                             case unbind:
198                                 response = new AckResponse(sender, 0);
199                                 request.setSender(sender);
200                                 break;
201                             default:
202                                 break;
203                         }
204                         if (request.getSender() != 0) {
205                             // Send the request to the other registries if the
206
// sender is not null
207
try {
208                                 server.getChannel().send(request);
209                             } catch (Exception JavaDoc e) {
210                                 log
211                                     .error(
212                                         "Error while send the request to the other registries",
213                                         e);
214                             }
215                         }
216                     } catch (NamingException JavaDoc e) {
217                         log.error(e.getMessage(), e);
218                         response = new ExceptionResponse(e, sender, request
219                             .getSender());
220                     }
221                 }
222                 // log.debug("Send response " + response.getType());
223
objectOutputStream = new ObjectOutputStream JavaDoc(socket
224                     .getOutputStream());
225                 objectOutputStream.writeObject(response);
226                 objectOutputStream.flush();
227                 objectOutputStream.close();
228             }
229         } catch (SocketException JavaDoc e) {
230             if (!"Socket closed".equals(e.getMessage())) {
231                 log.error(e.getMessage(), e);
232             }
233         } catch (IOException JavaDoc e) {
234             log.error("Error while starting socket server", e);
235             throw new RuntimeException JavaDoc("Error while starting socket server", e);
236         }
237     }
238
239     public void terminate() {
240         log.start();
241         run = false;
242         // Close the server socket
243
try {
244             serverSocket.close();
245             this.interrupt();
246         } catch (Exception JavaDoc e) {
247             log.error("Problem while closing server socket " + e);
248         }
249         log.end();
250     }
251
252 }
253
Popular Tags