mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-12-22 20:47:57 +01:00
Make ProviderManager static
also remove initialize() from ProviderFileLoader, by simply putting the code into the constructor.
This commit is contained in:
parent
0111be1a04
commit
90ea9ba2fc
12 changed files with 46 additions and 96 deletions
|
@ -49,7 +49,7 @@ public abstract class UrlProviderFileInitializer implements SmackInitializer {
|
|||
if (is != null) {
|
||||
LOGGER.log(Level.INFO, "Loading providers for file [" + filePath + "]");
|
||||
ProviderFileLoader pfl = new ProviderFileLoader(is);
|
||||
ProviderManager.getInstance().addLoader(pfl);
|
||||
ProviderManager.addLoader(pfl);
|
||||
exceptions.addAll(pfl.getLoadingExceptions());
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -39,45 +39,14 @@ import org.xmlpull.v1.XmlPullParser;
|
|||
*/
|
||||
public class ProviderFileLoader implements ProviderLoader {
|
||||
private static final Logger LOGGER = Logger.getLogger(ProviderFileLoader.class.getName());
|
||||
|
||||
|
||||
private Collection<IQProviderInfo> iqProviders;
|
||||
private Collection<ExtensionProviderInfo> extProviders;
|
||||
private InputStream providerStream;
|
||||
private List<Exception> exceptions = new LinkedList<Exception>();
|
||||
|
||||
public ProviderFileLoader(InputStream providerFileInputStream) {
|
||||
setInputStream(providerFileInputStream);
|
||||
}
|
||||
|
||||
public ProviderFileLoader() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<IQProviderInfo> getIQProviderInfo() {
|
||||
initialize();
|
||||
return iqProviders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ExtensionProviderInfo> getExtensionProviderInfo() {
|
||||
initialize();
|
||||
return extProviders;
|
||||
}
|
||||
|
||||
public List<Exception> getLoadingExceptions() {
|
||||
return Collections.unmodifiableList(exceptions);
|
||||
}
|
||||
private List<Exception> exceptions = new LinkedList<Exception>();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private synchronized void initialize() {
|
||||
// Check to see if already initialized
|
||||
if (iqProviders != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (providerStream == null) {
|
||||
throw new IllegalArgumentException("No input stream set for loader");
|
||||
}
|
||||
public ProviderFileLoader(InputStream providerStream) {
|
||||
iqProviders = new ArrayList<IQProviderInfo>();
|
||||
extProviders = new ArrayList<ExtensionProviderInfo>();
|
||||
|
||||
|
@ -151,6 +120,7 @@ public class ProviderFileLoader implements ProviderLoader {
|
|||
}
|
||||
catch (Exception e){
|
||||
LOGGER.log(Level.SEVERE, "Unknown error occurred while parsing provider file", e);
|
||||
exceptions.add(e);
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
|
@ -161,12 +131,18 @@ public class ProviderFileLoader implements ProviderLoader {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setInputStream(InputStream providerFileInput) {
|
||||
if (providerFileInput == null) {
|
||||
throw new IllegalArgumentException("InputStream cannot be null");
|
||||
}
|
||||
providerStream = providerFileInput;
|
||||
initialize();
|
||||
|
||||
@Override
|
||||
public Collection<IQProviderInfo> getIQProviderInfo() {
|
||||
return iqProviders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ExtensionProviderInfo> getExtensionProviderInfo() {
|
||||
return extProviders;
|
||||
}
|
||||
|
||||
public List<Exception> getLoadingExceptions() {
|
||||
return Collections.unmodifiableList(exceptions);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -105,32 +105,10 @@ import org.jivesoftware.smack.packet.IQ;
|
|||
*/
|
||||
public final class ProviderManager {
|
||||
|
||||
private static ProviderManager instance;
|
||||
private static final Map<String, Object> extensionProviders = new ConcurrentHashMap<String, Object>();
|
||||
private static final Map<String, Object> iqProviders = new ConcurrentHashMap<String, Object>();
|
||||
|
||||
private Map<String, Object> extensionProviders = new ConcurrentHashMap<String, Object>();
|
||||
private Map<String, Object> iqProviders = new ConcurrentHashMap<String, Object>();
|
||||
|
||||
/**
|
||||
* Returns the ProviderManager instance.
|
||||
*
|
||||
* @return the only ProviderManager valid instance.
|
||||
*/
|
||||
public static synchronized ProviderManager getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new ProviderManager();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private ProviderManager() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void addLoader(ProviderLoader loader) {
|
||||
if (loader == null) {
|
||||
throw new IllegalArgumentException("loader cannot be null");
|
||||
}
|
||||
|
||||
public static void addLoader(ProviderLoader loader) {
|
||||
if (loader.getIQProviderInfo() != null) {
|
||||
for (IQProviderInfo info : loader.getIQProviderInfo()) {
|
||||
iqProviders.put(getProviderKey(info.getElementName(), info.getNamespace()), info.getProvider());
|
||||
|
@ -164,7 +142,7 @@ public final class ProviderManager {
|
|||
* @param namespace the XML namespace.
|
||||
* @return the IQ provider.
|
||||
*/
|
||||
public Object getIQProvider(String elementName, String namespace) {
|
||||
public static Object getIQProvider(String elementName, String namespace) {
|
||||
String key = getProviderKey(elementName, namespace);
|
||||
return iqProviders.get(key);
|
||||
}
|
||||
|
@ -176,7 +154,7 @@ public final class ProviderManager {
|
|||
*
|
||||
* @return all IQProvider instances.
|
||||
*/
|
||||
public Collection<Object> getIQProviders() {
|
||||
public static Collection<Object> getIQProviders() {
|
||||
return Collections.unmodifiableCollection(iqProviders.values());
|
||||
}
|
||||
|
||||
|
@ -189,7 +167,7 @@ public final class ProviderManager {
|
|||
* @param namespace the XML namespace.
|
||||
* @param provider the IQ provider.
|
||||
*/
|
||||
public void addIQProvider(String elementName, String namespace,
|
||||
public static void addIQProvider(String elementName, String namespace,
|
||||
Object provider)
|
||||
{
|
||||
if (!(provider instanceof IQProvider || (provider instanceof Class &&
|
||||
|
@ -210,7 +188,7 @@ public final class ProviderManager {
|
|||
* @param elementName the XML element name.
|
||||
* @param namespace the XML namespace.
|
||||
*/
|
||||
public void removeIQProvider(String elementName, String namespace) {
|
||||
public static void removeIQProvider(String elementName, String namespace) {
|
||||
String key = getProviderKey(elementName, namespace);
|
||||
iqProviders.remove(key);
|
||||
}
|
||||
|
@ -234,7 +212,7 @@ public final class ProviderManager {
|
|||
* @param namespace namespace associated with extension provider.
|
||||
* @return the extenion provider.
|
||||
*/
|
||||
public Object getExtensionProvider(String elementName, String namespace) {
|
||||
public static Object getExtensionProvider(String elementName, String namespace) {
|
||||
String key = getProviderKey(elementName, namespace);
|
||||
return extensionProviders.get(key);
|
||||
}
|
||||
|
@ -248,7 +226,7 @@ public final class ProviderManager {
|
|||
* @param namespace the XML namespace.
|
||||
* @param provider the extension provider.
|
||||
*/
|
||||
public void addExtensionProvider(String elementName, String namespace,
|
||||
public static void addExtensionProvider(String elementName, String namespace,
|
||||
Object provider)
|
||||
{
|
||||
if (!(provider instanceof PacketExtensionProvider || provider instanceof Class)) {
|
||||
|
@ -267,7 +245,7 @@ public final class ProviderManager {
|
|||
* @param elementName the XML element name.
|
||||
* @param namespace the XML namespace.
|
||||
*/
|
||||
public void removeExtensionProvider(String elementName, String namespace) {
|
||||
public static void removeExtensionProvider(String elementName, String namespace) {
|
||||
String key = getProviderKey(elementName, namespace);
|
||||
extensionProviders.remove(key);
|
||||
}
|
||||
|
@ -279,7 +257,7 @@ public final class ProviderManager {
|
|||
*
|
||||
* @return all PacketExtensionProvider instances.
|
||||
*/
|
||||
public Collection<Object> getExtensionProviders() {
|
||||
public static Collection<Object> getExtensionProviders() {
|
||||
return Collections.unmodifiableCollection(extensionProviders.values());
|
||||
}
|
||||
|
||||
|
@ -290,9 +268,7 @@ public final class ProviderManager {
|
|||
* @param namespace the namespace.
|
||||
* @return a unique key for the element name and namespace pair.
|
||||
*/
|
||||
private String getProviderKey(String elementName, String namespace) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<").append(elementName).append("/><").append(namespace).append("/>");
|
||||
return buf.toString();
|
||||
private static String getProviderKey(String elementName, String namespace) {
|
||||
return elementName + '#' + namespace;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -284,7 +284,7 @@ public class PacketParserUtils {
|
|||
// Otherwise, see if there is a registered provider for
|
||||
// this element name and namespace.
|
||||
else {
|
||||
Object provider = ProviderManager.getInstance().getIQProvider(elementName, namespace);
|
||||
Object provider = ProviderManager.getIQProvider(elementName, namespace);
|
||||
if (provider != null) {
|
||||
if (provider instanceof IQProvider) {
|
||||
iqPacket = ((IQProvider)provider).parseIQ(parser);
|
||||
|
@ -657,7 +657,7 @@ public class PacketParserUtils {
|
|||
throws Exception
|
||||
{
|
||||
// See if a provider is registered to handle the extension.
|
||||
Object provider = ProviderManager.getInstance().getExtensionProvider(elementName, namespace);
|
||||
Object provider = ProviderManager.getExtensionProvider(elementName, namespace);
|
||||
if (provider != null) {
|
||||
if (provider instanceof PacketExtensionProvider) {
|
||||
return ((PacketExtensionProvider)provider).parseExtension(parser);
|
||||
|
|
|
@ -31,7 +31,6 @@ import org.junit.Test;
|
|||
import org.xmlpull.v1.XmlPullParser;
|
||||
|
||||
public class ParsingExceptionTest {
|
||||
private final static ProviderManager PM = ProviderManager.getInstance();
|
||||
|
||||
private final static String EXTENSION2 =
|
||||
"<extension2 xmlns='namespace'>" +
|
||||
|
@ -43,12 +42,12 @@ public class ParsingExceptionTest {
|
|||
|
||||
@Before
|
||||
public void init() {
|
||||
PM.addExtensionProvider(ThrowException.ELEMENT, ThrowException.NAMESPACE, new ThrowException());
|
||||
ProviderManager.addExtensionProvider(ThrowException.ELEMENT, ThrowException.NAMESPACE, new ThrowException());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tini() {
|
||||
PM.removeExtensionProvider(ThrowException.ELEMENT, ThrowException.NAMESPACE);
|
||||
ProviderManager.removeExtensionProvider(ThrowException.ELEMENT, ThrowException.NAMESPACE);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -36,7 +36,7 @@ public class ProviderConfigTest {
|
|||
|
||||
@Test
|
||||
public void addGenericLoaderProvider() {
|
||||
ProviderManager.getInstance().addLoader(new ProviderLoader() {
|
||||
ProviderManager.addLoader(new ProviderLoader() {
|
||||
|
||||
@Override
|
||||
public Collection<IQProviderInfo> getIQProviderInfo() {
|
||||
|
@ -51,13 +51,13 @@ public class ProviderConfigTest {
|
|||
}
|
||||
});
|
||||
|
||||
Assert.assertNotNull(ProviderManager.getInstance().getIQProvider("provider", "test:provider"));
|
||||
Assert.assertNotNull(ProviderManager.getIQProvider("provider", "test:provider"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addClasspathFileLoaderProvider() throws Exception{
|
||||
ProviderManager.getInstance().addLoader(new ProviderFileLoader(FileUtils.getStreamForUrl("classpath:test.providers", null)));
|
||||
Assert.assertNotNull(ProviderManager.getInstance().getIQProvider("provider", "test:file_provider"));
|
||||
ProviderManager.addLoader(new ProviderFileLoader(FileUtils.getStreamForUrl("classpath:test.providers", null)));
|
||||
Assert.assertNotNull(ProviderManager.getIQProvider("provider", "test:file_provider"));
|
||||
}
|
||||
|
||||
public static class TestIQProvider implements IQProvider {
|
||||
|
|
|
@ -223,7 +223,7 @@ public class EnhancedDebuggerWindow {
|
|||
iqProvidersPanel.setLayout(new GridLayout(1, 1));
|
||||
iqProvidersPanel.setBorder(BorderFactory.createTitledBorder("Installed IQ Providers"));
|
||||
Vector<String> providers = new Vector<String>();
|
||||
for (Object provider : ProviderManager.getInstance().getIQProviders()) {
|
||||
for (Object provider : ProviderManager.getIQProviders()) {
|
||||
if (provider.getClass() == Class.class) {
|
||||
providers.add(((Class<?>) provider).getName());
|
||||
}
|
||||
|
@ -242,7 +242,7 @@ public class EnhancedDebuggerWindow {
|
|||
extensionProvidersPanel.setLayout(new GridLayout(1, 1));
|
||||
extensionProvidersPanel.setBorder(BorderFactory.createTitledBorder("Installed Extension Providers"));
|
||||
providers = new Vector<String>();
|
||||
for (Object provider : ProviderManager.getInstance().getExtensionProviders()) {
|
||||
for (Object provider : ProviderManager.getExtensionProviders()) {
|
||||
if (provider.getClass() == Class.class) {
|
||||
providers.add(((Class<?>) provider).getName());
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ public class CarbonTest {
|
|||
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
ProviderManager.getInstance().addExtensionProvider("forwarded", "urn:xmpp:forward:0", new ForwardedProvider());
|
||||
ProviderManager.addExtensionProvider("forwarded", "urn:xmpp:forward:0", new ForwardedProvider());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -54,7 +54,7 @@ public class ItemProvider implements PacketExtensionProvider
|
|||
String payloadElemName = parser.getName();
|
||||
String payloadNS = parser.getNamespace();
|
||||
|
||||
if (ProviderManager.getInstance().getExtensionProvider(payloadElemName, payloadNS) == null)
|
||||
if (ProviderManager.getExtensionProvider(payloadElemName, payloadNS) == null)
|
||||
{
|
||||
boolean done = false;
|
||||
boolean isEmptyElement = false;
|
||||
|
|
|
@ -253,8 +253,7 @@ public class JingleManager implements JingleSessionListener {
|
|||
* method you just call it once and all new connections will report Jingle support.)
|
||||
*/
|
||||
public static void setJingleServiceEnabled() {
|
||||
ProviderManager providerManager = ProviderManager.getInstance();
|
||||
providerManager.addIQProvider("jingle", "urn:xmpp:tmp:jingle", new JingleProvider());
|
||||
ProviderManager.addIQProvider("jingle", "urn:xmpp:tmp:jingle", new JingleProvider());
|
||||
|
||||
// Enable the Jingle support on every established connection
|
||||
// The ServiceDiscoveryManager class should have been already
|
||||
|
|
|
@ -81,7 +81,7 @@ public class RTPBridge extends IQ {
|
|||
public static final String NAMESPACE = "http://www.jivesoftware.com/protocol/rtpbridge";
|
||||
|
||||
static {
|
||||
ProviderManager.getInstance().addIQProvider(NAME, NAMESPACE, new Provider());
|
||||
ProviderManager.addIQProvider(NAME, NAMESPACE, new Provider());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -66,7 +66,7 @@ public class STUN extends IQ {
|
|||
public static final String NAMESPACE = "google:jingleinfo";
|
||||
|
||||
static {
|
||||
ProviderManager.getInstance().addIQProvider(ELEMENT_NAME, NAMESPACE, new STUN.Provider());
|
||||
ProviderManager.addIQProvider(ELEMENT_NAME, NAMESPACE, new STUN.Provider());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue