KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > feedback > CheckpointTask


1 /**
2  * $Id: CheckpointTask.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 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.feedback;
30
31 import java.io.PrintStream JavaDoc;
32
33 import org.apache.tools.ant.types.Reference;
34
35 import com.idaremedia.antx.AntX;
36 import com.idaremedia.antx.NoiseLevel;
37 import com.idaremedia.antx.starters.EchoThingTask;
38
39 /**
40  * Task that emits a simple checkpoint to listening build monitors. Usually defined
41  * as &lt;checkpoint&gt; The 'tofile' parameter is exclusive-- it <em>prevents</em>
42  * the checkpoint from being broadcast to listening monitors.
43  * <p>
44  * <b>Examples:</b><pre>
45  * &lt;checkpoint/&gt;
46  * &lt;checkpoint msgid="cp.last.compiled"/&gt;
47  * &lt;checkpoint message="Checkstyle Warnings: ${N.warnings}"/&gt;
48  * &lt;checkpoint with="ec.metrics" tofile="${reports}/etc/profiled.last"/&gt;
49  * </pre>
50  *
51  * @since JWare/AntX 0.3
52  * @author ssmc, &copy;2003,2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
53  * @version 0.5
54  * @.safety single
55  * @.group api,helper
56  * @see EmitTask
57  **/

58
59 public class CheckpointTask extends EchoThingTask implements EmitConfigurable
60 {
61     /**
62      * Initializes a new CheckpointTask instance.
63      **/

64     public CheckpointTask()
65     {
66         super(AntX.feedback);
67         setThingRefId("_");
68     }
69
70
71     /**
72      * Returns the fallback emit configuration this task will
73      * use to generate diagnostics emitters, etc. Never returns
74      * <i>null</i>; usually an enclosing emit configure task.
75      **/

76     public EmitConfiguration getDefaults()
77     {
78         return EmitContext.getConfigurationNoNull();
79     }
80
81
82     /**
83      * Assigns a primary emit configuration definition to this
84      * task. This task will use this configuration instead of
85      * any enclosing context configuration.
86      * @param r the {@linkplain EmitConfiguration} reference
87      **/

88     public void setWith(Reference r)
89     {
90         require_(r!=null,"setWith- nonzro refid");
91         m_withEC= r;
92     }
93
94
95     /**
96      * Returns this task's user-defined emit configuration or
97      * <i>null</i> if none defined.
98      * @see #setWith
99      **/

100     public final Reference getWith()
101     {
102         return m_withEC;
103     }
104
105
106
107     /**
108      * Tells this checkpoint whether the owning target's label
109      * should be included in output message. Is on by default. Note
110      * that target information is <em>only</em> addable to
111      * user-supplied messages; plain timestamps never include other
112      * information.
113      * @param include <i>false</i> to exclude source label
114      * @since JWare/AntX 0.5
115      **/

116     public void setIncludeSource(boolean include)
117     {
118         m_includeSource = include;
119     }
120
121
122
123     /**
124      * Returns <i>true</i> if this checkpoint will include its
125      * owning target's name in emitted message. Defaults yes.
126      * @since JWare/AntX 0.5
127      **/

128     public final boolean willIncludeSource()
129     {
130         return m_includeSource;
131     }
132
133
134
135     /**
136      * Returns the effective EmitConfiguration used by this
137      * task. Never returns <i>null</i>.
138      * @see #setWith
139      * @see #getDefaults
140      * @.safety single
141      **/

142     protected synchronized EmitConfiguration getEmitHelper()
143     {
144         if (m_ECInstance!=null) {
145             return m_ECInstance;
146         }
147         if (getWith()!=null) {
148             m_ECInstance = (EmitConfiguration)getReferencedObject
149                 (getProject(), getWith().getRefId(), EmitConfiguration.class);
150         }
151         else {
152             m_ECInstance = getDefaults();
153         }
154         return m_ECInstance;
155     }
156
157
158     /**
159      * Emits the checkpoint line to either a file or any
160      * listening log4j monitors.
161      **/

162     protected void echoThing()
163     {
164         final long NOW = System.currentTimeMillis();
165
166         EmitConfiguration helper = getEmitHelper();
167
168         String JavaDoc finalMsg, userMsg = getMsg();
169         if (userMsg.length()!=0) {
170             if (willIncludeSource()) {
171                 finalMsg = uistrs().
172                     get("emit.checkpoint",
173                         /*who*/getOwningTarget().getName(),
174                         /*when*/helper.stampify(NOW),
175                         /*what*/userMsg);
176             } else {
177                 finalMsg = uistrs().
178                     get("emit.checkpoint.nosource",
179                         /*when*/helper.stampify(NOW),
180                         /*what*/userMsg);
181             }
182         } else {
183             finalMsg = helper.stampify(NOW);
184         }
185
186         if (getToFile()==null) {
187             Emit.broadcast(helper.getEmitter(), finalMsg, null, NoiseLevel.INFO);
188         } else {
189             PrintStream JavaDoc out = new PrintStream JavaDoc(getOutputStream());
190             out.println(finalMsg);
191             out.flush();
192             out.close();
193         }
194     }
195
196
197     private Reference m_withEC;
198     private EmitConfiguration m_ECInstance;
199     private boolean m_includeSource=true;//NB:true=>backward-compatible pre AntX-0.5!
200
}
201
202 /* end-of-CheckpointTask.java */
203
Popular Tags