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 org.apache.tools.ant.taskdefs.optional.metaboss;
016:
017: import java.io.File;
018: import java.io.IOException;
019: import java.util.Properties;
020:
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.Task;
023: import org.apache.tools.ant.types.Path;
024:
025: import com.metaboss.util.AntUtils;
026:
027: /** The abstract base class for all metaboss tasks regardless whether they require access to the model or not
028: * It looks after metabosshome attrtibute */
029: public abstract class MetaBossToolTask extends Task {
030: private static Properties sPathProperties = null;
031: private File mMetaBossHomeDir = null;
032: private Properties mInvocationParams = new Properties();
033: private Properties mInvocationEnvironment = new Properties();
034:
035: /** Adds environment variable to use when invoking the task */
036: public void addConfiguredParam(MetaBossToolInvocationParam pProperty)
037: throws BuildException {
038: if (pProperty.getName() == null
039: || pProperty.getName().length() == 0)
040: throw new BuildException(
041: "Invocation parameter must have a non-empty name");
042: if (mInvocationParams.put(pProperty.getName(), pProperty
043: .getContent()) != null)
044: throw new BuildException(
045: "Each invocation parameter must have a unique name. The name '"
046: + pProperty.getName()
047: + "' appears to have been used more than once.");
048: }
049:
050: /** Adds environment variable to use when invoking the task */
051: public void addConfiguredEnv(MetaBossToolInvocationParam pProperty)
052: throws BuildException {
053: if (pProperty.getName() == null
054: || pProperty.getName().length() == 0)
055: throw new BuildException(
056: "Environment entry must have a non-empty name");
057: if (mInvocationEnvironment.put(pProperty.getName(), pProperty
058: .getContent()) != null)
059: throw new BuildException(
060: "Each environment entry must have a unique name. The name '"
061: + pProperty.getName()
062: + "' appears to have been used more than once.");
063: }
064:
065: /** Returns additional invocation properties */
066: protected Properties getInvocationParams() {
067: return mInvocationParams;
068: }
069:
070: /** Returns additional invocation properties */
071: protected Properties getInvocationEnvironment() {
072: return mInvocationEnvironment;
073: }
074:
075: // The getter for the extra arguments to be passed to the task
076: protected String[] getExtraArguments() throws BuildException {
077: return new String[0];
078: }
079:
080: // The getter for the "metaboss_home" or METABOSS_HOME property
081: protected File getMetaBossHome() throws BuildException {
082: if (mMetaBossHomeDir == null) {
083: String lMetaBossHomeDirectoryName = AntUtils
084: .getMandatoryProjectOrEnvironmentProperty(
085: getProject(), "metaboss_home",
086: "METABOSS_HOME");
087: File lFile = new File(lMetaBossHomeDirectoryName);
088: if (lFile.exists() == false)
089: throw new BuildException(
090: "Directory "
091: + lMetaBossHomeDirectoryName
092: + " does not exist. Unable to access MetaBoss home directory, which is mandatory for <"
093: + getTaskName() + "> task.");
094: if (lFile.isDirectory() == false)
095: throw new BuildException(
096: lMetaBossHomeDirectoryName
097: + " is not a directory. Unable to access MetaBoss home directory, which is mandatory for <"
098: + getTaskName() + "> task.");
099: if (lFile.canRead() == false)
100: throw new BuildException(
101: lMetaBossHomeDirectoryName
102: + " directory is not accessible. Unable to access MetaBoss home directory, which is mandatory for <"
103: + getTaskName() + "> task.");
104: mMetaBossHomeDir = lFile;
105: }
106: return mMetaBossHomeDir;
107: }
108:
109: // Returns path by it's logical name. Allows to manage path in one place
110: protected Path getPath(String pPathName) throws BuildException {
111: Path lPath = new Path(getProject());
112: Properties lProps = getPathProperties();
113: for (int i = 1;; i++) {
114: String lNextPathElementName = pPathName + "." + i;
115: String lNextPathElement = lProps
116: .getProperty(lNextPathElementName);
117: if (lNextPathElement == null)
118: break;
119: lPath.createPathElement().setPath(
120: getMetaBossHome() + File.separator
121: + lNextPathElement);
122: }
123: return lPath;
124: }
125:
126: // Helper. Returns path properties
127: private static Properties getPathProperties() throws BuildException {
128: try {
129: if (sPathProperties == null) {
130: sPathProperties = new Properties();
131: sPathProperties.load(MetaBossModelToolTask.class
132: .getResourceAsStream("path.properties"));
133: }
134: return sPathProperties;
135: } catch (IOException e) {
136: throw new BuildException(
137: "Unable to load path properties from path.properties file.",
138: e);
139: }
140: }
141: }
|