ConnectionManagerImpl.java 35.1 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jivesoftware.openfire.spi;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.buffer.SimpleBufferAllocator;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
22 23 24 25
import org.jivesoftware.openfire.Connection;
import org.jivesoftware.openfire.ConnectionManager;
import org.jivesoftware.openfire.ServerPort;
import org.jivesoftware.openfire.XMPPServer;
26 27 28 29
import org.jivesoftware.openfire.container.BasicModule;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.container.PluginManagerListener;
import org.jivesoftware.openfire.http.HttpBindManager;
30
import org.jivesoftware.openfire.keystore.CertificateStoreManager;
31
import org.jivesoftware.openfire.net.SocketSendingTracker;
32
import org.jivesoftware.openfire.session.ConnectionSettings;
33 34 35 36
import org.jivesoftware.util.CertificateEventListener;
import org.jivesoftware.util.CertificateManager;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.PropertyEventListener;
37 38 39
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

40 41 42 43 44 45 46
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.KeyStore;
import java.security.cert.X509Certificate;
import java.util.*;

47 48
public class ConnectionManagerImpl extends BasicModule implements ConnectionManager, CertificateEventListener, PropertyEventListener
{
49 50 51 52 53 54 55 56
    public static final String EXECUTOR_FILTER_NAME = "threadModel";
    public static final String TLS_FILTER_NAME = "tls";
    public static final String COMPRESSION_FILTER_NAME = "compression";
    public static final String XMPP_CODEC_FILTER_NAME = "xmpp";
    public static final String CAPACITY_FILTER_NAME = "outCap";

    private static final Logger Log = LoggerFactory.getLogger(ConnectionManagerImpl.class);

57 58
    private final ConnectionListener clientListener;
    private final ConnectionListener clientSslListener;
59 60
    private final ConnectionListener boshListener;
    private final ConnectionListener boshSslListener;
61 62 63 64 65
    private final ConnectionListener serverListener;
    private final ConnectionListener componentListener;
    private final ConnectionListener componentSslListener;
    private final ConnectionListener connectionManagerListener; // Also known as 'multiplexer'
    private final ConnectionListener connectionManagerSslListener; // Also known as 'multiplexer'
66 67
    private final ConnectionListener webAdminListener;
    private final ConnectionListener webAdminSslListener;
68 69 70 71 72 73

