commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ima...@apache.org
Subject svn commit: r179591 - /jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/tasks/VfsTask.java
Date Thu, 02 Jun 2005 13:38:51 GMT
Author: imario
Date: Thu Jun  2 06:38:47 2005
New Revision: 179591

URL: http://svn.apache.org/viewcvs?rev=179591&view=rev
Log:
*) Hold reference to VFS per project.
*) PR: 34942
Reported By: Jacob Kjome <hoju -at- visi.com>
Close FilesystemManager even if called nested.

Modified:
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/tasks/VfsTask.java

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/tasks/VfsTask.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/tasks/VfsTask.java?rev=179591&r1=179590&r2=179591&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/tasks/VfsTask.java (original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/tasks/VfsTask.java Thu
Jun  2 06:38:47 2005
@@ -1,12 +1,12 @@
 /*
  * Copyright 2002-2005 The Apache Software Foundation.
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -18,6 +18,7 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.vfs.FileObject;
 import org.apache.commons.vfs.FileSystemException;
+import org.apache.commons.vfs.VFS;
 import org.apache.commons.vfs.impl.StandardFileSystemManager;
 import org.apache.tools.ant.BuildEvent;
 import org.apache.tools.ant.BuildListener;
@@ -35,7 +36,22 @@
 public class VfsTask
     extends Task
 {
-    private static StandardFileSystemManager manager;
+    // private static StandardFileSystemManager manager;
+
+    /**
+     * Hold the reference to VFS and a refcounter.
+     *
+     * For every "target" which is called within the current target the refcount is incremented.
+     * When the target ends this refcount is decremented.
+     * If 0 is reached VFS will shutdown.
+     */
+    private static class VfsRef
+    {
+        StandardFileSystemManager manager;
+
+        // start with refcount 1 as we are in an ant "target"
+        volatile int refcount = 1;
+    }
 
     /**
      * Resolves a URI to a file, relative to the project's base directory.
@@ -45,14 +61,26 @@
     protected FileObject resolveFile(final String uri)
         throws FileSystemException
     {
-        if (manager == null)
+        VfsRef vfsRef = (VfsRef) getProject().getReference(VFS.class.getName());
+        if (vfsRef == null)
+        {
+            vfsRef = new VfsRef();
+        }
+
+        synchronized(vfsRef)
         {
-            manager = new StandardFileSystemManager();
-            manager.setLogger(new AntLogger());
-            manager.init();
-            getProject().addBuildListener(new CloseListener());
+            if (vfsRef.manager == null)
+            {
+                vfsRef.manager = new StandardFileSystemManager();
+                vfsRef.manager.setLogger(new AntLogger());
+                vfsRef.manager.init();
+                getProject().addBuildListener(new CloseListener());
+
+                getProject().addReference(VFS.class.getName(), vfsRef);
+            }
         }
-        return manager.resolveFile(getProject().getBaseDir(), uri);
+
+        return vfsRef.manager.resolveFile(getProject().getBaseDir(), uri);
     }
 
     /**
@@ -61,17 +89,33 @@
     private class CloseListener
         implements BuildListener
     {
+        public void subBuildStarted(BuildEvent event)
+        {
+            // event.getProject().log("subbuild started", Project.MSG_ERR);
+        }
+
+        public void subBuildFinished(BuildEvent event)
+        {
+            // event.getProject().log("subbuild finished", Project.MSG_ERR);
+        }
+
         public void buildFinished(BuildEvent event)
         {
-            if (manager != null)
+            // event.getProject().log("build finished", Project.MSG_ERR);
+
+            /*
+            VfsRef vfsRef = (VfsRef) getProject().getReference(VFS.class.getName());
+            if (vfsRef != null)
             {
-                manager.close();
-                manager = null;
+                vfsRef.manager.close();
+                vfsRef = null;
             }
+            */
         }
 
         public void buildStarted(BuildEvent event)
         {
+            // event.getProject().log("build started", Project.MSG_ERR);
         }
 
         public void messageLogged(BuildEvent event)
@@ -80,18 +124,53 @@
 
         public void targetFinished(BuildEvent event)
         {
+            // event.getProject().log("target finished", Project.MSG_ERR);
+
+            VfsRef vfsRef = (VfsRef) getProject().getReference(VFS.class.getName());
+            if (vfsRef != null)
+            {
+                synchronized(vfsRef)
+                {
+                    if (vfsRef.manager != null)
+                    {
+                        vfsRef.refcount--;
+                        if (vfsRef.refcount < 1)
+                        {
+                            vfsRef.manager.close();
+                            vfsRef.manager = null;
+                        }
+                    }
+
+                    getProject().removeBuildListener(CloseListener.this);
+                }
+            }
         }
 
         public void targetStarted(BuildEvent event)
         {
+            // event.getProject().log("target started", Project.MSG_ERR);
+
+            VfsRef vfsRef = (VfsRef) getProject().getReference(VFS.class.getName());
+            if (vfsRef != null)
+            {
+                synchronized(vfsRef)
+                {
+                    if (vfsRef.manager != null)
+                    {
+                        vfsRef.refcount++;
+                    }
+                }
+            }
         }
 
         public void taskFinished(BuildEvent event)
         {
+            // event.getProject().log("task finished", Project.MSG_ERR);
         }
 
         public void taskStarted(BuildEvent event)
         {
+            // event.getProject().log("task started", Project.MSG_ERR);
         }
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message