diff --git a/resources/api/src/main/java/org/jboss/forge/addon/resource/AbstractFileResource.java b/resources/api/src/main/java/org/jboss/forge/addon/resource/AbstractFileResource.java deleted file mode 100644 index effa207c99..0000000000 --- a/resources/api/src/main/java/org/jboss/forge/addon/resource/AbstractFileResource.java +++ /dev/null @@ -1,382 +0,0 @@ -package org.jboss.forge.addon.resource; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.charset.Charset; - -import org.jboss.forge.addon.resource.monitor.ResourceMonitor; -import org.jboss.forge.furnace.util.Assert; -import org.jboss.forge.furnace.util.OperatingSystemUtils; -import org.jboss.forge.furnace.util.Streams; - -/** - * A standard, built-in resource for representing files on the filesystem. - * - * @author Lincoln Baxter, III - * @author George Gastaldi - */ -public abstract class AbstractFileResource> extends AbstractResource implements - FileResource -{ - private File file; - private long lastModification = -1; - - protected AbstractFileResource(final ResourceFactory factory, final File file) - { - super(factory, null); - this.file = file; - } - - @Override - public boolean create() - { - return createNewFile(); - } - - @Override - public String getName() - { - return file.getName(); - } - - @Override - public String toString() - { - return getFullyQualifiedName(); - } - - @Override - public File getUnderlyingResourceObject() - { - return file; - } - - @Override - public InputStream getResourceInputStream() - { - return getFileOperations().createInputStream(file); - } - - @Override - public DirectoryResource getParent() - { - return file.getParentFile() != null ? getResourceFactory().create(DirectoryResource.class, file.getParentFile()) - : null; - } - - @Override - public Resource getChild(final String name) - { - return null; - } - - /** - * Create a new {@link Resource} instance for the target file. The new {@link Resource} should be of the same type as - * this. - * - * @param file The file to create the resource instance from. - * @return A new resource. - */ - @Override - public abstract Resource createFrom(File file); - - @Override - public boolean exists() - { - return getFileOperations().exists(file); - } - - @Override - public boolean isDirectory() - { - return getFileOperations().existsAndIsDirectory(file); - } - - @Override - public boolean isStale() - { - return lastModification != getUnderlyingResourceObject().lastModified(); - } - - @Override - public void refresh() - { - lastModification = getUnderlyingResourceObject().lastModified(); - } - - @Override - public boolean mkdir() - { - return getFileOperations().mkdir(file); - } - - @Override - public boolean mkdirs() - { - return getFileOperations().mkdirs(file); - } - - @Override - public boolean delete() - { - return getFileOperations().delete(file); - } - - @Override - public boolean delete(final boolean recursive) - { - if (recursive) - { - if (_deleteRecursive(file, true)) - { - return true; - } - return false; - } - - File[] listFiles = getFileOperations().listChildren(file); - if ((listFiles != null) && (listFiles.length != 0)) - { - throw new RuntimeException("directory not empty"); - } - - if (OperatingSystemUtils.isWindows()) - { - System.gc(); // ensure no lingering handles that would prevent deletion - } - - if (getFileOperations().delete(file)) - { - return true; - } - return false; - } - - @Override - public void deleteOnExit() - { - getFileOperations().deleteOnExit(file); - } - - private boolean _deleteRecursive(final File file, final boolean collect) - { - if (collect && OperatingSystemUtils.isWindows()) - { - System.gc(); // ensure no lingering handles that would prevent deletion - } - - if (file == null) - { - return false; - } - - File[] children = getFileOperations().listChildren(file); - if (children != null) - { - for (File sf : children) - { - if (getFileOperations().existsAndIsDirectory(sf)) - { - _deleteRecursive(sf, false); - } - else - { - if (!getFileOperations().delete(sf)) - { - throw new RuntimeException("failed to delete: " + sf.getAbsolutePath()); - } - } - } - } - - return getFileOperations().delete(file); - } - - @Override - public T setContents(String data) - { - if (data == null) - { - data = ""; - } - return setContents(data.toCharArray()); - } - - @Override - public T setContents(String data, Charset charset) - { - if (data == null) - { - data = ""; - } - return setContents(data.toCharArray(), charset); - } - - @Override - public T setContents(char[] data, Charset charset) - { - return setContents(new ByteArrayInputStream(new String(data).getBytes(charset))); - } - - @Override - public T setContents(final char[] data) - { - return setContents(new ByteArrayInputStream(new String(data).getBytes())); - } - - @Override - @SuppressWarnings("unchecked") - public T setContents(final InputStream data) - { - Assert.notNull(data, "InputStream must not be null."); - - try - { - if (!exists()) - { - getParent().mkdirs(); - if (!createNewFile()) - { - throw new IOException("Failed to create file: " + file); - } - } - - OutputStream out = getFileOperations().createOutputStream(file); - try - { - Streams.write(data, out); - } - finally - { - Streams.closeQuietly(data); - out.flush(); - Streams.closeQuietly(out); - if (OperatingSystemUtils.isWindows()) - { - System.gc(); - } - } - } - catch (IOException e) - { - throw new ResourceException("Error while setting the contents", e); - } - return (T) this; - } - - @Override - public boolean createNewFile() - { - getParent().mkdirs(); - if (getFileOperations().create(file)) - { - return true; - } - return false; - } - - @Override - @SuppressWarnings("unchecked") - public T createTempResource() - { - try - { - T result = (T) createFrom(File.createTempFile("forgetemp", "")); - return result; - } - catch (IOException e) - { - throw new ResourceException("Error while creating a temporary resource", e); - } - } - - @Override - public boolean renameTo(final String pathspec) - { - return renameTo(new File(pathspec)); - } - - @Override - public boolean renameTo(final FileResource target) - { - return renameTo(target.getUnderlyingResourceObject()); - } - - private boolean renameTo(final File target) - { - if (getFileOperations().rename(file, target)) - { - file = target; - return true; - } - return false; - } - - @Override - public long getSize() - { - return getFileOperations().getLength(file); - } - - @Override - public boolean isExecutable() - { - return (this.file.canExecute() && !getFileOperations().existsAndIsDirectory(file)); - } - - @Override - public boolean isReadable() - { - return (this.file.canRead() && !getFileOperations().existsAndIsDirectory(file)); - } - - @Override - public boolean isWritable() - { - return (this.file.canWrite() && !getFileOperations().existsAndIsDirectory(file)); - } - - @Override - public String getFullyQualifiedName() - { - return this.file.getAbsolutePath(); - } - - @Override - public ResourceMonitor monitor() - { - return getResourceFactory().monitor(this); - } - - @Override - public ResourceMonitor monitor(ResourceFilter filter) - { - return getResourceFactory().monitor(this, filter); - } - - protected ResourceOperations getFileOperations() - { - return getResourceFactory(). getResourceOperations(File.class); - } - - @Override - public long getLastModified() - { - return file.lastModified(); - } - - @Override - public void setLastModified(long time) - { - file.setLastModified(time); - } - - @Override - public OutputStream getResourceOutputStream() - { - return getFileOperations().createOutputStream(file); - } -} \ No newline at end of file diff --git a/resources/api/src/main/java/org/jboss/forge/addon/resource/CreatableResource.java b/resources/api/src/main/java/org/jboss/forge/addon/resource/CreatableResource.java index b130b2d4a6..d514a2d969 100644 --- a/resources/api/src/main/java/org/jboss/forge/addon/resource/CreatableResource.java +++ b/resources/api/src/main/java/org/jboss/forge/addon/resource/CreatableResource.java @@ -16,10 +16,16 @@ */ public interface CreatableResource, R> extends Resource { + /** - * Create this {@link Resource} in the underlying resource system. Necessary parent paths will be created + * Create this leaf {@link Resource} in the underlying resource system. Necessary parent paths will be created * automatically. */ - boolean create(); + boolean createLeaf(); + /** + * Create this container {@link Resource} in the underlying resource system. Necessary parent paths will be created + * automatically. + */ + boolean createContainer(); } \ No newline at end of file diff --git a/resources/api/src/main/java/org/jboss/forge/addon/resource/DirectoryResource.java b/resources/api/src/main/java/org/jboss/forge/addon/resource/DirectoryResource.java deleted file mode 100644 index 815252b274..0000000000 --- a/resources/api/src/main/java/org/jboss/forge/addon/resource/DirectoryResource.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2012 Red Hat, Inc. and/or its affiliates. - * - * Licensed under the Eclipse Public License version 1.0, available at - * http://www.eclipse.org/legal/epl-v10.html - */ - -package org.jboss.forge.addon.resource; - - -/** - * A standard, build-in, resource for representing directories on the file-system. - * - * @author Mike Brock - * @author Lincoln Baxter, III - */ -public interface DirectoryResource extends FileResource -{ - /** - * Obtain a reference to the child {@link DirectoryResource}. If that resource does not exist, return a new instance. - * If the resource exists and is not a {@link DirectoryResource}, throw {@link ResourceException} - */ - public DirectoryResource getChildDirectory(final String name) throws ResourceException; - - /** - * Obtain a reference to the child {@link DirectoryResource}. If that resource does not exist, return a new instance - * and attempt to create the a directory of the given name. If the resource exists and is not a - * {@link DirectoryResource}, throw {@link ResourceException} - */ - public DirectoryResource getOrCreateChildDirectory(String name); - - /** - * Using the given type, obtain a reference to the child resource of the given type. If the result is not of the - * requested type and does not exist, return null. If the result is not of the requested type and exists, throw - * {@link ResourceException} - */ - public > T getChildOfType(final Class type, final String name) throws ResourceException; - -} diff --git a/resources/api/src/main/java/org/jboss/forge/addon/resource/FileResource.java b/resources/api/src/main/java/org/jboss/forge/addon/resource/FileResource.java deleted file mode 100644 index a47bafb7e0..0000000000 --- a/resources/api/src/main/java/org/jboss/forge/addon/resource/FileResource.java +++ /dev/null @@ -1,120 +0,0 @@ -package org.jboss.forge.addon.resource; - -import java.io.File; - -import org.jboss.forge.addon.resource.monitor.ResourceMonitor; - -/** - * A standard, built-in resource for representing files on the filesystem. - * - * @author Lincoln Baxter, III - */ -public interface FileResource> extends Resource, - CreatableResource, File>, - WriteableResource, File> -{ - /** - * Return true if this {@link FileResource} exists and is actually a directory, otherwise return false; - */ - public boolean isDirectory(); - - /** - * Returns true if the underlying resource has been modified on the file system since it was initially - * loaded. - * - * @return boolean true if resource is changed. - */ - public boolean isStale(); - - /** - * Re-read the file-system meta-data for this resource (such as last modified time-stamp, and permissions.) - */ - public void refresh(); - - /** - * Requests that the file or directory denoted by this resource be deleted when the virtual machine terminates. - *

- * Once deletion has been requested, it is not possible to cancel the request. This method should therefore be used - * with care. - */ - public void deleteOnExit(); - - /** - * Create the file in the underlying resource system. Necessary directory paths will be created automatically. - * - * @deprecated See {@link #create()}) - */ - public boolean createNewFile(); - - /** - * Create a temporary {@link FileResource} - */ - public T createTempResource(); - - /** - * Create a new single directory for this resource. This will not succeed if any parent directories needed for this - * resource to exist are missing. You should consider using {@link #mkdirs()} - */ - public boolean mkdir(); - - /** - * Create all directories required for this resource to exist. - */ - public boolean mkdirs(); - - /** - * Rename this {@link Resource} to the given path. - */ - public boolean renameTo(final String pathspec); - - /** - * Rename this {@link Resource} to the given {@link FileResource} - */ - public boolean renameTo(final FileResource target); - - /** - * Returns the size of the file denoted by this abstract pathname - */ - public long getSize(); - - /** - * Returns if a file is writable - */ - public boolean isWritable(); - - /** - * Returns if a file is readable - */ - public boolean isReadable(); - - /** - * Returns if a file is executable - */ - public boolean isExecutable(); - - /** - * A parent for a FileResource is always a DirectoryResource - */ - @Override - public DirectoryResource getParent(); - - /** - * Monitors this FileResource - */ - ResourceMonitor monitor(); - - /** - * Monitors this FileResource using the given filter - */ - ResourceMonitor monitor(ResourceFilter filter); - - /** - * Get the last modified time-stamp of this resource. - */ - public long getLastModified(); - - /** - * Set the last modified time-stamp of this resource. - */ - public void setLastModified(long currentTimeMillis); -} \ No newline at end of file diff --git a/resources/api/src/main/java/org/jboss/forge/addon/resource/PathResource.java b/resources/api/src/main/java/org/jboss/forge/addon/resource/PathResource.java index 60e259010f..010b106575 100644 --- a/resources/api/src/main/java/org/jboss/forge/addon/resource/PathResource.java +++ b/resources/api/src/main/java/org/jboss/forge/addon/resource/PathResource.java @@ -14,11 +14,6 @@ public interface PathResource extends Resource, CreatableResource, WriteableResource { - /** - * Return true if this {@link FileResource} exists and is actually a directory, otherwise return false; - */ - public boolean isDirectory(); - /** * Returns true if the underlying resource has been modified on the file system since it was initially loaded. * @@ -31,17 +26,6 @@ public interface PathResource extends Resource, */ public void refresh(); - /** - * Create a new single directory for this resource. This will not succeed if any parent directories needed for this - * resource to exist are missing. You should consider using {@link #mkdirs()} - */ - public boolean mkdir(); - - /** - * Create all directories required for this resource to exist. - */ - public boolean mkdirs(); - /** * Requests that the file or directory denoted by this resource be deleted when the virtual machine terminates. *

@@ -50,16 +34,6 @@ public interface PathResource extends Resource, */ public void deleteOnExit(); - /** - * Create the file in the underlying resource system. Necessary directory paths will be created automatically. - */ - public boolean createNewPath(); - - /** - * Create a temporary {@link PathResource} - */ - public PathResource createTempResource(); - /** * Rename this {@link Resource} to the given path. */ diff --git a/resources/api/src/main/java/org/jboss/forge/addon/resource/Resource.java b/resources/api/src/main/java/org/jboss/forge/addon/resource/Resource.java index 96c84e0ae2..ece6e35774 100644 --- a/resources/api/src/main/java/org/jboss/forge/addon/resource/Resource.java +++ b/resources/api/src/main/java/org/jboss/forge/addon/resource/Resource.java @@ -73,15 +73,6 @@ public interface Resource extends Faceted */ Resource getParent(); - /** - * Create a new resource instance for the target resource reference of the type that this current resource is. - * - * @deprecated use {@link ResourceFactory#create(Class, Object)} - * @param file The target reference to create the resource instance from. - * @return A new {@link Resource} instance. - */ - Resource createFrom(T file); - /** * Return a list of child resources of the current resource. (Never null.) */ @@ -122,6 +113,16 @@ public interface Resource extends Faceted */ boolean exists(); + /** + * Returns true if this {@link Resource} is a leaf + */ + boolean isLeaf(); + + /** + * Returns true if this {@link Resource} is a container + */ + boolean isContainer(); + /** * Ask this {@link Resource} if it is actually a resource of the given type; if it is, return a new reference to the * resource as the given type, otherwise return null. diff --git a/resources/api/src/main/java/org/jboss/forge/addon/resource/ResourceOperations.java b/resources/api/src/main/java/org/jboss/forge/addon/resource/ResourceOperations.java index 61ef0a77db..52e09bc4a8 100644 --- a/resources/api/src/main/java/org/jboss/forge/addon/resource/ResourceOperations.java +++ b/resources/api/src/main/java/org/jboss/forge/addon/resource/ResourceOperations.java @@ -28,11 +28,9 @@ public interface ResourceOperations void deleteOnExit(T resource); - boolean create(T resource) throws ResourceException; + boolean createLeaf(T resource) throws ResourceException; - boolean mkdir(T resource) throws ResourceException; - - boolean mkdirs(T resource) throws ResourceException; + boolean createContainer(T resource) throws ResourceException; OutputStream createOutputStream(T resource) throws ResourceException; diff --git a/resources/api/src/main/java/org/jboss/forge/addon/resource/VirtualResource.java b/resources/api/src/main/java/org/jboss/forge/addon/resource/VirtualResource.java index 9272fc9291..fef914b282 100644 --- a/resources/api/src/main/java/org/jboss/forge/addon/resource/VirtualResource.java +++ b/resources/api/src/main/java/org/jboss/forge/addon/resource/VirtualResource.java @@ -9,19 +9,27 @@ */ public abstract class VirtualResource extends AbstractResource { - protected VirtualResource(ResourceFactory factory, final Resource parent) + private Resource underlyingResource; + + protected VirtualResource(ResourceFactory factory, final Resource underlyingResource) + { + super(factory, null); + this.underlyingResource = underlyingResource; + } + + public Resource getUnderlyingResource() { - super(factory, parent); + return underlyingResource; } @Override - public Resource getChild(final String name) + public Resource getParent() { - throw new RuntimeException("not implemented"); + return underlyingResource.getParent(); } @Override - public Resource createFrom(T resource) + public Resource getChild(final String name) { throw new RuntimeException("not implemented"); } diff --git a/resources/api/src/main/java/org/jboss/forge/addon/resource/util/ResourcePathResolver.java b/resources/api/src/main/java/org/jboss/forge/addon/resource/util/ResourcePathResolver.java index 89219f4a61..c460a26637 100644 --- a/resources/api/src/main/java/org/jboss/forge/addon/resource/util/ResourcePathResolver.java +++ b/resources/api/src/main/java/org/jboss/forge/addon/resource/util/ResourcePathResolver.java @@ -16,7 +16,7 @@ import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; -import org.jboss.forge.addon.resource.DirectoryResource; +import org.jboss.forge.addon.resource.PathResource; import org.jboss.forge.addon.resource.Resource; import org.jboss.forge.addon.resource.ResourceFactory; import org.jboss.forge.addon.resource.URLResource; @@ -93,19 +93,19 @@ else if (path.startsWith("~")) if (path.length() == 1) { - return singleResult(factory.create(DirectoryResource.class, homeDir)); + return singleResult(factory.create(PathResource.class, homeDir.toPath())); } else { cursor++; - r = factory.create(DirectoryResource.class, homeDir); + r = factory.create(PathResource.class, homeDir.toPath()); } } // for windows, support drive letter prefixes here. else if (isWindows && path.matches("^[a-zA-Z]{1,1}:(/|\\\\).*")) { int idx = path.lastIndexOf(slashChar) + 1; - r = factory.create(DirectoryResource.class, new File(path.substring(0, idx)).getAbsoluteFile()); + r = factory.create(PathResource.class, new File(path.substring(0, idx)).getAbsoluteFile().toPath()); cursor = idx; } // Is an URL ? @@ -264,7 +264,7 @@ private static List> match(String[] matchLevels, if (matchPattern.matcher(res.getName()).matches()) { // if ((nestStart < matchLevels.length) && res.isFlagSet(ResourceFlag.Node)) - if ((nestStart < matchLevels.length) && res instanceof DirectoryResource) + if ((nestStart < matchLevels.length) && res instanceof PathResource) { return match(matchLevels, nestStart + 1, res, candidates); } @@ -280,7 +280,7 @@ private static List> match(String[] matchLevels, * bail. */ // if (!res.isFlagSet(ResourceFlag.Node) || (nestStart == matchLevels.length)) - if (!(res instanceof DirectoryResource) || (nestStart == matchLevels.length)) + if (!(res instanceof PathResource) || (nestStart == matchLevels.length)) { return candidates; } diff --git a/resources/api/src/main/java/org/jboss/forge/addon/resource/util/ResourceUtil.java b/resources/api/src/main/java/org/jboss/forge/addon/resource/util/ResourceUtil.java index dfc88f29f6..fc54b1902a 100644 --- a/resources/api/src/main/java/org/jboss/forge/addon/resource/util/ResourceUtil.java +++ b/resources/api/src/main/java/org/jboss/forge/addon/resource/util/ResourceUtil.java @@ -16,7 +16,7 @@ import java.util.Collection; import java.util.List; -import org.jboss.forge.addon.resource.DirectoryResource; +import org.jboss.forge.addon.resource.PathResource; import org.jboss.forge.addon.resource.Resource; import org.jboss.forge.addon.resource.ResourceFilter; import org.jboss.forge.furnace.util.Assert; @@ -93,14 +93,14 @@ public static File getContextFile(Resource r) return null; } - public static DirectoryResource getContextDirectory(final Resource r) + public static PathResource getContextDirectory(final Resource r) { Resource temp = r; do { - if (temp instanceof DirectoryResource) + if (temp instanceof PathResource) { - return (DirectoryResource) temp; + return (PathResource) temp; } } while ((temp != null) && ((temp = temp.getParent()) != null)); diff --git a/resources/impl/src/main/java/org/jboss/forge/addon/resource/DirectoryResourceImpl.java b/resources/impl/src/main/java/org/jboss/forge/addon/resource/DirectoryResourceImpl.java deleted file mode 100644 index 37e5409608..0000000000 --- a/resources/impl/src/main/java/org/jboss/forge/addon/resource/DirectoryResourceImpl.java +++ /dev/null @@ -1,249 +0,0 @@ -/* - * Copyright 2012 Red Hat, Inc. and/or its affiliates. - * - * Licensed under the Eclipse Public License version 1.0, available at - * http://www.eclipse.org/legal/epl-v10.html - */ - -package org.jboss.forge.addon.resource; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.charset.Charset; -import java.util.LinkedList; -import java.util.List; - -import org.jboss.forge.furnace.util.OperatingSystemUtils; - -/** - * A standard, build-in, resource for representing directories on the file-system. - * - * @author Lincoln Baxter, III - */ -public class DirectoryResourceImpl extends AbstractFileResource implements DirectoryResource -{ - private volatile List> listCache; - - public DirectoryResourceImpl(final ResourceFactory factory, final File file) - { - super(factory, file); - - isStale(); - } - - @Override - public boolean create() - { - return mkdirs(); - } - - @Override - protected List> doListResources() - { - if (isStale()) - { - listCache = null; - } - - if (listCache == null) - { - refresh(); - listCache = new LinkedList<>(); - - File[] files = getFileOperations().listChildren(getUnderlyingResourceObject()); - if (files != null) - { - for (File f : files) - { - listCache.add(getResourceFactory().create(f)); - } - } - } - - return listCache; - } - - @Override - public Resource getChild(final String name) - { - return getResourceFactory().create(new File(getUnderlyingResourceObject().getAbsolutePath(), name)); - } - - @Override - public DirectoryResource getChildDirectory(final String name) throws ResourceException - { - Resource result = getChild(name); - - if (!(result instanceof DirectoryResource)) - { - if (result.exists()) - { - throw new ResourceException("The resource [" + result.getFullyQualifiedName() - + "] is not a DirectoryResource"); - } - else - { - result = getResourceFactory().create(DirectoryResource.class, - new File(getUnderlyingResourceObject().getAbsoluteFile(), name)); - } - } - - return (DirectoryResource) result; - } - - @Override - public DirectoryResource getOrCreateChildDirectory(String name) - { - DirectoryResource child = getChildDirectory(name); - if (!child.exists()) - { - child.mkdir(); - } - return child; - } - - @Override - @SuppressWarnings("unchecked") - public > T getChildOfType(final Class type, final String name) throws ResourceException - { - T result; - Resource child = getChild(name); - if (type.isAssignableFrom(child.getClass())) - { - result = (T) child; - } - else if (child.exists()) - { - throw new ResourceException("Requested resource [" + name + "] was not of type [" + type.getName() - + "], but was instead [" + child.getClass().getName() + "]"); - } - else - { - E underlyingResource = (E) child.getUnderlyingResourceObject(); - result = getResourceFactory().create(type, underlyingResource); - } - return result; - } - - @Override - public DirectoryResource createTempResource() - { - try - { - File tempFile = File.createTempFile("forgetemp", ""); - tempFile.delete(); - return createFrom(tempFile); - } - catch (IOException e) - { - throw new ResourceException("Error while creating temporary directory", e); - } - } - - @Override - public DirectoryResource createFrom(final File file) - { - if (getFileOperations().exists(file) && !getFileOperations().existsAndIsDirectory(file)) - { - throw new ResourceException("File reference is not a directory: " + file.getAbsolutePath()); - } - else if (!getFileOperations().exists(file)) - { - getFileOperations().mkdirs(file); - } - - return getResourceFactory().create(DirectoryResource.class, file); - } - - @Override - public synchronized DirectoryResource getParent() - { - if (super.getParent() == null) - { - File parentFile = getUnderlyingResourceObject().getParentFile(); - if (parentFile == null) - { - return null; - } - - super.setParent(createFrom(parentFile)); - } - return super.getParent(); - } - - @Override - public String getName() - { - String fileName = getUnderlyingResourceObject().getName(); - // Windows: drive letter is needed. If filename is empty, we are on a root folder - return (OperatingSystemUtils.isWindows() && fileName.length() == 0) ? getUnderlyingResourceObject().getPath() - : fileName; - } - - @Override - public boolean equals(final Object obj) - { - return (obj instanceof DirectoryResourceImpl) - && ((DirectoryResourceImpl) obj).getUnderlyingResourceObject().equals(getUnderlyingResourceObject()); - } - - @Override - public long getSize() - { - throw new UnsupportedOperationException("getSize not supported for DirectoryResource objects"); - } - - @Override - public DirectoryResource setContents(char[] data) - { - throw new UnsupportedOperationException("setContents(char[]) is not supported on DirectoryResource objects"); - } - - @Override - public DirectoryResource setContents(InputStream data) - { - throw new UnsupportedOperationException("setContents(InputStream) is not supported on DirectoryResource objects"); - } - - @Override - public DirectoryResource setContents(String data) - { - throw new UnsupportedOperationException("setContents(String) is not supported on DirectoryResource objects"); - } - - @Override - public String getContents() - { - throw new UnsupportedOperationException("getContents() is not supported on DirectoryResource objects"); - } - - @Override - public DirectoryResource setContents(char[] data, Charset charset) - { - throw new UnsupportedOperationException( - "setContents(char[], Charset) is not supported on DirectoryResource objects"); - } - - @Override - public DirectoryResource setContents(String data, Charset charset) - { - throw new UnsupportedOperationException( - "setContents(String, Charset) is not supported on DirectoryResource objects"); - } - - @Override - public String getContents(Charset charset) - { - throw new UnsupportedOperationException( - "getContents(Charset) is not supported on DirectoryResource objects"); - } - - @Override - public OutputStream getResourceOutputStream() - { - throw new UnsupportedOperationException( - "getResourceOutputStream() is not supported on DirectoryResource objects"); - } -} \ No newline at end of file diff --git a/resources/impl/src/main/java/org/jboss/forge/addon/resource/FileResourceGenerator.java b/resources/impl/src/main/java/org/jboss/forge/addon/resource/FileResourceGenerator.java deleted file mode 100644 index f81930f12a..0000000000 --- a/resources/impl/src/main/java/org/jboss/forge/addon/resource/FileResourceGenerator.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.jboss.forge.addon.resource; - -import java.io.File; - -public class FileResourceGenerator implements ResourceGenerator, File> -{ - @Override - public boolean handles(Class type, Object resource) - { - return (resource instanceof File); - } - - @Override - @SuppressWarnings("unchecked") - public > T getResource(ResourceFactory factory, Class> type, File resource) - { - ResourceOperations fileOperations = factory.getResourceOperations(File.class); - if ((DirectoryResource.class.isAssignableFrom(type) && (!fileOperations.exists(resource))) - || (fileOperations.existsAndIsDirectory(resource))) - return (T) new DirectoryResourceImpl(factory, resource); - return (T) new FileResourceImpl(factory, resource); - } - - @Override - public > Class getResourceType(ResourceFactory factory, Class> type, - File resource) - { - ResourceOperations fileOperations = factory.getResourceOperations(File.class); - if ((DirectoryResource.class.isAssignableFrom(type) && (!fileOperations.exists(resource))) - || (fileOperations.existsAndIsDirectory(resource))) - return DirectoryResource.class; - return FileResource.class; - } -} diff --git a/resources/impl/src/main/java/org/jboss/forge/addon/resource/FileResourceImpl.java b/resources/impl/src/main/java/org/jboss/forge/addon/resource/FileResourceImpl.java deleted file mode 100644 index e896370aa0..0000000000 --- a/resources/impl/src/main/java/org/jboss/forge/addon/resource/FileResourceImpl.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2012 Red Hat, Inc. and/or its affiliates. - * - * Licensed under the Eclipse Public License version 1.0, available at - * http://www.eclipse.org/legal/epl-v10.html - */ - -package org.jboss.forge.addon.resource; - -import java.io.File; -import java.util.Collections; -import java.util.List; - -/** - * Represents any regular file which Furnace does not have any special handler for. - * - * @author Mike Brock - * @author Lincoln Baxter, III - */ -public class FileResourceImpl extends AbstractFileResource -{ - public FileResourceImpl(final ResourceFactory factory, final File file) - { - super(factory, file); - } - - @Override - public FileResourceImpl createFrom(final File file) - { - return new FileResourceImpl(getResourceFactory(), file); - } - - @Override - protected List> doListResources() - { - return Collections.emptyList(); - } - - @Override - public boolean supports(ResourceFacet type) - { - return false; - } -} diff --git a/resources/impl/src/main/java/org/jboss/forge/addon/resource/PathResourceImpl.java b/resources/impl/src/main/java/org/jboss/forge/addon/resource/PathResourceImpl.java index cd4d1a69d2..7b1f6d63ce 100644 --- a/resources/impl/src/main/java/org/jboss/forge/addon/resource/PathResourceImpl.java +++ b/resources/impl/src/main/java/org/jboss/forge/addon/resource/PathResourceImpl.java @@ -50,11 +50,17 @@ protected PathResourceImpl(ResourceFactory factory, Path file) throws IOExceptio } @Override - public boolean create() + public boolean createLeaf() { - return createNewPath(); + return getPathOperations().createLeaf(path); } - + + @Override + public boolean createContainer() + { + return getPathOperations().createContainer(path); + } + @Override public String getName() { @@ -98,7 +104,13 @@ public boolean exists() } @Override - public boolean isDirectory() + public boolean isLeaf() + { + return getPathOperations().exists(path) && getPathOperations().existsAndIsDirectory(path); + } + + @Override + public boolean isContainer() { return getPathOperations().existsAndIsDirectory(path); } @@ -115,18 +127,6 @@ public void refresh() lastModification = getPathOperations().getLastModifiedTime(path); } - @Override - public boolean mkdir() - { - return getPathOperations().mkdir(path); - } - - @Override - public boolean mkdirs() - { - return getPathOperations().mkdirs(path); - } - @Override public boolean delete() { @@ -224,8 +224,8 @@ public PathResource setContents(final InputStream data) { if (!exists()) { - getParent().mkdirs(); - if (!createNewPath()) + getParent().createContainer(); + if (!createLeaf()) { throw new ResourceException("Failed to create path: " + path); } @@ -254,31 +254,6 @@ public PathResource setContents(final InputStream data) return this; } - @Override - public boolean createNewPath() - { - getParent().mkdirs(); - if (getPathOperations().create(path)) - { - return true; - } - return false; - } - - @Override - public PathResource createTempResource() - { - try - { - PathResource result = createFrom(Files.createTempFile("forgetemp", "")); - return result; - } - catch (IOException e) - { - throw new ResourceException(e.getMessage(), e); - } - } - @Override public boolean renameTo(final String pathspec) { @@ -388,7 +363,7 @@ public PathResource createFrom(Path path) @Override protected List> doListResources() { - if (isDirectory()) + if (isContainer()) { if (isStale()) { diff --git a/resources/impl/src/main/java/org/jboss/forge/addon/resource/ResourceFactoryImpl.java b/resources/impl/src/main/java/org/jboss/forge/addon/resource/ResourceFactoryImpl.java index 4341b5a360..3b50b70ff9 100644 --- a/resources/impl/src/main/java/org/jboss/forge/addon/resource/ResourceFactoryImpl.java +++ b/resources/impl/src/main/java/org/jboss/forge/addon/resource/ResourceFactoryImpl.java @@ -115,11 +115,7 @@ public ResourceMonitor monitor(Resource resource, ResourceFilter resourceFilt throw new IllegalStateException("Resource must exist to be monitored"); } - if (resource instanceof FileResource) - { - return fileMonitor.registerMonitor(this, (FileResource) resource, resourceFilter); - } - else if (resource instanceof PathResource) + if (resource instanceof PathResource) { return fileMonitor.registerMonitor(this, (PathResource) resource, resourceFilter); } diff --git a/resources/impl/src/main/java/org/jboss/forge/addon/resource/URLResourceImpl.java b/resources/impl/src/main/java/org/jboss/forge/addon/resource/URLResourceImpl.java index 574ca96bdb..4447f4c964 100644 --- a/resources/impl/src/main/java/org/jboss/forge/addon/resource/URLResourceImpl.java +++ b/resources/impl/src/main/java/org/jboss/forge/addon/resource/URLResourceImpl.java @@ -129,4 +129,16 @@ public String toString() { return getFullyQualifiedName(); } + + @Override + public boolean isLeaf() + { + return true; + } + + @Override + public boolean isContainer() + { + return false; + } }