    /**
     * Instantiates a new connection manager.
     */
    public ConnectionManagerImpl() throws IOException
    {
74 75
        super("Connection Manager");

76
        InetAddress bindAddress = null;
77 78
        InetAddress adminConsoleBindAddress = null;

79 80 81
        try
        {
            bindAddress = getListenAddress();
82
        }
83 84 85 86 87
        catch ( UnknownHostException e )
        {
            Log.warn( "Unable to resolve bind address: ", e );
        }

88 89 90 91 92 93 94 95 96 97 98 99 100
        try
        {
            adminConsoleBindAddress = getAdminConsoleListenAddress();
            if( adminConsoleBindAddress == null )
            {
                adminConsoleBindAddress = bindAddress;
            }
        }
        catch( UnknownHostException e )
        {
            Log.warn(  "Unable to resolve admin console bind address: ", e );
        }

101
        final CertificateStoreManager certificateStoreManager = XMPPServer.getInstance().getCertificateStoreManager();
102

103 104 105 106 107 108 109 110 111 112 113
        // client-to-server
        clientListener = new ConnectionListener(
                ConnectionType.SOCKET_C2S,
                ConnectionSettings.Client.PORT,
                DEFAULT_PORT,
                ConnectionSettings.Client.SOCKET_ACTIVE,
                ConnectionSettings.Client.MAX_THREADS,
                ConnectionSettings.Client.MAX_READ_BUFFER,
                ConnectionSettings.Client.TLS_POLICY,
                ConnectionSettings.Client.AUTH_PER_CLIENTCERT_POLICY,
                bindAddress,
114
                certificateStoreManager.getIdentityStoreConfiguration( ConnectionType.SOCKET_C2S ),
115 116
                certificateStoreManager.getTrustStoreConfiguration( ConnectionType.SOCKET_C2S ),
                ConnectionSettings.Client.COMPRESSION_SETTINGS
117 118 119 120 121 122 123 124 125 126 127
        );
        clientSslListener = new ConnectionListener(
                ConnectionType.SOCKET_C2S,
                ConnectionSettings.Client.OLD_SSLPORT,
                DEFAULT_SSL_PORT,
                ConnectionSettings.Client.ENABLE_OLD_SSLPORT,
                ConnectionSettings.Client.MAX_THREADS_SSL,
                ConnectionSettings.Client.MAX_READ_BUFFER_SSL,
                Connection.TLSPolicy.legacyMode.name(), // force legacy mode
                ConnectionSettings.Client.AUTH_PER_CLIENTCERT_POLICY,
                bindAddress,
128
                certificateStoreManager.getIdentityStoreConfiguration( ConnectionType.SOCKET_C2S ),
129 130
                certificateStoreManager.getTrustStoreConfiguration( ConnectionType.SOCKET_C2S ),
                ConnectionSettings.Client.COMPRESSION_SETTINGS
131
        );
132 133 134 135 136 137 138 139 140 141 142
        // BOSH / HTTP-bind
        boshListener = new ConnectionListener(
                ConnectionType.BOSH_C2S,
                HttpBindManager.HTTP_BIND_PORT,
                HttpBindManager.HTTP_BIND_PORT_DEFAULT,
                HttpBindManager.HTTP_BIND_ENABLED, // TODO this one property enables/disables both normal and legacymode port. Should be separated into two.
                HttpBindManager.HTTP_BIND_THREADS,
                null,
                Connection.TLSPolicy.disabled.name(), // StartTLS over HTTP? Should use boshSslListener instead.
                HttpBindManager.HTTP_BIND_AUTH_PER_CLIENTCERT_POLICY,
                bindAddress,
143
                certificateStoreManager.getIdentityStoreConfiguration( ConnectionType.BOSH_C2S ),
144 145
                certificateStoreManager.getTrustStoreConfiguration( ConnectionType.BOSH_C2S ),
                ConnectionSettings.Client.COMPRESSION_SETTINGS // Existing code re-used the generic client compression property. Should we have a BOSH-specific one?
146 147 148 149 150 151 152 153 154 155 156
        );
        boshSslListener = new ConnectionListener(
                ConnectionType.BOSH_C2S,
                HttpBindManager.HTTP_BIND_SECURE_PORT,
                HttpBindManager.HTTP_BIND_SECURE_PORT_DEFAULT,
                HttpBindManager.HTTP_BIND_ENABLED, // TODO this one property enables/disables both normal and legacymode port. Should be separated into two.
                HttpBindManager.HTTP_BIND_THREADS,
                null,
                Connection.TLSPolicy.legacyMode.name(),
                HttpBindManager.HTTP_BIND_AUTH_PER_CLIENTCERT_POLICY,
                bindAddress,
157
                certificateStoreManager.getIdentityStoreConfiguration( ConnectionType.BOSH_C2S ),
158 159
                certificateStoreManager.getTrustStoreConfiguration( ConnectionType.BOSH_C2S ),
                ConnectionSettings.Client.COMPRESSION_SETTINGS // Existing code re-used the generic client compression property. Should we have a BOSH-specific one?
160
        );
161 162 163 164 165 166 167 168 169 170 171
        // server-to-server (federation)
        serverListener = new ConnectionListener(
                ConnectionType.SOCKET_S2S,
                ConnectionSettings.Server.PORT,
                DEFAULT_SERVER_PORT,
                ConnectionSettings.Server.SOCKET_ACTIVE,
                "xmpp.server.processing.threads",
                null,
                ConnectionSettings.Server.TLS_POLICY,
                ConnectionSettings.Server.AUTH_PER_CLIENTCERT_POLICY,
                bindAddress,
172
                certificateStoreManager.getIdentityStoreConfiguration( ConnectionType.SOCKET_S2S ),
173 174
                certificateStoreManager.getTrustStoreConfiguration( ConnectionType.SOCKET_S2S ),
                ConnectionSettings.Server.COMPRESSION_SETTINGS
175 176 177 178 179 180 181 182 183 184 185 186
        );
        // external components (XEP 0114)
        componentListener = new ConnectionListener(
                ConnectionType.COMPONENT,
                ConnectionSettings.Component.PORT,
                DEFAULT_COMPONENT_PORT,
                ConnectionSettings.Component.SOCKET_ACTIVE,
                ConnectionSettings.Component.MAX_THREADS,
                null,
                ConnectionSettings.Component.TLS_POLICY,
                ConnectionSettings.Component.AUTH_PER_CLIENTCERT_POLICY,
                bindAddress,
187
                certificateStoreManager.getIdentityStoreConfiguration( ConnectionType.COMPONENT ),
188 189
                certificateStoreManager.getTrustStoreConfiguration( ConnectionType.COMPONENT ),
                ConnectionSettings.Component.COMPRESSION_SETTINGS
190 191 192 193 194 195 196 197 198 199 200
        );
        componentSslListener = new ConnectionListener(
                ConnectionType.COMPONENT,
                ConnectionSettings.Component.OLD_SSLPORT,
                DEFAULT_COMPONENT_SSL_PORT,
                ConnectionSettings.Component.ENABLE_OLD_SSLPORT,
                ConnectionSettings.Component.MAX_THREADS_SSL,
                null,
                Connection.TLSPolicy.legacyMode.name(), // force legacy mode
                ConnectionSettings.Component.AUTH_PER_CLIENTCERT_POLICY,
                bindAddress,
201
                certificateStoreManager.getIdentityStoreConfiguration( ConnectionType.COMPONENT ),
202 203
                certificateStoreManager.getTrustStoreConfiguration( ConnectionType.COMPONENT ),
                ConnectionSettings.Component.COMPRESSION_SETTINGS
204 205 206 207 208 209 210 211 212 213 214 215 216
        );

        // Multiplexers (our propertietary connection manager implementation)
        connectionManagerListener = new ConnectionListener(
                ConnectionType.CONNECTION_MANAGER,
                ConnectionSettings.Multiplex.PORT,
                DEFAULT_MULTIPLEX_PORT,
                ConnectionSettings.Multiplex.SOCKET_ACTIVE,
                ConnectionSettings.Multiplex.MAX_THREADS,
                null,
                ConnectionSettings.Multiplex.TLS_POLICY,
                ConnectionSettings.Multiplex.AUTH_PER_CLIENTCERT_POLICY,
                bindAddress,
217
                certificateStoreManager.getIdentityStoreConfiguration( ConnectionType.CONNECTION_MANAGER ),
218 219
                certificateStoreManager.getTrustStoreConfiguration( ConnectionType.CONNECTION_MANAGER ),
                ConnectionSettings.Multiplex.COMPRESSION_SETTINGS
220 221 222 223 224 225 226 227 228 229 230
        );
        connectionManagerSslListener = new ConnectionListener(
                ConnectionType.CONNECTION_MANAGER,
                ConnectionSettings.Multiplex.OLD_SSLPORT,
                DEFAULT_MULTIPLEX_SSL_PORT,
                ConnectionSettings.Multiplex.ENABLE_OLD_SSLPORT,
                ConnectionSettings.Multiplex.MAX_THREADS_SSL,
                null,
                Connection.TLSPolicy.legacyMode.name(), // force legacy mode
                ConnectionSettings.Multiplex.AUTH_PER_CLIENTCERT_POLICY,
                bindAddress,
231
                certificateStoreManager.getIdentityStoreConfiguration( ConnectionType.CONNECTION_MANAGER ),
232 233
                certificateStoreManager.getTrustStoreConfiguration( ConnectionType.CONNECTION_MANAGER ),
                ConnectionSettings.Multiplex.COMPRESSION_SETTINGS
234
        );
235

236 237
        // Admin console (the Openfire web-admin) // TODO these use the XML properties instead of normal properties!
        webAdminListener = new ConnectionListener(
238 239 240 241 242 243 244 245
                ConnectionType.WEBADMIN,
                "adminConsole.port",
                9090,
                null,
                "adminConsole.serverThreads",
                null,
                Connection.TLSPolicy.disabled.name(), // StartTLS over HTTP? Should use webAdminSslListener instead.
                null,
246
                adminConsoleBindAddress,
247
                certificateStoreManager.getIdentityStoreConfiguration( ConnectionType.WEBADMIN ),
248 249
                certificateStoreManager.getTrustStoreConfiguration( ConnectionType.WEBADMIN ),
                null // Should we have compression on the admin console?
250 251 252 253 254 255 256 257 258 259 260
        );

        webAdminSslListener = new ConnectionListener(
                ConnectionType.WEBADMIN,
                "adminConsole.securePort",
                9091,
                null,
                "adminConsole.serverThreads",
                null,
                Connection.TLSPolicy.legacyMode.name(),
                null,
261
                adminConsoleBindAddress,
262
                certificateStoreManager.getIdentityStoreConfiguration( ConnectionType.WEBADMIN ),
263 264
                certificateStoreManager.getTrustStoreConfiguration( ConnectionType.WEBADMIN ),
                null // Should we have compression on the admin console?
265
        );
266

267 268 269 270 271 272 273
    }

