[jboss-cvs] JBossAS SVN: r75185 - in projects/vfs/trunk/src: main/java/org/jboss/virtual/plugins/context and 8 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Sun Jun 29 17:22:00 EDT 2008
Author: mstruk
Date: 2008-06-29 17:22:00 -0400 (Sun, 29 Jun 2008)
New Revision: 75185
Modified:
projects/vfs/trunk/src/main/java/org/jboss/virtual/VirtualFile.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractURLHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVirtualFileHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/DelegatingHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/file/FileHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/file/LinkHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarEntryContents.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarEntryHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarFromStream.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NoCopyNestedJarHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/SynthenticDirEntryHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContextHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledDirectoryHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/ByteArrayHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipBytesWrapper.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipFileLockReaper.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipFileWrapper.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipWrapper.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/VirtualFileHandler.java
projects/vfs/trunk/src/test/java/org/jboss/test/virtual/support/AbstractMockVirtualFileHandler.java
projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/FileVFSUnitTestCase.java
projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/ZipEntryHandlerUnitTestCase.java
Log:
VirtualFile.delete() method
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/VirtualFile.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/VirtualFile.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/VirtualFile.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -275,6 +275,30 @@
}
/**
+ * Delete this virtual file
+ *
+ * @return true if file was deleted
+ * @throws IOException if an error occurs
+ */
+ public boolean delete() throws IOException
+ {
+ // gracePeriod of 2 seconds
+ return getHandler().delete(2000);
+ }
+
+ /**
+ * Delete this virtual file
+ *
+ * @param gracePeriod max time to wait for any locks (in milliseconds)
+ * @return true if file was deleted
+ * @throws IOException if an error occurs
+ */
+ public boolean delete(int gracePeriod) throws IOException
+ {
+ return getHandler().delete(gracePeriod);
+ }
+
+ /**
* Get the VFS instance for this virtual file
*
* @return the VFS
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractURLHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractURLHandler.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractURLHandler.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -23,6 +23,7 @@
import java.io.IOException;
import java.io.InputStream;
+import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
@@ -96,14 +97,36 @@
{
try
{
- this.cachedLastModified = openConnection().getLastModified();
+ URLConnection c = openConnection();
+ try
+ {
+ this.cachedLastModified = c.getLastModified();
+ }
+ finally
+ {
+ closeConnection(c);
+ }
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
-
+
+ private void closeConnection(URLConnection c)
+ {
+ try
+ {
+ if (c instanceof JarURLConnection == false)
+ c.getInputStream().close();
+ }
+ catch (Exception ex)
+ {
+ if (log.isDebugEnabled())
+ log.debug("IGNORING: Exception while closing connection", ex);
+ }
+ }
+
/**
* Get the url
*
@@ -123,14 +146,28 @@
{
checkClosed();
URLConnection c = openConnection();
- return c.getLastModified();
+ try
+ {
+ return c.getLastModified();
+ }
+ finally
+ {
+ closeConnection(c);
+ }
}
public long getSize() throws IOException
{
checkClosed();
URLConnection c = openConnection();
- return c.getContentLength();
+ try
+ {
+ return c.getContentLength();
+ }
+ finally
+ {
+ closeConnection(c);
+ }
}
/**
@@ -143,7 +180,14 @@
public boolean exists() throws IOException
{
URLConnection c = openConnection();
- return c.getLastModified() != 0;
+ try
+ {
+ return c.getLastModified() != 0;
+ }
+ finally
+ {
+ closeConnection(c);
+ }
}
public boolean isHidden() throws IOException
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVirtualFileHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVirtualFileHandler.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVirtualFileHandler.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -460,6 +460,34 @@
}
/**
+ * Delete the file represented by this handler.
+ *
+ * File deletion is comprised of two parts:
+ *
+ * <ol>
+ * <li>physical file deletion - performed by this method or its override</li>
+ * <li>removal of any child references from the parent - performed by {@link #removeChild(String)} of the parent</li>
+ * </ol>
+ *
+ * This method doesn't do any physical file removal because it has no concept of underlying physical file.
+ * An implementation that does physical file removal should override this method and call super.delete() at the end.
+ *
+ * @param gracePeriod max time to wait for any locks
+ * @return true if file was deleted, false otherwise
+ * @throws IOException if an error occurs
+ */
+ public boolean delete(int gracePeriod) throws IOException
+ {
+ VirtualFileHandler parent = getParent();
+ if (parent != null)
+ {
+ return parent.removeChild(getName());
+ }
+
+ return false;
+ }
+
+ /**
* Structured implementation of get child
*
* @param path the path
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/DelegatingHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/DelegatingHandler.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/DelegatingHandler.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -97,6 +97,11 @@
return child;
}
+ public boolean removeChild(String path) throws IOException
+ {
+ throw new IOException("This method should never get called!");
+ }
+
public List<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException
{
return getDelegate().getChildren(ignoreErrors);
@@ -132,6 +137,11 @@
return getDelegate().isNested();
}
+ public boolean delete(int gracePeriod) throws IOException
+ {
+ return getDelegate().delete(gracePeriod);
+ }
+
public InputStream openStream() throws IOException
{
return getDelegate().openStream();
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/file/FileHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/file/FileHandler.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/file/FileHandler.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -148,6 +148,46 @@
return false;
}
+ public boolean delete(int gracePeriod) throws IOException
+ {
+ File f = getFile();
+
+ boolean exists = f.exists();
+ if (exists == false)
+ return false;
+
+ if (f.delete() == false)
+ {
+ long endOfGrace = System.currentTimeMillis() + gracePeriod;
+ while(System.currentTimeMillis() < endOfGrace)
+ {
+ boolean done = f.delete();
+ if (done)
+ {
+ childCache.remove(f.getName());
+ return true;
+ }
+
+ if (f.isDirectory())
+ return false;
+
+ try
+ {
+ Thread.sleep(100);
+ }
+ catch (InterruptedException e)
+ {
+ throw new IOException("Interrupted: ", e);
+ }
+ }
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+ }
+
public List<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException
{
File parent = getFile();
@@ -233,6 +273,11 @@
return structuredFindChild(path);
}
+ public boolean removeChild(String name) throws IOException
+ {
+ return childCache.remove(name) != null;
+ }
+
protected void internalReplaceChild(VirtualFileHandler original, VirtualFileHandler replacement)
{
childCache.put(original.getName(), replacement);
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/file/LinkHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/file/LinkHandler.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/file/LinkHandler.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -89,6 +89,11 @@
return structuredFindChild(path);
}
+ public boolean removeChild(String name) throws IOException
+ {
+ return children.remove(name) != null;
+ }
+
public List<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException
{
return null;
@@ -213,6 +218,11 @@
return structuredFindChild(path);
}
+ public boolean removeChild(String name) throws IOException
+ {
+ return linkTargets.remove(name) != null;
+ }
+
@Override
protected void doClose()
{
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarEntryContents.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarEntryContents.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarEntryContents.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -173,6 +173,11 @@
return null;
}
+ public boolean removeChild(String path)
+ {
+ return false;
+ }
+
/**
* Find the handler.
* TODO: synchronization on lazy entryMap creation
@@ -211,6 +216,11 @@
return isJar == false && getEntry().isDirectory() == false;
}
+ public boolean delete(int gracePeriod)
+ {
+ return false;
+ }
+
// Stream accessor
public synchronized InputStream openStream() throws IOException
{
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarEntryHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarEntryHandler.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarEntryHandler.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -171,6 +171,16 @@
return structuredFindChild(path);
}
+ public boolean removeChild(String path) throws IOException
+ {
+ return false;
+ }
+
+ public boolean delete(String path)
+ {
+ return false;
+ }
+
protected void internalReplaceChild(VirtualFileHandler original, VirtualFileHandler replacement)
{
entryChildren.remove(original);
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarHandler.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarHandler.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -87,6 +87,18 @@
}
}
+ public boolean removeChild(String path) throws IOException
+ {
+ return false;
+ }
+
+ public boolean delete(int gracePeriod) throws IOException
+ {
+ getJar().close();
+ File jarFile = new File(getJar().getName());
+ return jarFile.delete();
+ }
+
public boolean isNested() throws IOException
{
return false;
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarFromStream.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarFromStream.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarFromStream.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -123,6 +123,11 @@
return super.getChild(path);
}
+ public boolean removeChild(String name) throws IOException
+ {
+ return false;
+ }
+
public VirtualFileHandler createChildHandler(String name) throws IOException
{
init();
@@ -205,6 +210,11 @@
return true;
}
+ public boolean delete(int gracePeriod)
+ {
+ return false;
+ }
+
// Stream accessor
public InputStream openStream() throws IOException
{
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarHandler.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarHandler.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -183,6 +183,16 @@
return new FileInputStream(temp);
}
+ public boolean removeChild(String name) throws IOException
+ {
+ return false;
+ }
+
+ public boolean delete(int gracePeriod)
+ {
+ return false;
+ }
+
/**
* Restore the temp file
*
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NoCopyNestedJarHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NoCopyNestedJarHandler.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NoCopyNestedJarHandler.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -135,11 +135,21 @@
return njar.getChild(path);
}
+ public boolean removeChild(String name) throws IOException
+ {
+ return false;
+ }
+
public List<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException
{
return njar.getChildren(ignoreErrors);
}
+ public boolean delete(int gracePeriod)
+ {
+ return false;
+ }
+
protected void internalReplaceChild(VirtualFileHandler original, VirtualFileHandler replacement)
{
njar.internalReplaceChild(original, replacement);
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/SynthenticDirEntryHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/SynthenticDirEntryHandler.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/SynthenticDirEntryHandler.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -142,6 +142,11 @@
return true;
}
+ public boolean delete(int gracePeriod)
+ {
+ return false;
+ }
+
@Override
public InputStream openStream() throws IOException
{
@@ -185,6 +190,11 @@
return structuredFindChild(path);
}
+ public boolean removeChild(String name) throws IOException
+ {
+ return false;
+ }
+
protected void internalReplaceChild(VirtualFileHandler original, VirtualFileHandler replacement)
{
entryChildren.remove(original);
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContextHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContextHandler.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContextHandler.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -105,6 +105,11 @@
return structuredFindChild(path);
}
+ public boolean removeChild(String name) throws IOException
+ {
+ return entryMap.remove(name) != null;
+ }
+
@Override
public boolean exists() throws IOException
{
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledDirectoryHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledDirectoryHandler.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledDirectoryHandler.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -29,6 +29,7 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -80,6 +81,29 @@
return handler;
}
+ /**
+ * This method removes a specified child from its list of children map
+ *
+ * @param name child name
+ * @return true if child was removed, false otherwise
+ */
+ public boolean removeChild(String name)
+ {
+ Iterator<VirtualFileHandler> it = children.iterator();
+ while (it.hasNext())
+ {
+ VirtualFileHandler child = it.next();
+ if (child.getName().equals(name))
+ {
+ it.remove();
+ childrenMap.remove(name);
+ lastModified = System.currentTimeMillis();
+ return true;
+ }
+ }
+ return false;
+ }
+
public VirtualFileHandler findChild(String name)
{
return childrenMap.get(name);
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/ByteArrayHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/ByteArrayHandler.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/ByteArrayHandler.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -110,4 +110,9 @@
{
throw new IOException("File cannot have children");
}
+
+ public boolean removeChild(String name)
+ {
+ return false;
+ }
}
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipBytesWrapper.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipBytesWrapper.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipBytesWrapper.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -128,6 +128,12 @@
zipBytes = null;
}
+ boolean delete(int gracePeriod) throws IOException
+ {
+ close();
+ return true;
+ }
+
/**
* String description of this archive
*
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -678,6 +678,15 @@
return Collections.emptyList();
}
+ public boolean delete(ZipEntryHandler handler, int gracePeriod) throws IOException
+ {
+ if (getRoot().equals(handler))
+ {
+ return zipSource.delete(gracePeriod);
+ }
+ return false;
+ }
+
/**
* Returns lastModified timestamp for a given handler
*
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryHandler.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryHandler.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -139,6 +139,21 @@
return structuredFindChild(path);
}
+ public boolean removeChild(String name) throws IOException
+ {
+ return false;
+ }
+
+ public boolean delete(int gracePeriod) throws IOException
+ {
+ checkClosed();
+ boolean deleted = getZipEntryContext().delete(this, gracePeriod);
+ if (deleted)
+ super.delete(gracePeriod);
+
+ return deleted;
+ }
+
public VirtualFileHandler createChildHandler(String name) throws IOException
{
return getZipEntryContext().getChild(this, name);
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipFileLockReaper.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipFileLockReaper.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipFileLockReaper.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -26,6 +26,7 @@
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentLinkedQueue;
+import java.io.IOException;
import org.jboss.logging.Logger;
@@ -115,6 +116,19 @@
log.trace("Unregistered: " + w);
}
+ public void deleteFile(ZipFileWrapper zipFileWrapper) throws IOException
+ {
+ synchronized (ZipFileLockReaper.this)
+ {
+ Iterator it = monitored.iterator();
+ while (it.hasNext())
+ {
+ ZipFileWrapper w = (ZipFileWrapper) it.next();
+ w.deleteFile(zipFileWrapper);
+ }
+ }
+ }
+
/** Timer task that does the actual reaping */
class ReaperTimerTask extends TimerTask
{
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipFileWrapper.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipFileWrapper.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipFileWrapper.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -282,7 +282,52 @@
file.delete();
}
+ void deleteFile(ZipFileWrapper wrapper) throws IOException
+ {
+ if (file.equals(wrapper.file))
+ {
+ closeZipFile();
+ file.delete();
+ }
+ }
+
/**
+ * Delete the archive
+ *
+ * @param gracePeriod max time to wait for any locks
+ * @return true if file was deleted, false otherwise
+ * @throws IOException if an error occurs
+ */
+ boolean delete(int gracePeriod) throws IOException
+ {
+ boolean exists = file.isFile();
+ if (exists == false)
+ return false;
+
+ long endOfGrace = System.currentTimeMillis() + gracePeriod;
+ do
+ {
+ closeZipFile();
+ ZipFileLockReaper.getInstance().deleteFile(this);
+ try
+ {
+ if (file.exists() && file.delete() == false)
+ Thread.sleep(100);
+ else
+ return true;
+ }
+ catch (InterruptedException e)
+ {
+ throw new IOException("Interrupted: ", e);
+ }
+ }
+ while(System.currentTimeMillis() < endOfGrace);
+
+ file.delete();
+ return file.exists() == false;
+ }
+
+ /**
* toString
*
* @return String description of this archive
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipWrapper.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipWrapper.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipWrapper.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -180,4 +180,14 @@
* Close this archive
*/
abstract void close();
+
+ /**
+ * Delete this archive
+ *
+ * @param gracePeriod maximum time to wait for any locks
+ * @return true if file was deleted, false otherwise
+ * @throws IOException for any error
+ */
+ abstract boolean delete(int gracePeriod) throws IOException;
+
}
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/VirtualFileHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/VirtualFileHandler.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/VirtualFileHandler.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -182,6 +182,16 @@
VirtualFileHandler getChild(String path) throws IOException;
/**
+ * Remove a child
+ *
+ * @param name child name
+ * @return true if child was removed, false otherwise
+ * @throws IllegalStateException if closed
+ * @throws IOException if an error occurs
+ */
+ boolean removeChild(String name) throws IOException;
+
+ /**
* Get the VFSContext this file belongs to
*
* @return the context
@@ -217,4 +227,13 @@
* @throws IOException for any error
*/
boolean isNested() throws IOException;
+
+ /**
+ * Delete a file represented by this handler
+ *
+ * @param gracePeriod max time to wait for locks (in milliseconds)
+ * @return boolean true if file was deleted, false otherwise
+ * @throws IOException for any error
+ */
+ public boolean delete(int gracePeriod) throws IOException;
}
Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/support/AbstractMockVirtualFileHandler.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/support/AbstractMockVirtualFileHandler.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/support/AbstractMockVirtualFileHandler.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -30,6 +30,7 @@
import java.net.URL;
import java.util.Collections;
import java.util.List;
+import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
import org.jboss.util.UnexpectedThrowable;
@@ -74,10 +75,13 @@
/** The stream */
private byte[] stream;
-
+
/** When to throw an IOException */
private String ioException = "";
-
+
+ /** Delete succeeds */
+ private boolean doDelete = true;
+
/**
* Create a root mock uri
*
@@ -160,6 +164,24 @@
leaf = false;
}
+ public boolean removeChild(String path) throws IOException
+ {
+ if (path == null)
+ throw new IllegalArgumentException("Null path");
+
+ Iterator<VirtualFileHandler> it = children.iterator();
+ while (it.hasNext())
+ {
+ VirtualFileHandler handler = it.next();
+ if (path.equals(handler.getName()))
+ {
+ it.remove();
+ return true;
+ }
+ }
+ return false;
+ }
+
public long getLastModified() throws IOException
{
checkClosed();
@@ -248,6 +270,13 @@
this.nested = nested;
}
+ public boolean delete(int gracePeriod) throws IOException
+ {
+ checkClosed();
+ throwIOException("delete");
+ return doDelete;
+ }
+
/**
* Set the stream.
*
Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/FileVFSUnitTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/FileVFSUnitTestCase.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/FileVFSUnitTestCase.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -66,7 +66,7 @@
* @author adrian at jboss.org
* @version $Revision: 55523 $
*/
-public class FileVFSUnitTestCase extends OSAwareVFSTest
+public class FileVFSUnitTestCase extends AbstractVFSTest
{
public FileVFSUnitTestCase(String name)
{
@@ -1351,9 +1351,9 @@
VFS vfs = VFS.getVFS(rootURL);
VirtualFile tmpVF = vfs.findChild(tmp.getName());
assertTrue(tmpVF.getPathName()+".exists()", tmpVF.exists());
- assertTrue("tmp.delete()", tmp.delete() || isWindowsOS());
- assertFalse(tmpVF.getPathName()+".exists()", tmpVF.exists() && isWindowsOS() == false);
- assertTrue(tmpRoot+".delete()", tmpRoot.delete() || isWindowsOS());
+ assertTrue("tmp.delete()", tmpVF.delete());
+ assertFalse(tmpVF.getPathName()+".exists()", tmpVF.exists());
+ assertTrue(tmpRoot+".delete()", tmpRoot.delete());
}
/**
@@ -1408,9 +1408,9 @@
VirtualFile tmpVF = vfs.findChild(tmpJar.getName());
assertTrue(tmpVF.getPathName()+".exists()", tmpVF.exists());
assertTrue(tmpVF.getPathName()+".size() > 0", tmpVF.getSize() > 0);
- assertTrue("tmp.delete()", tmpJar.delete() || isWindowsOS());
- assertFalse(tmpVF.getPathName()+".exists()", tmpVF.exists() && isWindowsOS() == false);
- assertTrue(tmpRoot+".delete()", tmpRoot.delete() || isWindowsOS());
+ assertTrue("tmp.delete()", tmpVF.delete());
+ assertFalse(tmpVF.getPathName()+".exists()", tmpVF.exists());
+ assertTrue(tmpRoot+".delete()", tmpRoot.delete());
}
/**
@@ -1441,6 +1441,52 @@
}
/**
+ * Test VirtualFile.delete() for file based urls
+ *
+ * @throws Exception
+ */
+ public void testFileDelete() throws Exception
+ {
+ File tmpRoot = File.createTempFile("vfs", ".root");
+ VFS vfs = VFS.getVFS(tmpRoot.toURL());
+
+ // non-existent directory - exists() not
+ tmpRoot.delete();
+ assertFalse(tmpRoot + ".exits() == false", vfs.getRoot().exists());
+
+ // existing directory - exists(), delete()
+ tmpRoot.mkdir();
+ assertTrue(tmpRoot + ".exits()", vfs.getRoot().exists());
+ assertTrue(tmpRoot + ".delete()", vfs.getRoot().delete());
+ tmpRoot.mkdir();
+
+ // non-empty directory - delete() not
+ File tmp = File.createTempFile("testFileDelete", ".jar", tmpRoot);
+ assertFalse(tmpRoot + ".delete() == false", vfs.getRoot().delete());
+
+ // children() exist
+ List<VirtualFile> children = vfs.getChildren();
+ assertTrue(tmpRoot + ".getChildren().size() == 1", children.size() == 1);
+
+ // specific child exists(), delete(), exists() not
+ VirtualFile tmpVF = vfs.getChild(tmp.getName());
+ assertTrue(tmp + ".exists()", tmpVF.exists());
+ assertTrue(tmp + ".delete()", tmpVF.delete());
+ assertFalse(tmp + ".exists() == false", tmpVF.exists());
+
+ // children() don't exist
+ children = vfs.getChildren();
+ assertTrue(tmpRoot + ".getChildren().size() == 0", children.size() == 0);
+
+ // getChild() returns null
+ tmpVF = vfs.getChild(tmp.getName());
+ assertNull(tmpRoot + ".getChild('" + tmp.getName() + "') == null", tmpVF);
+
+ // directory delete()
+ assertTrue(tmpRoot + ".delete()", vfs.getRoot().delete());
+ }
+
+ /**
* Test for <em>caseSensitive=true</em>
*
* If this test passes on unixes, it doesn't mean much, because there it should pass without
Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/ZipEntryHandlerUnitTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/ZipEntryHandlerUnitTestCase.java 2008-06-29 21:09:38 UTC (rev 75184)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/ZipEntryHandlerUnitTestCase.java 2008-06-29 21:22:00 UTC (rev 75185)
@@ -22,11 +22,18 @@
package org.jboss.test.virtual.test;
import java.net.URL;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.util.jar.Manifest;
+import java.util.jar.JarOutputStream;
+import java.util.List;
import junit.framework.Test;
import org.jboss.virtual.plugins.context.jar.JarUtils;
import org.jboss.virtual.plugins.context.zip.ZipEntryContext;
import org.jboss.virtual.spi.VFSContext;
+import org.jboss.virtual.VFS;
+import org.jboss.virtual.VirtualFile;
/**
* ZipEntryHandlerUnitTestCase.
@@ -52,4 +59,124 @@
url = JarUtils.createJarURL(url);
return new ZipEntryContext(url);
}
+
+ /**
+ * Test VirtualFile.delete() for zip archives accessed through FileSystemContext
+ *
+ * @throws Exception if an error occurs
+ */
+ public void testFileContextZipDelete() throws Exception
+ {
+ File tmpRoot = File.createTempFile("vfs", ".root");
+ tmpRoot.delete();
+ tmpRoot.mkdir();
+
+ File tmp = File.createTempFile("testFileContextZipDelete", ".jar", tmpRoot);
+ VFS vfs = VFS.getVFS(tmpRoot.toURL());
+
+ Manifest mf = new Manifest();
+ mf.getMainAttributes().putValue("Created-By", getClass().getName() + "." + "testEntryModified");
+ FileOutputStream fos = new FileOutputStream(tmp);
+ JarOutputStream jos = new JarOutputStream(fos, mf);
+ try
+ {
+ jos.setComment("testJarURLs");
+ jos.setLevel(0);
+ jos.flush();
+ }
+ finally
+ {
+ jos.close();
+ }
+
+ // children() exist
+ List<VirtualFile> children = vfs.getChildren();
+ assertTrue(tmpRoot + ".getChildren().size() == 1", children.size() == 1);
+
+ // specific child exists()
+ VirtualFile tmpVF = vfs.getChild(tmp.getName());
+ assertTrue(tmp + ".exists()", tmpVF.exists());
+
+
+ // test jar entry
+ // specific zip entry exists(), delete() not, exists()
+ VirtualFile entryVF = tmpVF.getChild("META-INF");
+ assertTrue(entryVF.getName() + " .exists()", entryVF.exists());
+ assertFalse(entryVF.getName() + " .delete() == false", entryVF.delete());
+ assertTrue(entryVF.getName() + " .exists()", entryVF.exists());
+
+ // children() exist
+ children = tmpVF.getChildren();
+ assertTrue(tmpVF + ".getChildren().size() == 1", children.size() == 1);
+
+ // getChild() returns not-null
+ entryVF = tmpVF.getChild("META-INF");
+ assertNotNull(tmpVF + ".getChild('META-INF') != null", entryVF);
+
+
+ // continue with jar
+ // specific child delete(), exists() not
+ assertTrue(tmp + ".delete()", tmpVF.delete());
+ assertFalse(tmp + ".exists() == false", tmpVF.exists());
+
+ // children() don't exist
+ children = vfs.getChildren();
+ assertTrue(tmpRoot + ".getChildren().size() == 0", children.size() == 0);
+
+ // getChild() returns null
+ tmpVF = vfs.getChild(tmp.getName());
+ assertNull(tmpRoot + ".getChild('" + tmp.getName() + "') == null", tmpVF);
+
+ // directory delete()
+ assertTrue(tmpRoot + ".delete()", vfs.getRoot().delete());
+ }
+
+ /**
+ * Test VirtualFile.delete() for zip archive accessed through ZipEntryContext
+ *
+ * @throws Exception
+ */
+ public void testZipContextDelete() throws Exception
+ {
+ File tmp = File.createTempFile("testZipContextDelete", ".jar");
+
+ Manifest mf = new Manifest();
+ mf.getMainAttributes().putValue("Created-By", getClass().getName() + "." + "testEntryModified");
+ FileOutputStream fos = new FileOutputStream(tmp);
+ JarOutputStream jos = new JarOutputStream(fos, mf);
+ try
+ {
+ jos.setComment("testJarURLs");
+ jos.setLevel(0);
+ jos.flush();
+ }
+ finally
+ {
+ jos.close();
+ }
+
+ VFS vfs = VFS.getVFS(tmp.toURL());
+
+ // children() exist
+ List<VirtualFile> children = vfs.getChildren();
+ assertTrue(tmp + ".getChildren().size() == 1", children.size() == 1);
+
+ // specific child exists(), delete() not, exists()
+ VirtualFile tmpVF = vfs.getChild("META-INF");
+ assertTrue(tmp + ".exists()", tmpVF.exists());
+ assertFalse(tmp + ".delete() == false", tmpVF.delete());
+ assertTrue(tmp + ".exists()", tmpVF.exists());
+
+ // children() exist
+ children = vfs.getChildren();
+ assertTrue(tmp + ".getChildren().size() == 1", children.size() == 1);
+
+ // getChild() returns not-null
+ tmpVF = vfs.getChild("META-INF");
+ assertNotNull(tmp + ".getChild('META-INF') != null", tmpVF);
+
+ // archive delete(), exists() not
+ assertTrue(tmp + ".delete()", vfs.getRoot().delete());
+ assertFalse(tmp + ".exists() == false", vfs.getRoot().exists());
+ }
}
\ No newline at end of file
More information about the jboss-cvs-commits
mailing list