You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/09/15 14:13:45 UTC
[commons-configuration] branch master updated: Better exception
message and refactor common calls into private methods.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-configuration.git
The following commit(s) were added to refs/heads/master by this push:
new 828e45e Better exception message and refactor common calls into private methods.
828e45e is described below
commit 828e45e662064d5148e1e9b6f7ce4dfc259b7fe6
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Sep 15 10:13:37 2020 -0400
Better exception message and refactor common calls into private methods.
---
.../commons/configuration2/io/VFSFileSystem.java | 265 +++++++++++----------
1 file changed, 136 insertions(+), 129 deletions(-)
diff --git a/src/main/java/org/apache/commons/configuration2/io/VFSFileSystem.java b/src/main/java/org/apache/commons/configuration2/io/VFSFileSystem.java
index cf58397..e69697c 100644
--- a/src/main/java/org/apache/commons/configuration2/io/VFSFileSystem.java
+++ b/src/main/java/org/apache/commons/configuration2/io/VFSFileSystem.java
@@ -43,16 +43,74 @@ import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.provider.UriParser;
/**
- * FileSystem that uses Commons VFS
+ * FileSystem that uses <a href="https://commons.apache.org/proper/commons-vfs/">Apache Commons VFS</a>.
+ *
* @since 1.7
*/
public class VFSFileSystem extends DefaultFileSystem
{
+ /**
+ * Stream handler required to create URL.
+ */
+ private static class VFSURLStreamHandler extends URLStreamHandler
+ {
+ /** The Protocol used */
+ private final String protocol;
+
+ public VFSURLStreamHandler(final FileName file)
+ {
+ this.protocol = file.getScheme();
+ }
+
+ @Override
+ protected URLConnection openConnection(final URL url) throws IOException
+ {
+ throw new IOException("VFS URLs can only be used with VFS APIs");
+ }
+ }
+
/** The logger. */
private final Log log = LogFactory.getLog(getClass());
public VFSFileSystem()
{
+ // empty
+ }
+
+ @Override
+ public String getBasePath(final String path)
+ {
+ if (UriParser.extractScheme(path) == null)
+ {
+ return super.getBasePath(path);
+ }
+ try
+ {
+ return resolveURI(path).getParent().getURI();
+ }
+ catch (final FileSystemException fse)
+ {
+ fse.printStackTrace();
+ return null;
+ }
+ }
+
+ @Override
+ public String getFileName(final String path)
+ {
+ if (UriParser.extractScheme(path) == null)
+ {
+ return super.getFileName(path);
+ }
+ try
+ {
+ return resolveURI(path).getBaseName();
+ }
+ catch (final FileSystemException fse)
+ {
+ fse.printStackTrace();
+ return null;
+ }
}
@Override
@@ -62,8 +120,11 @@ public class VFSFileSystem extends DefaultFileSystem
try
{
final FileSystemOptions opts = getOptions(url.getProtocol());
- file = opts == null ? VFS.getManager().resolveFile(url.toString())
- : VFS.getManager().resolveFile(url.toString(), opts);
+ file = opts == null ? resolveFile(url.toString()) : getManager().resolveFile(url.toString(), opts);
+ if (!file.exists())
+ {
+ throw new ConfigurationException("File not found");
+ }
if (file.getType() != FileType.FILE)
{
throw new ConfigurationException("Cannot load a configuration from a directory");
@@ -83,15 +144,66 @@ public class VFSFileSystem extends DefaultFileSystem
}
}
+ private FileSystemManager getManager() throws FileSystemException {
+ return VFS.getManager();
+ }
+
+ private FileSystemOptions getOptions(final String scheme)
+ {
+ final FileSystemOptions opts = new FileSystemOptions();
+ FileSystemConfigBuilder builder;
+ try
+ {
+ builder = getManager().getFileSystemConfigBuilder(scheme);
+ }
+ catch (final Exception ex)
+ {
+ return null;
+ }
+ final FileOptionsProvider provider = getFileOptionsProvider();
+ if (provider != null)
+ {
+ final Map<String, Object> map = provider.getOptions();
+ if (map == null)
+ {
+ return null;
+ }
+ int count = 0;
+ for (final Map.Entry<String, Object> entry : map.entrySet())
+ {
+ try
+ {
+ String key = entry.getKey();
+ if (FileOptionsProvider.CURRENT_USER.equals(key))
+ {
+ key = "creatorName";
+ }
+ setProperty(builder, opts, key, entry.getValue());
+ ++count;
+ }
+ catch (final Exception ex)
+ {
+ // Ignore an incorrect property.
+ continue;
+ }
+ }
+ if (count > 0)
+ {
+ return opts;
+ }
+ }
+ return null;
+
+ }
+
@Override
public OutputStream getOutputStream(final URL url) throws ConfigurationException
{
try
{
final FileSystemOptions opts = getOptions(url.getProtocol());
- final FileSystemManager fsManager = VFS.getManager();
- final FileObject file = opts == null ? fsManager.resolveFile(url.toString())
- : fsManager.resolveFile(url.toString(), opts);
+ final FileObject file = opts == null ? resolveFile(url.toString())
+ : getManager().resolveFile(url.toString(), opts);
// throw an exception if the target URL is a directory
if (file == null || file.getType() == FileType.FOLDER)
{
@@ -120,10 +232,9 @@ public class VFSFileSystem extends DefaultFileSystem
}
try
{
- final FileSystemManager fsManager = VFS.getManager();
if (url != null)
{
- final FileName name = fsManager.resolveURI(url.toString());
+ final FileName name = resolveURI(url.toString());
if (name != null)
{
return name.toString();
@@ -136,14 +247,14 @@ public class VFSFileSystem extends DefaultFileSystem
}
else if (basePath != null)
{
- final FileName base = fsManager.resolveURI(basePath);
- return fsManager.resolveName(base, fileName).getURI();
+ final FileName base = resolveURI(basePath);
+ return getManager().resolveName(base, fileName).getURI();
}
else
{
- final FileName name = fsManager.resolveURI(fileName);
+ final FileName name = resolveURI(fileName);
final FileName base = name.getParent();
- return fsManager.resolveName(base, name.getBaseName()).getURI();
+ return getManager().resolveName(base, name.getBaseName()).getURI();
}
}
catch (final FileSystemException fse)
@@ -154,46 +265,6 @@ public class VFSFileSystem extends DefaultFileSystem
}
@Override
- public String getBasePath(final String path)
- {
- if (UriParser.extractScheme(path) == null)
- {
- return super.getBasePath(path);
- }
- try
- {
- final FileSystemManager fsManager = VFS.getManager();
- final FileName name = fsManager.resolveURI(path);
- return name.getParent().getURI();
- }
- catch (final FileSystemException fse)
- {
- fse.printStackTrace();
- return null;
- }
- }
-
- @Override
- public String getFileName(final String path)
- {
- if (UriParser.extractScheme(path) == null)
- {
- return super.getFileName(path);
- }
- try
- {
- final FileSystemManager fsManager = VFS.getManager();
- final FileName name = fsManager.resolveURI(path);
- return name.getBaseName();
- }
- catch (final FileSystemException fse)
- {
- fse.printStackTrace();
- return null;
- }
- }
-
- @Override
public URL getURL(final String basePath, final String file) throws MalformedURLException
{
if ((basePath != null && UriParser.extractScheme(basePath) == null)
@@ -203,17 +274,15 @@ public class VFSFileSystem extends DefaultFileSystem
}
try
{
- final FileSystemManager fsManager = VFS.getManager();
-
FileName path;
if (basePath != null && UriParser.extractScheme(file) == null)
{
- final FileName base = fsManager.resolveURI(basePath);
- path = fsManager.resolveName(base, file);
+ final FileName base = resolveURI(basePath);
+ path = getManager().resolveName(base, file);
}
else
{
- path = fsManager.resolveURI(file);
+ path = resolveURI(file);
}
final URLStreamHandler handler = new VFSURLStreamHandler(path);
@@ -238,28 +307,26 @@ public class VFSFileSystem extends DefaultFileSystem
}
try
{
- final FileSystemManager fsManager = VFS.getManager();
-
FileObject file;
// Only use the base path if the file name doesn't have a scheme.
if (basePath != null && fileScheme == null)
{
final String scheme = UriParser.extractScheme(basePath);
final FileSystemOptions opts = scheme != null ? getOptions(scheme) : null;
- FileObject base = opts == null ? fsManager.resolveFile(basePath)
- : fsManager.resolveFile(basePath, opts);
+ FileObject base = opts == null ? resolveFile(basePath)
+ : getManager().resolveFile(basePath, opts);
if (base.getType() == FileType.FILE)
{
base = base.getParent();
}
- file = fsManager.resolveFile(base, fileName);
+ file = getManager().resolveFile(base, fileName);
}
else
{
final FileSystemOptions opts = fileScheme != null ? getOptions(fileScheme) : null;
- file = opts == null ? fsManager.resolveFile(fileName)
- : fsManager.resolveFile(fileName, opts);
+ file = opts == null ? resolveFile(fileName)
+ : getManager().resolveFile(fileName, opts);
}
if (!file.exists())
@@ -280,52 +347,12 @@ public class VFSFileSystem extends DefaultFileSystem
}
}
- private FileSystemOptions getOptions(final String scheme)
- {
- final FileSystemOptions opts = new FileSystemOptions();
- FileSystemConfigBuilder builder;
- try
- {
- builder = VFS.getManager().getFileSystemConfigBuilder(scheme);
- }
- catch (final Exception ex)
- {
- return null;
- }
- final FileOptionsProvider provider = getFileOptionsProvider();
- if (provider != null)
- {
- final Map<String, Object> map = provider.getOptions();
- if (map == null)
- {
- return null;
- }
- int count = 0;
- for (final Map.Entry<String, Object> entry : map.entrySet())
- {
- try
- {
- String key = entry.getKey();
- if (FileOptionsProvider.CURRENT_USER.equals(key))
- {
- key = "creatorName";
- }
- setProperty(builder, opts, key, entry.getValue());
- ++count;
- }
- catch (final Exception ex)
- {
- // Ignore an incorrect property.
- continue;
- }
- }
- if (count > 0)
- {
- return opts;
- }
- }
- return null;
+ private FileObject resolveFile(final String basePath) throws FileSystemException {
+ return getManager().resolveFile(basePath);
+ }
+ private FileName resolveURI(final String path) throws FileSystemException {
+ return getManager().resolveURI(path);
}
private void setProperty(final FileSystemConfigBuilder builder, final FileSystemOptions options,
@@ -350,24 +377,4 @@ public class VFSFileSystem extends DefaultFileSystem
}
}
-
- /**
- * Stream handler required to create URL.
- */
- private static class VFSURLStreamHandler extends URLStreamHandler
- {
- /** The Protocol used */
- private final String protocol;
-
- public VFSURLStreamHandler(final FileName file)
- {
- this.protocol = file.getScheme();
- }
-
- @Override
- protected URLConnection openConnection(final URL url) throws IOException
- {
- throw new IOException("VFS URLs can only be used with VFS APIs");
- }
- }
}