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