diff options
Diffstat (limited to 'framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector')
8 files changed, 0 insertions, 1824 deletions
diff --git a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/Algorithm.java b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/Algorithm.java deleted file mode 100644 index 82e043b5..00000000 --- a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/Algorithm.java +++ /dev/null @@ -1,48 +0,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. - * - */ - -package org.apache.tools.ant.types.selectors.modifiedselector; - - -import java.io.File; - - -/** - * The <i>Algorithm</i> defines how a value for a file is computed. - * It must be sure that multiple calls for the same file results in the - * same value. - * The implementing class should implement a useful toString() method. - * - * @version 2003-09-13 - * @since Ant 1.6 - */ -public interface Algorithm { - - /** - * Checks its prerequisites. - * @return <i>true</i> if all is ok, otherwise <i>false</i>. - */ - boolean isValid(); - - /** - * Get the value for a file. - * @param file File object for which the value should be evaluated. - * @return The value for that file - */ - String getValue(File file); -} diff --git a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/Cache.java b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/Cache.java deleted file mode 100644 index 13c74c2b..00000000 --- a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/Cache.java +++ /dev/null @@ -1,72 +0,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. - * - */ - -package org.apache.tools.ant.types.selectors.modifiedselector; - - -import java.util.Iterator; - - -/** - * A Cache let the user store key-value-pairs in a permanent manner and access - * them. - * It is possible that a client uses get() before load() therefore the - * implementation must ensure that no error occurred because of the wrong - * <i>order</i>. - * The implementing class should implement a useful toString() method. - * - * @version 2003-09-13 - * @since Ant 1.6 - */ -public interface Cache { - - /** - * Checks its prerequisites. - * @return <i>true</i> if all is ok, otherwise <i>false</i>. - */ - boolean isValid(); - - /** Deletes the cache. If file based the file has to be deleted also. */ - void delete(); - - /** Loads the cache, must handle not existing cache. */ - void load(); - - /** Saves modification of the cache. */ - void save(); - - /** - * Returns a value for a given key from the cache. - * @param key the key - * @return the stored value - */ - Object get(Object key); - - /** - * Saves a key-value-pair in the cache. - * @param key the key - * @param value the value - */ - void put(Object key, Object value); - - /** - * Returns an iterator over the keys in the cache. - * @return An iterator over the keys. - */ - Iterator<String> iterator(); -} diff --git a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ChecksumAlgorithm.java b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ChecksumAlgorithm.java deleted file mode 100644 index 210d5dc9..00000000 --- a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ChecksumAlgorithm.java +++ /dev/null @@ -1,151 +0,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. - * - */ - -package org.apache.tools.ant.types.selectors.modifiedselector; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.security.NoSuchAlgorithmException; -import java.util.Locale; -import java.util.zip.Adler32; -import java.util.zip.CRC32; -import java.util.zip.CheckedInputStream; -import java.util.zip.Checksum; - -import org.apache.tools.ant.BuildException; - - -/** - * Computes a 'checksum' for the content of file using - * java.util.zip.CRC32 and java.util.zip.Adler32. - * Use of this algorithm doesn't require any additional nested <param>s. - * Supported <param>s are: - * <table> - * <tr> - * <th>name</th><th>values</th><th>description</th><th>required</th> - * </tr> - * <tr> - * <td> algorithm.algorithm </td> - * <td> ADLER | CRC ( default ) </td> - * <td> name of the algorithm the checksum should use </td> - * <td> no, defaults to CRC </td> - * </tr> - * </table> - * - * @version 2004-06-17 - * @since Ant 1.7 - */ -public class ChecksumAlgorithm implements Algorithm { - - - // ----- member variables ----- - - - /** - * Checksum algorithm to be used. - */ - private String algorithm = "CRC"; - - /** - * Checksum interface instance - */ - private Checksum checksum = null; - - - // ----- Algorithm-Configuration ----- - - - /** - * Specifies the algorithm to be used to compute the checksum. - * Defaults to "CRC". Other popular algorithms like "ADLER" may be used as well. - * @param algorithm the digest algorithm to use - */ - public void setAlgorithm(String algorithm) { - this.algorithm = - algorithm != null ? algorithm.toUpperCase(Locale.ENGLISH) : null; - } - - - /** Initialize the checksum interface. */ - public void initChecksum() { - if (checksum != null) { - return; - } - if ("CRC".equals(algorithm)) { - checksum = new CRC32(); - } else if ("ADLER".equals(algorithm)) { - checksum = new Adler32(); - } else { - throw new BuildException(new NoSuchAlgorithmException()); - } - } - - - // ----- Logic ----- - - - /** - * This algorithm supports only CRC and Adler. - * @return <i>true</i> if all is ok, otherwise <i>false</i>. - */ - public boolean isValid() { - return "CRC".equals(algorithm) || "ADLER".equals(algorithm); - } - - - /** - * Computes a value for a file content with the specified checksum algorithm. - * @param file File object for which the value should be evaluated. - * @return The value for that file - */ - public String getValue(File file) { - initChecksum(); - String rval = null; - - try { - if (file.canRead()) { - checksum.reset(); - FileInputStream fis = new FileInputStream(file); - CheckedInputStream check = new CheckedInputStream(fis, checksum); - BufferedInputStream in = new BufferedInputStream(check); - while (in.read() != -1) { - // Read the file - } - rval = Long.toString(check.getChecksum().getValue()); - in.close(); - } - } catch (Exception e) { - rval = null; - } - return rval; - } - - - /** - * Override Object.toString(). - * @return some information about this algorithm. - */ - public String toString() { - StringBuilder buf = new StringBuilder(); - buf.append("<ChecksumAlgorithm:"); - buf.append("algorithm=").append(algorithm); - buf.append(">"); - return buf.toString(); - } -} diff --git a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/DigestAlgorithm.java b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/DigestAlgorithm.java deleted file mode 100644 index 085b4fe7..00000000 --- a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/DigestAlgorithm.java +++ /dev/null @@ -1,208 +0,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. - * - */ - -package org.apache.tools.ant.types.selectors.modifiedselector; - - -import java.io.File; -import java.io.FileInputStream; -import java.security.DigestInputStream; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; -import java.util.Locale; - -import org.apache.tools.ant.BuildException; - - -/** - * Computes a 'hashvalue' for the content of file using - * java.security.MessageDigest. - * Use of this algorithm doesn't require any additional nested <param>s. - * Supported <param>s are: - * <table> - * <tr> - * <th>name</th><th>values</th><th>description</th><th>required</th> - * </tr> - * <tr> - * <td> algorithm.algorithm </td> - * <td> MD5 | SHA (default provider) </td> - * <td> name of the algorithm the provider should use </td> - * <td> no, defaults to MD5 </td> - * </tr> - * <tr> - * <td> algorithm.provider </td> - * <td> </td> - * <td> name of the provider to use </td> - * <td> no, defaults to <i>null</i> </td> - * </tr> - * </table> - * - * @version 2004-07-08 - * @since Ant 1.6 - */ -public class DigestAlgorithm implements Algorithm { - - private static final int BYTE_MASK = 0xFF; - private static final int BUFFER_SIZE = 8192; - - // ----- member variables ----- - - - /** - * MessageDigest algorithm to be used. - */ - private String algorithm = "MD5"; - - /** - * MessageDigest Algorithm provider - */ - private String provider = null; - - /** - * Message Digest instance - */ - private MessageDigest messageDigest = null; - - /** - * Size of the read buffer to use. - */ - private int readBufferSize = BUFFER_SIZE; - - - // ----- Algorithm-Configuration ----- - - - /** - * Specifies the algorithm to be used to compute the checksum. - * Defaults to "MD5". Other popular algorithms like "SHA" may be used as well. - * @param algorithm the digest algorithm to use - */ - public void setAlgorithm(String algorithm) { - this.algorithm = algorithm != null - ? algorithm.toUpperCase(Locale.ENGLISH) : null; - } - - - /** - * Sets the MessageDigest algorithm provider to be used - * to calculate the checksum. - * @param provider provider to use - */ - public void setProvider(String provider) { - this.provider = provider; - } - - - /** Initialize the security message digest. */ - public void initMessageDigest() { - if (messageDigest != null) { - return; - } - - if ((provider != null) && !"".equals(provider) && !"null".equals(provider)) { - try { - messageDigest = MessageDigest.getInstance(algorithm, provider); - } catch (NoSuchAlgorithmException noalgo) { - throw new BuildException(noalgo); - } catch (NoSuchProviderException noprovider) { - throw new BuildException(noprovider); - } - } else { - try { - messageDigest = MessageDigest.getInstance(algorithm); - } catch (NoSuchAlgorithmException noalgo) { - throw new BuildException(noalgo); - } - } - } - - - // ----- Logic ----- - - - /** - * This algorithm supports only MD5 and SHA. - * @return <i>true</i> if all is ok, otherwise <i>false</i>. - */ - public boolean isValid() { - return "SHA".equals(algorithm) || "MD5".equals(algorithm); - } - - - /** - * Computes a value for a file content with the specified digest algorithm. - * @param file File object for which the value should be evaluated. - * @return The value for that file - */ - // implementation adapted from ...taskdefs.Checksum, thanks to Magesh for hint - public String getValue(File file) { - initMessageDigest(); - String checksum = null; - try { - if (!file.canRead()) { - return null; - } - FileInputStream fis = null; - - byte[] buf = new byte[readBufferSize]; - try { - messageDigest.reset(); - fis = new FileInputStream(file); - DigestInputStream dis = new DigestInputStream(fis, - messageDigest); - while (dis.read(buf, 0, readBufferSize) != -1) { - // do nothing - } - dis.close(); - fis.close(); - fis = null; - byte[] fileDigest = messageDigest.digest(); - StringBuffer checksumSb = new StringBuffer(); - for (int i = 0; i < fileDigest.length; i++) { - String hexStr - = Integer.toHexString(BYTE_MASK & fileDigest[i]); - if (hexStr.length() < 2) { - checksumSb.append("0"); - } - checksumSb.append(hexStr); - } - checksum = checksumSb.toString(); - } catch (Exception e) { - return null; - } - } catch (Exception e) { - return null; - } - return checksum; - } - - - /** - * Override Object.toString(). - * @return some information about this algorithm. - */ - public String toString() { - StringBuilder buf = new StringBuilder(); - buf.append("<DigestAlgorithm:"); - buf.append("algorithm=").append(algorithm); - buf.append(";provider=").append(provider); - buf.append(">"); - return buf.toString(); - } -} diff --git a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/EqualComparator.java b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/EqualComparator.java deleted file mode 100644 index 94fb9351..00000000 --- a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/EqualComparator.java +++ /dev/null @@ -1,58 +0,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. - * - */ - -package org.apache.tools.ant.types.selectors.modifiedselector; - - -import java.util.Comparator; - - -/** - * Simple implementation of Comparator for use in CacheSelector. - * compare() returns '0' (should not be selected) if both parameter - * are equal otherwise '1' (should be selected). - * - * @version 2003-09-13 - * @since Ant 1.6 - */ -public class EqualComparator implements Comparator<Object> { - - /** - * Implements Comparator.compare(). - * @param o1 the first object - * @param o2 the second object - * @return 0, if both are equal, otherwise 1 - */ - public int compare(Object o1, Object o2) { - if (o1 == null) { - if (o2 == null) { - return 1; - } - return 0; - } - return (o1.equals(o2)) ? 0 : 1; - } - - /** - * Override Object.toString(). - * @return information about this comparator - */ - public String toString() { - return "EqualComparator"; - } -} diff --git a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/HashvalueAlgorithm.java b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/HashvalueAlgorithm.java deleted file mode 100644 index 8af9d12e..00000000 --- a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/HashvalueAlgorithm.java +++ /dev/null @@ -1,80 +0,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. - * - */ - -package org.apache.tools.ant.types.selectors.modifiedselector; - -import java.io.File; -import java.io.FileReader; -import java.io.Reader; - -import org.apache.tools.ant.util.FileUtils; - -/** - * Computes a 'hashvalue' for the content of file using String.hashValue(). - * Use of this algorithm doesn't require any additional nested <param>s and - * doesn't support any. - * - * @version 2003-09-13 - * @since Ant 1.6 - */ -public class HashvalueAlgorithm implements Algorithm { - - /** - * This algorithm doesn't need any configuration. - * Therefore it's always valid. - * @return always true - */ - public boolean isValid() { - return true; - } - - /** - * Computes a 'hashvalue' for a file content. - * It reads the content of a file, convert that to String and use the - * String.hashCode() method. - * @param file The file for which the value should be computed - * @return the hashvalue or <i>null</i> if the file couldn't be read - */ - // Because the content is only read the file will not be damaged. I tested - // with JPG, ZIP and PDF as binary files. - public String getValue(File file) { - Reader r = null; - try { - if (!file.canRead()) { - return null; - } - r = new FileReader(file); - int hash = FileUtils.readFully(r).hashCode(); - return Integer.toString(hash); - } catch (Exception e) { - return null; - } finally { - FileUtils.close(r); - } - } - - - /** - * Override Object.toString(). - * @return information about this comparator - */ - public String toString() { - return "HashvalueAlgorithm"; - } - -} diff --git a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ModifiedSelector.java b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ModifiedSelector.java deleted file mode 100644 index 9f538098..00000000 --- a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ModifiedSelector.java +++ /dev/null @@ -1,971 +0,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. - * - */ - -package org.apache.tools.ant.types.selectors.modifiedselector; - - -// Java -import java.io.File; -import java.util.Comparator; -import java.util.Iterator; -import java.util.Vector; - -import org.apache.tools.ant.BuildEvent; -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.BuildListener; -import org.apache.tools.ant.IntrospectionHelper; -// Ant -import org.apache.tools.ant.Project; -import org.apache.tools.ant.types.EnumeratedAttribute; -import org.apache.tools.ant.types.Parameter; -import org.apache.tools.ant.types.Path; -import org.apache.tools.ant.types.Resource; -import org.apache.tools.ant.types.resources.FileResource; -import org.apache.tools.ant.types.resources.selectors.ResourceSelector; -import org.apache.tools.ant.types.selectors.BaseExtendSelector; -import org.apache.tools.ant.util.FileUtils; -import org.apache.tools.ant.util.ResourceUtils; - - -/** - * <p>Selector class that uses <i>Algorithm</i>, <i>Cache</i> and <i>Comparator</i> - * for its work. - * The <i>Algorithm</i> is used for computing a hashvalue for a file. - * The <i>Comparator</i> decides whether to select or not. - * The <i>Cache</i> stores the other value for comparison by the <i>Comparator</i> - * in a persistent manner.</p> - * - * <p>The ModifiedSelector is implemented as a <b>CoreSelector</b> and uses default - * values for all its attributes therefore the simplest example is <pre> - * <copy todir="dest"> - * <filelist dir="src"> - * <modified/> - * </filelist> - * </copy> - * </pre></p> - * - * <p>The same example rewritten as CoreSelector with setting the all values - * (same as defaults are) would be <pre> - * <copy todir="dest"> - * <filelist dir="src"> - * <modified update="true" - * cache="propertyfile" - * algorithm="digest" - * comparator="equal"> - * <param name="cache.cachefile" value="cache.properties"/> - * <param name="algorithm.algorithm" value="MD5"/> - * </modified> - * </filelist> - * </copy> - * </pre></p> - * - * <p>And the same rewritten as CustomSelector would be<pre> - * <copy todir="dest"> - * <filelist dir="src"> - * <custom class="org.apache.tools.ant.type.selectors.ModifiedSelector"> - * <param name="update" value="true"/> - * <param name="cache" value="propertyfile"/> - * <param name="algorithm" value="digest"/> - * <param name="comparator" value="equal"/> - * <param name="cache.cachefile" value="cache.properties"/> - * <param name="algorithm.algorithm" value="MD5"/> - * </custom> - * </filelist> - * </copy> - * </pre></p> - * - * <p>If you want to provide your own interface implementation you can do - * that via the *classname attributes. If the classes are not on Ant's core - * classpath, you will have to provide the path via nested <classpath> - * element, so that the selector can find the classes. <pre> - * <modified cacheclassname="com.mycompany.MyCache"> - * <classpath> - * <pathelement location="lib/mycompany-antutil.jar"/> - * </classpath> - * </modified> - * </pre></p> - * - * <p>All these three examples copy the files from <i>src</i> to <i>dest</i> - * using the ModifiedSelector. The ModifiedSelector uses the <i>PropertyfileCache - * </i>, the <i>DigestAlgorithm</i> and the <i>EqualComparator</i> for its - * work. The PropertyfileCache stores key-value-pairs in a simple java - * properties file. The filename is <i>cache.properties</i>. The <i>update</i> - * flag lets the selector update the values in the cache (and on first call - * creates the cache). The <i>DigestAlgorithm</i> computes a hashvalue using the - * java.security.MessageDigest class with its MD5-Algorithm and its standard - * provider. The new computed hashvalue and the stored one are compared by - * the <i>EqualComparator</i> which returns 'true' (more correct a value not - * equals zero (1)) if the values are not the same using simple String - * comparison.</p> - * - * <p>A useful scenario for this selector is inside a build environment - * for homepage generation (e.g. with <a href="http://forrest.apache.org/"> - * Apache Forrest</a>). <pre> - * <target name="generate-and-upload-site"> - * <echo> generate the site using forrest </echo> - * <antcall target="site"/> - * - * <echo> upload the changed files </echo> - * <ftp server="${ftp.server}" userid="${ftp.user}" password="${ftp.pwd}"> - * <fileset dir="htdocs/manual"> - * <modified/> - * </fileset> - * </ftp> - * </target> - * </pre> Here all <b>changed</b> files are uploaded to the server. The - * ModifiedSelector saves therefore much upload time.</p> - * - * - * <p>This selector uses reflection for setting the values of its three interfaces - * (using org.apache.tools.ant.IntrospectionHelper) therefore no special - * 'configuration interfaces' has to be implemented by new caches, algorithms or - * comparators. All present <i>set</i>XX methods can be used. E.g. the DigestAlgorithm - * can use a specified provider for computing its value. For selecting this - * there is a <i>setProvider(String providername)</i> method. So you can use - * a nested <i><param name="algorithm.provider" value="MyProvider"/></i>. - * - * - * @since Ant 1.6 - */ -public class ModifiedSelector extends BaseExtendSelector - implements BuildListener, ResourceSelector { - - private static final String CACHE_PREFIX = "cache."; - private static final String ALGORITHM_PREFIX = "algorithm."; - private static final String COMPARATOR_PREFIX = "comparator."; - - - // ----- attributes ----- - - - /** Cache name for later instantiation. */ - private CacheName cacheName = null; - - /** User specified classname for Cache. */ - private String cacheClass; - - /** Algorithm name for later instantiation. */ - private AlgorithmName algoName = null; - - /** User specified classname for Algorithm. */ - private String algorithmClass; - - /** Comparator name for later instantiation. */ - private ComparatorName compName = null; - - /** User specified classname for Comparator. */ - private String comparatorClass; - - /** Should the cache be updated? */ - private boolean update = true; - - /** Are directories selected? */ - private boolean selectDirectories = true; - - /** - * Should Resources whithout an InputStream, and - * therefore without checking, be selected? - */ - private boolean selectResourcesWithoutInputStream = true; - - /** Delay the writing of the cache file */ - private boolean delayUpdate = true; - - - // ----- internal member variables ----- - - - /** How should the cached value and the new one compared? */ - private Comparator<? super String> comparator = null; - - /** Algorithm for computing new values and updating the cache. */ - private Algorithm algorithm = null; - - /** The Cache containing the old values. */ - private Cache cache = null; - - /** Count of modified properties */ - private int modified = 0; - - /** Flag whether this object is configured. Configuration is only done once. */ - private boolean isConfigured = false; - - /** - * Parameter vector with parameters for later initialization. - * @see #configure - */ - private Vector<Parameter> configParameter = new Vector<Parameter>(); - - /** - * Parameter vector with special parameters for later initialization. - * The names have the pattern '*.*', e.g. 'cache.cachefile'. - * These parameters are used <b>after</b> the parameters with the pattern '*'. - * @see #configure - */ - private Vector<Parameter> specialParameter = new Vector<Parameter>(); - - /** The classloader of this class. */ - private ClassLoader myClassLoader = null; - - /** provided classpath for the classloader */ - private Path classpath = null; - - - // ----- constructors ----- - - - /** Bean-Constructor. */ - public ModifiedSelector() { - } - - - // ----- configuration ----- - - - /** Overrides BaseSelector.verifySettings(). */ - public void verifySettings() { - configure(); - if (cache == null) { - setError("Cache must be set."); - } else if (algorithm == null) { - setError("Algorithm must be set."); - } else if (!cache.isValid()) { - setError("Cache must be proper configured."); - } else if (!algorithm.isValid()) { - setError("Algorithm must be proper configured."); - } - } - - - /** - * Configures this Selector. - * Does this work only once per Selector object. - * <p>Because some problems while configuring from <custom>Selector - * the configuration is done in the following order:<ol> - * <li> collect the configuration data </li> - * <li> wait for the first isSelected() call </li> - * <li> set the default values </li> - * <li> set values for name pattern '*': update, cache, algorithm, comparator </li> - * <li> set values for name pattern '*.*: cache.cachefile, ... </li> - * </ol></p> - * <p>This configuration algorithm is needed because you don't know - * the order of arriving config-data. E.g. if you first set the - * <i>cache.cachefilename</i> and after that the <i>cache</i> itself, - * the default value for cachefilename is used, because setting the - * cache implies creating a new Cache instance - with its defaults.</p> - */ - public void configure() { - // - // ----- The "Singleton" ----- - // - if (isConfigured) { - return; - } - isConfigured = true; - - // - // ----- Set default values ----- - // - Project p = getProject(); - String filename = "cache.properties"; - File cachefile = null; - if (p != null) { - // normal use inside Ant - cachefile = new File(p.getBaseDir(), filename); - - // set self as a BuildListener to delay cachefile saves - getProject().addBuildListener(this); - } else { - // no reference to project - e.g. during normal JUnit tests - cachefile = new File(filename); - setDelayUpdate(false); - } - Cache defaultCache = new PropertiesfileCache(cachefile); - Algorithm defaultAlgorithm = new DigestAlgorithm(); - Comparator<? super String> defaultComparator = new EqualComparator(); - - // - // ----- Set the main attributes, pattern '*' ----- - // - for (Parameter parameter : configParameter) { - if (parameter.getName().indexOf(".") > 0) { - // this is a *.* parameter for later use - specialParameter.add(parameter); - } else { - useParameter(parameter); - } - } - configParameter = new Vector<Parameter>(); - - // specify the algorithm classname - if (algoName != null) { - // use Algorithm defined via name - if ("hashvalue".equals(algoName.getValue())) { - algorithm = new HashvalueAlgorithm(); - } else if ("digest".equals(algoName.getValue())) { - algorithm = new DigestAlgorithm(); - } else if ("checksum".equals(algoName.getValue())) { - algorithm = new ChecksumAlgorithm(); - } - } else { - if (algorithmClass != null) { - // use Algorithm specified by classname - algorithm = loadClass( - algorithmClass, - "is not an Algorithm.", - Algorithm.class); - } else { - // nothing specified - use default - algorithm = defaultAlgorithm; - } - } - - // specify the cache classname - if (cacheName != null) { - // use Cache defined via name - if ("propertyfile".equals(cacheName.getValue())) { - cache = new PropertiesfileCache(); - } - } else { - if (cacheClass != null) { - // use Cache specified by classname - cache = loadClass(cacheClass, "is not a Cache.", Cache.class); - } else { - // nothing specified - use default - cache = defaultCache; - } - } - - // specify the comparator classname - if (compName != null) { - // use Algorithm defined via name - if ("equal".equals(compName.getValue())) { - comparator = new EqualComparator(); - } else if ("rule".equals(compName.getValue())) { - // TODO there is a problem with the constructor for the RBC. - // you have to provide the rules in the constructors - no setters - // available. - throw new BuildException("RuleBasedCollator not yet supported."); - // Have to think about lazy initialization here... JHM - // comparator = new java.text.RuleBasedCollator(); - } - } else { - if (comparatorClass != null) { - // use Algorithm specified by classname - @SuppressWarnings("unchecked") - Comparator<? super String> localComparator = loadClass(comparatorClass, "is not a Comparator.", Comparator.class); - comparator = localComparator; - } else { - // nothing specified - use default - comparator = defaultComparator; - } - } - - // - // ----- Set the special attributes, pattern '*.*' ----- - // - for (Iterator<Parameter> itSpecial = specialParameter.iterator(); itSpecial.hasNext();) { - Parameter par = itSpecial.next(); - useParameter(par); - } - specialParameter = new Vector<Parameter>(); - } - - - /** - * Loads the specified class and initializes an object of that class. - * Throws a BuildException using the given message if an error occurs during - * loading/instantiation or if the object is not from the given type. - * @param classname the classname - * @param msg the message-part for the BuildException - * @param type the type to check against - * @return a castable object - */ - protected <T> T loadClass(String classname, String msg, Class<? extends T> type) { - try { - // load the specified class - ClassLoader cl = getClassLoader(); - Class<?> clazz = null; - if (cl != null) { - clazz = cl.loadClass(classname); - } else { - clazz = Class.forName(classname); - } - - Object rv = clazz.newInstance(); - - if (!type.isInstance(rv)) { - throw new BuildException("Specified class (" + classname + ") " + msg); - } - return (T) rv; - } catch (ClassNotFoundException e) { - throw new BuildException("Specified class (" + classname + ") not found."); - } catch (Exception e) { - throw new BuildException(e); - } - } - - - // ----- the selection work ----- - - - /** - * Implementation of ResourceSelector.isSelected(). - * - * @param resource The resource to check - * @return whether the resource is selected - * @see ResourceSelector#isSelected(Resource) - */ - public boolean isSelected(Resource resource) { - if (resource.isFilesystemOnly()) { - // We have a 'resourced' file, so reconvert it and use - // the 'old' implementation. - FileResource fileResource = (FileResource) resource; - File file = fileResource.getFile(); - String filename = fileResource.getName(); - File basedir = fileResource.getBaseDir(); - return isSelected(basedir, filename, file); - } else { - try { - // How to handle non-file-Resources? I copy temporarily the - // resource to a file and use the file-implementation. - FileUtils fu = FileUtils.getFileUtils(); - File tmpFile = fu.createTempFile("modified-", ".tmp", null, true, false); - Resource tmpResource = new FileResource(tmpFile); - ResourceUtils.copyResource(resource, tmpResource); - boolean isSelected = isSelected(tmpFile.getParentFile(), - tmpFile.getName(), - resource.toLongString()); - tmpFile.delete(); - return isSelected; - } catch (UnsupportedOperationException uoe) { - log("The resource '" - + resource.getName() - + "' does not provide an InputStream, so it is not checked. " - + "Akkording to 'selres' attribute value it is " - + ((selectResourcesWithoutInputStream) ? "" : " not") - + "selected.", Project.MSG_INFO); - return selectResourcesWithoutInputStream; - } catch (Exception e) { - throw new BuildException(e); - } - } - } - - - /** - * Implementation of BaseExtendSelector.isSelected(). - * - * @param basedir as described in BaseExtendSelector - * @param filename as described in BaseExtendSelector - * @param file as described in BaseExtendSelector - * @return as described in BaseExtendSelector - */ - public boolean isSelected(File basedir, String filename, File file) { - return isSelected(basedir, filename, file.getAbsolutePath()); - } - - - /** - * The business logic of this selector for use as ResourceSelector of - * FileSelector. - * - * @param basedir as described in BaseExtendSelector - * @param filename as described in BaseExtendSelector - * @param cacheKey the name for the key for storing the hashvalue - * @return <tt>true</tt> if the file is selected otherwise <tt>false</tt> - */ - private boolean isSelected(File basedir, String filename, String cacheKey) { - validate(); - File f = new File(basedir, filename); - - // You can not compute a value for a directory - if (f.isDirectory()) { - return selectDirectories; - } - - // Get the values and do the comparison - String cachedValue = String.valueOf(cache.get(f.getAbsolutePath())); - String newValue = algorithm.getValue(f); - - boolean rv = (comparator.compare(cachedValue, newValue) != 0); - - // Maybe update the cache - if (update && rv) { - cache.put(f.getAbsolutePath(), newValue); - setModified(getModified() + 1); - if (!getDelayUpdate()) { - saveCache(); - } - } - - return rv; - } - - - /** - * save the cache file - */ - protected void saveCache() { - if (getModified() > 0) { - cache.save(); - setModified(0); - } - } - - - // ----- attribute and nested element support ----- - - - /** - * Setter for algorithmClass. - * @param classname new value - */ - public void setAlgorithmClass(String classname) { - algorithmClass = classname; - } - - - /** - * Setter for comparatorClass. - * @param classname new value - */ - public void setComparatorClass(String classname) { - comparatorClass = classname; - } - - - /** - * Setter for cacheClass. - * @param classname new value - */ - public void setCacheClass(String classname) { - cacheClass = classname; - } - - - /** - * Support for <i>update</i> attribute. - * @param update new value - */ - public void setUpdate(boolean update) { - this.update = update; - } - - - /** - * Support for <i>seldirs</i> attribute. - * @param seldirs new value - */ - public void setSeldirs(boolean seldirs) { - selectDirectories = seldirs; - } - - - /** - * Support for <i>selres</i> attribute. - * @param newValue the new value - */ - public void setSelres(boolean newValue) { - this.selectResourcesWithoutInputStream = newValue; - } - - - /** - * Getter for the modified count - * @return modified count - */ - public int getModified() { - return modified; - } - - - /** - * Setter for the modified count - * @param modified count - */ - public void setModified(int modified) { - this.modified = modified; - } - - - /** - * Getter for the delay update - * @return true if we should delay for performance - */ - public boolean getDelayUpdate() { - return delayUpdate; - } - - - /** - * Setter for the delay update - * @param delayUpdate true if we should delay for performance - */ - public void setDelayUpdate(boolean delayUpdate) { - this.delayUpdate = delayUpdate; - } - - - /** - * Add the classpath. - * @param path the classpath - */ - public void addClasspath(Path path) { - if (classpath != null) { - throw new BuildException("<classpath> can be set only once."); - } - classpath = path; - } - - - /** - * Returns and initializes the classloader for this class. - * @return the classloader - */ - public ClassLoader getClassLoader() { - if (myClassLoader == null) { - myClassLoader = (classpath == null) - // the usual classloader - ? getClass().getClassLoader() - // additional use the provided classpath - // Memory leak in line below - : getProject().createClassLoader(classpath); - } - return myClassLoader; - } - - - /** - * Set the used ClassLoader. - * If you invoke this selector by API (e.g. inside some testcases) the selector - * will use a different classloader for loading the interface implementations than - * the caller. Therefore you will get a ClassCastException if you get the - * implementations from the selector and cast them. - * @param loader the ClassLoader to use - */ - public void setClassLoader(ClassLoader loader) { - myClassLoader = loader; - } - - - /** - * Support for nested <param> tags. - * @param key the key of the parameter - * @param value the value of the parameter - */ - public void addParam(String key, Object value) { - Parameter par = new Parameter(); - par.setName(key); - par.setValue(String.valueOf(value)); - configParameter.add(par); - } - - - /** - * Support for nested <param> tags. - * @param parameter the parameter object - */ - public void addParam(Parameter parameter) { - configParameter.add(parameter); - } - - - /** - * Defined in org.apache.tools.ant.types.Parameterizable. - * Overwrite implementation in superclass because only special - * parameters are valid. - * @see #addParam(String,Object). - * @param parameters the parameters to set. - */ - public void setParameters(Parameter[] parameters) { - if (parameters != null) { - for (int i = 0; i < parameters.length; i++) { - configParameter.add(parameters[i]); - } - } - } - - - /** - * Support for nested <param name="" value=""/> tags. - * Parameter named <i>cache</i>, <i>algorithm</i>, - * <i>comparator</i> or <i>update</i> are mapped to - * the respective set-Method. - * Parameter which names starts with <i>cache.</i> or - * <i>algorithm.</i> or <i>comparator.</i> are tried - * to set on the appropriate object via its set-methods. - * Other parameters are invalid and an BuildException will - * be thrown. - * - * @param parameter Key and value as parameter object - */ - public void useParameter(Parameter parameter) { - String key = parameter.getName(); - String value = parameter.getValue(); - if ("cache".equals(key)) { - CacheName cn = new CacheName(); - cn.setValue(value); - setCache(cn); - } else if ("algorithm".equals(key)) { - AlgorithmName an = new AlgorithmName(); - an.setValue(value); - setAlgorithm(an); - } else if ("comparator".equals(key)) { - ComparatorName cn = new ComparatorName(); - cn.setValue(value); - setComparator(cn); - } else if ("update".equals(key)) { - boolean updateValue = - ("true".equalsIgnoreCase(value)) - ? true - : false; - setUpdate(updateValue); - } else if ("delayupdate".equals(key)) { - boolean updateValue = - ("true".equalsIgnoreCase(value)) - ? true - : false; - setDelayUpdate(updateValue); - } else if ("seldirs".equals(key)) { - boolean sdValue = - ("true".equalsIgnoreCase(value)) - ? true - : false; - setSeldirs(sdValue); - } else if (key.startsWith(CACHE_PREFIX)) { - String name = key.substring(CACHE_PREFIX.length()); - tryToSetAParameter(cache, name, value); - } else if (key.startsWith(ALGORITHM_PREFIX)) { - String name = key.substring(ALGORITHM_PREFIX.length()); - tryToSetAParameter(algorithm, name, value); - } else if (key.startsWith(COMPARATOR_PREFIX)) { - String name = key.substring(COMPARATOR_PREFIX.length()); - tryToSetAParameter(comparator, name, value); - } else { - setError("Invalid parameter " + key); - } - } - - - /** - * Try to set a value on an object using reflection. - * Helper method for easier access to IntrospectionHelper.setAttribute(). - * @param obj the object on which the attribute should be set - * @param name the attributename - * @param value the new value - */ - protected void tryToSetAParameter(Object obj, String name, String value) { - Project prj = (getProject() != null) ? getProject() : new Project(); - IntrospectionHelper iHelper - = IntrospectionHelper.getHelper(prj, obj.getClass()); - try { - iHelper.setAttribute(prj, obj, name, value); - } catch (org.apache.tools.ant.BuildException e) { - // no-op - } - } - - - // ----- 'beautiful' output ----- - - - /** - * Override Object.toString(). - * @return information about this selector - */ - public String toString() { - StringBuffer buf = new StringBuffer("{modifiedselector"); - buf.append(" update=").append(update); - buf.append(" seldirs=").append(selectDirectories); - buf.append(" cache=").append(cache); - buf.append(" algorithm=").append(algorithm); - buf.append(" comparator=").append(comparator); - buf.append("}"); - return buf.toString(); - } - - - // ----- BuildListener interface methods ----- - - - /** - * Signals that the last target has finished. - * @param event received BuildEvent - */ - public void buildFinished(BuildEvent event) { - if (getDelayUpdate()) { - saveCache(); - } - } - - - /** - * Signals that a target has finished. - * @param event received BuildEvent - */ - public void targetFinished(BuildEvent event) { - if (getDelayUpdate()) { - saveCache(); - } - } - - - /** - * Signals that a task has finished. - * @param event received BuildEvent - */ - public void taskFinished(BuildEvent event) { - if (getDelayUpdate()) { - saveCache(); - } - } - - - /** - * Signals that a build has started. - * @param event received BuildEvent - */ - public void buildStarted(BuildEvent event) { - // no-op - } - - - /** - * Signals that a target is starting. - * @param event received BuildEvent - */ - public void targetStarted(BuildEvent event) { - // no-op - } - - - - /** - * Signals that a task is starting. - * @param event received BuildEvent - */ - public void taskStarted(BuildEvent event) { - // no-op - } - - - /** - * Signals a message logging event. - * @param event received BuildEvent - */ - public void messageLogged(BuildEvent event) { - // no-op - } - - - // The EnumeratedAttributes for the three interface implementations. - // Name-Classname mapping is done in the configure() method. - - - /** - * Get the cache type to use. - * @return the enumerated cache type - */ - public Cache getCache() { - return cache; - } - - /** - * Set the cache type to use. - * @param name an enumerated cache type. - */ - public void setCache(CacheName name) { - cacheName = name; - } - - /** - * The enumerated type for cache. - * The values are "propertyfile". - */ - public static class CacheName extends EnumeratedAttribute { - /** - * {@inheritDoc} - * @see EnumeratedAttribute#getValues() - */ - public String[] getValues() { - return new String[] {"propertyfile" }; - } - } - - /** - * Get the algorithm type to use. - * @return the enumerated algorithm type - */ - public Algorithm getAlgorithm() { - return algorithm; - } - - /** - * Set the algorithm type to use. - * @param name an enumerated algorithm type. - */ - public void setAlgorithm(AlgorithmName name) { - algoName = name; - } - - /** - * The enumerated type for algorithm. - * The values are "hashValue", "digest" and "checksum". - */ - public static class AlgorithmName extends EnumeratedAttribute { - /** - * {@inheritDoc} - * @see EnumeratedAttribute#getValues() - */ - public String[] getValues() { - return new String[] {"hashvalue", "digest", "checksum" }; - } - } - - /** - * Get the comparator type to use. - * @return the enumerated comparator type - */ - public Comparator<? super String> getComparator() { - return comparator; - } - - /** - * Set the comparator type to use. - * @param name an enumerated comparator type. - */ - public void setComparator(ComparatorName name) { - compName = name; - } - - /** - * The enumerated type for algorithm. - * The values are "equal" and "rule". - */ - public static class ComparatorName extends EnumeratedAttribute { - /** - * {@inheritDoc} - * @see EnumeratedAttribute#getValues() - */ - public String[] getValues() { - return new String[] {"equal", "rule" }; - } - } - -} diff --git a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/PropertiesfileCache.java b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/PropertiesfileCache.java deleted file mode 100644 index 1446e890..00000000 --- a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/selectors/modifiedselector/PropertiesfileCache.java +++ /dev/null @@ -1,236 +0,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. - * - */ - -package org.apache.tools.ant.types.selectors.modifiedselector; - - -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.util.Enumeration; -import java.util.Iterator; -import java.util.Properties; -import java.util.Vector; - - -/** - * Use java.util.Properties for storing the values. - * The use of this Cache-implementation requires the use of the parameter - * <param name="cache.cachefile" .../> for defining, where to store the - * properties file. - * - * The ModifiedSelector sets the <i>cachefile</i> to the default value - * <i>cache.properties</i>. - * - * Supported <param>s are: - * <table> - * <tr> - * <th>name</th><th>values</th><th>description</th><th>required</th> - * </tr> - * <tr> - * <td> cache.cachefile </td> - * <td> <i>path to file</i> </td> - * <td> the name of the properties file </td> - * <td> yes </td> - * </tr> - * </table> - * - * @version 2003-09-13 - * @since Ant 1.6 - */ -public class PropertiesfileCache implements Cache { - - - // ----- member variables - configuration ----- - - - /** Where to store the properties? */ - private File cachefile = null; - - /** Object for storing the key-value-pairs. */ - private Properties cache = new Properties(); - - - // ----- member variables - internal use ----- - - - /** Is the cache already loaded? Prevents from multiple load operations. */ - private boolean cacheLoaded = false; - - /** Must the cache be saved? Prevents from multiple save operations. */ - private boolean cacheDirty = true; - - - // ----- Constructors ----- - - - /** Bean-Constructor. */ - public PropertiesfileCache() { - } - - /** - * Constructor. - * @param cachefile set the cachefile - */ - public PropertiesfileCache(File cachefile) { - this.cachefile = cachefile; - } - - - // ----- Cache-Configuration ----- - - - /** - * Setter. - * @param file new value - */ - public void setCachefile(File file) { - cachefile = file; - } - - - /** - * Getter. - * @return the cachefile - */ - public File getCachefile() { - return cachefile; - } - - /** - * This cache is valid if the cachefile is set. - * @return true if all is ok false otherwise - */ - public boolean isValid() { - return (cachefile != null); - } - - - // ----- Data Access - - - /** - * Load the cache from underlying properties file. - */ - public void load() { - if ((cachefile != null) && cachefile.isFile() && cachefile.canRead()) { - try { - BufferedInputStream bis = new BufferedInputStream( - new FileInputStream(cachefile)); - cache.load(bis); - bis.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } - // after loading the cache is up to date with the file - cacheLoaded = true; - cacheDirty = false; - } - - /** - * Saves modification of the cache. - * Cache is only saved if there is one ore more entries. - * Because entries can not be deleted by this API, this Cache - * implementation checks the existence of entries before creating the file - * for performance optimisation. - */ - public void save() { - if (!cacheDirty) { - return; - } - if ((cachefile != null) && cache.propertyNames().hasMoreElements()) { - try { - BufferedOutputStream bos = new BufferedOutputStream( - new FileOutputStream(cachefile)); - cache.store(bos, null); - bos.flush(); - bos.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } - cacheDirty = false; - } - - /** Deletes the cache and its underlying file. */ - public void delete() { - cache = new Properties(); - cachefile.delete(); - cacheLoaded = true; - cacheDirty = false; - } - - /** - * Returns a value for a given key from the cache. - * @param key the key - * @return the stored value - */ - public Object get(Object key) { - if (!cacheLoaded) { - load(); - } - try { - return cache.getProperty(String.valueOf(key)); - } catch (ClassCastException e) { - return null; - } - } - - /** - * Saves a key-value-pair in the cache. - * @param key the key - * @param value the value - */ - public void put(Object key, Object value) { - cache.put(String.valueOf(key), String.valueOf(value)); - cacheDirty = true; - } - - /** - * Returns an iterator over the keys in the cache. - * @return An iterator over the keys. - */ - public Iterator<String> iterator() { - Vector<String> v = new Vector<String>(); - Enumeration<?> en = cache.propertyNames(); - while (en.hasMoreElements()) { - v.add(en.nextElement().toString()); - } - return v.iterator(); - } - - - // ----- additional ----- - - - /** - * Override Object.toString(). - * @return information about this cache - */ - public String toString() { - StringBuffer buf = new StringBuffer(); - buf.append("<PropertiesfileCache:"); - buf.append("cachefile=").append(cachefile); - buf.append(";noOfEntries=").append(cache.size()); - buf.append(">"); - return buf.toString(); - } -} |