    /**
     * Starts all listeners. This ensures that all those that are enabled will start accept connections.
     */
    private synchronized void startListeners()
    {
274
        // Check if plugins have been loaded
275
        Log.debug( "Received a request to start listeners. Have plugins been loaded?" );
276
        PluginManager pluginManager = XMPPServer.getInstance().getPluginManager();
277 278 279 280 281 282 283 284
        if ( !pluginManager.isExecuted() )
        {
            Log.debug( "Plugins not yet loaded. Waiting for plugins to be loaded..." );
            pluginManager.addPluginManagerListener( new PluginManagerListener()
            {
                public void pluginsMonitored()
                {
                    Log.debug( "Received plugin monitor event! Plugins should now be loaded." );
285
                    // Stop listening for plugin events
286
                    XMPPServer.getInstance().getPluginManager().removePluginManagerListener( this );
287 288 289
                    // Start listeners
                    startListeners();
                }
290
            } );
291 292 293
            return;
        }

294
        Log.debug( "Starting listeners..." );
295 296 297 298 299
        for ( final ConnectionListener listener : getListeners() )
        {
            try
            {
                listener.start();
300
                Log.debug( "Started '{}' (port {}) listener.", listener.getType(), listener.getPort() );
301
            }
302 303 304
            catch ( RuntimeException ex )
            {
                Log.error( "An exception occurred while starting listener " + listener, ex );
305 306 307
            }
        }

308 309 310 311
        // Start the HTTP client listener.
        try
        {
            HttpBindManager.getInstance().start();
312
            Log.debug( "Started HTTP client listener." );
313
        }
314 315 316
        catch ( RuntimeException ex )
        {
            Log.error( "An exception occurred while starting HTTP Bind listener ", ex );
317 318 319
        }
    }

320 321 322 323 324 325 326 327 328 329 330
    /**
     * Stops all listeners. This ensures no listener will accept new connections.
     */
    private synchronized void stopListeners()
    {
        for ( final ConnectionListener listener : getListeners() )
        {
            // TODO determine by purpose exactly what needs and what need not be restarted.
            try
            {
                listener.stop();
331
            }
332 333 334
            catch ( RuntimeException ex )
            {
                Log.error( "An exception occurred while stopping listener " + listener, ex );
335 336 337
            }
        }

338 339 340 341
        // Stop the HTTP client listener.
        try
        {
            HttpBindManager.getInstance().stop();
342
        }
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
        catch ( RuntimeException ex )
        {
            Log.error( "An exception occurred while stopping HTTP Bind listener ", ex );
        }
    }

