KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > capture > LogsUsingTask


1 /**
2  * $Id: LogsUsingTask.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2003-2005 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 (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any 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 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 GNU 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.capture;
30
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Project;
33
34 import com.idaremedia.antx.AssertableTask;
35 import com.idaremedia.antx.ownhelpers.FeedbackSink;
36
37 /**
38  * Starter implementation for any task that reads and/or manipulates information
39  * captured by the various {@linkplain LogsRecorder LogsRecorders}.
40  *
41  * @since JWare/AntX 0.3
42  * @author ssmc, &copy;2003-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
43  * @version 0.5
44  * @.safety single
45  * @.group impl,helper
46  **/

47
48 public abstract class LogsUsingTask extends AssertableTask
49 {
50     /**
51      * Initializes a CV-labeled task instance.
52      * @param iam CV-label (non-null)
53      **/

54     protected LogsUsingTask(String JavaDoc iam)
55     {
56         super(iam);
57     }
58
59
60 // ---------------------------------------------------------------------------------------
61
// Parameters:
62
// ---------------------------------------------------------------------------------------
63

64
65     /**
66      * Sets whether only the captured important logs will be used by
67      * this task. Is <i>true</i> by default.
68      * @param is <i>false</i> to use all captured logs
69      **/

70     public void setImportant(boolean is)
71     {
72         m_isImportant = is;
73     }
74
75
76     /**
77      * Returns <i>true</i> if only the important logs are used.
78      * Is <i>true</i> by default. If <i>false</i> all captured
79      * log information is used.
80      **/

81     public final boolean shouldBeImportant()
82     {
83         return m_isImportant;
84     }
85
86
87     /**
88      * Tells this task to clear its enclosing logs recorder after
89      * copy.
90      **/

91     public void setReset(boolean reset)
92     {
93         m_resetAfter = reset;
94     }
95
96
97     /**
98      * Returns <i>true</i> if this task will clear its enclosing
99      * logs recorder's buffers after using it. Recorder is cleared
100      * only if this task succesfully completes processing.
101      **/

102     public final boolean willReset()
103     {
104         return m_resetAfter;
105     }
106
107
108     /**
109      * Sets the log information source for this task. Output is used
110      * from the nearest logs recorder of matching type.
111      * @param source one of [stdio,antlog]
112      * @throws BuildException if bad source defined
113      **/

114     public void setFrom(FeedbackSink source)
115     {
116         require_(source!=null,"setFrom- nonzro src");
117         verifyLogsSource(source);
118         m_from = source;
119     }
120
121
122     /**
123      * Returns this task's log information source. If never defined
124      * will return {@linkplain FeedbackSink#ANTLOG ANTLOG}. Never
125      * returns <i>null</i>.
126      **/

127     public FeedbackSink getFrom()
128     {
129         return m_from;
130     }
131
132
133     /**
134      * Verifies the log information source for this task. By default
135      * ensures the logs source is a grouping identifier (like "antlog").
136      * @param source one of [stdio,antlog]
137      * @throws BuildException if bad source defined
138      **/

139     protected void verifyLogsSource(FeedbackSink source)
140     {
141         if (!FeedbackSink.isGrouping(source)) {
142             String JavaDoc ermsg = getAntXMsg("brul.logs.bad.capture.sink",
143                                       getTaskName(), source.getValue());
144             log(ermsg,Project.MSG_ERR);
145             throw new BuildException(ermsg,getLocation());
146         }
147     }
148
149 // ---------------------------------------------------------------------------------------
150
// Execution:
151
// ---------------------------------------------------------------------------------------
152

153    /**
154     * Return <i>true</i> if this task can accept it's inputs from a
155     * source other than a logs recorder. Important to support sources other
156     * than logs recorders for some evaluation tasks.
157     * @since JWare/AntX 0.5
158     **/

159     protected boolean isSourceFlexible()
160     {
161         return false;
162     }
163
164
165
166     /**
167      * Determine the logs recorder to use. The current value of
168      * the "{@linkplain #setFrom from}" option determines the recorder.
169      * @param allowNull <i>true</i> if can return <i>null</i> otherwise
170      * the {@linkplain EmptyLogs#INSTANCE null-proxy} is returned
171      **/

172     protected final LogsRecorder getRecorder(boolean allowNull)
173     {
174         LogsRecorder r=null;
175
176         switch (getFrom().getIndex()) {
177             case FeedbackSink.ANTLOG_INDEX: {
178                 r = CapturedLogs.getRecorder();
179                 break;
180             }
181             case FeedbackSink.STDIO_INDEX:
182             case FeedbackSink.STDOUT_INDEX:
183             case FeedbackSink.STDERR_INDEX: {
184                 r = CapturedLogs.getStdIORecorder();
185                 break;
186             }
187         }
188         if (r==null && !allowNull) {
189             r = EmptyLogs.INSTANCE;
190         }
191         return r;
192     }
193
194
195     /**
196      * Determine the logs to be copied. Never returns <i>null</i>,
197      * but can return the empty string.
198      * @see CapturedLogs#getRecorder
199      **/

200     protected final String JavaDoc getVUTLog()
201     {
202         LogsRecorder r = getRecorder(false);
203         if (shouldBeImportant()) {
204             return r.copyOfImportantLogs();
205         }
206         return r.copyOfAllLogs();
207     }
208
209
210     /**
211      * Verifies that this task is enclosed by at least one
212      * {@linkplain LogsRecorder} enabled taskset.
213      **/

214     protected void verifyCanExecute_(String JavaDoc calr)
215     {
216         super.verifyCanExecute_(calr);
217
218         if (getRecorder(true)==null) {
219             String JavaDoc warning = getAntXMsg("brul.logs.no.recorder");
220             int level = isSourceFlexible() ? Project.MSG_VERBOSE : Project.MSG_WARN;
221             log(warning,level);
222         }
223     }
224
225
226     private boolean m_isImportant=true;//NB:by default only important checked
227
private boolean m_resetAfter;//NB: leaves logs alone
228
private FeedbackSink m_from = FeedbackSink.ANTLOG;//NB:compatible with 0.2
229
}
230
231 /* end-of-LogsUsingTask.java */
232
Popular Tags