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 {
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);

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");
* 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<Exception> exceptions = new LinkedList<Exception>();
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;
}
}

View file

@ -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<ClassLoader> 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<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;
}
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<String> set) throws MalformedURLException, IOException {
InputStream is = getStreamForUrl(url, null);
if (is == null) return false;
public static boolean addLines(String uriString, Set<String> 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;

View file

@ -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"));
}

View file

@ -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";
}
}

View file

@ -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";
}
}

View file

@ -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";
}

View file

@ -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";
}
}

View file

@ -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";
}
}

View file

@ -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";
}