1 24 25 package org.objectweb.cjdbc.controller.core; 26 27 import java.io.IOException ; 28 import java.net.InetAddress ; 29 import java.net.ServerSocket ; 30 import java.net.Socket ; 31 import java.util.ArrayList ; 32 33 import javax.net.ServerSocketFactory; 34 35 import org.objectweb.cjdbc.common.i18n.Translate; 36 import org.objectweb.cjdbc.common.log.Trace; 37 import org.objectweb.cjdbc.common.net.SSLException; 38 import org.objectweb.cjdbc.common.net.SocketFactoryFactory; 39 import org.objectweb.cjdbc.common.stream.CJDBCOutputStream; 40 41 51 public class ControllerServerThread extends Thread 52 { 53 private ServerSocket serverSocket; 54 private boolean isShuttingDown = false; 55 protected Controller controller; 56 57 protected ArrayList controllerServerThreadPendingQueue = new ArrayList (); 58 62 protected int idleWorkerThreads = 0; 63 64 65 static Trace logger = Trace 66 .getLogger("org.objectweb.cjdbc.controller.core.Controller"); 67 68 73 public ControllerServerThread(Controller controller) 74 { 75 super("ControllerServerThread"); 76 this.controller = controller; 77 78 try 79 { 80 InetAddress bindAddress = InetAddress 81 .getByName(controller.getIPAddress()); 82 83 88 if (!controller.getIPAddress().equals( 89 InetAddress.getLocalHost().getHostAddress())) 90 { 94 if (controller.isSecurityEnabled() 99 && controller.getSecurity().isSSLEnabled()) 100 { 101 ServerSocketFactory sslFact = SocketFactoryFactory 102 .createServerFactory(controller.getSecurity().getSslConfig()); 103 serverSocket = sslFact.createServerSocket(controller.getPortNumber(), 104 controller.getBacklogSize(), bindAddress); 105 } 106 else 107 { 108 serverSocket = new ServerSocket (controller.getPortNumber(), 109 controller.getBacklogSize(), bindAddress); 110 } 111 } 112 else 113 { 116 if (controller.isSecurityEnabled() 117 && controller.getSecurity().isSSLEnabled()) 118 { 119 ServerSocketFactory sslFact = SocketFactoryFactory 120 .createServerFactory(controller.getSecurity().getSslConfig()); 121 serverSocket = sslFact.createServerSocket(controller.getPortNumber(), 122 controller.getBacklogSize()); 123 } 124 else 125 { 126 serverSocket = new ServerSocket (controller.getPortNumber(), 127 controller.getBacklogSize()); 128 } 129 } 130 131 } 132 catch (java.net.BindException e) 133 { logger.fatal(Translate.get("controller.server.thread.illegal.ip", 135 new String []{controller.getIPAddress(), e.getMessage()})); 136 controller.endOfController(e); 137 } 138 catch (IOException e) 139 { 140 logger.fatal(Translate.get("controller.server.thread.socket.failed", 141 controller.getPortNumber() + "")); 142 controller.endOfController(e); 143 } 144 catch (SSLException e) 145 { 146 logger.fatal(Translate.get("controller.server.thread.socket.failed", 147 controller.getPortNumber() + "")); 148 controller.endOfController(e); 149 } 150 if (logger.isInfoEnabled()) 151 { 152 logger.info(Translate.get("controller.server.thread.waiting.connections", 153 new String []{serverSocket.getInetAddress().getHostAddress(), 154 String.valueOf(serverSocket.getLocalPort())})); 155 logger.debug(Translate.get("controller.server.thread.backlog.size", "" 156 + controller.getBacklogSize())); 157 } 158 } 159 160 164 public void run() 165 { 166 if (controller == null) 167 { 168 logger.error(Translate.get("controller.server.thread.controller.null")); 169 isShuttingDown = true; 170 } 171 172 Socket clientSocket = null; 173 while (!isShuttingDown) 174 { 175 try 176 { clientSocket = serverSocket.accept(); 178 if (isShuttingDown) 179 break; 180 if (controller.isSecurityEnabled() 181 && !controller.getSecurity().allowConnection(clientSocket)) 182 { 183 String errmsg = Translate.get( 184 "controller.server.thread.connection.refused", clientSocket 185 .getInetAddress().getHostName()); 186 logger.warn(errmsg); 187 CJDBCOutputStream out = new CJDBCOutputStream(clientSocket); 188 out.writeBoolean(false); 189 out.writeUTF(errmsg); 190 out.flush(); clientSocket = null; 192 continue; 193 } 194 else 195 { 196 if (logger.isDebugEnabled()) 197 logger.debug(Translate.get( 198 "controller.server.thread.connection.accept", clientSocket 199 .getInetAddress().getHostName())); 200 } 201 boolean createThread = false; 202 if (isShuttingDown) 203 break; 204 synchronized (controllerServerThreadPendingQueue) 205 { 206 controllerServerThreadPendingQueue.add(clientSocket); 208 if (idleWorkerThreads == 0) 211 createThread = true; 212 else 213 controllerServerThreadPendingQueue.notifyAll(); 218 } 219 if (createThread) 220 { ControllerWorkerThread thread = new ControllerWorkerThread(this); 222 thread.start(); 223 if (logger.isDebugEnabled()) 224 logger.debug(Translate.get("controller.server.thread.starting")); 225 } 226 } 227 catch (IOException e) 228 { 229 if (!isShuttingDown) 230 { 231 logger.warn(Translate.get( 232 "controller.server.thread.new.connection.error", e), e); 233 } 234 } 235 } 236 if (logger.isInfoEnabled()) 237 logger.info(Translate.get("controller.server.thread.terminating")); 238 } 239 240 243 public void shutdown() 244 { 245 isShuttingDown = true; 246 try 248 { 249 serverSocket.close(); 250 } 251 catch (Exception e) 252 { 253 logger.warn(Translate.get("controller.shutdown.server.socket.exception"), 254 e); 255 } 256 257 261 Object lock = controllerServerThreadPendingQueue; 262 synchronized (lock) 263 { 264 int nbSockets = controllerServerThreadPendingQueue.size(); 266 Socket socket = null; 267 for (int i = 0; i < nbSockets; i++) 268 { 269 socket = (Socket ) controllerServerThreadPendingQueue.get(i); 270 logger.info(Translate.get("controller.shutdown.client.socket", socket 271 .getInetAddress().toString())); 272 273 try 274 { 275 socket.close(); 276 } 277 catch (Exception e) 278 { 279 logger.warn(Translate 280 .get("controller.shutdown.client.socket.exception"), e); 281 } 282 } 283 284 this.controllerServerThreadPendingQueue = null; 287 lock.notifyAll(); 288 } 289 } 290 291 294 public int getControllerServerThreadPendingQueueSize() 295 { 296 synchronized (controllerServerThreadPendingQueue) 297 { 298 return controllerServerThreadPendingQueue.size(); 299 } 300 } 301 302 305 public int getIdleWorkerThreads() 306 { 307 synchronized (controllerServerThreadPendingQueue) 308 { 309 return idleWorkerThreads; 310 } 311 } 312 313 318 public boolean isShuttingDown() 319 { 320 return isShuttingDown; 321 } 322 } | Popular Tags |