diff --git a/smack-core/src/main/java/org/jivesoftware/smack/SmackInitialization.java b/smack-core/src/main/java/org/jivesoftware/smack/SmackInitialization.java index 3a8684cbe..1b9d18d21 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/SmackInitialization.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/SmackInitialization.java @@ -46,7 +46,7 @@ import org.xmlpull.v1.XmlPullParserFactory; public final class SmackInitialization { static final String SMACK_VERSION; - private static final String DEFAULT_CONFIG_FILE = "classpath:org.jivesoftware.smack/smack-config.xml"; + private static final String DEFAULT_CONFIG_FILE = "org.jivesoftware.smack/smack-config.xml"; private static final Logger LOGGER = Logger.getLogger(SmackInitialization.class.getName()); @@ -61,7 +61,7 @@ public final class SmackInitialization { static { String smackVersion; try { - BufferedReader reader = new BufferedReader(new InputStreamReader(FileUtils.getStreamForUrl("classpath:org.jivesoftware.smack/version", null), StringUtils.UTF8)); + BufferedReader reader = new BufferedReader(new InputStreamReader(FileUtils.getStreamForClasspathFile("org.jivesoftware.smack/version", null), StringUtils.UTF8)); smackVersion = reader.readLine(); try { reader.close(); @@ -109,7 +109,7 @@ public final class SmackInitialization { InputStream configFileStream; try { - configFileStream = FileUtils.getStreamForUrl(DEFAULT_CONFIG_FILE, null); + configFileStream = FileUtils.getStreamForClasspathFile(DEFAULT_CONFIG_FILE, null); } catch (Exception e) { throw new IllegalStateException(e); diff --git a/smack-core/src/main/java/org/jivesoftware/smack/initializer/UrlInitializer.java b/smack-core/src/main/java/org/jivesoftware/smack/initializer/UrlInitializer.java index 11e4f36ab..d58e7ee38 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/initializer/UrlInitializer.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/initializer/UrlInitializer.java @@ -1,6 +1,6 @@ /** * - * Copyright 2014 Florian Schmaus + * Copyright 2014-2018 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,8 @@ */ package org.jivesoftware.smack.initializer; -import java.io.IOException; import java.io.InputStream; +import java.net.URI; import java.util.LinkedList; import java.util.List; import java.util.logging.Level; @@ -29,8 +29,8 @@ import org.jivesoftware.smack.provider.ProviderManager; import org.jivesoftware.smack.util.FileUtils; /** - * Loads the provider file defined by the URL returned by {@link #getProvidersUrl()} and the generic - * smack configuration file returned {@link #getConfigUrl()}. + * Loads the provider file defined by the URL returned by {@link #getProvidersUri()} and the generic + * smack configuration file returned {@link #getConfigUri()}. * * @author Florian Schmaus */ @@ -42,32 +42,27 @@ public abstract class UrlInitializer implements SmackInitializer { InputStream is; final ClassLoader classLoader = this.getClass().getClassLoader(); final List exceptions = new LinkedList(); - final String providerUrl = getProvidersUrl(); - if (providerUrl != null) { + final String providerUriString = getProvidersUri(); + if (providerUriString != null) { try { - is = FileUtils.getStreamForUrl(providerUrl, classLoader); + final URI providerUri = URI.create(providerUriString); + is = FileUtils.getStreamForUri(providerUri, classLoader); - if (is != null) { - LOGGER.log(Level.FINE, "Loading providers for providerUrl [" + providerUrl - + "]"); - ProviderFileLoader pfl = new ProviderFileLoader(is, classLoader); - ProviderManager.addLoader(pfl); - exceptions.addAll(pfl.getLoadingExceptions()); - } - else { - LOGGER.log(Level.WARNING, "No input stream created for " + providerUrl); - exceptions.add(new IOException("No input stream created for " + providerUrl)); - } + LOGGER.log(Level.FINE, "Loading providers for providerUri [" + providerUri + "]"); + ProviderFileLoader pfl = new ProviderFileLoader(is, classLoader); + ProviderManager.addLoader(pfl); + exceptions.addAll(pfl.getLoadingExceptions()); } catch (Exception e) { - LOGGER.log(Level.SEVERE, "Error trying to load provider file " + providerUrl, e); + LOGGER.log(Level.SEVERE, "Error trying to load provider file " + providerUriString, e); exceptions.add(e); } } - final String configUrl = getConfigUrl(); - if (configUrl != null) { + final String configUriString = getConfigUri(); + if (configUriString != null) { try { - is = FileUtils.getStreamForUrl(configUrl, classLoader); + final URI configUri = URI.create(configUriString); + is = FileUtils.getStreamForUri(configUri, classLoader); SmackInitialization.processConfigFile(is, exceptions, classLoader); } catch (Exception e) { @@ -77,11 +72,11 @@ public abstract class UrlInitializer implements SmackInitializer { return exceptions; } - protected String getProvidersUrl() { + protected String getProvidersUri() { return null; } - protected String getConfigUrl() { + protected String getConfigUri() { return null; } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/FileUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/FileUtils.java index 3fc30ffe1..e86473ab0 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/FileUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/FileUtils.java @@ -27,6 +27,7 @@ import java.io.InputStreamReader; import java.io.Reader; import java.net.MalformedURLException; import java.net.URI; +import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -37,31 +38,31 @@ public final class FileUtils { private static final Logger LOGGER = Logger.getLogger(FileUtils.class.getName()); - public static InputStream getStreamForUrl(String url, ClassLoader loader) throws MalformedURLException, IOException { - URI fileUri = URI.create(url); - - if (fileUri.getScheme() == null) { - throw new MalformedURLException("No protocol found in file URL: " + url); + public static InputStream getStreamForClasspathFile(String path, ClassLoader loader) throws IOException { + // Get an array of class loaders to try loading the providers files from. + List classLoaders = getClassLoaders(); + if (loader != null) { + classLoaders.add(0, loader); } + for (ClassLoader classLoader : classLoaders) { + InputStream is = classLoader.getResourceAsStream(path); - if (fileUri.getScheme().equals("classpath")) { - // Get an array of class loaders to try loading the providers files from. - List classLoaders = getClassLoaders(); - if (loader != null) { - classLoaders.add(0, loader); - } - for (ClassLoader classLoader : classLoaders) { - InputStream is = classLoader.getResourceAsStream(fileUri.getSchemeSpecificPart()); - - if (is != null) { - return is; - } + if (is != null) { + return is; } } - else { - return fileUri.toURL().openStream(); + throw new IOException("Unable to get '" + path + "' from classpath. Tried ClassLoaders:" + classLoaders); + } + + public static InputStream getStreamForUri(URI uri, ClassLoader loader) throws IOException { + String protocol = uri.getScheme(); + if (protocol.equals("classpath")) { + String path = uri.getSchemeSpecificPart(); + return getStreamForClasspathFile(path, loader); } - return null; + + URL url = uri.toURL(); + return url.openStream(); } /** @@ -84,9 +85,9 @@ public final class FileUtils { return loaders; } - public static boolean addLines(String url, Set set) throws MalformedURLException, IOException { - InputStream is = getStreamForUrl(url, null); - if (is == null) return false; + public static boolean addLines(String uriString, Set set) throws MalformedURLException, IOException { + URI uri = URI.create(uriString); + InputStream is = getStreamForUri(uri, null); InputStreamReader sr = new InputStreamReader(is, StringUtils.UTF8); BufferedReader br = new BufferedReader(sr); String line; diff --git a/smack-core/src/test/java/org/jivesoftware/smack/provider/ProviderConfigTest.java b/smack-core/src/test/java/org/jivesoftware/smack/provider/ProviderConfigTest.java index 89e2b7093..64d0d3899 100644 --- a/smack-core/src/test/java/org/jivesoftware/smack/provider/ProviderConfigTest.java +++ b/smack-core/src/test/java/org/jivesoftware/smack/provider/ProviderConfigTest.java @@ -55,7 +55,7 @@ public class ProviderConfigTest { @Test public void addClasspathFileLoaderProvider() throws Exception { - ProviderManager.addLoader(new ProviderFileLoader(FileUtils.getStreamForUrl("classpath:test.providers", null))); + ProviderManager.addLoader(new ProviderFileLoader(FileUtils.getStreamForClasspathFile("test.providers", null))); Assert.assertNotNull(ProviderManager.getIQProvider("provider", "test:file_provider")); } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smack/experimental/ExperimentalInitializer.java b/smack-experimental/src/main/java/org/jivesoftware/smack/experimental/ExperimentalInitializer.java index 7509fc907..81161a429 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smack/experimental/ExperimentalInitializer.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smack/experimental/ExperimentalInitializer.java @@ -26,12 +26,12 @@ import org.jivesoftware.smack.initializer.UrlInitializer; public class ExperimentalInitializer extends UrlInitializer { @Override - protected String getProvidersUrl() { + protected String getProvidersUri() { return "classpath:org.jivesoftware.smack.experimental/experimental.providers"; } @Override - protected String getConfigUrl() { + protected String getConfigUri() { return "classpath:org.jivesoftware.smack.experimental/experimental.xml"; } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smack/extensions/ExtensionsInitializer.java b/smack-extensions/src/main/java/org/jivesoftware/smack/extensions/ExtensionsInitializer.java index db451137d..fe26056cf 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smack/extensions/ExtensionsInitializer.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smack/extensions/ExtensionsInitializer.java @@ -26,12 +26,12 @@ import org.jivesoftware.smack.initializer.UrlInitializer; public class ExtensionsInitializer extends UrlInitializer { @Override - protected String getProvidersUrl() { + protected String getProvidersUri() { return "classpath:org.jivesoftware.smack.extensions/extensions.providers"; } @Override - protected String getConfigUrl() { + protected String getConfigUri() { return "classpath:org.jivesoftware.smack.extensions/extensions.xml"; } } diff --git a/smack-im/src/main/java/org/jivesoftware/smack/im/SmackImInitializer.java b/smack-im/src/main/java/org/jivesoftware/smack/im/SmackImInitializer.java index 6a301f198..15ac50caf 100644 --- a/smack-im/src/main/java/org/jivesoftware/smack/im/SmackImInitializer.java +++ b/smack-im/src/main/java/org/jivesoftware/smack/im/SmackImInitializer.java @@ -21,12 +21,12 @@ import org.jivesoftware.smack.initializer.UrlInitializer; public class SmackImInitializer extends UrlInitializer { @Override - protected String getProvidersUrl() { + protected String getProvidersUri() { return "classpath:org.jivesoftware.smack.im/smackim.providers"; } @Override - protected String getConfigUrl() { + protected String getConfigUri() { return "classpath:org.jivesoftware.smack.im/smackim.xml"; } diff --git a/smack-legacy/src/main/java/org/jivesoftware/smack/legacy/LegacyInitializer.java b/smack-legacy/src/main/java/org/jivesoftware/smack/legacy/LegacyInitializer.java index 6e04b7486..5f40ae37b 100644 --- a/smack-legacy/src/main/java/org/jivesoftware/smack/legacy/LegacyInitializer.java +++ b/smack-legacy/src/main/java/org/jivesoftware/smack/legacy/LegacyInitializer.java @@ -21,7 +21,7 @@ import org.jivesoftware.smack.initializer.UrlInitializer; public class LegacyInitializer extends UrlInitializer { @Override - protected String getProvidersUrl() { + protected String getProvidersUri() { return "classpath:org.jivesoftware.smack.legacy/legacy.providers"; } } diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoInitializer.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoInitializer.java index cea430df6..5f293490f 100644 --- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoInitializer.java +++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoInitializer.java @@ -27,12 +27,12 @@ import org.jivesoftware.smack.initializer.UrlInitializer; public class OmemoInitializer extends UrlInitializer { @Override - protected String getProvidersUrl() { + protected String getProvidersUri() { return "classpath:org.jivesoftware.smackx.omemo/omemo.providers"; } @Override - protected String getConfigUrl() { + protected String getConfigUri() { return "classpath:org.jivesoftware.smackx.omemo/omemo.xml"; } } diff --git a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/TCPInitializer.java b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/TCPInitializer.java index 1d70c39c8..7ecf5b4db 100644 --- a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/TCPInitializer.java +++ b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/TCPInitializer.java @@ -21,7 +21,7 @@ import org.jivesoftware.smack.initializer.UrlInitializer; public class TCPInitializer extends UrlInitializer { @Override - protected String getProvidersUrl() { + protected String getProvidersUri() { return "classpath:org.jivesoftware.smack.tcp/smacktcp.providers"; }