001/**
002 *
003 * Copyright © 2016 Fernando Ramirez
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.smackx.chat_markers;
018
019import java.util.Map;
020import java.util.WeakHashMap;
021
022import org.jivesoftware.smack.ConnectionCreationListener;
023import org.jivesoftware.smack.Manager;
024import org.jivesoftware.smack.SmackException.NoResponseException;
025import org.jivesoftware.smack.SmackException.NotConnectedException;
026import org.jivesoftware.smack.XMPPConnection;
027import org.jivesoftware.smack.XMPPConnectionRegistry;
028import org.jivesoftware.smack.XMPPException.XMPPErrorException;
029
030import org.jivesoftware.smackx.chat_markers.element.ChatMarkersElements;
031import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
032
033/**
034 * Chat Markers Manager class (XEP-0333).
035 * 
036 * @see <a href="http://xmpp.org/extensions/xep-0333.html">XEP-0333: Chat
037 *      Markers</a>
038 * @author Fernando Ramirez
039 * 
040 */
041public final class ChatMarkersManager extends Manager {
042
043    static {
044        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
045            @Override
046            public void connectionCreated(XMPPConnection connection) {
047                getInstanceFor(connection);
048            }
049        });
050    }
051
052    private static final Map<XMPPConnection, ChatMarkersManager> INSTANCES = new WeakHashMap<>();
053
054    /**
055     * Get the singleton instance of ChatMarkersManager.
056     * 
057     * @param connection
058     * @return the instance of ChatMarkersManager
059     */
060    public static synchronized ChatMarkersManager getInstanceFor(XMPPConnection connection) {
061        ChatMarkersManager chatMarkersManager = INSTANCES.get(connection);
062
063        if (chatMarkersManager == null) {
064            chatMarkersManager = new ChatMarkersManager(connection);
065            INSTANCES.put(connection, chatMarkersManager);
066        }
067
068        return chatMarkersManager;
069    }
070
071    private ChatMarkersManager(XMPPConnection connection) {
072        super(connection);
073    }
074
075    /**
076     * Returns true if Chat Markers is supported by the server.
077     * 
078     * @return true if Chat Markers is supported by the server.
079     * @throws NotConnectedException
080     * @throws XMPPErrorException
081     * @throws NoResponseException
082     * @throws InterruptedException
083     */
084    public boolean isSupportedByServer()
085            throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
086        return ServiceDiscoveryManager.getInstanceFor(connection())
087                .serverSupportsFeature(ChatMarkersElements.NAMESPACE);
088    }
089
090}