001: /*
002: * @(#)ValidateJavaType.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.taskdefs.Definer;
034: import org.apache.tools.ant.taskdefs.Property;
035: import org.apache.tools.ant.types.Reference;
036:
037: import java.util.Vector;
038: import java.util.Enumeration;
039: import java.io.File;
040:
041: import net.sourceforge.groboutils.mbtf.v1.IValidate;
042: import net.sourceforge.groboutils.mbtf.v1.IAction;
043: import net.sourceforge.groboutils.mbtf.v1.IErrors;
044: import net.sourceforge.groboutils.mbtf.v1.ISystem;
045:
046: import org.apache.log4j.Logger;
047:
048: /**
049: * A specific IValidate instance, which is Ant compatible, that instantiates
050: * a set of Classes using the default constructor, and iterates through them
051: * as though they each were IValidate instances.
052: *
053: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
054: * @version $Date: 2003/02/10 22:52:25 $
055: * @since June 13, 2002
056: */
057: public class ValidateJavaType extends Definer implements IValidate,
058: IAction {
059: private static final Logger LOG = Logger
060: .getLogger(ValidateJavaType.class);
061:
062: // intended to be an imbedded task, but can be defined outside
063: // the state iterator.
064: public static final String DEFAULT_ANT_NAME = "mbtf.validatejava";
065:
066: private Vector validators = new Vector();
067: private Vector properties = new Vector();
068:
069: public void execute() throws BuildException {
070: if (project == null) {
071: throw new BuildException("ValidateJavaType project is null");
072: }
073:
074: // clean out the old classes, if any remain
075: this .validators = new Vector();
076: try {
077: super .execute();
078: } catch (BuildException e) {
079: // allow for the name to be empty
080: setName("ignore");
081: super .execute();
082: }
083: }
084:
085: protected void addDefinition(String name, Class c)
086: throws BuildException {
087: LOG.debug("added " + c);
088: addClass(c);
089: }
090:
091: public void addClass(Class c) throws BuildException {
092: try {
093: Object o = c.newInstance();
094: this .validators.addElement(o);
095: } catch (Exception e) {
096: throw new BuildException("Could not instantiate "
097: + c.getName(), e, location);
098: }
099: }
100:
101: public void addParam(Property prop) {
102: if (prop != null) {
103: this .properties.addElement(prop);
104: }
105: }
106:
107: public void validate( ISystem system, IErrors errors )
108: {
109: LOG.debug(this +": Entering validate()");
110: try
111: {
112: execute();
113: }
114: catch (BuildException e)
115: {
116: LOG.info("execute() caused exception", e);
117: errors.addError("Failed to correctly process Ant structure.", e );
118: LOG.debug(this +": Leaving validate() due to build exception");
119: return;
120: }
121:
122: Enumeration enum = this .validators.elements();
123: int count = 0;
124: while (enum.hasMoreElements())
125: {
126: Object o = enum.nextElement();
127: if (o instanceof IValidate)
128: {
129: ++count;
130: setupObject( o );
131: LOG.debug(this +": Start Validate "+o);
132: ((IValidate)o).validate( system, errors );
133: LOG.debug(this +": End Validate "+o);
134: }
135: }
136: LOG.debug(this +": Leaving validate() ["+count+" validators]");
137: }
138:
139: public void performAction( ISystem system, IErrors errors )
140: {
141: LOG.debug( this +": Entering performAction()" );
142: try
143: {
144: execute();
145: }
146: catch (BuildException e)
147: {
148: LOG.info("execute() caused exception", e);
149: errors.addError("Failed to correctly process Ant structure.", e );
150: LOG.debug(this +": Leaving performAction() due to build exception");
151: return;
152: }
153:
154: Enumeration enum = this .validators.elements();
155: int count = 0;
156: while (enum.hasMoreElements())
157: {
158: Object o = enum.nextElement();
159: if (o instanceof IAction)
160: {
161: ++count;
162: setupObject( o );
163: LOG.debug(this +": Start Perform action on "+o);
164: ((IAction)o).performAction( system, errors );
165: LOG.debug(this +": End perform action on "+o);
166: }
167: }
168: LOG.debug(this +": Leaving performAction() ["+count+" actions]");
169: }
170:
171: protected void setupObject( Object o )
172: {
173: if (o != null && o instanceof IUsesProperties)
174: {
175: IUsesProperties iup = (IUsesProperties)o;
176: iup.setProject( project );
177:
178: Enumeration enum = this.properties.elements();
179: while (enum.hasMoreElements())
180: {
181: Property p = (Property)enum.nextElement();
182: String name = p.getName();
183: String value = p.getValue();
184: File file = p.getFile();
185: Reference refid = p.getRefid();
186: String resource = p.getResource();
187: String env = p.getEnvironment();
188:
189: if (value != null)
190: {
191: iup.addProperty( name, value );
192: }
193: if (file != null)
194: {
195: iup.addFile( name, file );
196: }
197: if (refid != null)
198: {
199: LOG.debug("Adding reference '"+name+"' to '"+refid+"'");
200: iup.addReference( name, refid );
201: }
202: if (resource != null)
203: {
204: iup.addResource( name, resource );
205: }
206: if (env != null)
207: {
208: iup.addEnvironment(name, env );
209: }
210: }
211: }
212: }
213: }
|