/[Apache-SVN]/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/tasks/VfsTask.java
ViewVC logotype

Diff of /jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/tasks/VfsTask.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/tasks/VfsTask.java	2005/06/02 13:08:04	179590
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/tasks/VfsTask.java	2005/06/02 13:38:47	179591
@@ -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 @@ package org.apache.commons.vfs.tasks;
 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 @@ import org.apache.tools.ant.Task;
 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 @@ public class VfsTask
     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 @@ public class VfsTask
     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 class VfsTask
 
         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);
         }
     }
 

 

infrastructure at apache.org
ViewVC Help
Powered by ViewVC 1.1.26