KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > flowcontrol > StopTask


1 /**
2  * $Id: StopTask.java 186 2007-03-16 13:42:35Z ssmc $
3  * Copyright 2002-2004 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.flowcontrol;
30
31 import java.lang.reflect.Constructor JavaDoc;
32
33 import org.apache.tools.ant.BuildException;
34 import org.apache.tools.ant.Location;
35 import org.apache.tools.ant.Project;
36
37 import com.idaremedia.antx.AntX;
38 import com.idaremedia.antx.NoiseLevel;
39 import com.idaremedia.antx.apis.BuildError;
40 import com.idaremedia.antx.helpers.InnerString;
41 import com.idaremedia.antx.helpers.Tk;
42 import com.idaremedia.antx.ownhelpers.LocalTk;
43 import com.idaremedia.antx.starters.MsgTask;
44
45 /**
46  * Throws either a {@linkplain BuildException BuildException} or a
47  * {@linkplain BuildError BuildError}. Usually defined as &lt;stop&gt; or &lt;stopbuild&gt;
48  * <em>This task does not call System.exit</em>. Specifying a stop with "fatal=yes"
49  * lets you bypass any enclosing protected taskset catch-clauses (their associated
50  * <i>always</i> tasks are still executed however).
51  * <p>
52  * As of Ant 1.6, the StopTask's effectiveness has been greatly reduced. Because Ant
53  * 1.6 will capture and unconditionally convert any <em>Throwable</em> to a
54  * <span class="src">BuildException</span>, it is possible that in spite of a fatal
55  * stop request, the enclosing Ant process will "keep going".
56  * <p>
57  * Some examples:<pre>
58  * &lt;stopbuild fatal="yes" msgid="log4j.required"/&gt;
59  * &lt;stop msgid="require.build.rules" with="my.MissingResourcesException"/&gt;
60  * &lt;stop message="Ant 1.5 or higher is required" unless="atleast.ant15.present"/&gt;
61  * </pre>
62  *
63  * @since JWare/AntX 0.2
64  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
65  * @version 0.5
66  * @.safety single
67  * @.group api,infra
68  **/

69
70 public class StopTask extends MsgTask
71 {
72     /**
73      * Initializes a new StopTask instance.
74      **/

75     public StopTask()
76     {
77         super(AntX.flow+"stop");
78     }
79
80
81     /**
82      * Initializes a new CV-labeled StopTask instance.
83      * @param iam CV-label (non-null)
84      **/

85     public StopTask(String JavaDoc iam)
86     {
87         super(iam);
88     }
89
90
91     /**
92      * Updates the fallback default message of this task if none
93      * has been defined. Only replaces default once. Moved from
94      * 'setLocation' for Ant 1.6.1 and UnknownElement workability.
95      * @since JWare/AntX 0.4
96      **/

97     public void init()
98     {
99         super.init();
100
101         Location loc = getLocation();
102
103         if (loc!=Location.UNKNOWN_LOCATION) {
104             if (Tk.isWhitespace(getDefaultMsg())) {
105                 String JavaDoc locationString = uistrs().get
106                     ("stop.defaultmsg",getTaskName(),LocalTk.purtyLocation(loc));
107                 addConfiguredDefaultMsg(new InnerString(locationString));
108             }
109         }
110     }
111
112
113
114     /**
115      * Overridden to show that this task emits messages at the error
116      * level by default.
117      **/

118     public NoiseLevel getDefaultMsgLevel()
119     {
120         return isFatal() ? NoiseLevel.FATAL : NoiseLevel.ERROR;
121     }
122
123
124
125     /**
126      * Sets the fatal flag on this task. This task will generate (the more
127      * unblockable) {@linkplain BuildError} when executed.
128      **/

129     public void setFatal(boolean fatal)
130     {
131         m_tryFatal = fatal;
132     }
133
134
135     /**
136      * Returns <i>true</i> if this task will generate the more serious
137      * {@linkplain BuildError BuildError} when executed.
138      **/

139     public final boolean isFatal()
140     {
141         return m_tryFatal;
142     }
143
144
145     /**
146      * Define a custom exception {@linkplain BuildException BuildException}
147      * class for this task to throw. The specified class must be a subclass
148      * of BuildException with a constructor that takes a String (the message)
149      * and a {@linkplain Location location}.
150      * @param claz the exception class's name (non-null)
151      **/

152     public void setWith(Class JavaDoc claz)
153     {
154         require_(claz!=null,"setWith- nonzro clas");
155         if (!BuildException.class.isAssignableFrom(claz)) {
156             String JavaDoc ermsg = uistrs().get("stop.bad.throwclass",claz.getName());
157             log(ermsg,Project.MSG_ERR);
158             throw new BuildException(ermsg,getLocation());
159         }
160         m_throwClass = claz;
161     }
162
163
164     /**
165      * Returns the custom exception class used by this task. Returns
166      * <i>null</i> if never set.
167      **/

168     public final Class JavaDoc getWith()
169     {
170         return m_throwClass;
171     }
172
173
174     /**
175      * Generates either a build exception or a build error if this
176      * task's filter conditions (if/unless) permit.
177      * @throws BuildException if not fatal
178      * @throws BuildError if fatal
179      **/

180     public void execute()
181     {
182         verifyCanExecute_("execute");
183
184         if (testIfCondition() && testUnlessCondition()) {
185             String JavaDoc ermsg = getMsg();
186             aboutToStop(ermsg);
187             if (isFatal()) {
188                 throw new BuildError(ermsg,getLocation());
189             }
190             throw new_BuildException(ermsg,getLocation());
191         }
192     }
193
194
195     /**
196      * Called to generate feedback to build monitor(s). By default
197      * just logs the message at this task's feedback noise level.
198      * @param ermsg the event's feedback message (non-null)
199      **/

200     protected void aboutToStop(String JavaDoc ermsg)
201     {
202         log(ermsg, getMsgLevel().getNativeIndex());
203     }
204
205
206     /**
207      * Factory method for a build exception thrown by this task.
208      **/

209     private BuildException new_BuildException(String JavaDoc ermsg, Location theLocation)
210     {
211         BuildException tbt=null;
212
213         if (getWith()!=null) {
214             Class JavaDoc claz= getWith();
215             try {
216                 Constructor JavaDoc ctor= claz.getConstructor(DFLT_SIGNATURE);
217                 Object JavaDoc[] params = new Object JavaDoc[]{ermsg,theLocation};
218                 tbt = (BuildException)ctor.newInstance(params);
219             } catch(Exception JavaDoc anyX) {
220                 log(uistrs().get("stop.cant.make.custom.reason",claz.getName(),
221                                  anyX.getMessage()), Project.MSG_WARN);
222             }
223         }
224         if (tbt==null) {
225             tbt= new BuildException(ermsg,theLocation);
226         }
227         return tbt;
228     }
229
230
231
232     private boolean m_tryFatal;//NB:no
233
private Class JavaDoc m_throwClass;
234
235
236     /** Required constructor signature of custom exception class. **/
237     protected static final Class JavaDoc[] DFLT_SIGNATURE= new Class JavaDoc[] {
238         String JavaDoc.class, Location.class
239     };
240 }
241
242 /* end-of-StopTask.java */
243
Popular Tags