Author: mbenson
Date: Mon Jun 30 10:59:37 2008
New Revision: 672850

URL: http://svn.apache.org/viewvc?rev=672850&view=rev
Log:
initial code for filesystem cache antlib

Added:
    ant/sandbox/antlibs/fscache/trunk/NOTICE   (with props)
    ant/sandbox/antlibs/fscache/trunk/build.xml   (with props)
    ant/sandbox/antlibs/fscache/trunk/changes.xml   (with props)
    ant/sandbox/antlibs/fscache/trunk/contributors.xml   (with props)
    ant/sandbox/antlibs/fscache/trunk/docs/
    ant/sandbox/antlibs/fscache/trunk/docs/index.html   (with props)
    ant/sandbox/antlibs/fscache/trunk/docs/style.css   (with props)
    ant/sandbox/antlibs/fscache/trunk/src/
    ant/sandbox/antlibs/fscache/trunk/src/main/
    ant/sandbox/antlibs/fscache/trunk/src/main/org/
    ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/
    ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/
    ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/
    
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/CachedCollection.java
   (with props)
    
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/CachedResource.java
   (with props)
    
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/Preload.java  
 (with props)
    
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/antlib.xml   
(with props)
    ant/sandbox/antlibs/fscache/trunk/src/tests/
    ant/sandbox/antlibs/fscache/trunk/src/tests/antunit/
    ant/sandbox/antlibs/fscache/trunk/src/tests/antunit/fscache-test.xml   
(with props)

