001/**
002 *
003 * Copyright 2014-2018 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack;
018
019import java.util.Collection;
020import java.util.Collections;
021import java.util.Set;
022import java.util.concurrent.CopyOnWriteArraySet;
023
024public class XMPPConnectionRegistry {
025
026    /**
027     * A set of listeners which will be invoked if a new connection is created.
028     */
029    private static final Set<ConnectionCreationListener> connectionEstablishedListeners =
030            new CopyOnWriteArraySet<>();
031
032    /**
033     * Adds a new listener that will be notified when new Connections are created. Note
034     * that newly created connections will not be actually connected to the server.
035     *
036     * @param connectionCreationListener a listener interested on new connections.
037     */
038    public static void addConnectionCreationListener(
039            ConnectionCreationListener connectionCreationListener) {
040        connectionEstablishedListeners.add(connectionCreationListener);
041    }
042
043    /**
044     * Removes a listener that was interested in connection creation events.
045     *
046     * @param connectionCreationListener a listener interested on new connections.
047     */
048    public static void removeConnectionCreationListener(
049            ConnectionCreationListener connectionCreationListener) {
050        connectionEstablishedListeners.remove(connectionCreationListener);
051    }
052
053
054
055    /**
056     * Get the collection of listeners that are interested in connection creation events.
057     *
058     * @return a collection of listeners interested on new connections.
059     */
060    protected static Collection<ConnectionCreationListener> getConnectionCreationListeners() {
061        return Collections.unmodifiableCollection(connectionEstablishedListeners);
062    }
063
064}