1 22 package org.objectweb.petals.kernel.registry.thread; 23 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.net.InetSocketAddress ; 28 import java.net.ServerSocket ; 29 import java.net.Socket ; 30 import java.net.SocketException ; 31 32 import javax.naming.Binding ; 33 import javax.naming.NameClassPair ; 34 import javax.naming.NamingException ; 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 54 public class SocketServerThread extends Thread { 55 56 59 protected LoggingUtil log; 60 61 64 protected boolean run = true; 65 66 69 protected long sender; 70 71 74 protected RegistryServer server; 75 76 79 protected ServerSocket 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 ("unchecked") 89 public void run() { 90 log.call(); 91 try { 92 serverSocket = new ServerSocket (); 93 serverSocket.bind(new InetSocketAddress (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 objectInputStream = null; 102 ObjectOutputStream objectOutputStream = null; 103 104 Socket socket = null; 105 while (run) { 106 socket = serverSocket.accept(); 107 109 try { 110 objectInputStream = new ObjectInputStream (socket 111 .getInputStream()); 112 request = (RegistryRequest) objectInputStream.readObject(); 113 } catch (Exception e) { 114 log.error("Unable to read JNDIRequest", e); 115 } 116 117 if (request != null) { 118 try { 119 Object object = server.processRequest(request); 122 switch (request.getType()) { 123 126 case bind: 127 response = new AckResponse(sender, 0); 128 request.setSender(sender); 129 break; 130 133 case createSubcontext: 134 response = new CreateSubcontextResponse( 135 (String ) object, sender, 0); 136 request.setSender(sender); 137 break; 138 141 case destroySubcontext: 142 response = new AckResponse(sender, 0); 143 request.setSender(sender); 144 break; 145 148 case list: 149 response = new ListResponse( 150 (NameClassPair []) object, sender, 0); 151 break; 152 155 case listBindings: 156 response = new ListBindingsResponse( 157 (Binding []) object, sender, 0); 158 break; 159 162 case lookup: 163 response = new LookupResponse(object, sender, 164 request.getSender()); 165 break; 166 169 case lookupLink: 170 response = new LookupLinkResponse(object, 171 sender, request.getSender()); 172 break; 173 176 case ping: 177 response = new AckResponse(sender, request 178 .getSender()); 179 break; 180 183 case rebind: 184 response = new AckResponse(sender, 0); 185 request.setSender(sender); 186 break; 187 190 case rename: 191 response = new AckResponse(sender, 0); 192 request.setSender(sender); 193 break; 194 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 try { 208 server.getChannel().send(request); 209 } catch (Exception e) { 210 log 211 .error( 212 "Error while send the request to the other registries", 213 e); 214 } 215 } 216 } catch (NamingException e) { 217 log.error(e.getMessage(), e); 218 response = new ExceptionResponse(e, sender, request 219 .getSender()); 220 } 221 } 222 objectOutputStream = new ObjectOutputStream (socket 224 .getOutputStream()); 225 objectOutputStream.writeObject(response); 226 objectOutputStream.flush(); 227 objectOutputStream.close(); 228 } 229 } catch (SocketException e) { 230 if (!"Socket closed".equals(e.getMessage())) { 231 log.error(e.getMessage(), e); 232 } 233 } catch (IOException e) { 234 log.error("Error while starting socket server", e); 235 throw new RuntimeException ("Error while starting socket server", e); 236 } 237 } 238 239 public void terminate() { 240 log.start(); 241 run = false; 242 try { 244 serverSocket.close(); 245 this.interrupt(); 246 } catch (Exception e) { 247 log.error("Problem while closing server socket " + e); 248 } 249 log.end(); 250 } 251 252 } 253 | Popular Tags |