ConnectionManagerImpl.java 5.76 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
package org.jivesoftware.messenger.spi;

import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.audit.AuditManager;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.net.SSLSocketAcceptThread;
import org.jivesoftware.messenger.net.SocketAcceptThread;
import org.jivesoftware.messenger.net.SocketConnection;
import org.jivesoftware.messenger.net.SocketReadThread;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ConnectionManagerImpl extends BasicModule implements ConnectionManager {

    private SocketAcceptThread socketThread;
    private SSLSocketAcceptThread sslSocketThread;
    private ArrayList ports;

38 39 40 41 42 43 44
    private AuditManager auditManager;
    private SessionManager sessionManager;
    private PacketDeliverer deliverer;
    private PacketRouter router;
    private String serverName;
    private XMPPServer server;

Matt Tucker's avatar
Matt Tucker committed
45 46 47 48 49 50
    public ConnectionManagerImpl() {
        super("Connection Manager");
        ports = new ArrayList(2);
    }

    private void createSocket() {
51 52
        if (!isStarted ||  isSocketStarted || auditManager == null ||
                sessionManager == null || deliverer == null ||
Derek DeMoro's avatar
Derek DeMoro committed
53
                router == null || serverName == null)
54
        {
Matt Tucker's avatar
Matt Tucker committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
            return;
        }
        isSocketStarted = true;

        // Setup port info
        String localIPAddress = null;
        try {
            localIPAddress = InetAddress.getLocalHost().getHostAddress();
        }
        catch (UnknownHostException e) {
            if (localIPAddress == null) {
                localIPAddress = "Unknown";
            }
        }

Matt Tucker's avatar
Matt Tucker committed
70 71
        // Start SSL unless it's been disabled.
        if (JiveGlobals.getBooleanProperty("xmpp.socket.ssl.active", true)) {
Matt Tucker's avatar
Matt Tucker committed
72 73
            try {
                sslSocketThread = new SSLSocketAcceptThread(this);
74
                String algorithm = JiveGlobals.getProperty("xmpp.socket.ssl.algorithm");
Matt Tucker's avatar
Matt Tucker committed
75 76 77
                if ("".equals(algorithm) || algorithm == null) {
                    algorithm = "TLS";
                }
78 79
                ports.add(new ServerPortImpl(sslSocketThread.getPort(), serverName,
                        localIPAddress, true, algorithm));
Matt Tucker's avatar
Matt Tucker committed
80 81 82 83 84 85 86 87 88 89 90
                sslSocketThread.setDaemon(true);
                sslSocketThread.start();

                List params = new ArrayList();
                params.add(Integer.toString(sslSocketThread.getPort()));
                Log.info(LocaleUtils.getLocalizedString("startup.ssl", params));
            }
            catch (Exception e) {
                Log.error(LocaleUtils.getLocalizedString("admin.error.ssl"), e);
            }
        }
91 92
        // Start plain socket unless it's been disabled.
        if (JiveGlobals.getBooleanProperty("xmpp.socket.plain.active", true)) {
93 94
            socketThread = new SocketAcceptThread(this);
            ports.add(new ServerPortImpl(socketThread.getPort(),
Matt Tucker's avatar
Matt Tucker committed
95
                    serverName, localIPAddress, false, null));
96 97 98 99 100 101 102
            socketThread.setDaemon(true);
            socketThread.start();

            List params = new ArrayList();
            params.add(Integer.toString(socketThread.getPort()));
            Log.info(LocaleUtils.getLocalizedString("startup.plain", params));
        }
Matt Tucker's avatar
Matt Tucker committed
103 104 105 106 107 108
    }

    public Iterator getPorts() {
        return ports.iterator();
    }

Derek DeMoro's avatar
Derek DeMoro committed
109
    public void addSocket(Socket sock, boolean isSecure)  {
Matt Tucker's avatar
Matt Tucker committed
110 111 112 113 114 115 116 117
        try {
            // the order of these calls is critical (stupid huh?)
            Connection conn = new SocketConnection(deliverer,
                    auditManager.getAuditor(),
                    sock,
                    isSecure);
            Session session = sessionManager.createSession(conn);
            SocketReadThread reader = new SocketReadThread(router,
Derek DeMoro's avatar
Derek DeMoro committed
118
                    serverName, auditManager.getAuditor(),
Matt Tucker's avatar
Matt Tucker committed
119
                    sock, session);
Matt Tucker's avatar
Matt Tucker committed
120 121 122 123 124 125 126 127 128 129 130
            reader.setDaemon(true);
            reader.start();
        }
        catch (UnauthorizedException e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
        catch (IOException e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
    }

131 132 133 134 135 136 137
    public void initialize(XMPPServer server) {
        super.initialize(server);
        this.server = server;
        router = server.getPacketRouter();
        deliverer = server.getPacketDeliverer();
        auditManager = server.getAuditManager();
        sessionManager = server.getSessionManager();
Matt Tucker's avatar
Matt Tucker committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151
    }

    // Used to know if the sockets can be started (the connection manager has been started)
    private boolean isStarted = false;
    // Used to know if the sockets have been started
    private boolean isSocketStarted = false;

    // #####################################################################
    // Module management
    // #####################################################################

    public void start() {
        super.start();
        isStarted = true;
152
        serverName = server.getServerInfo().getName();
Matt Tucker's avatar
Matt Tucker committed
153 154 155 156 157 158 159 160 161 162 163 164 165
        createSocket();
    }

    public void stop() {
        super.stop();
        if (socketThread != null) {
            socketThread.shutdown();
            socketThread = null;
        }
        if (sslSocketThread != null) {
            sslSocketThread.shutdown();
            sslSocketThread = null;
        }
166
        serverName = null;
Matt Tucker's avatar
Matt Tucker committed
167 168
    }
}