001: /*
002: * ========================================================================
003: *
004: * Copyright 2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.integration.ant.util;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.tools.ant.Project;
024: import org.apache.tools.ant.Target;
025: import org.apache.tools.ant.Task;
026:
027: /**
028: * Support class that lets classes log to Ant using the Commons Logging API.
029: *
030: * This is not intended to be a general solution, rather as a thin separation
031: * layer to not have to pass around full-blown Ant <code>Project</code>,
032: * <code>Target</code> or <code>Task</code> objects just to enable logging.
033: *
034: * Note that as there is no log level in Commons-Logging that corresponds to
035: * Ant's <em>VERBOSE</em> level (the level between <em>INFO</em> and
036: * <em>DEBUG</em>), the <em>TRACE</em> level of Commons-Logging gets mapped to
037: * <em>VERBOSE</em>, which is probably inappropriate for components that do not
038: * know they are using the <code>AntLog</code> class.
039: *
040: * @version $Id: AntLog.java 238812 2004-02-29 10:21:34Z vmassol $
041: */
042: public final class AntLog implements Log {
043:
044: // Constants ---------------------------------------------------------------
045:
046: /**
047: * Singleton log implementation that simply ignores all log requests.
048: */
049: public static final Log NULL = new Log() {
050:
051: // Log Implementation --------------------------------------------------
052:
053: /**
054: * @see org.apache.commons.logging.Log#isFatalEnabled()
055: */
056: public boolean isFatalEnabled() {
057: return false;
058: }
059:
060: /**
061: * @see org.apache.commons.logging.Log#fatal(Object)
062: */
063: public void fatal(Object theMessage) {
064: // do nothing
065: }
066:
067: /**
068: * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
069: */
070: public void fatal(Object theMessage, Throwable theThrowable) {
071: // do nothing
072: }
073:
074: /**
075: * @see org.apache.commons.logging.Log#isErrorEnabled()
076: */
077: public boolean isErrorEnabled() {
078: return false;
079: }
080:
081: /**
082: * @see org.apache.commons.logging.Log#error(Object)
083: */
084: public void error(Object theMessage) {
085: // do nothing
086: }
087:
088: /**
089: * @see org.apache.commons.logging.Log#error(Object, Throwable)
090: */
091: public void error(Object theMessage, Throwable theThrowable) {
092: // do nothing
093: }
094:
095: /**
096: * @see org.apache.commons.logging.Log#isWarnEnabled()
097: */
098: public boolean isWarnEnabled() {
099: return false;
100: }
101:
102: /**
103: * @see org.apache.commons.logging.Log#warn(Object)
104: */
105: public void warn(Object theMessage) {
106: // do nothing
107: }
108:
109: /**
110: * @see org.apache.commons.logging.Log#warn(Object, Throwable)
111: */
112: public void warn(Object theMessage, Throwable theThrowable) {
113: // do nothing
114: }
115:
116: /**
117: * @see org.apache.commons.logging.Log#isInfoEnabled()
118: */
119: public boolean isInfoEnabled() {
120: return false;
121: }
122:
123: /**
124: * @see org.apache.commons.logging.Log#info(Object)
125: */
126: public void info(Object theMessage) {
127: // do nothing
128: }
129:
130: /**
131: * @see org.apache.commons.logging.Log#info(Object, Throwable)
132: */
133: public void info(Object theMessage, Throwable theThrowable) {
134: // do nothing
135: }
136:
137: /**
138: * @see org.apache.commons.logging.Log#isDebugEnabled()
139: */
140: public boolean isDebugEnabled() {
141: return false;
142: }
143:
144: /**
145: * @see org.apache.commons.logging.Log#debug(Object)
146: */
147: public void debug(Object theMessage) {
148: // do nothing
149: }
150:
151: /**
152: * @see org.apache.commons.logging.Log#debug(Object, Throwable)
153: */
154: public void debug(Object theMessage, Throwable theThrowable) {
155: // do nothing
156: }
157:
158: /**
159: * @see org.apache.commons.logging.Log#isTraceEnabled()
160: */
161: public boolean isTraceEnabled() {
162: return false;
163: }
164:
165: /**
166: * @see org.apache.commons.logging.Log#trace(Object)
167: */
168: public void trace(Object theMessage) {
169: // do nothing
170: }
171:
172: /**
173: * @see org.apache.commons.logging.Log#trace(Object, Throwable)
174: */
175: public void trace(Object theMessage, Throwable theThrowable) {
176: // do nothing
177: }
178:
179: };
180:
181: // Instance Variables ------------------------------------------------------
182:
183: /**
184: * The Ant project.
185: */
186: private Project project;
187:
188: /**
189: * The current target, or <code>null</code> if used outside of a target.
190: */
191: private Target target;
192:
193: /**
194: * The task, or <code>null</code> if not used by a task.
195: */
196: private Task task;
197:
198: // Constructors ------------------------------------------------------------
199:
200: /**
201: * Constructor.
202: *
203: * @param theTask The Ant task
204: */
205: public AntLog(Task theTask) {
206: this .project = theTask.getProject();
207: this .task = theTask;
208: }
209:
210: /**
211: * Constructor.
212: *
213: * @param theTarget The current target
214: */
215: public AntLog(Target theTarget) {
216: this .project = theTarget.getProject();
217: this .target = theTarget;
218: }
219:
220: /**
221: * Constructor.
222: *
223: * @param theProject The Ant project
224: */
225: public AntLog(Project theProject) {
226: this .project = theProject;
227: }
228:
229: // Log Implementation ------------------------------------------------------
230:
231: /**
232: * @see org.apache.commons.logging.Log#isFatalEnabled()
233: */
234: public boolean isFatalEnabled() {
235: return true;
236: }
237:
238: /**
239: * @see org.apache.commons.logging.Log#fatal(Object)
240: */
241: public void fatal(Object theMessage) {
242: log(theMessage, null, Project.MSG_ERR);
243: }
244:
245: /**
246: * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
247: */
248: public void fatal(Object theMessage, Throwable theThrowable) {
249: log(theMessage, theThrowable, Project.MSG_ERR);
250: }
251:
252: /**
253: * @see org.apache.commons.logging.Log#isErrorEnabled()
254: */
255: public boolean isErrorEnabled() {
256: return true;
257: }
258:
259: /**
260: * @see org.apache.commons.logging.Log#error(Object)
261: */
262: public void error(Object theMessage) {
263: log(theMessage, null, Project.MSG_ERR);
264: }
265:
266: /**
267: * @see org.apache.commons.logging.Log#error(Object, Throwable)
268: */
269: public void error(Object theMessage, Throwable theThrowable) {
270: log(theMessage, theThrowable, Project.MSG_ERR);
271: }
272:
273: /**
274: * @see org.apache.commons.logging.Log#isWarnEnabled()
275: */
276: public boolean isWarnEnabled() {
277: return true;
278: }
279:
280: /**
281: * @see org.apache.commons.logging.Log#warn(Object)
282: */
283: public void warn(Object theMessage) {
284: log(theMessage, null, Project.MSG_WARN);
285: }
286:
287: /**
288: * @see org.apache.commons.logging.Log#warn(Object, Throwable)
289: */
290: public void warn(Object theMessage, Throwable theThrowable) {
291: log(theMessage, theThrowable, Project.MSG_WARN);
292: }
293:
294: /**
295: * @see org.apache.commons.logging.Log#isInfoEnabled()
296: */
297: public boolean isInfoEnabled() {
298: return true;
299: }
300:
301: /**
302: * @see org.apache.commons.logging.Log#info(Object)
303: */
304: public void info(Object theMessage) {
305: log(theMessage, null, Project.MSG_INFO);
306: }
307:
308: /**
309: * @see org.apache.commons.logging.Log#info(Object, Throwable)
310: */
311: public void info(Object theMessage, Throwable theThrowable) {
312: log(theMessage, theThrowable, Project.MSG_INFO);
313: }
314:
315: /**
316: * @see org.apache.commons.logging.Log#isDebugEnabled()
317: */
318: public boolean isDebugEnabled() {
319: return true;
320: }
321:
322: /**
323: * @see org.apache.commons.logging.Log#debug(Object)
324: */
325: public void debug(Object theMessage) {
326: log(theMessage, null, Project.MSG_DEBUG);
327: }
328:
329: /**
330: * @see org.apache.commons.logging.Log#debug(Object, Throwable)
331: */
332: public void debug(Object theMessage, Throwable theThrowable) {
333: log(theMessage, theThrowable, Project.MSG_DEBUG);
334: }
335:
336: /**
337: * @see org.apache.commons.logging.Log#isTraceEnabled()
338: */
339: public boolean isTraceEnabled() {
340: return true;
341: }
342:
343: /**
344: * @see org.apache.commons.logging.Log#trace(Object)
345: */
346: public void trace(Object theMessage) {
347: log(theMessage, null, Project.MSG_VERBOSE);
348: }
349:
350: /**
351: * @see org.apache.commons.logging.Log#trace(Object, Throwable)
352: */
353: public void trace(Object theMessage, Throwable theThrowable) {
354: log(theMessage, theThrowable, Project.MSG_VERBOSE);
355: }
356:
357: // Private Methods ---------------------------------------------------------
358:
359: /**
360: * Helper method to log to a certain Ant log level.
361: *
362: * @param theMessage The log message
363: * @param theThrowable The excecption to be logged, or <code>null</code>
364: * @param theLogLevel The Ant log level
365: */
366: private void log(Object theMessage, Throwable theThrowable,
367: int theLogLevel) {
368: String message = String.valueOf(theMessage);
369: if (theThrowable != null) {
370: message += " (" + theThrowable.getMessage() + ")";
371: }
372: if (this.task != null) {
373: this.project.log(this.task, message, theLogLevel);
374: } else if (this.target != null) {
375: this.project.log(this.target, message, theLogLevel);
376: } else {
377: this.project.log(message, theLogLevel);
378: }
379: }
380:
381: }
|