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