1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-24 15:22:07 +01:00

Rework FileUtils

This commit is contained in:
Florian Schmaus 2018-04-04 16:41:24 +02:00
parent 632c172f6d
commit 6a42d5baff
10 changed files with 57 additions and 61 deletions

View file

@ -46,7 +46,7 @@ import org.xmlpull.v1.XmlPullParserFactory;
public final class SmackInitialization { public final class SmackInitialization {
static final String SMACK_VERSION; 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()); private static final Logger LOGGER = Logger.getLogger(SmackInitialization.class.getName());
@ -61,7 +61,7 @@ public final class SmackInitialization {
static { static {
String smackVersion; String smackVersion;
try { 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(); smackVersion = reader.readLine();
try { try {
reader.close(); reader.close();
@ -109,7 +109,7 @@ public final class SmackInitialization {
InputStream configFileStream; InputStream configFileStream;
try { try {
configFileStream = FileUtils.getStreamForUrl(DEFAULT_CONFIG_FILE, null); configFileStream = FileUtils.getStreamForClasspathFile(DEFAULT_CONFIG_FILE, null);
} }
catch (Exception e) { catch (Exception e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2014 Florian Schmaus * Copyright 2014-2018 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,8 +16,8 @@
*/ */
package org.jivesoftware.smack.initializer; package org.jivesoftware.smack.initializer;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
@ -29,8 +29,8 @@ import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.util.FileUtils; import org.jivesoftware.smack.util.FileUtils;
/** /**
* Loads the provider file defined by the URL returned by {@link #getProvidersUrl()} and the generic * Loads the provider file defined by the URL returned by {@link #getProvidersUri()} and the generic
* smack configuration file returned {@link #getConfigUrl()}. * smack configuration file returned {@link #getConfigUri()}.
* *
* @author Florian Schmaus * @author Florian Schmaus
*/ */
@ -42,32 +42,27 @@ public abstract class UrlInitializer implements SmackInitializer {
InputStream is; InputStream is;
final ClassLoader classLoader = this.getClass().getClassLoader(); final ClassLoader classLoader = this.getClass().getClassLoader();
final List<Exception> exceptions = new LinkedList<Exception>(); final List<Exception> exceptions = new LinkedList<Exception>();
final String providerUrl = getProvidersUrl(); final String providerUriString = getProvidersUri();
if (providerUrl != null) { if (providerUriString != null) {
try { 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 providerUri [" + providerUri + "]");
LOGGER.log(Level.FINE, "Loading providers for providerUrl [" + providerUrl ProviderFileLoader pfl = new ProviderFileLoader(is, classLoader);
+ "]"); ProviderManager.addLoader(pfl);
ProviderFileLoader pfl = new ProviderFileLoader(is, classLoader); exceptions.addAll(pfl.getLoadingExceptions());
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));
}
} }
catch (Exception e) { 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); exceptions.add(e);
} }
} }
final String configUrl = getConfigUrl(); final String configUriString = getConfigUri();
if (configUrl != null) { if (configUriString != null) {
try { try {
is = FileUtils.getStreamForUrl(configUrl, classLoader); final URI configUri = URI.create(configUriString);
is = FileUtils.getStreamForUri(configUri, classLoader);
SmackInitialization.processConfigFile(is, exceptions, classLoader); SmackInitialization.processConfigFile(is, exceptions, classLoader);
} }
catch (Exception e) { catch (Exception e) {
@ -77,11 +72,11 @@ public abstract class UrlInitializer implements SmackInitializer {
return exceptions; return exceptions;
} }
protected String getProvidersUrl() { protected String getProvidersUri() {
return null; return null;
} }
protected String getConfigUrl() { protected String getConfigUri() {
return null; return null;
} }
} }

View file

@ -27,6 +27,7 @@ import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -37,31 +38,31 @@ public final class FileUtils {
private static final Logger LOGGER = Logger.getLogger(FileUtils.class.getName()); private static final Logger LOGGER = Logger.getLogger(FileUtils.class.getName());
public static InputStream getStreamForUrl(String url, ClassLoader loader) throws MalformedURLException, IOException { public static InputStream getStreamForClasspathFile(String path, ClassLoader loader) throws IOException {
URI fileUri = URI.create(url); // Get an array of class loaders to try loading the providers files from.
List<ClassLoader> classLoaders = getClassLoaders();
if (fileUri.getScheme() == null) { if (loader != null) {
throw new MalformedURLException("No protocol found in file URL: " + url); classLoaders.add(0, loader);
} }
for (ClassLoader classLoader : classLoaders) {
InputStream is = classLoader.getResourceAsStream(path);
if (fileUri.getScheme().equals("classpath")) { if (is != null) {
// Get an array of class loaders to try loading the providers files from. return is;
List<ClassLoader> classLoaders = getClassLoaders();
if (loader != null) {
classLoaders.add(0, loader);
}
for (ClassLoader classLoader : classLoaders) {
InputStream is = classLoader.getResourceAsStream(fileUri.getSchemeSpecificPart());
if (is != null) {
return is;
}
} }
} }
else { throw new IOException("Unable to get '" + path + "' from classpath. Tried ClassLoaders:" + classLoaders);
return fileUri.toURL().openStream(); }
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; return loaders;
} }
public static boolean addLines(String url, Set<String> set) throws MalformedURLException, IOException { public static boolean addLines(String uriString, Set<String> set) throws MalformedURLException, IOException {
InputStream is = getStreamForUrl(url, null); URI uri = URI.create(uriString);
if (is == null) return false; InputStream is = getStreamForUri(uri, null);
InputStreamReader sr = new InputStreamReader(is, StringUtils.UTF8); InputStreamReader sr = new InputStreamReader(is, StringUtils.UTF8);
BufferedReader br = new BufferedReader(sr); BufferedReader br = new BufferedReader(sr);
String line; String line;

View file

@ -55,7 +55,7 @@ public class ProviderConfigTest {
@Test @Test
public void addClasspathFileLoaderProvider() throws Exception { 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")); Assert.assertNotNull(ProviderManager.getIQProvider("provider", "test:file_provider"));
} }

View file

@ -26,12 +26,12 @@ import org.jivesoftware.smack.initializer.UrlInitializer;
public class ExperimentalInitializer extends UrlInitializer { public class ExperimentalInitializer extends UrlInitializer {
@Override @Override
protected String getProvidersUrl() { protected String getProvidersUri() {
return "classpath:org.jivesoftware.smack.experimental/experimental.providers"; return "classpath:org.jivesoftware.smack.experimental/experimental.providers";
} }
@Override @Override
protected String getConfigUrl() { protected String getConfigUri() {
return "classpath:org.jivesoftware.smack.experimental/experimental.xml"; return "classpath:org.jivesoftware.smack.experimental/experimental.xml";
} }
} }

View file

@ -26,12 +26,12 @@ import org.jivesoftware.smack.initializer.UrlInitializer;
public class ExtensionsInitializer extends UrlInitializer { public class ExtensionsInitializer extends UrlInitializer {
@Override @Override
protected String getProvidersUrl() { protected String getProvidersUri() {
return "classpath:org.jivesoftware.smack.extensions/extensions.providers"; return "classpath:org.jivesoftware.smack.extensions/extensions.providers";
} }
@Override @Override
protected String getConfigUrl() { protected String getConfigUri() {
return "classpath:org.jivesoftware.smack.extensions/extensions.xml"; return "classpath:org.jivesoftware.smack.extensions/extensions.xml";
} }
} }

View file

@ -21,12 +21,12 @@ import org.jivesoftware.smack.initializer.UrlInitializer;
public class SmackImInitializer extends UrlInitializer { public class SmackImInitializer extends UrlInitializer {
@Override @Override
protected String getProvidersUrl() { protected String getProvidersUri() {
return "classpath:org.jivesoftware.smack.im/smackim.providers"; return "classpath:org.jivesoftware.smack.im/smackim.providers";
} }
@Override @Override
protected String getConfigUrl() { protected String getConfigUri() {
return "classpath:org.jivesoftware.smack.im/smackim.xml"; return "classpath:org.jivesoftware.smack.im/smackim.xml";
} }

View file

@ -21,7 +21,7 @@ import org.jivesoftware.smack.initializer.UrlInitializer;
public class LegacyInitializer extends UrlInitializer { public class LegacyInitializer extends UrlInitializer {
@Override @Override
protected String getProvidersUrl() { protected String getProvidersUri() {
return "classpath:org.jivesoftware.smack.legacy/legacy.providers"; return "classpath:org.jivesoftware.smack.legacy/legacy.providers";
} }
} }

View file

@ -27,12 +27,12 @@ import org.jivesoftware.smack.initializer.UrlInitializer;
public class OmemoInitializer extends UrlInitializer { public class OmemoInitializer extends UrlInitializer {
@Override @Override
protected String getProvidersUrl() { protected String getProvidersUri() {
return "classpath:org.jivesoftware.smackx.omemo/omemo.providers"; return "classpath:org.jivesoftware.smackx.omemo/omemo.providers";
} }
@Override @Override
protected String getConfigUrl() { protected String getConfigUri() {
return "classpath:org.jivesoftware.smackx.omemo/omemo.xml"; return "classpath:org.jivesoftware.smackx.omemo/omemo.xml";
} }
} }

View file

@ -21,7 +21,7 @@ import org.jivesoftware.smack.initializer.UrlInitializer;
public class TCPInitializer extends UrlInitializer { public class TCPInitializer extends UrlInitializer {
@Override @Override
protected String getProvidersUrl() { protected String getProvidersUri() {
return "classpath:org.jivesoftware.smack.tcp/smacktcp.providers"; return "classpath:org.jivesoftware.smack.tcp/smacktcp.providers";
} }