KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > mbtf > v1 > ant > ReferenceType


1 /*
2  * @(#)ReferenceType.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Part of the GroboUtils package at:
9  * http://groboutils.sourceforge.net
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */

29 package net.sourceforge.groboutils.mbtf.v1.ant;
30
31 import org.apache.tools.ant.Project;
32 import org.apache.tools.ant.BuildException;
33 import org.apache.tools.ant.Location;
34 import org.apache.tools.ant.taskdefs.Definer;
35 import org.apache.tools.ant.types.Reference;
36 import org.apache.tools.ant.types.DataType;
37 import org.apache.tools.ant.Task;
38
39 import java.lang.reflect.Method JavaDoc;
40
41 import net.sourceforge.groboutils.mbtf.v1.IValidate;
42 import net.sourceforge.groboutils.mbtf.v1.IErrors;
43 import net.sourceforge.groboutils.mbtf.v1.ISystem;
44 import net.sourceforge.groboutils.mbtf.v1.TestHaltRuntimeException;
45
46
47 /**
48  * Use the "refid" attribute to set the reference task/target.
49  * Set the "sysid" to set the ID to register the "system" variable in the
50  * project. The execute() method will execute the referenced object.
51  *
52  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
53  * @version $Date: 2003/02/10 22:52:25 $
54  * @since June 13, 2002
55  */

56 public class ReferenceType extends DataType
57 {
58     private String JavaDoc sysRefId;
59     
60
61     protected Location location = Location.UNKNOWN_LOCATION;
62     /**
63      * Sets the file location where this task was defined.
64      */

65     public void setLocation(Location location) {
66         this.location = location;
67     }
68     
69     
70     public void setSysId( String JavaDoc id )
71     {
72         this.sysRefId = id;
73     }
74     
75     
76     public void setSystem( ISystem sys )
77     {
78         if (this.sysRefId != null && sys != null)
79         {
80             project.addReference( this.sysRefId, sys );
81         }
82     }
83     
84     
85     /**
86      * Execute the referenced object
87      */

88     public void execute()
89             throws BuildException
90     {
91         // get the referenced object
92
if (!isReference())
93         {
94             throw new BuildException( "does not have a reference", location );
95         }
96         Object JavaDoc o = getCheckedRef( Object JavaDoc.class, "task or target" );
97         
98         // use reflection to find the execute() method.
99
Class JavaDoc c = o.getClass();
100         Method JavaDoc m;
101         try
102         {
103             m = c.getMethod( "execute", new Class JavaDoc[0] );
104         }
105         catch (Exception JavaDoc e)
106         {
107             throw new BuildException("No 'execute' method for "+c.getName(),
108                 e, location );
109         }
110         
111         try
112         {
113             m.invoke( o, new Object JavaDoc[0] );
114         }
115         catch (BuildException be)
116         {
117             throw be;
118         }
119         catch (TestHaltRuntimeException thre)
120         {
121             throw thre;
122         }
123         catch (Exception JavaDoc e)
124         {
125             throw new BuildException( "Unexpected exception", e, location );
126         }
127     }
128     
129     
130     public void execute( ISystem system, IErrors errors )
131     {
132         setSystem( system );
133         
134         try
135         {
136             execute();
137         }
138         catch (TestHaltRuntimeException thre)
139         {
140             throw thre;
141         }
142         catch (Exception JavaDoc e)
143         {
144             errors.addError( e.getMessage(), e );
145         }
146     }
147 }
148
149
Popular Tags