[jboss-cvs] JBossAS SVN: r97652 - projects/jboss-deployers/branches/vfs3/deployers-vfs/src/main/java/org/jboss/deployers/vfs.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Dec 9 20:36:21 EST 2009
Author: david.lloyd at jboss.com
Date: 2009-12-09 20:36:20 -0500 (Wed, 09 Dec 2009)
New Revision: 97652
Added:
projects/jboss-deployers/branches/vfs3/deployers-vfs/src/main/java/org/jboss/deployers/vfs/Refcountable.java
Log:
Example refcountable implementation
Added: projects/jboss-deployers/branches/vfs3/deployers-vfs/src/main/java/org/jboss/deployers/vfs/Refcountable.java
===================================================================
--- projects/jboss-deployers/branches/vfs3/deployers-vfs/src/main/java/org/jboss/deployers/vfs/Refcountable.java (rev 0)
+++ projects/jboss-deployers/branches/vfs3/deployers-vfs/src/main/java/org/jboss/deployers/vfs/Refcountable.java 2009-12-10 01:36:20 UTC (rev 97652)
@@ -0,0 +1,93 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.deployers.vfs;
+
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
+import java.io.Closeable;
+import java.io.IOException;
+
+public abstract class Refcountable<T> implements Closeable
+{
+ private final T resource;
+ private volatile int cnt;
+
+ private static final AtomicIntegerFieldUpdater<Refcountable> cntUpdater = AtomicIntegerFieldUpdater.newUpdater(Refcountable.class, "cnt");
+
+ private static final int CLOSED_VAL = -0x40000000;
+
+ protected Refcountable(T resource)
+ {
+ this.resource = resource;
+ }
+
+ public interface Handle<T> extends Closeable
+ {
+ T get();
+ }
+
+ public final void close() throws IOException
+ {
+ if (cntUpdater.getAndSet(this, CLOSED_VAL) < 0) {
+ closeAction();
+ }
+ }
+
+ public final Handle<T> getHandle() throws IOException {
+ if (cntUpdater.getAndIncrement(this) < 0) {
+ cntUpdater.getAndDecrement(this);
+ throw new IOException("Resource is already closed");
+ } else {
+ return new HandleImpl<T>(getHandle().get(), this);
+ }
+ }
+
+ protected abstract void closeAction() throws IOException;
+
+ private static final class HandleImpl<T> implements Handle<T>
+ {
+ private final Refcountable<T> refcountable;
+ private final T resource;
+ private volatile int closed = 0;
+
+ private static final AtomicIntegerFieldUpdater<HandleImpl> closedUpdater = AtomicIntegerFieldUpdater.newUpdater(HandleImpl.class, "closed");
+
+ private HandleImpl(T resource, Refcountable<T> refcountable)
+ {
+ this.resource = resource;
+ this.refcountable = refcountable;
+ }
+
+ public T get()
+ {
+ return resource;
+ }
+
+ public void close() throws IOException
+ {
+ if (closedUpdater.compareAndSet(this, 0, 1) && 0 == cntUpdater.decrementAndGet(refcountable) && cntUpdater.compareAndSet(refcountable, 0, CLOSED_VAL)) {
+ // we caused the resource to close
+ refcountable.close();
+ }
+ }
+ }
+}
More information about the jboss-cvs-commits
mailing list