1 11 12 package org.jivesoftware.messenger.spi; 13 14 import org.jivesoftware.messenger.*; 15 import org.jivesoftware.messenger.container.BasicModule; 16 import org.jivesoftware.messenger.net.*; 17 import org.jivesoftware.util.LocaleUtils; 18 import org.jivesoftware.util.Log; 19 import org.jivesoftware.util.JiveGlobals; 20 21 import java.io.IOException ; 22 import java.net.InetAddress ; 23 import java.net.Socket ; 24 import java.net.UnknownHostException ; 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 public class ConnectionManagerImpl extends BasicModule implements ConnectionManager { 30 31 private SocketAcceptThread socketThread; 32 private SSLSocketAcceptThread sslSocketThread; 33 private SocketAcceptThread componentSocketThread; 34 private SocketAcceptThread serverSocketThread; 35 private ArrayList <ServerPort> ports; 36 37 private SessionManager sessionManager; 38 private PacketDeliverer deliverer; 39 private PacketRouter router; 40 private String serverName; 41 private XMPPServer server; 42 private String localIPAddress = null; 43 44 private boolean isStarted = false; 46 private boolean isSocketStarted = false; 48 49 public ConnectionManagerImpl() { 50 super("Connection Manager"); 51 ports = new ArrayList <ServerPort>(4); 52 } 53 54 private void createSocket() { 55 if (!isStarted || isSocketStarted || sessionManager == null || deliverer == null || 56 router == null || 57 serverName == null) 58 { 59 return; 60 } 61 isSocketStarted = true; 62 63 try { 65 localIPAddress = InetAddress.getLocalHost().getHostAddress(); 66 } 67 catch (UnknownHostException e) { 68 if (localIPAddress == null) { 69 localIPAddress = "Unknown"; 70 } 71 } 72 startServerListener(localIPAddress); 74 startComponentListener(localIPAddress); 76 startClientListeners(localIPAddress); 78 startClientSSLListeners(localIPAddress); 80 } 81 82 private void startServerListener(String localIPAddress) { 83 if (isServerListenerEnabled()) { 85 int port = getServerListenerPort(); 86 String interfaceName = JiveGlobals.getProperty("xmpp.server.socket.interface"); 87 ServerPort serverPort = new ServerPort(port, interfaceName, serverName, localIPAddress, 88 false, null, ServerPort.Type.server); 89 try { 90 serverSocketThread = new SocketAcceptThread(this, serverPort); 91 ports.add(serverPort); 92 serverSocketThread.setDaemon(true); 93 serverSocketThread.start(); 94 95 List params = new ArrayList (); 96 params.add(Integer.toString(serverSocketThread.getPort())); 97 Log.info(LocaleUtils.getLocalizedString("startup.server", params)); 98 } 99 catch (Exception e) { 100 System.err.println("Error starting server listener on port " + port + ": " + 101 e.getMessage()); 102 Log.error(LocaleUtils.getLocalizedString("admin.error.socket-setup"), e); 103 } 104 } 105 } 106 107 private void stopServerListener() { 108 if (serverSocketThread != null) { 109 serverSocketThread.shutdown(); 110 ports.remove(serverSocketThread.getServerPort()); 111 serverSocketThread = null; 112 } 113 } 114 115 private void startComponentListener(String localIPAddress) { 116 if (isComponentListenerEnabled()) { 118 int port = getComponentListenerPort(); 119 String interfaceName = JiveGlobals.getProperty("xmpp.component.socket.interface"); 120 ServerPort serverPort = new ServerPort(port, interfaceName, serverName, localIPAddress, 121 false, null, ServerPort.Type.component); 122 try { 123 componentSocketThread = new SocketAcceptThread(this, serverPort); 124 ports.add(serverPort); 125 componentSocketThread.setDaemon(true); 126 componentSocketThread.start(); 127 128 List params = new ArrayList (); 129 params.add(Integer.toString(componentSocketThread.getPort())); 130 Log.info(LocaleUtils.getLocalizedString("startup.component", params)); 131 } 132 catch (Exception e) { 133 System.err.println("Error starting component listener on port " + port + ": " + 134 e.getMessage()); 135 Log.error(LocaleUtils.getLocalizedString("admin.error.socket-setup"), e); 136 } 137 } 138 } 139 140 private void stopComponentListener() { 141 if (componentSocketThread != null) { 142 componentSocketThread.shutdown(); 143 ports.remove(componentSocketThread.getServerPort()); 144 componentSocketThread = null; 145 } 146 } 147 148 private void startClientListeners(String localIPAddress) { 149 if (isClientListenerEnabled()) { 151 int port = getClientListenerPort(); 152 String interfaceName = JiveGlobals.getProperty("xmpp.socket.plain.interface"); 153 ServerPort serverPort = new ServerPort(port, interfaceName, serverName, localIPAddress, 154 false, null, ServerPort.Type.client); 155 try { 156 socketThread = new SocketAcceptThread(this, serverPort); 157 ports.add(serverPort); 158 socketThread.setDaemon(true); 159 socketThread.start(); 160 161 List params = new ArrayList (); 162 params.add(Integer.toString(socketThread.getPort())); 163 Log.info(LocaleUtils.getLocalizedString("startup.plain", params)); 164 } 165 catch (Exception e) { 166 System.err.println("Error starting XMPP listener on port " + port + ": " + 167 e.getMessage()); 168 Log.error(LocaleUtils.getLocalizedString("admin.error.socket-setup"), e); 169 } 170 } 171 } 172 173 private void stopClientListeners() { 174 if (socketThread != null) { 175 socketThread.shutdown(); 176 ports.remove(socketThread.getServerPort()); 177 socketThread = null; 178 } 179 } 180 181 private void startClientSSLListeners(String localIPAddress) { 182 if (isClientSSLListenerEnabled()) { 184 int port = getClientSSLListenerPort(); 185 String interfaceName = JiveGlobals.getProperty("xmpp.socket.ssl.interface"); 186 String algorithm = JiveGlobals.getProperty("xmpp.socket.ssl.algorithm"); 187 if ("".equals(algorithm) || algorithm == null) { 188 algorithm = "TLS"; 189 } 190 ServerPort serverPort = new ServerPort(port, interfaceName, serverName, localIPAddress, 191 true, algorithm, ServerPort.Type.client); 192 try { 193 sslSocketThread = new SSLSocketAcceptThread(this, serverPort); 194 ports.add(serverPort); 195 sslSocketThread.setDaemon(true); 196 sslSocketThread.start(); 197 198 List params = new ArrayList (); 199 params.add(Integer.toString(sslSocketThread.getPort())); 200 Log.info(LocaleUtils.getLocalizedString("startup.ssl", params)); 201 } 202 catch (Exception e) { 203 System.err.println("Error starting SSL XMPP listener on port " + port + ": " + 204 e.getMessage()); 205 Log.error(LocaleUtils.getLocalizedString("admin.error.ssl"), e); 206 } 207 } 208 } 209 210 private void stopClientSSLListeners() { 211 if (sslSocketThread != null) { 212 sslSocketThread.shutdown(); 213 ports.remove(sslSocketThread.getServerPort()); 214 sslSocketThread = null; 215 } 216 } 217 218 public Iterator <ServerPort> getPorts() { 219 return ports.iterator(); 220 } 221 222 public void addSocket(Socket sock, boolean isSecure, ServerPort serverPort) { 223 try { 224 SocketConnection conn = new SocketConnection(deliverer, sock, isSecure); 226 SocketReader reader = null; 227 String threadName = null; 228 if (serverPort.isClientPort()) { 229 reader = new ClientSocketReader(router, serverName, sock, conn); 230 threadName = "Client SR"; 231 } 232 else if (serverPort.isComponentPort()) { 233 reader = new ComponentSocketReader(router, serverName, sock, conn); 234 threadName = "Component SR"; 235 } 236 else { 237 reader = new ServerSocketReader(router, serverName, sock, conn); 238 threadName = "Server SR"; 239 } 240 Thread thread = new Thread (reader, threadName); 241 thread.setDaemon(true); 242 thread.start(); 243 } 244 catch (IOException e) { 245 Log.error(LocaleUtils.getLocalizedString("admin.error"), e); 246 } 247 } 248 249 public void initialize(XMPPServer server) { 250 super.initialize(server); 251 this.server = server; 252 router = server.getPacketRouter(); 253 deliverer = server.getPacketDeliverer(); 254 sessionManager = server.getSessionManager(); 255 } 256 257 public void enableClientListener(boolean enabled) { 258 if (enabled == isClientListenerEnabled()) { 259 return; 261 } 262 if (enabled) { 263 JiveGlobals.setProperty("xmpp.socket.plain.active", "true"); 264 startClientListeners(localIPAddress); 266 } 267 else { 268 JiveGlobals.setProperty("xmpp.socket.plain.active", "false"); 269 stopClientListeners(); 271 } 272 } 273 274 public boolean isClientListenerEnabled() { 275 return JiveGlobals.getBooleanProperty("xmpp.socket.plain.active", true); 276 } 277 278 public void enableClientSSLListener(boolean enabled) { 279 if (enabled == isClientSSLListenerEnabled()) { 280 return; 282 } 283 if (enabled) { 284 JiveGlobals.setProperty("xmpp.socket.ssl.active", "true"); 285 startClientSSLListeners(localIPAddress); 287 } 288 else { 289 JiveGlobals.setProperty("xmpp.socket.ssl.active", "false"); 290 stopClientSSLListeners(); 292 } 293 } 294 295 public boolean isClientSSLListenerEnabled() { 296 return JiveGlobals.getBooleanProperty("xmpp.socket.ssl.active", true); 297 } 298 299 public void enableComponentListener(boolean enabled) { 300 if (enabled == isComponentListenerEnabled()) { 301 return; 303 } 304 if (enabled) { 305 JiveGlobals.setProperty("xmpp.component.socket.active", "true"); 306 startComponentListener(localIPAddress); 308 } 309 else { 310 JiveGlobals.setProperty("xmpp.component.socket.active", "false"); 311 stopComponentListener(); 313 } 314 } 315 316 public boolean isComponentListenerEnabled() { 317 return JiveGlobals.getBooleanProperty("xmpp.component.socket.active", false); 318 } 319 320 public void enableServerListener(boolean enabled) { 321 if (enabled == isServerListenerEnabled()) { 322 return; 324 } 325 if (enabled) { 326 JiveGlobals.setProperty("xmpp.server.socket.active", "true"); 327 startServerListener(localIPAddress); 329 } 330 else { 331 JiveGlobals.setProperty("xmpp.server.socket.active", "false"); 332 stopServerListener(); 334 } 335 } 336 337 public boolean isServerListenerEnabled() { 338 return JiveGlobals.getBooleanProperty("xmpp.server.socket.active", true); 339 } 340 341 public void setClientListenerPort(int port) { 342 if (port == getClientListenerPort()) { 343 return; 345 } 346 JiveGlobals.setProperty("xmpp.socket.plain.port", String.valueOf(port)); 347 stopClientListeners(); 349 if (isClientListenerEnabled()) { 350 startClientListeners(localIPAddress); 352 } 353 } 354 355 public int getClientListenerPort() { 356 return JiveGlobals.getIntProperty("xmpp.socket.plain.port", 357 SocketAcceptThread.DEFAULT_PORT); 358 } 359 360 public void setClientSSLListenerPort(int port) { 361 if (port == getClientSSLListenerPort()) { 362 return; 364 } 365 JiveGlobals.setProperty("xmpp.socket.ssl.port", String.valueOf(port)); 366 stopClientSSLListeners(); 368 if (isClientSSLListenerEnabled()) { 369 startClientSSLListeners(localIPAddress); 371 } 372 } 373 374 public int getClientSSLListenerPort() { 375 return JiveGlobals.getIntProperty("xmpp.socket.ssl.port", 376 SSLSocketAcceptThread.DEFAULT_PORT); 377 } 378 379 public void setComponentListenerPort(int port) { 380 if (port == getComponentListenerPort()) { 381 return; 383 } 384 JiveGlobals.setProperty("xmpp.component.socket.port", String.valueOf(port)); 385 stopComponentListener(); 387 if (isComponentListenerEnabled()) { 388 startComponentListener(localIPAddress); 390 } 391 } 392 393 public int getComponentListenerPort() { 394 return JiveGlobals.getIntProperty("xmpp.component.socket.port", 395 SocketAcceptThread.DEFAULT_COMPONENT_PORT); 396 } 397 398 public void setServerListenerPort(int port) { 399 if (port == getServerListenerPort()) { 400 return; 402 } 403 JiveGlobals.setProperty("xmpp.server.socket.port", String.valueOf(port)); 404 stopServerListener(); 406 if (isServerListenerEnabled()) { 407 startServerListener(localIPAddress); 409 } 410 } 411 412 public int getServerListenerPort() { 413 return JiveGlobals.getIntProperty("xmpp.server.socket.port", 414 SocketAcceptThread.DEFAULT_SERVER_PORT); 415 } 416 417 421 public void start() { 422 super.start(); 423 isStarted = true; 424 serverName = server.getServerInfo().getName(); 425 createSocket(); 426 SocketSendingTracker.getInstance().start(); 427 } 428 429 public void stop() { 430 super.stop(); 431 stopClientListeners(); 432 stopClientSSLListeners(); 433 stopComponentListener(); 434 stopServerListener(); 435 SocketSendingTracker.getInstance().shutdown(); 436 serverName = null; 437 } 438 } 439 | Popular Tags |