001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.util;
016:
017: import java.util.Arrays;
018: import java.util.HashSet;
019: import java.util.Iterator;
020: import java.util.Map;
021: import java.util.Properties;
022: import java.util.Set;
023:
024: import org.apache.tools.ant.BuildException;
025: import org.apache.tools.ant.Project;
026: import org.apache.tools.ant.taskdefs.Property;
027: import org.apache.tools.ant.types.Path;
028:
029: /** Toolbox for ant utils */
030: public class AntUtils {
031: private static Properties sEnvironmentProperties = new Properties();
032:
033: // Static initialiser - executed once per Ant run
034: static {
035: // Populate system properties from temp project
036: Project lTempProject = new Project();
037: lTempProject.init();
038: Property lPropertyTask = (Property) lTempProject
039: .createTask("property");
040: lPropertyTask.setEnvironment("env");
041: lPropertyTask.execute();
042: // Now that properties are loae we can filter them and only leave environment ones
043: Iterator lIter = lTempProject.getProperties().entrySet()
044: .iterator();
045: while (lIter.hasNext()) {
046: Map.Entry lNextEntry = (Map.Entry) lIter.next();
047: String lKey = (String) lNextEntry.getKey();
048: if (lKey.startsWith("env."))
049: sEnvironmentProperties.setProperty(lKey.substring(4),
050: (String) lNextEntry.getValue());
051: }
052: }
053:
054: /** Returns the specified environment property or throws exception if none found */
055: public static String getMandatoryEnvironmentProperty(
056: String pPropertyName) throws BuildException {
057: if (!sEnvironmentProperties.containsKey(pPropertyName))
058: throw new BuildException(
059: "'"
060: + pPropertyName
061: + "' system property is not defined in the system environment.");
062: return sEnvironmentProperties.getProperty(pPropertyName);
063: }
064:
065: /** Returns the specified environment property or null if none found */
066: public static String getOptionalEnvironmentProperty(
067: String pPropertyName, String pDefaultPropertyValue)
068: throws BuildException {
069: if (!sEnvironmentProperties.containsKey(pPropertyName))
070: return pDefaultPropertyValue;
071: return sEnvironmentProperties.getProperty(pPropertyName);
072: }
073:
074: /** Returns the specified project property or, in its absence environment property or throws exception if none found */
075: public static String getMandatoryProjectOrEnvironmentProperty(
076: Project pProject, String pProjectPropertyName,
077: String pEnvironmentPropertyName) throws BuildException {
078: String lPropertyValue = pProject
079: .getProperty(pProjectPropertyName);
080: if (lPropertyValue != null)
081: return lPropertyValue;
082: return getMandatoryEnvironmentProperty(pEnvironmentPropertyName);
083: }
084:
085: /** Returns the specified project property or, in its absence environment property or throws exception if none found */
086: public static String getOptionalProjectProperty(Project pProject,
087: String pProjectPropertyName, String pDefaultPropertyValue)
088: throws BuildException {
089: String lPropertyValue = pProject
090: .getProperty(pProjectPropertyName);
091: if (lPropertyValue != null)
092: return lPropertyValue;
093: return pDefaultPropertyValue;
094: }
095:
096: /** Returns the specified project property or, in its absence environment property or throws exception if none found */
097: public static String getOptionalProjectOrEnvironmentProperty(
098: Project pProject, String pProjectPropertyName,
099: String pEnvironmentPropertyName,
100: String pDefaultPropertyValue) throws BuildException {
101: String lPropertyValue = pProject
102: .getProperty(pProjectPropertyName);
103: if (lPropertyValue != null)
104: return lPropertyValue;
105: return getOptionalEnvironmentProperty(pEnvironmentPropertyName,
106: pDefaultPropertyValue);
107: }
108:
109: /** Compares if two Path data types have exactly the same contents (same order and same content)
110: * @return true if two path elements are equal */
111: public static boolean equals(Path pPath1, Path pPath2) {
112: if (pPath1 == null && pPath2 == null)
113: return true;
114: if (pPath1 == null || pPath2 == null)
115: return false;
116: return Arrays.equals(pPath1.list(), pPath2.list());
117: }
118:
119: /** Compares if two Path data types have exactly the same contents, but not necessarily in the ame order
120: * @return true if two path elements are the same */
121: public static boolean same(Path pPath1, Path pPath2) {
122: if (pPath1 == null && pPath2 == null)
123: return true;
124: if (pPath1 == null || pPath2 == null)
125: return false;
126: String[] lPathElements1 = pPath1.list();
127: String[] lPathElements2 = pPath2.list();
128: if (lPathElements1 == null && lPathElements2 == null)
129: return false;
130: if (lPathElements1 == null || lPathElements2 == null)
131: return false;
132: // Reconcile elemenet sof an array
133: Set lSecondElementsSet = new HashSet();
134: lSecondElementsSet.addAll(Arrays.asList(lPathElements2));
135: for (int i = 0; i < lPathElements1.length; i++) {
136: if (!lSecondElementsSet.remove(lPathElements1[i]))
137: return false; // There is no element anywhere
138: }
139: return true;
140: }
141: }
|