    /**
     * Returns the specific network interface on which Openfire is configured to listen, or null when no such preference
     * has been configured.
     *
     * @return A network interface or null.
     * @throws UnknownHostException When the configured network name cannot be resolved.
     */
    public InetAddress getListenAddress() throws UnknownHostException
    {
        String interfaceName = JiveGlobals.getXMLProperty( "network.interface" );
        InetAddress bindInterface = null;
        if (interfaceName != null) {
            if (interfaceName.trim().length() > 0) {
                bindInterface = InetAddress.getByName(interfaceName);
            }
364
        }
365 366 367
        return bindInterface;
    }

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
    /**
     * Returns the specific network interface on which the Openfire administration
     * console should be configured to listen, or null when no such preference
     * has been configured.
     *
     * @return A network interface or null.
     * @throws UnknownHostException When the configured network name cannot be resolved.
     */
    public InetAddress getAdminConsoleListenAddress() throws UnknownHostException
    {
        String acInterfaceName = JiveGlobals.getXMLProperty( "adminConsole.interface" );
        InetAddress acBindInterface = null;
        if (acInterfaceName != null) {
            if (acInterfaceName.trim().length() > 0) {
                acBindInterface = InetAddress.getByName(acInterfaceName);
            }
        }
        return acBindInterface;
    }

388 389 390 391 392 393 394 395 396
    /**
     * Returns all connection listeners.
     *
     * @return All connection listeners (never null).
     */
    public Set<ConnectionListener> getListeners() {
        final Set<ConnectionListener> listeners = new LinkedHashSet<>();
        listeners.add( clientListener );
        listeners.add( clientSslListener );
397 398
        listeners.add( boshListener );
        listeners.add( boshSslListener );
399 400 401 402 403
        listeners.add( serverListener );
        listeners.add( componentListener );
        listeners.add( componentSslListener );
        listeners.add( connectionManagerListener );
        listeners.add( connectionManagerSslListener );
404 405
        listeners.add( webAdminListener );
        listeners.add( webAdminSslListener );
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
        return listeners;
    }

