[jboss-cvs] JBossAS SVN: r94171 - projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Sep 30 14:16:32 EDT 2009
Author: david.lloyd at jboss.com
Date: 2009-09-30 14:16:31 -0400 (Wed, 30 Sep 2009)
New Revision: 94171
Added:
projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/AbstractRefCounted.java
projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/Handle.java
Modified:
projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/FileSystem.java
projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/JZipFileSystem.java
projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/JavaZipFileSystem.java
projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/RealFileSystem.java
Log:
Make FileSystems be refcounted. Is this a bad idea? Maybe.
Added: projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/AbstractRefCounted.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/AbstractRefCounted.java (rev 0)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/AbstractRefCounted.java 2009-09-30 18:16:31 UTC (rev 94171)
@@ -0,0 +1,105 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, JBoss, a division of Red Hat, Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.vfs.spi;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
+
+/**
+ * A resource which is reference-counted.
+ */
+public abstract class AbstractRefCounted implements Closeable {
+ private volatile int refcount = 0;
+
+ private static final AtomicIntegerFieldUpdater<AbstractRefCounted> refcountUpdater = AtomicIntegerFieldUpdater.newUpdater(AbstractRefCounted.class, "refcount");
+
+ private static final int DEAD_VALUE = 0x87ffffff;
+ private static final int MAX_REFCOUNT = 0x3fffffff;
+
+ @SuppressWarnings({ "unchecked" })
+ protected final <T extends AbstractRefCounted> Handle<T> doGetHandle() throws IOException {
+ final int idx = refcountUpdater.getAndIncrement(this);
+ if (idx < 0) {
+ unreference();
+ throw new IOException("Resource is already closed");
+ }
+ if (idx > MAX_REFCOUNT) {
+ unreference();
+ throw new IOException("Too many references to this resource");
+ }
+ return new RefHandle<T>((T) this);
+ }
+
+ void unreference() throws IOException {
+ final int cnt = refcountUpdater.decrementAndGet(this);
+ if (cnt == 0) {
+ if (refcountUpdater.compareAndSet(this, 0, DEAD_VALUE)) {
+ // we won the race to close, lucky us...
+ doClose();
+ }
+ }
+ }
+
+ public final void close() throws IOException {
+ if (refcountUpdater.getAndSet(this, DEAD_VALUE) >= 0) {
+ doClose();
+ }
+ }
+
+ public final boolean isOpen() {
+ return refcount >= 0;
+ }
+
+ protected abstract void doClose() throws IOException;
+}
+
+final class RefHandle<T extends AbstractRefCounted> implements Handle<T> {
+ private volatile T resource;
+
+ private static final AtomicReferenceFieldUpdater<RefHandle, Object> resourceUpdater = AtomicReferenceFieldUpdater.newUpdater(RefHandle.class, Object.class, "resource");
+
+ RefHandle(final T resource) {
+ this.resource = resource;
+ }
+
+ public T getResource() throws IOException {
+ final T resource = this.resource;
+ if (resource == null) {
+ throw new IOException("Handle is closed");
+ }
+ return resource;
+ }
+
+ public void close() throws IOException {
+ final Object oldValue = resourceUpdater.getAndSet(this, null);
+ if (oldValue != null) {
+ ((AbstractRefCounted)oldValue).unreference();
+ }
+ }
+
+ protected void finalize() throws Throwable {
+ close();
+ }
+}
Modified: projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/FileSystem.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/FileSystem.java 2009-09-30 17:04:31 UTC (rev 94170)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/FileSystem.java 2009-09-30 18:16:31 UTC (rev 94171)
@@ -142,6 +142,14 @@
List<String> getDirectoryEntries(VirtualFile mountPoint, VirtualFile target);
/**
+ * Get a handle to this filesystem. Throws an exception if the filesystem was already closed.
+ *
+ * @return
+ * @throws IOException
+ */
+ Handle<? extends FileSystem> getHandle() throws IOException;
+
+ /**
* Destroy this filesystem instance. After this method is called, the filesystem may not be used in any way. This
* method should be called only after all mounts of this filesystem have been cleared; otherwise, VFS accesses may
* result in {@code IOException}s.
Added: projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/Handle.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/Handle.java (rev 0)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/Handle.java 2009-09-30 18:16:31 UTC (rev 94171)
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, JBoss, a division of Red Hat, Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.vfs.spi;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+/**
+ * A counted reference to some closeable resource.
+ */
+public interface Handle<T extends Closeable> extends Closeable {
+
+ /**
+ * Get the referenced resource. If this handle is closed, a {@code java.io.IOException} is thrown.
+ *
+ * @return the resource
+ * @throws IOException if this handle was already closed
+ */
+ T getResource() throws IOException;
+}
Modified: projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/JZipFileSystem.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/JZipFileSystem.java 2009-09-30 17:04:31 UTC (rev 94170)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/JZipFileSystem.java 2009-09-30 18:16:31 UTC (rev 94171)
@@ -52,7 +52,7 @@
* This implementation is backed by a zip file. The provided file must be owned by this instance; otherwise, if the
* file disappears unexpectedly, the filesystem will malfunction.
*/
-public final class JZipFileSystem implements FileSystem {
+public final class JZipFileSystem extends AbstractRefCounted implements FileSystem {
private final File zipFile;
private final long zipTime;
@@ -239,10 +239,14 @@
return true;
}
- public void close() throws IOException {
+ protected void doClose() throws IOException {
tempDir.close();
}
+ public Handle<? extends JZipFileSystem> getHandle() throws IOException {
+ return doGetHandle();
+ }
+
private static final class ZipNode {
// immutable child map
Modified: projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/JavaZipFileSystem.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/JavaZipFileSystem.java 2009-09-30 17:04:31 UTC (rev 94170)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/JavaZipFileSystem.java 2009-09-30 18:16:31 UTC (rev 94171)
@@ -33,7 +33,6 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
-import java.io.Closeable;
import java.io.BufferedOutputStream;
import java.util.List;
import java.util.Iterator;
@@ -52,7 +51,7 @@
* This implementation is backed by a zip file. The provided file must be owned by this instance; otherwise, if the
* file disappears unexpectedly, the filesystem will malfunction.
*/
-public final class JavaZipFileSystem implements FileSystem {
+public final class JavaZipFileSystem extends AbstractRefCounted implements FileSystem {
private final ZipFile zipFile;
private final File archiveFile;
@@ -253,12 +252,12 @@
return true;
}
- public void close() throws IOException {
- VFSUtils.safeClose(new Closeable() {
- public void close() throws IOException {
- zipFile.close();
- }
- });
+ public Handle<? extends JavaZipFileSystem> getHandle() throws IOException {
+ return doGetHandle();
+ }
+
+ protected void doClose() throws IOException {
+ VFSUtils.safeClose(zipFile);
tempDir.close();
}
Modified: projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/RealFileSystem.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/RealFileSystem.java 2009-09-30 17:04:31 UTC (rev 94170)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/RealFileSystem.java 2009-09-30 18:16:31 UTC (rev 94171)
@@ -35,7 +35,7 @@
/**
* A real filesystem.
*/
-public final class RealFileSystem implements FileSystem {
+public final class RealFileSystem extends AbstractRefCounted implements FileSystem {
/**
* The root real filesystem (singleton instance).
@@ -121,10 +121,15 @@
return names == null ? Collections.<String>emptyList() : Arrays.asList(names);
}
+ /** {@inheritDoc} */
+ public Handle<? extends RealFileSystem> getHandle() throws IOException {
+ return doGetHandle();
+ }
+
/**
* {@inheritDoc}
*/
- public void close() throws IOException {
+ protected void doClose() throws IOException {
// no operation - the real FS can't be closed
}
}
More information about the jboss-cvs-commits
mailing list