001/**
002 *
003 * Copyright 2017 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.smackx.eme;
018
019import java.util.Map;
020import java.util.WeakHashMap;
021
022import org.jivesoftware.smack.ConnectionCreationListener;
023import org.jivesoftware.smack.XMPPConnection;
024import org.jivesoftware.smack.XMPPConnectionRegistry;
025
026import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
027import org.jivesoftware.smackx.eme.element.ExplicitMessageEncryptionElement;
028
029public final class ExplicitMessageEncryptionManager {
030
031    private static final Map<XMPPConnection, ExplicitMessageEncryptionManager> INSTANCES = new WeakHashMap<>();
032
033    static {
034        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
035            @Override
036            public void connectionCreated(XMPPConnection connection) {
037                getInstanceFor(connection);
038            }
039        });
040    }
041
042    public static final String NAMESPACE_V0 = ExplicitMessageEncryptionElement.NAMESPACE;
043
044    public static synchronized ExplicitMessageEncryptionManager getInstanceFor(XMPPConnection connection) {
045        ExplicitMessageEncryptionManager manager = INSTANCES.get(connection);
046        if (manager == null) {
047            manager = new ExplicitMessageEncryptionManager(connection);
048            INSTANCES.put(connection, manager);
049        }
050        return manager;
051    }
052
053    private ExplicitMessageEncryptionManager(XMPPConnection connection) {
054        ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
055        sdm.addFeature(NAMESPACE_V0);
056    }
057
058}