    /**
     * Returns a connection listener.
     *
     * The #startInSslMode parameter is used to distinguish between listeners that expect to receive SSL encrypted data
     * immediately, as opposed to connections that initially accept plain text data (the latter are typically subject to
     * StartTLS for in-band encryption configuration). When for a particular connection type only one of these options
     * is implemented, the parameter value is ignored.
     *
     * @param type The connection type for which a listener is to be configured.
     * @param startInSslMode true when the listener to be configured is in legacy SSL mode, otherwise false.
     * @return The connection listener (never null).
     */
    public ConnectionListener getListener( ConnectionType type, boolean startInSslMode )
    {
        switch ( type )
        {
            case SOCKET_C2S:
                if (startInSslMode) {
                    return clientSslListener;
                } else {
                    return clientListener;
430 431
                }

432 433 434 435 436 437
            case BOSH_C2S:
                if (startInSslMode) {
                    return boshSslListener;
                } else {
                    return boshListener;
                }
438 439
            case SOCKET_S2S:
                return serverListener; // there's no legacy-mode server listener.
440

441 442 443 444 445 446
            case COMPONENT:
                if (startInSslMode) {
                    return componentSslListener;
                } else {
                    return componentListener;
                }
447

448 449 450 451 452
            case CONNECTION_MANAGER:
                if (startInSslMode) {
                    return connectionManagerSslListener;
                } else {
                    return connectionManagerListener;
453 454
                }

455 456 457 458 459 460
            case WEBADMIN:
                if (startInSslMode) {
                    return webAdminSslListener;
                } else {
                    return webAdminListener;
                }
461 462
            default:
                throw new IllegalStateException( "Unknown connection type: "+ type );
463 464 465
        }
    }

466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
    /**
     * Returns al connection listeners for the provided type.
     *
     * @param type The connection type for which a listener is to be configured.
     * @return The connection listener (never null).
     */
    public Set<ConnectionListener> getListeners( ConnectionType type )
    {
        final Set<ConnectionListener> result = new HashSet<>();
        switch ( type )
        {
            case SOCKET_C2S:
                result.add( clientListener );
                result.add( clientSslListener );
                break;
481 482 483
            case BOSH_C2S:
                result.add( boshListener );
                result.add( boshSslListener );
484 485 486 487 488 489 490 491 492 493 494 495 496 497

            case SOCKET_S2S:
                result.add( serverListener ); // there's no legacy-mode server listener.
                break;

            case COMPONENT:
                result.add( componentListener );
                result.add( componentSslListener );
                break;

            case CONNECTION_MANAGER:
                result.add( connectionManagerListener );
                result.add( connectionManagerSslListener );
                break;
498 499 500
            case WEBADMIN:
                result.add( webAdminListener );
                result.add( webAdminSslListener );
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573

            default:
                throw new IllegalStateException( "Unknown connection type: "+ type );
        }

        return result;
    }

    /**
     * Return if the configuration allows this listener to be enabled (but does not verify that the listener is
     * indeed active)
     *
     * The #startInSslMode parameter is used to distinguish between listeners that expect to receive SSL encrypted data
     * immediately, as opposed to connections that initially accept plain text data (the latter are typically subject to
     * StartTLS for in-band encryption configuration). When for a particular connection type only one of these options
     * is implemented, the parameter value is ignored.
     *
     * @param type The connection type for which a listener is to be configured.
     * @param startInSslMode true when the listener to be configured is in legacy SSL mode, otherwise false.
     * @return true if configuration allows this listener to be enabled, otherwise false.
     */
    public boolean isEnabled( ConnectionType type, boolean startInSslMode )
    {
        return getListener( type, startInSslMode ).isEnabled();
    }

