KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > spi > ConnectionManagerImpl


1 /**
2  * $RCSfile: ConnectionManagerImpl.java,v $
3  * $Revision: 1.21 $
4  * $Date: 2005/07/04 16:18:25 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

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 JavaDoc;
22 import java.net.InetAddress JavaDoc;
23 import java.net.Socket JavaDoc;
24 import java.net.UnknownHostException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
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 JavaDoc<ServerPort> ports;
36
37     private SessionManager sessionManager;
38     private PacketDeliverer deliverer;
39     private PacketRouter router;
40     private String JavaDoc serverName;
41     private XMPPServer server;
42     private String JavaDoc localIPAddress = null;
43
44     // Used to know if the sockets can be started (the connection manager has been started)
45
private boolean isStarted = false;
46     // Used to know if the sockets have been started
47
private boolean isSocketStarted = false;
48
49     public ConnectionManagerImpl() {
50         super("Connection Manager");
51         ports = new ArrayList JavaDoc<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         // Setup port info
64
try {
65             localIPAddress = InetAddress.getLocalHost().getHostAddress();
66         }
67         catch (UnknownHostException JavaDoc e) {
68             if (localIPAddress == null) {
69                 localIPAddress = "Unknown";
70             }
71         }
72         // Start the port listener for s2s communication
73
startServerListener(localIPAddress);
74         // Start the port listener for external components
75
startComponentListener(localIPAddress);
76         // Start the port listener for clients
77
startClientListeners(localIPAddress);
78         // Start the port listener for secured clients
79
startClientSSLListeners(localIPAddress);
80     }
81
82     private void startServerListener(String JavaDoc localIPAddress) {
83         // Start servers socket unless it's been disabled.
84
if (isServerListenerEnabled()) {
85             int port = getServerListenerPort();
86             String JavaDoc 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 JavaDoc params = new ArrayList JavaDoc();
96                 params.add(Integer.toString(serverSocketThread.getPort()));
97                 Log.info(LocaleUtils.getLocalizedString("startup.server", params));
98             }
99             catch (Exception JavaDoc 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 JavaDoc localIPAddress) {
116         // Start components socket unless it's been disabled.
117
if (isComponentListenerEnabled()) {
118             int port = getComponentListenerPort();
119             String JavaDoc 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 JavaDoc params = new ArrayList JavaDoc();
129                 params.add(Integer.toString(componentSocketThread.getPort()));
130                 Log.info(LocaleUtils.getLocalizedString("startup.component", params));
131             }
132             catch (Exception JavaDoc 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 JavaDoc localIPAddress) {
149         // Start clients plain socket unless it's been disabled.
150
if (isClientListenerEnabled()) {
151             int port = getClientListenerPort();
152             String JavaDoc 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 JavaDoc params = new ArrayList JavaDoc();
162                 params.add(Integer.toString(socketThread.getPort()));
163                 Log.info(LocaleUtils.getLocalizedString("startup.plain", params));
164             }
165             catch (Exception JavaDoc 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 JavaDoc localIPAddress) {
182         // Start clients SSL unless it's been disabled.
183
if (isClientSSLListenerEnabled()) {
184             int port = getClientSSLListenerPort();
185             String JavaDoc interfaceName = JiveGlobals.getProperty("xmpp.socket.ssl.interface");
186             String JavaDoc 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 JavaDoc params = new ArrayList JavaDoc();
199                 params.add(Integer.toString(sslSocketThread.getPort()));
200                 Log.info(LocaleUtils.getLocalizedString("startup.ssl", params));
201             }
202             catch (Exception JavaDoc 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 JavaDoc<ServerPort> getPorts() {
219         return ports.iterator();
220     }
221
222     public void addSocket(Socket JavaDoc sock, boolean isSecure, ServerPort serverPort) {
223         try {
224             // the order of these calls is critical (stupid huh?)
225
SocketConnection conn = new SocketConnection(deliverer, sock, isSecure);
226             SocketReader reader = null;
227             String JavaDoc 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 JavaDoc thread = new Thread JavaDoc(reader, threadName);
241             thread.setDaemon(true);
242             thread.start();
243         }
244         catch (IOException JavaDoc 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             // Ignore new setting
260
return;
261         }
262         if (enabled) {
263             JiveGlobals.setProperty("xmpp.socket.plain.active", "true");
264             // Start the port listener for clients
265
startClientListeners(localIPAddress);
266         }
267         else {
268             JiveGlobals.setProperty("xmpp.socket.plain.active", "false");
269             // Stop the port listener for clients
270
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             // Ignore new setting
281
return;
282         }
283         if (enabled) {
284             JiveGlobals.setProperty("xmpp.socket.ssl.active", "true");
285             // Start the port listener for secured clients
286
startClientSSLListeners(localIPAddress);
287         }
288         else {
289             JiveGlobals.setProperty("xmpp.socket.ssl.active", "false");
290             // Stop the port listener for secured clients
291
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             // Ignore new setting
302
return;
303         }
304         if (enabled) {
305             JiveGlobals.setProperty("xmpp.component.socket.active", "true");
306             // Start the port listener for external components
307
startComponentListener(localIPAddress);
308         }
309         else {
310             JiveGlobals.setProperty("xmpp.component.socket.active", "false");
311             // Stop the port listener for external components
312
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             // Ignore new setting
323
return;
324         }
325         if (enabled) {
326             JiveGlobals.setProperty("xmpp.server.socket.active", "true");
327             // Start the port listener for s2s communication
328
startServerListener(localIPAddress);
329         }
330         else {
331             JiveGlobals.setProperty("xmpp.server.socket.active", "false");
332             // Stop the port listener for s2s communication
333
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             // Ignore new setting
344
return;
345         }
346         JiveGlobals.setProperty("xmpp.socket.plain.port", String.valueOf(port));
347         // Stop the port listener for clients
348
stopClientListeners();
349         if (isClientListenerEnabled()) {
350             // Start the port listener for clients
351
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             // Ignore new setting
363
return;
364         }
365         JiveGlobals.setProperty("xmpp.socket.ssl.port", String.valueOf(port));
366         // Stop the port listener for secured clients
367
stopClientSSLListeners();
368         if (isClientSSLListenerEnabled()) {
369             // Start the port listener for secured clients
370
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             // Ignore new setting
382
return;
383         }
384         JiveGlobals.setProperty("xmpp.component.socket.port", String.valueOf(port));
385         // Stop the port listener for external components
386
stopComponentListener();
387         if (isComponentListenerEnabled()) {
388             // Start the port listener for external components
389
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             // Ignore new setting
401
return;
402         }
403         JiveGlobals.setProperty("xmpp.server.socket.port", String.valueOf(port));
404         // Stop the port listener for s2s communication
405
stopServerListener();
406         if (isServerListenerEnabled()) {
407             // Start the port listener for s2s communication
408
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     // #####################################################################
418
// Module management
419
// #####################################################################
420

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