001: /*
002: * @(#)ReferenceType.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Part of the GroboUtils package at:
009: * http://groboutils.sourceforge.net
010: *
011: * Permission is hereby granted, free of charge, to any person obtaining a
012: * copy of this software and associated documentation files (the "Software"),
013: * to deal in the Software without restriction, including without limitation
014: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
015: * and/or sell copies of the Software, and to permit persons to whom the
016: * Software is furnished to do so, subject to the following conditions:
017: *
018: * The above copyright notice and this permission notice shall be included in
019: * all copies or substantial portions of the Software.
020: *
021: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
022: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
024: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
025: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
026: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
027: * DEALINGS IN THE SOFTWARE.
028: */
029: package net.sourceforge.groboutils.mbtf.v1.ant;
030:
031: import org.apache.tools.ant.Project;
032: import org.apache.tools.ant.BuildException;
033: import org.apache.tools.ant.Location;
034: import org.apache.tools.ant.taskdefs.Definer;
035: import org.apache.tools.ant.types.Reference;
036: import org.apache.tools.ant.types.DataType;
037: import org.apache.tools.ant.Task;
038:
039: import java.lang.reflect.Method;
040:
041: import net.sourceforge.groboutils.mbtf.v1.IValidate;
042: import net.sourceforge.groboutils.mbtf.v1.IErrors;
043: import net.sourceforge.groboutils.mbtf.v1.ISystem;
044: import net.sourceforge.groboutils.mbtf.v1.TestHaltRuntimeException;
045:
046: /**
047: * Use the "refid" attribute to set the reference task/target.
048: * Set the "sysid" to set the ID to register the "system" variable in the
049: * project. The execute() method will execute the referenced object.
050: *
051: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
052: * @version $Date: 2003/02/10 22:52:25 $
053: * @since June 13, 2002
054: */
055: public class ReferenceType extends DataType {
056: private String sysRefId;
057:
058: protected Location location = Location.UNKNOWN_LOCATION;
059:
060: /**
061: * Sets the file location where this task was defined.
062: */
063: public void setLocation(Location location) {
064: this .location = location;
065: }
066:
067: public void setSysId(String id) {
068: this .sysRefId = id;
069: }
070:
071: public void setSystem(ISystem sys) {
072: if (this .sysRefId != null && sys != null) {
073: project.addReference(this .sysRefId, sys);
074: }
075: }
076:
077: /**
078: * Execute the referenced object
079: */
080: public void execute() throws BuildException {
081: // get the referenced object
082: if (!isReference()) {
083: throw new BuildException("does not have a reference",
084: location);
085: }
086: Object o = getCheckedRef(Object.class, "task or target");
087:
088: // use reflection to find the execute() method.
089: Class c = o.getClass();
090: Method m;
091: try {
092: m = c.getMethod("execute", new Class[0]);
093: } catch (Exception e) {
094: throw new BuildException("No 'execute' method for "
095: + c.getName(), e, location);
096: }
097:
098: try {
099: m.invoke(o, new Object[0]);
100: } catch (BuildException be) {
101: throw be;
102: } catch (TestHaltRuntimeException thre) {
103: throw thre;
104: } catch (Exception e) {
105: throw new BuildException("Unexpected exception", e,
106: location);
107: }
108: }
109:
110: public void execute(ISystem system, IErrors errors) {
111: setSystem(system);
112:
113: try {
114: execute();
115: } catch (TestHaltRuntimeException thre) {
116: throw thre;
117: } catch (Exception e) {
118: errors.addError(e.getMessage(), e);
119: }
120: }
121: }
|