    /**
     * Enables or disables a connection listener. Does nothing if the particular listener is already in the requested
     * state.
     *
     * The #startInSslMode parameter is used to distinguish between listeners that expect to receive SSL encrypted data
     * immediately, as opposed to connections that initially accept plain text data (the latter are typically subject to
     * StartTLS for in-band encryption configuration). When for a particular connection type only one of these options
     * is implemented, the parameter value is ignored.
     *
     * @param type The connection type for which a listener is to be configured.
     * @param startInSslMode true when the listener to be configured is in legacy SSL mode, otherwise false.
     * @param enabled true if the listener is to be enabled, otherwise false.
     */
    public void enable( ConnectionType type, boolean startInSslMode, boolean enabled )
    {
        getListener( type, startInSslMode ).enable( enabled );
    }

    /**
     * Retrieves the configured TCP port on which a listener accepts connections.
     *
     * @param type The connection type for which a listener is to be configured.
     * @param startInSslMode true when the listener to be configured is in legacy SSL mode, otherwise false.
     * @return a port number.
     */
    public int getPort( ConnectionType type, boolean startInSslMode )
    {
        return getListener( type, startInSslMode ).getPort();
    }

    /**
     * Sets the TCP port on which a listener accepts connections.
     *
     * @param type The connection type for which a listener is to be configured.
     * @param startInSslMode true when the listener to be configured is in legacy SSL mode, otherwise false.
     * @param port a port number.
     */
    public void setPort( ConnectionType type, boolean startInSslMode, int port )
    {
        getListener( type, startInSslMode ).setPort( port );
    }

    // TODO see if we can avoid exposing MINA internals.
    public NioSocketAcceptor getSocketAcceptor( ConnectionType type, boolean startInSslMode )
    {
        return getListener( type, startInSslMode ).getSocketAcceptor();
    }
574

575 576 577
    // #####################################################################
    // Certificates events
    // #####################################################################
578

579 580 581 582 583 584 585 586
    public void certificateCreated(KeyStore keyStore, String alias, X509Certificate cert) {
        // Note that all non-SSL listeners can be using TLS - these also need to be restarted.
        for ( final ConnectionListener listener : getListeners() )
        {
            // TODO determine by purpose exactly what needs and what need not be restarted.
            try
            {
                listener.restart();
587
            }
588 589 590
            catch ( RuntimeException ex )
            {
                Log.error( "An exception occurred while restarting listener " + listener + ". The reason for restart was a certificate store change.", ex );
591 592 593 594
            }
        }
    }

595 596 597 598 599 600 601 602
    public void certificateDeleted(KeyStore keyStore, String alias) {
        // Note that all non-SSL listeners can be using TLS - these also need to be restarted.
        for ( final ConnectionListener listener : getListeners() )
        {
            // TODO determine by purpose exactly what needs and what need not be restarted.
            try
            {
                listener.restart();
603
            }
604 605 606
            catch ( RuntimeException ex )
            {
                Log.error( "An exception occurred while restarting listener " + listener + ". The reason for restart was a certificate store change.", ex );
607 608 609 610
            }
        }
    }

611 612 613 614 615 616 617 618
    public void certificateSigned(KeyStore keyStore, String alias, List<X509Certificate> certificates) {
        // Note that all non-SSL listeners can be using TLS - these also need to be restarted.
        for ( final ConnectionListener listener : getListeners() )
        {
            // TODO determine by purpose exactly what needs and what need not be restarted.
            try
            {
                listener.restart();
619
            }
620 621 622
            catch ( RuntimeException ex )
            {
                Log.error( "An exception occurred while restarting listener " + listener + ". The reason for restart was a certificate store change.", ex );
623 624 625 626
            }
        }
    }

627 628 629 630 631 632
    // #####################################################################
    // Property events
    // #####################################################################
    @Override
    public void propertySet( String property, Map<String, Object> params ) {
        processPropertyValueChange( property, params );
633 634
    }

635 636 637
    @Override
    public void propertyDeleted( String property, Map<String, Object> params ) {
        processPropertyValueChange( property, params );
638 639 640
    }

    @Override
641 642
    public void xmlPropertySet( String property, Map<String, Object> params ) {
        processPropertyValueChange( property, params );
643 644 645
    }

