ExplicitMessageEncryptionManager.java

  1. /**
  2.  *
  3.  * Copyright 2017 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.eme;

  18. import java.util.Map;
  19. import java.util.WeakHashMap;

  20. import org.jivesoftware.smack.ConnectionCreationListener;
  21. import org.jivesoftware.smack.XMPPConnection;
  22. import org.jivesoftware.smack.XMPPConnectionRegistry;

  23. import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
  24. import org.jivesoftware.smackx.eme.element.ExplicitMessageEncryptionElement;

  25. public final class ExplicitMessageEncryptionManager {

  26.     private static final Map<XMPPConnection, ExplicitMessageEncryptionManager> INSTANCES = new WeakHashMap<>();

  27.     static {
  28.         XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
  29.             @Override
  30.             public void connectionCreated(XMPPConnection connection) {
  31.                 getInstanceFor(connection);
  32.             }
  33.         });
  34.     }

  35.     public static final String NAMESPACE_V0 = ExplicitMessageEncryptionElement.NAMESPACE;

  36.     public static synchronized ExplicitMessageEncryptionManager getInstanceFor(XMPPConnection connection) {
  37.         ExplicitMessageEncryptionManager manager = INSTANCES.get(connection);
  38.         if (manager == null) {
  39.             manager = new ExplicitMessageEncryptionManager(connection);
  40.             INSTANCES.put(connection, manager);
  41.         }
  42.         return manager;
  43.     }

  44.     private ExplicitMessageEncryptionManager(XMPPConnection connection) {
  45.         ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
  46.         sdm.addFeature(NAMESPACE_V0);
  47.     }

  48. }