Added: ant/sandbox/antlibs/fscache/trunk/NOTICE
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/fscache/trunk/NOTICE?rev=672850&view=auto
==============================================================================
--- ant/sandbox/antlibs/fscache/trunk/NOTICE (added)
+++ ant/sandbox/antlibs/fscache/trunk/NOTICE Mon Jun 30 10:59:37 2008
@@ -0,0 +1,11 @@
+   =========================================================================
+   ==  NOTICE file corresponding to the section 4 d of                    ==
+   ==  the Apache License, Version 2.0                                    ==
+   =========================================================================
+
+   Apache Ant Property Extensions
+   Copyright 2008 The Apache Software Foundation
+
+   This product includes software developed by
+   The Apache Software Foundation (http://www.apache.org/).
+

Propchange: ant/sandbox/antlibs/fscache/trunk/NOTICE
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ant/sandbox/antlibs/fscache/trunk/build.xml
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/fscache/trunk/build.xml?rev=672850&view=auto
==============================================================================
--- ant/sandbox/antlibs/fscache/trunk/build.xml (added)
+++ ant/sandbox/antlibs/fscache/trunk/build.xml Mon Jun 30 10:59:37 2008
@@ -0,0 +1,29 @@
+<?xml version="1.0"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you 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.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+<project default="compile" name="fscache">
+
+  <!-- easy way to override properties -->
+  <property file="build.properties"/>
+
+  <!-- don't fork junit; regexp classes not available -->
+  <property name="junit.fork" value="false" />
+
+  <import file="common/build.xml"/>
+</project>

Propchange: ant/sandbox/antlibs/fscache/trunk/build.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ant/sandbox/antlibs/fscache/trunk/changes.xml
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/fscache/trunk/changes.xml?rev=672850&view=auto
==============================================================================
--- ant/sandbox/antlibs/fscache/trunk/changes.xml (added)
+++ ant/sandbox/antlibs/fscache/trunk/changes.xml Mon Jun 30 10:59:37 2008
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE document
+[
+<!ELEMENT document (properties?, release+)>
+<!ELEMENT properties (title)>
+<!ELEMENT title (#PCDATA)>
+<!ELEMENT release (action*)>
+<!ELEMENT action (#PCDATA)>
+<!ATTLIST release
+          version CDATA #REQUIRED
+          date CDATA #REQUIRED
+          description CDATA #IMPLIED>
+<!ATTLIST action
+          issue CDATA #IMPLIED
+          type (add|fix|remove|update) #REQUIRED
+          breaks-bwc (true|false) #IMPLIED>
+]
+>
+<document>
+  <properties>
+    <title>Apache Ant FSCache</title>
+  </properties>
+
+  <release version="SVN trunk" date="unpublished">
+    <!--action type="fix" issue="42745">
+      assertFileExists example wrong, should use @file instead of @name
+    </action-->
+  </release>
+
+</document>

Propchange: ant/sandbox/antlibs/fscache/trunk/changes.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ant/sandbox/antlibs/fscache/trunk/contributors.xml
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/fscache/trunk/contributors.xml?rev=672850&view=auto
==============================================================================
--- ant/sandbox/antlibs/fscache/trunk/contributors.xml (added)
+++ ant/sandbox/antlibs/fscache/trunk/contributors.xml Mon Jun 30 10:59:37 2008
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE contributors
+[
+<!ELEMENT name (first?, middle?, last)>
+<!ELEMENT contributors (introduction, name+)>
+<!ELEMENT first (#PCDATA)>
+<!ELEMENT introduction (#PCDATA)>
+<!ELEMENT middle (#PCDATA)>
+<!ELEMENT last (#PCDATA)>
+]
+>
+
+<contributors>
+  <introduction>
+  These are some of the people who have helped with this Ant Library.
+  </introduction>
+  <name>
+    <first>Matt</first>
+    <last>Benson</last>
+  </name>
+</contributors>

Propchange: ant/sandbox/antlibs/fscache/trunk/contributors.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ant/sandbox/antlibs/fscache/trunk/docs/index.html
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/fscache/trunk/docs/index.html?rev=672850&view=auto
==============================================================================
--- ant/sandbox/antlibs/fscache/trunk/docs/index.html (added)
+++ ant/sandbox/antlibs/fscache/trunk/docs/index.html Mon Jun 30 10:59:37 2008
@@ -0,0 +1,190 @@
+<!--
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You 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.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+-->
+<html>
+  <head>
+    <meta http-equiv="Content-Language" content="en-us"></meta>
+    <link rel="stylesheet" type="text/css" href="style.css">
+    <title>FSCache Ant Library</title>
+  </head>
+
+  <body>
+    <h2>Introduction</h2>
+
+    <p>This is a library of tasks/types to allow arbitrary resources to be,
+       as transparently as possible, cached to the filesystem for use with
+       tasks that require file resources, e.g. Ant's core &lt;unzip&gt; and
+       &lt;javac&gt; tasks among others.</p>
+
+    <h2>Requirements</h2>
+
+    <p>The current version of this antlib is built against the Ant 1.8.x 
development version,
+       to be found in Ant's Subversion trunk.
+    </p>
+
+    <h2>Where is it?</h2>
+
+    <p>The source code for the library lives in the sandbox antlibs subspace 
of Ant's SVN -
+    <a 
href="http://svn.apache.org/viewvc/ant/sandbox/antlibs/fscache/trunk/";>http://svn.apache.org/viewvc/ant/sandbox/antlibs/fscache/trunk/</a>.</p>
+
+    <h2>Installation</h2>
+
+    <p>If you are building this from sources, run the antlib target
+    and you'll get a file <code>ant-fscache.jar</code>.</p>
+
+    <p>There are several ways to use the tasks:</p>
+
+    <ul>
+      <li><p>The traditional way:
+        <pre>
+          &lt;typedef resource="org/apache/ant/fscache/antlib.xml"
+                   classpath="YOUR-PATH-TO/ant-fscache.jar"/&gt;
+        </pre>
+
+        Using this approach the provided extensions will live in the default 
namespace.
+        Note that the antlib is written in such a way that the default 
task/typenames
+        (collect, single, preload) may be too generic and should either be
+        explicitly overridden or used with a namespace.</p>
+      </li>
+
+      <li><p>Similar, but assigning a namespace URI
+        <pre>
+          &lt;typedef uri="antlib:org.apache.ant.fscache"
+                   resource="org/apache/ant/fscache/antlib.xml"
+                   classpath="YOUR-PATH-TO/ant-fscache.jar"/&gt;
+        </pre>
+
+        Placing the properties extensions into their own namespace, usable 
e.g.:
+
+        <pre>
+          &lt;project xmlns:fscache="antlib:org.apache.ant.fscache"&gt;
+            ...
+            &lt;collect&gt;
+              &lt;!-- other resources --&gt;
+            &lt;/collect&gt;
+        </pre>
+
+        or a variation thereof.</p>
+      </li>
+
+      <li><p>Using Ant's autodiscovery.  Place <code>ant-fscache.jar</code>
+      into a directory and use <code>ant -lib
+      DIR-CONTAINING-THE-JAR</code> or copy it into
+      <code>ANT_HOME/lib</code> - and then in your build file, simply
+      declare the namespace on the <code>project</code> tag:
+
+        <pre>
+          &lt;project xmlns:fscache="antlib:org.apache.ant.fscache"&gt;
+        </pre>
+
+        And all tasks of this library will automatically be available
+        in the <code>fscache</code> namespace without any 
<code>typedef</code>.</p>
+      </li>
+    </ul>
+
+    <h2>Tasks and Types</h2>
+
+    <a name="collect" />
+    <h3>collect</h3>
+    <p>Presents nested <a 
href="http://ant.apache.org/manual/CoreTypes/resources.html#collection";>
+      Resource Collection</a>s of arbitrary types such that they live in a 
single directory
+      structure on the filesystem.</p>
+
+    <table border="1" cellpadding="2" cellspacing="0">
+      <tr>
+        <td valign="top"><b>Attribute</b></td>
+        <td valign="top"><b>Description</b></td>
+        <td align="center" valign="top"><b>Required</b></td>
+      </tr>
+      <tr>
+        <td valign="top">basedir</td>
+        <td valign="top">Directory that will hold the cache.</td>
+        <td align="center" valign="top">No, default 
<i>${java.io.tmpdir}</i></td>
+      </tr>
+      <tr>
+        <td valign="top">deleteonexit</td>
+        <td valign="top">Whether cached resources should be deleted on JVM 
exit via
+          <code>java.io.File#deleteOnExit()</code>. This may or may not be 
reliable.
+        </td>
+        <td align="center" valign="top">No, defaults to <i>false</i> if 
<code>basedir</code>
+          is specified, <i>true</i> otherwise.
+        </td>
+      </tr>
+      <tr>
+        <td valign="top">enablemultiplemappings</td>
+        <td valign="top">Whether resources will be created for all names 
(rather than the
+          first only) returned from the configured
+          <a href="http://ant.apache.org/manual/CoreTypes/mapper.html";>File 
Mapper</a>, if present.
+        </td>
+        <td align="center" valign="top">No, default <i>false</i></td>
+      </tr>
+    </table>
+    <h4>Parameters specified as nested elements</h4>
+    <p>One or more nested
+      <a 
href="http://ant.apache.org/manual/CoreTypes/resources.html#collection";>Resource
+      Collections</a> must be specified.</p>
+
+    <p>A nested <a 
href="http://ant.apache.org/manual/CoreTypes/mapper.html";>File Mapper</a>
+      may be specified.</p>
+
+    <a name="single" />
+    <h3>single</h3>
+    <p>Filesystem cache for a single nested
+      <a 
href="http://ant.apache.org/manual/CoreTypes/resources.html";>Resource</a>.
+      The specific file used can be specified explicitly or derived using the 
name of
+      the nested resource.</p>
+
+    <p>This type inherits from the
+      <a 
href="http://ant.apache.org/manual/CoreTypes/resources.html#file";>file</a> 
resource type
+      and therefore supports its same attributes in addition to the 
following:</p>
+
+    <table border="1" cellpadding="2" cellspacing="0">
+      <tr>
+        <td valign="top"><b>Attribute</b></td>
+        <td valign="top"><b>Description</b></td>
+        <td align="center" valign="top"><b>Required</b></td>
+      </tr>
+      <tr>
+        <td valign="top">deleteonexit</td>
+        <td valign="top">Whether the cached resource should be deleted on JVM 
exit via
+          <code>java.io.File#deleteOnExit()</code>. This may or may not be 
reliable.
+        </td>
+        <td align="center" valign="top">No, defaults to <i>false</i> if 
<code>basedir</code>
+          or <code>file</code> is specified, <i>true</i> otherwise.
+        </td>
+      </tr>
+    </table>
+
+    <h4>Parameters specified as nested elements</h4>
+    <p>A single nested 
+      <a 
href="http://ant.apache.org/manual/CoreTypes/resources.html";>Resource</a>
+      is required.</p>
+
+    <a name="preload" />
+    <h3>preload</h3>
+    <p>Task to preload cached resources onto disk. This is necessary when one 
needs to
+      use a filesystem cache with e.g. directory-based tasks such as
+      <a 
href="http://ant.apache.org/manual/CoreTasks/javac.html";>Javac</a>.</p>
+
+    <h4>Parameters specified as nested elements</h4>
+    <p>One or more nested
+      <a 
href="http://ant.apache.org/manual/CoreTypes/resources.html#collection";>Resource
+      Collection</a>s must be specified. It is considered an error condition 
if any resource
+      returned from these is not a cached resource.</p>
+
+    <hr/>
+  </body>
+</html>

Propchange: ant/sandbox/antlibs/fscache/trunk/docs/index.html
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ant/sandbox/antlibs/fscache/trunk/docs/style.css
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/fscache/trunk/docs/style.css?rev=672850&view=auto
==============================================================================
--- ant/sandbox/antlibs/fscache/trunk/docs/style.css (added)
+++ ant/sandbox/antlibs/fscache/trunk/docs/style.css Mon Jun 30 10:59:37 2008
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+h2 {
+  font-size: 200%;
+  background-color: ffffff;
+}
+
+h3 {
+  font-size: 130%;
+  color:     #ffffff;
+  background-color: #525D76;
+}
+
+h4 {
+  color:  #ffffff;
+  background-color: #828DA6;
+}
+
+td {
+   background-color: eeeeee;
+   color:            000000;
+}
+
+ /* first row */
+table tr:first-child td {
+   background-color: cccccc;
+   color:            000000;
+}
+
+ /* or th as first row */
+table th {
+   background-color: cccccc;
+   color:            000000;
+}
+
+pre {
+   background-color: efefef;
+}

Propchange: ant/sandbox/antlibs/fscache/trunk/docs/style.css
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/CachedCollection.java
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/CachedCollection.java?rev=672850&view=auto
==============================================================================
--- 
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/CachedCollection.java
 (added)
+++ 
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/CachedCollection.java
 Mon Jun 30 10:59:37 2008
@@ -0,0 +1,193 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You 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.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.ant.fscache;
+
+import java.io.File;
+import java.util.Iterator;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.types.Mapper;
+import org.apache.tools.ant.types.Resource;
+import org.apache.tools.ant.types.resources.Resources;
+import org.apache.tools.ant.util.FileNameMapper;
+
+/**
+ * Collects the Resources from one or more nested ResourceCollections under a 
single
+ * directory on the filesystem, for tasks that require files to operate.
+ */
+public class CachedCollection extends Resources {
+    private class MyIterator implements Iterator {
+        private Iterator wrapped;
+        private CachedResource[] set;
+        private int pos;
+
+        /**
+         * Create a new MyIterator.
+         */
+        MyIterator() {
+            this.wrapped = CachedCollection.super.iterator();
+        }
+
+        /**
+         * [EMAIL PROTECTED]
+         */
+        public boolean hasNext() {
+            return set != null && pos < set.length || wrapped.hasNext();
+        }
+
+        /**
+         * [EMAIL PROTECTED]
+         */
+        public Object next() {
+            if (set != null && pos < set.length) {
+                return set[pos++];
+            }
+            do {
+                set = wrap((Resource) wrapped.next());
+            } while (set.length == 0);
+            pos = 1;
+            return set[0];
+        }
+
+        /**
+         * [EMAIL PROTECTED]
+         */
+        public void remove() {
+            throw new UnsupportedOperationException();
+        }
+
+    }
+
+    private File basedir;
+    private Boolean deleteOnExit;
+    private FileNameMapper fileNameMapper;
+    private boolean enableMultipleMappings;
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public boolean isFilesystemOnly() {
+        return true;
+    }
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public synchronized Iterator iterator() {
+        return new MyIterator();
+    }
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public synchronized int size() {
+        if (fileNameMapper == null || !enableMultipleMappings) {
+            return super.size();
+        }
+        int result = 0;
+        for (Iterator iter = super.iterator(); iter.hasNext();) {
+            String[] s = fileNameMapper.mapFileName(((Resource) 
iter.next()).getName());
+            result += s == null || s.length == 0 ? 1 : s.length;
+        }
+        return result;
+    }
+
+    /**
+     * Set the basedir. Defaults to a generated directory under 
<code>${java.io.tmpdir}</code>.
+     * @param basedir the File to set
+     */
+    public void setBasedir(File basedir) {
+        this.basedir = basedir;
+        invalidateExistingIterators();
+    }
+
+    /**
+     * Set the deleteOnExit. Default behavior is <code>false</code> if 
<code>basedir</code> is set, <code>true</code> otherwise.
+     * @param deleteOnExit the boolean to set
+     */
+    public void setDeleteOnExit(boolean deleteOnExit) {
+        this.deleteOnExit = deleteOnExit ? Boolean.TRUE : Boolean.FALSE;
+        invalidateExistingIterators();
+    }
+
+    /**
+     * Add the FileNameMapper to assist in converting resource names to 
filesystem paths below <code>basedir</code>.
+     * @param fileNameMapper to add
+     */
+    public synchronized void addConfigured(FileNameMapper fileNameMapper) {
+        if (this.fileNameMapper != null) {
+            throw new BuildException("Cannot add > 1 filenamemapper");
+        }
+        this.fileNameMapper = fileNameMapper;
+        invalidateExistingIterators();
+    }
+
+    /**
+     * Add a FileNameMapper using the <mapper> element.
+     * @param mapper to add
+     */
+    public void addConfigured(Mapper mapper) {
+        addConfigured(mapper.getImplementation());
+    }
+
+    /**
+     * Set the enableMultipleMappings. Default is false.
+     * @param enableMultipleMappings the boolean to set
+     */
+    public void setEnableMultipleMappings(boolean enableMultipleMappings) {
+        this.enableMultipleMappings = enableMultipleMappings;
+    }
+
+    private CachedResource[] wrap(Resource resource) {
+        boolean del = Boolean.TRUE == deleteOnExit || deleteOnExit == null && 
basedir == null;
+        File dir = basedir == null ? generateTempDir(del) : basedir;
+        getProject().log("FilesystemCache del = " + del, Project.MSG_DEBUG);
+        String[] names = fileNameMapper == null ? null : 
fileNameMapper.mapFileName(resource
+                .getName());
+        int count = names != null && names.length > 0 && 
enableMultipleMappings ? names.length : 1;
+        CachedResource[] result = new CachedResource[count];
+        if (names == null || names.length == 0) {
+            result[0] = new CachedResource(getProject(), resource, dir,
+                    resource.getName(), del);
+        } else {
+            for (int i = 0; i < count; i++) {
+                result[i] = new CachedResource(getProject(), resource, dir, 
names[i], del);
+            }
+        }
+        return result;
+    }
+
+    private static synchronized File generateTempDir(boolean deleteOnExit) {
+        try {
+            File result = new File(System.getProperty("java.io.tmpdir"),
+                    "org.apache.tools.ant.types.resources.FilesystemCache"
+                            + System.currentTimeMillis());
+            if (deleteOnExit) {
+                result.deleteOnExit();
+            }
+            return result;
+        } finally {
+            try {
+                Thread.sleep(1);
+            } catch (InterruptedException e) {
+                // ignore
+            }
+        }
+    }
+}

Propchange: 
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/CachedCollection.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/CachedResource.java
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/CachedResource.java?rev=672850&view=auto
==============================================================================
--- 
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/CachedResource.java
 (added)
+++ 
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/CachedResource.java
 Mon Jun 30 10:59:37 2008
@@ -0,0 +1,139 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You 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.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.ant.fscache;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.types.Resource;
+import org.apache.tools.ant.types.resources.FileResource;
+import org.apache.tools.ant.util.ResourceUtils;
+
+/**
+ * Resource that caches another Resource to the filesystem before returning 
its content.
+ */
+public class CachedResource extends FileResource {
+    private Resource wrapped;
+    private boolean deleteOnExit;
+
+    private boolean cached;
+
+    /**
+     * Create a new FilesystemCacheResource.
+     */
+    public CachedResource() {
+    }
+
+    /**
+     * Create a new FilesystemCacheResource.
+     */
+    public CachedResource(Project project, Resource wrapped, File basedir, 
String name,
+            boolean deleteOnExit) {
+        super(basedir, name);
+        setProject(project);
+        this.wrapped = wrapped;
+        this.deleteOnExit = deleteOnExit;
+    }
+
+    /**
+     * Add the wrapped Resource.
+     * @param wrapped to set
+     */
+    public void addConfigured(Resource wrapped) {
+        if (this.wrapped != null) {
+            throw new BuildException("Cannot wrap > 1 Resource");
+        }
+        this.wrapped = wrapped;
+        
+    }
+
+    /**
+     * Set whether to delete the cached content on JVM exit. Defaults to 
<code>false</code>
+     * if <code>file</code> or <code>basedir</code> have been specified.
+     * @param deleteOnExit to set
+     */
+    public void setDeleteOnExit(boolean deleteOnExit) {
+        this.deleteOnExit = deleteOnExit;
+    }
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public File getFile() {
+        cacheContent();
+        return super.getFile();
+    }
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public InputStream getInputStream() throws IOException {
+        cacheContent();
+        return super.getInputStream();
+    }
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public OutputStream getOutputStream() throws IOException {
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public void setFile(File f) {
+        super.setFile(f);
+        cached = false;
+    }
+
+    private synchronized void cacheContent() {
+        if (cached) {
+            return;
+        }
+        if (wrapped == null) {
+            throw new BuildException("A wrapped Resource is required");
+        }
+        boolean del = deleteOnExit;
+        File f = super.getFile();
+        if (f == null) {
+            File basedir = super.getBaseDir();
+            if (basedir == null) {
+                basedir = new File(System.getProperty("java.io.tmpdir"));
+                setBaseDir(basedir);
+                del = true;
+            }
+            f = new File(basedir, wrapped.getName());
+            setFile(f);
+        }
+        try {
+            ResourceUtils.copyResource(wrapped, new FileResource(f), null, 
null, true, true, null,
+                    null, getProject());
+        } catch (IOException e) {
+            throw new BuildException(e);
+        }
+        if (del) {
+            f.deleteOnExit();
+        }
+        cached = true;
+    }
+}

Propchange: 
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/CachedResource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/Preload.java
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/Preload.java?rev=672850&view=auto
==============================================================================
--- 
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/Preload.java 
(added)
+++ 
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/Preload.java 
Mon Jun 30 10:59:37 2008
@@ -0,0 +1,69 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You 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.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.ant.fscache;
+
+import java.util.Iterator;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Task;
+import org.apache.tools.ant.types.ResourceCollection;
+import org.apache.tools.ant.types.resources.Resources;
+
+/**
+ * Task to force cached resources to be written to disk, for use with
+ * tasks that operate on a directory structure rather than individual files 
(e.g. <javac>).
+ */
+public class Preload extends Task {
+    private static final String INCOMPLETE = "At least one CachedCollection or 
CachedResource is required";
+    private static final String WRONG_TYPE = "Only CachedResources are 
supported";
+
+    private Resources resources;
+
+    /**
+     * Add the specified ResourceCollection<CachedResource>.
+     * @param resourceCollection to add
+     */
+    public void addConfigured(ResourceCollection resourceCollection) {
+        if (resourceCollection == null) {
+            throw new IllegalArgumentException("Cannot add null 
ResourceCollection");
+        }
+        synchronized (this) {
+            if (resources == null) {
+                resources = new Resources();
+            }
+        }
+        resources.add(resourceCollection);
+    }
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public void execute() {
+        if (this.resources == null) {
+            throw new BuildException(INCOMPLETE);
+        }
+        for (Iterator iter = resources.iterator(); iter.hasNext();) {
+            Object o = iter.next();
+            if (o instanceof CachedResource == false) {
+                throw new BuildException(WRONG_TYPE);
+            }
+            //force the file to be written:
+            ((CachedResource) o).getFile();
+        }
+    }
+}

Propchange: 
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/Preload.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/antlib.xml
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/antlib.xml?rev=672850&view=auto
==============================================================================
--- 
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/antlib.xml 
(added)
+++ 
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/antlib.xml 
Mon Jun 30 10:59:37 2008
@@ -0,0 +1,24 @@
+<?xml version="1.0"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you 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.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+<antlib>
+  <taskdef name="preload" classname="org.apache.ant.fscache.Preload" />
+  <typedef name="collect" classname="org.apache.ant.fscache.CachedCollection" 
/>
+  <typedef name="single" classname="org.apache.ant.fscache.CachedResource" />
+</antlib>

Propchange: 
ant/sandbox/antlibs/fscache/trunk/src/main/org/apache/ant/fscache/antlib.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ant/sandbox/antlibs/fscache/trunk/src/tests/antunit/fscache-test.xml
URL: 
http://svn.apache.org/viewvc/ant/sandbox/antlibs/fscache/trunk/src/tests/antunit/fscache-test.xml?rev=672850&view=auto
==============================================================================
--- ant/sandbox/antlibs/fscache/trunk/src/tests/antunit/fscache-test.xml (added)
+++ ant/sandbox/antlibs/fscache/trunk/src/tests/antunit/fscache-test.xml Mon 
Jun 30 10:59:37 2008
@@ -0,0 +1,181 @@
+<?xml version="1.0"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You 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.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+
+<project name="test-fscache"
+         xmlns:fscache="antlib:org.apache.ant.fscache"
+         xmlns:au="antlib:org.apache.ant.antunit">
+
+  <property name="out.dir" location="tempdir"/>
+  <property name="zip.filename" value="stuff.zip" />
+  <property name="zip.file" location="${out.dir}/${zip.filename}" />
+  <property name="nested.zip.filename" value="nonsense.zip" />
+  <property name="nested.zip.file" 
location="${out.dir}/${nested.zip.filename}" />
+  <property name="testCollect.cache" location="${out.dir}/testCollect" />
+  <property name="java.src.cache" location="${out.dir}/src" />
+
+  <resources id="foobarbaz">
+    <string>foo</string>
+    <string>bar</string>
+    <string>baz</string>
+  </resources>
+
+  <target name="setUp">
+    <mkdir dir="${out.dir}" />
+  </target>
+
+  <target name="tearDown">
+    <delete dir="${out.dir}" quiet="true"/>
+  </target>
+
+  <target name="-setupArchive">
+    <zip destfile="${zip.file}">
+      <resources refid="foobarbaz" />
+    </zip>
+    <au:assertTrue>
+      <resourcecount count="3">
+        <zipfileset src="${zip.file}" />
+      </resourcecount>
+    </au:assertTrue>
+  </target>
+
+  <target name="-setupNestedArchive" depends="-setupArchive">
+    <zip destfile="${nested.zip.file}">
+      <file file="${zip.file}" />
+    </zip>
+    <au:assertTrue>
+      <resourcecount count="1">
+        <zipfileset src="${nested.zip.file}" />
+      </resourcecount>
+    </au:assertTrue>
+  </target>
+
+  <macrodef name="readCollectResource">
+    <attribute name="name" />
+    <attribute name="value" default="@{name}" />
+    <attribute name="refid" />
+    <sequential>
+      <au:assertTrue>
+        <resourcesmatch>
+          <restrict>
+            <resources refid="@{refid}" />
+            <name name="@{name}" />
+          </restrict>
+          <string>@{value}</string>
+        </resourcesmatch>
+      </au:assertTrue>
+    </sequential>
+  </macrodef>
+
+  <target name="testCollect" depends="-setupArchive">
+    <fscache:collect id="cacheTestCollect" basedir="${testCollect.cache}">
+      <zipfileset src="${zip.file}" />
+    </fscache:collect>
+    <au:assertTrue>
+      <resourcecount count="3">
+        <resources refid="cacheTestCollect" />
+      </resourcecount>
+    </au:assertTrue>
+    <readCollectResource name="foo" refid="cacheTestCollect" />
+    <readCollectResource name="bar" refid="cacheTestCollect" />
+    <readCollectResource name="baz" refid="cacheTestCollect" />
+  </target>
+
+  <target name="testMapping" depends="-setupArchive">
+    <fscache:collect id="cacheTestMapping" basedir="${testCollect.cache}">
+      <zipfileset src="${zip.file}" />
+      <globmapper from="*" to="*.txt" />
+    </fscache:collect>
+    <au:assertTrue>
+      <resourcecount count="3">
+        <resources refid="cacheTestMapping" />
+      </resourcecount>
+    </au:assertTrue>
+    <readCollectResource name="foo.txt" value="foo" refid="cacheTestMapping" />
+    <readCollectResource name="bar.txt" value="bar" refid="cacheTestMapping" />
+    <readCollectResource name="baz.txt" value="baz" refid="cacheTestMapping" />
+  </target>
+
+  <target name="testSingle" depends="-setupNestedArchive">
+    <au:assertTrue>
+      <resourcesmatch>
+        <restrict>
+          <zipfileset>
+            <fscache:single>
+              <zipentry name="${zip.filename}" zipfile="${nested.zip.file}" />
+            </fscache:single>
+          </zipfileset>
+          <name name="foo" />
+        </restrict>
+        <string>foo</string>
+      </resourcesmatch>
+    </au:assertTrue>
+  </target>
+
+  <target name="testPreloadSingle">
+    <loadresource property="Foo.java">
+      <string>
+public class Foo {
+  public static void main(String[] args) {
+    System.out.print("foo");
+  }
+}
+      </string>
+    </loadresource>
+
+    <au:assertFalse>
+      <available file="${java.src.cache}/Foo.java" type="file" />
+    </au:assertFalse>
+
+    <fscache:preload>
+      <fscache:single basedir="${java.src.cache}">
+        <propertyresource name="Foo.java" />
+      </fscache:single>
+    </fscache:preload>
+
+    <au:assertTrue>
+      <available file="${java.src.cache}/Foo.java" type="file" />
+    </au:assertTrue>
+
+    <javac srcdir="${java.src.cache}" />
+
+    <au:assertTrue>
+      <available file="${java.src.cache}/Foo.class" type="file" />
+    </au:assertTrue>
+
+    <java classname="Foo" classpath="${java.src.cache}" 
outputproperty="Foo.out" />
+
+    <au:assertTrue>
+      <equals arg1="${Foo.out}" arg2="foo" />
+    </au:assertTrue>
+
+  </target>
+
+  <target name="testPreloadCollect">
+    <fscache:preload>
+      <fscache:collect id="cachePreloadCollect" basedir="${testCollect.cache}">
+        <resources refid="foobarbaz" />
+      </fscache:collect>
+    </fscache:preload>
+    <resourcecount count="3">
+      <fileset dir="${testCollect.cache}" />
+    </resourcecount>
+    <readCollectResource name="foo" refid="cachePreloadCollect" />
+    <readCollectResource name="bar" refid="cachePreloadCollect" />
+    <readCollectResource name="baz" refid="cachePreloadCollect" />
+  </target>
+</project>

Propchange: ant/sandbox/antlibs/fscache/trunk/src/tests/antunit/fscache-test.xml
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to