    @Override
646 647
    public void xmlPropertyDeleted( String property, Map<String, Object> params ) {
        processPropertyValueChange( property, params );
648 649
    }

650 651 652 653 654 655 656
    private void processPropertyValueChange( String property, Map<String, Object> params ) {
        Log.debug( "Processing property value change for '"+property +"'. Params: " + params );

        // TODO there are more properties on which a restart is required (and this also applies to other listeners)!
        if ("xmpp.client.cert.policy".equalsIgnoreCase( property )) {
            clientSslListener.restart();
        }
657 658
    }

659 660 661 662
	// #####################################################################
    // Module management
    // #####################################################################

663
    @Override
664
    public void initialize(XMPPServer server) {
665
        super.initialize(server);
666

667 668 669 670 671 672 673 674 675
        // Check if we need to configure MINA to use Direct or Heap Buffers
        // Note: It has been reported that heap buffers are 50% faster than direct buffers
        if (JiveGlobals.getBooleanProperty("xmpp.socket.heapBuffer", true)) {
            IoBuffer.setUseDirectBuffer(false);
            IoBuffer.setAllocator(new SimpleBufferAllocator());
        }
    }

    @Override
676 677 678 679 680
	public void start() {
        super.start();
        startListeners();
        SocketSendingTracker.getInstance().start();
        CertificateManager.addListener(this);
681 682 683
    }

    @Override
684 685 686 687 688
	public void stop() {
        CertificateManager.removeListener(this);
        SocketSendingTracker.getInstance().shutdown();
        stopListeners();
        super.stop();
689 690
    }

691 692 693 694 695 696 697 698 699
    // #####################################################################
    // Deprecated delegation methods to individual listeners (as dictated by legacy API design).
    // #####################################################################

