KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > util > AntLog


1 /*
2  * ========================================================================
3  *
4  * Copyright 2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.integration.ant.util;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.Target;
25 import org.apache.tools.ant.Task;
26
27 /**
28  * Support class that lets classes log to Ant using the Commons Logging API.
29  *
30  * This is not intended to be a general solution, rather as a thin separation
31  * layer to not have to pass around full-blown Ant <code>Project</code>,
32  * <code>Target</code> or <code>Task</code> objects just to enable logging.
33  *
34  * Note that as there is no log level in Commons-Logging that corresponds to
35  * Ant's <em>VERBOSE</em> level (the level between <em>INFO</em> and
36  * <em>DEBUG</em>), the <em>TRACE</em> level of Commons-Logging gets mapped to
37  * <em>VERBOSE</em>, which is probably inappropriate for components that do not
38  * know they are using the <code>AntLog</code> class.
39  *
40  * @version $Id: AntLog.java,v 1.6 2004/02/29 10:19:57 vmassol Exp $
41  */

42 public final class AntLog implements Log
43 {
44
45     // Constants ---------------------------------------------------------------
46

47     /**
48      * Singleton log implementation that simply ignores all log requests.
49      */

50     public static final Log NULL = new Log()
51     {
52
53         // Log Implementation --------------------------------------------------
54

55         /**
56          * @see org.apache.commons.logging.Log#isFatalEnabled()
57          */

58         public boolean isFatalEnabled()
59         {
60             return false;
61         }
62
63         /**
64          * @see org.apache.commons.logging.Log#fatal(Object)
65          */

66         public void fatal(Object JavaDoc theMessage)
67         {
68             // do nothing
69
}
70
71         /**
72          * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
73          */

74         public void fatal(Object JavaDoc theMessage, Throwable JavaDoc theThrowable)
75         {
76             // do nothing
77
}
78
79         /**
80          * @see org.apache.commons.logging.Log#isErrorEnabled()
81          */

82         public boolean isErrorEnabled()
83         {
84             return false;
85         }
86
87         /**
88          * @see org.apache.commons.logging.Log#error(Object)
89          */

90         public void error(Object JavaDoc theMessage)
91         {
92             // do nothing
93
}
94
95         /**
96          * @see org.apache.commons.logging.Log#error(Object, Throwable)
97          */

98         public void error(Object JavaDoc theMessage, Throwable JavaDoc theThrowable)
99         {
100             // do nothing
101
}
102
103         /**
104          * @see org.apache.commons.logging.Log#isWarnEnabled()
105          */

106         public boolean isWarnEnabled()
107         {
108             return false;
109         }
110
111         /**
112          * @see org.apache.commons.logging.Log#warn(Object)
113          */

114         public void warn(Object JavaDoc theMessage)
115         {
116             // do nothing
117
}
118
119         /**
120          * @see org.apache.commons.logging.Log#warn(Object, Throwable)
121          */

122         public void warn(Object JavaDoc theMessage, Throwable JavaDoc theThrowable)
123         {
124             // do nothing
125
}
126
127         /**
128          * @see org.apache.commons.logging.Log#isInfoEnabled()
129          */

130         public boolean isInfoEnabled()
131         {
132             return false;
133         }
134
135         /**
136          * @see org.apache.commons.logging.Log#info(Object)
137          */

138         public void info(Object JavaDoc theMessage)
139         {
140             // do nothing
141
}
142
143         /**
144          * @see org.apache.commons.logging.Log#info(Object, Throwable)
145          */

146         public void info(Object JavaDoc theMessage, Throwable JavaDoc theThrowable)
147         {
148             // do nothing
149
}
150
151         /**
152          * @see org.apache.commons.logging.Log#isDebugEnabled()
153          */

154         public boolean isDebugEnabled()
155         {
156             return false;
157         }
158
159         /**
160          * @see org.apache.commons.logging.Log#debug(Object)
161          */

162         public void debug(Object JavaDoc theMessage)
163         {
164             // do nothing
165
}
166
167         /**
168          * @see org.apache.commons.logging.Log#debug(Object, Throwable)
169          */

170         public void debug(Object JavaDoc theMessage, Throwable JavaDoc theThrowable)
171         {
172             // do nothing
173
}
174
175         /**
176          * @see org.apache.commons.logging.Log#isTraceEnabled()
177          */

178         public boolean isTraceEnabled()
179         {
180             return false;
181         }
182
183         /**
184          * @see org.apache.commons.logging.Log#trace(Object)
185          */

186         public void trace(Object JavaDoc theMessage)
187         {
188             // do nothing
189
}
190
191         /**
192          * @see org.apache.commons.logging.Log#trace(Object, Throwable)
193          */

194         public void trace(Object JavaDoc theMessage, Throwable JavaDoc theThrowable)
195         {
196             // do nothing
197
}
198
199     };
200
201     // Instance Variables ------------------------------------------------------
202

203     /**
204      * The Ant project.
205      */

206     private Project project;
207
208     /**
209      * The current target, or <code>null</code> if used outside of a target.
210      */

211     private Target target;
212
213     /**
214      * The task, or <code>null</code> if not used by a task.
215      */

216     private Task task;
217
218     // Constructors ------------------------------------------------------------
219

220     /**
221      * Constructor.
222      *
223      * @param theTask The Ant task
224      */

225     public AntLog(Task theTask)
226     {
227         this.project = theTask.getProject();
228         this.task = theTask;
229     }
230
231     /**
232      * Constructor.
233      *
234      * @param theTarget The current target
235      */

236     public AntLog(Target theTarget)
237     {
238         this.project = theTarget.getProject();
239         this.target = theTarget;
240     }
241
242     /**
243      * Constructor.
244      *
245      * @param theProject The Ant project
246      */

247     public AntLog(Project theProject)
248     {
249         this.project = theProject;
250     }
251
252     // Log Implementation ------------------------------------------------------
253

254     /**
255      * @see org.apache.commons.logging.Log#isFatalEnabled()
256      */

257     public boolean isFatalEnabled()
258     {
259         return true;
260     }
261
262     /**
263      * @see org.apache.commons.logging.Log#fatal(Object)
264      */

265     public void fatal(Object JavaDoc theMessage)
266     {
267         log(theMessage, null, Project.MSG_ERR);
268     }
269
270     /**
271      * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
272      */

273     public void fatal(Object JavaDoc theMessage, Throwable JavaDoc theThrowable)
274     {
275         log(theMessage, theThrowable, Project.MSG_ERR);
276     }
277
278     /**
279      * @see org.apache.commons.logging.Log#isErrorEnabled()
280      */

281     public boolean isErrorEnabled()
282     {
283         return true;
284     }
285
286     /**
287      * @see org.apache.commons.logging.Log#error(Object)
288      */

289     public void error(Object JavaDoc theMessage)
290     {
291         log(theMessage, null, Project.MSG_ERR);
292     }
293
294     /**
295      * @see org.apache.commons.logging.Log#error(Object, Throwable)
296      */

297     public void error(Object JavaDoc theMessage, Throwable JavaDoc theThrowable)
298     {
299         log(theMessage, theThrowable, Project.MSG_ERR);
300     }
301
302     /**
303      * @see org.apache.commons.logging.Log#isWarnEnabled()
304      */

305     public boolean isWarnEnabled()
306     {
307         return true;
308     }
309
310     /**
311      * @see org.apache.commons.logging.Log#warn(Object)
312      */

313     public void warn(Object JavaDoc theMessage)
314     {
315         log(theMessage, null, Project.MSG_WARN);
316     }
317
318     /**
319      * @see org.apache.commons.logging.Log#warn(Object, Throwable)
320      */

321     public void warn(Object JavaDoc theMessage, Throwable JavaDoc theThrowable)
322     {
323         log(theMessage, theThrowable, Project.MSG_WARN);
324     }
325
326     /**
327      * @see org.apache.commons.logging.Log#isInfoEnabled()
328      */

329     public boolean isInfoEnabled()
330     {
331         return true;
332     }
333
334     /**
335      * @see org.apache.commons.logging.Log#info(Object)
336      */

337     public void info(Object JavaDoc theMessage)
338     {
339         log(theMessage, null, Project.MSG_INFO);
340     }
341
342     /**
343      * @see org.apache.commons.logging.Log#info(Object, Throwable)
344      */

345     public void info(Object JavaDoc theMessage, Throwable JavaDoc theThrowable)
346     {
347         log(theMessage, theThrowable, Project.MSG_INFO);
348     }
349
350     /**
351      * @see org.apache.commons.logging.Log#isDebugEnabled()
352      */

353     public boolean isDebugEnabled()
354     {
355         return true;
356     }
357
358     /**
359      * @see org.apache.commons.logging.Log#debug(Object)
360      */

361     public void debug(Object JavaDoc theMessage)
362     {
363         log(theMessage, null, Project.MSG_DEBUG);
364     }
365
366     /**
367      * @see org.apache.commons.logging.Log#debug(Object, Throwable)
368      */

369     public void debug(Object JavaDoc theMessage, Throwable JavaDoc theThrowable)
370     {
371         log(theMessage, theThrowable, Project.MSG_DEBUG);
372     }
373
374     /**
375      * @see org.apache.commons.logging.Log#isTraceEnabled()
376      */

377     public boolean isTraceEnabled()
378     {
379         return true;
380     }
381
382     /**
383      * @see org.apache.commons.logging.Log#trace(Object)
384      */

385     public void trace(Object JavaDoc theMessage)
386     {
387         log(theMessage, null, Project.MSG_VERBOSE);
388     }
389
390     /**
391      * @see org.apache.commons.logging.Log#trace(Object, Throwable)
392      */

393     public void trace(Object JavaDoc theMessage, Throwable JavaDoc theThrowable)
394     {
395         log(theMessage, theThrowable, Project.MSG_VERBOSE);
396     }
397
398     // Private Methods ---------------------------------------------------------
399

400     /**
401      * Helper method to log to a certain Ant log level.
402      *
403      * @param theMessage The log message
404      * @param theThrowable The excecption to be logged, or <code>null</code>
405      * @param theLogLevel The Ant log level
406      */

407     private void log(Object JavaDoc theMessage, Throwable JavaDoc theThrowable, int theLogLevel)
408     {
409         String JavaDoc message = String.valueOf(theMessage);
410         if (theThrowable != null)
411         {
412             message += " (" + theThrowable.getMessage() + ")";
413         }
414         if (this.task != null)
415         {
416             this.project.log(this.task, message, theLogLevel);
417         }
418         else if (this.target != null)
419         {
420             this.project.log(this.target, message, theLogLevel);
421         }
422         else
423         {
424             this.project.log(message, theLogLevel);
425         }
426     }
427
428 }
429
Popular Tags