    // Client
    @Deprecated
    public void enableClientListener( boolean enabled )
    {
        enable( ConnectionType.SOCKET_C2S, false, enabled);
700 701
    }

702 703 704 705
    @Deprecated
    public boolean isClientListenerEnabled()
    {
        return isEnabled( ConnectionType.SOCKET_C2S, false );
706 707
    }

708 709 710 711
    @Deprecated
    public NioSocketAcceptor getSocketAcceptor()
    {
        return getSocketAcceptor( ConnectionType.SOCKET_C2S, false );
712 713
    }

714 715 716 717
    @Deprecated
    public void setClientListenerPort( int port )
    {
        setPort( ConnectionType.SOCKET_C2S, false, port );
718 719
    }

720 721 722 723
    @Deprecated
    public int getClientListenerPort()
    {
        return getPort( ConnectionType.SOCKET_C2S, false );
724 725
    }

726 727 728 729 730
    // Client in legacy mode
    @Deprecated
    public void enableClientSSLListener( boolean enabled )
    {
        enable( ConnectionType.SOCKET_C2S, true, enabled );
731 732
    }

733 734 735 736
    @Deprecated
    public boolean isClientSSLListenerEnabled()
    {
        return isEnabled( ConnectionType.SOCKET_C2S, true );
737 738
    }

739 740 741 742
    @Deprecated
    public NioSocketAcceptor getSSLSocketAcceptor()
    {
        return getSocketAcceptor( ConnectionType.SOCKET_C2S, true );
743 744
    }

745 746 747 748
    @Deprecated
    public void setClientSSLListenerPort( int port )
    {
        setPort( ConnectionType.SOCKET_C2S, true, port );
749 750
    }

751 752 753 754
    @Deprecated
    public int getClientSSLListenerPort()
    {
        return getPort( ConnectionType.SOCKET_C2S, true );
755 756
    }

757 758 759 760 761
    // Component
    @Deprecated
    public void enableComponentListener( boolean enabled )
    {
        enable( ConnectionType.COMPONENT, false, enabled );
762 763
    }

764 765 766 767
    @Deprecated
    public boolean isComponentListenerEnabled()
    {
        return isEnabled( ConnectionType.COMPONENT, false );
768 769
    }

770 771 772 773
    @Deprecated
    public NioSocketAcceptor getComponentAcceptor()
    {
        return getSocketAcceptor( ConnectionType.COMPONENT, false );
774 775
    }

776 777 778 779
    @Deprecated
    public void setComponentListenerPort( int port )
    {
        setPort( ConnectionType.COMPONENT, false, port );
780 781
    }

782 783 784 785
    @Deprecated
    public int getComponentListenerPort()
    {
        return getPort( ConnectionType.COMPONENT, false );
786 787
    }

788 789 790 791 792
    // Component in legacy mode
    @Deprecated
    public void enableComponentSslListener( boolean enabled )
    {
        enable( ConnectionType.COMPONENT, true, enabled );
793 794
    }

795 796 797 798
    @Deprecated
    public boolean isComponentSslListenerEnabled()
    {
        return isEnabled( ConnectionType.COMPONENT, true );
799 800
    }

801 802 803 804
    @Deprecated
    public NioSocketAcceptor getComponentSslAcceptor()
    {
        return getSocketAcceptor( ConnectionType.COMPONENT, true);
805 806
    }

807 808 809 810
    @Deprecated
    public void setComponentSslListenerPort( int port )
    {
        setPort( ConnectionType.COMPONENT, true, port );
811 812
    }

813 814 815 816
    @Deprecated
    public int getComponentSslListenerPort()
    {
        return getPort( ConnectionType.COMPONENT, true );
817 818
    }

819 820 821 822 823
    // Server
    @Deprecated
    public void enableServerListener( boolean enabled )
    {
        enable( ConnectionType.SOCKET_S2S, false, enabled );
824 825
    }

826 827 828 829
    @Deprecated
    public boolean isServerListenerEnabled()
    {
        return isEnabled( ConnectionType.SOCKET_S2S, false );
830 831
    }

832 833 834 835 836
    @Deprecated
    public NioSocketAcceptor getServerListenerSocketAcceptor()
    {
        return getSocketAcceptor( ConnectionType.SOCKET_S2S, false );
    }
837

838 839 840 841
    @Deprecated
    public void setServerListenerPort( int port )
    {
        setPort( ConnectionType.SOCKET_S2S, false, port );
842 843
    }

844 845 846 847
    @Deprecated
    public int getServerListenerPort()
    {
        return getPort( ConnectionType.SOCKET_S2S, false );
848 849
    }

850 851 852 853 854
    // Connection Manager
    @Deprecated
    public void enableConnectionManagerListener( boolean enabled )
    {
        enable( ConnectionType.CONNECTION_MANAGER, false, enabled );
855 856
    }

857 858 859 860
    @Deprecated
    public boolean isConnectionManagerListenerEnabled()
    {
        return isEnabled( ConnectionType.CONNECTION_MANAGER, false );
861 862
    }

863 864 865 866 867 868 869
    /**
     * @deprecated Replaced by #getConnectionManagerSocketAcceptor
     */
    @Deprecated
    public NioSocketAcceptor getMultiplexerSocketAcceptor()
    {
        return getSocketAcceptor( ConnectionType.CONNECTION_MANAGER, false );
870 871
    }

872 873 874 875
    @Deprecated
    public NioSocketAcceptor getConnectionManagerSocketAcceptor()
    {
        return getSocketAcceptor( ConnectionType.CONNECTION_MANAGER, false );
876 877
    }

878 879 880 881
    @Deprecated
    public void setConnectionManagerListenerPort( int port )
    {
        setPort( ConnectionType.CONNECTION_MANAGER, false, port );
882 883
    }

884 885 886 887 888
    @Deprecated
    public int getConnectionManagerListenerPort()
    {
        return getPort( ConnectionType.CONNECTION_MANAGER, false );
    }
889

890 891 892 893 894
    // Connection Manager in legacy mode
    @Deprecated
    public void enableConnectionManagerSslListener( boolean enabled )
    {
        enable( ConnectionType.CONNECTION_MANAGER, true, enabled );
895 896
    }

897 898 899 900 901
    @Deprecated
    public boolean isConnectionManagerSslListenerEnabled()
    {
        return isEnabled( ConnectionType.CONNECTION_MANAGER, true );
    }
902

903 904 905 906 907
    @Deprecated
    public NioSocketAcceptor getConnectionManagerSslSocketAcceptor()
    {
        return getSocketAcceptor( ConnectionType.CONNECTION_MANAGER, true );
    }
908

909 910 911 912
    @Deprecated
    public void setConnectionManagerSslListenerPort( int port )
    {
        setPort( ConnectionType.CONNECTION_MANAGER, true, port );
913 914
    }

915 916 917 918
    @Deprecated
    public int getConnectionManagerSslListenerPort()
    {
        return getPort( ConnectionType.CONNECTION_MANAGER, true );
919 920
    }

921 922 923
    // #####################################################################
    // Other deprecated implementations.
    // #####################################################################
924

925 926 927 928 929 930 931 932 933 934 935 936
    /**
     * @deprecated use #getListeners
     */
    @Deprecated
    public Collection<ServerPort> getPorts() {
        final Set<ServerPort> result = new LinkedHashSet<>();
        for ( ConnectionListener listener : getListeners() )
        {
            if (listener.getServerPort() != null)
            {
                result.add( listener.getServerPort() );
            }
937
        }
938 939
        return result;
    }
940
}