KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > log > LoggerTag


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

16
17 package org.apache.taglibs.log;
18
19 import javax.servlet.jsp.JspException JavaDoc;
20 import javax.servlet.jsp.PageContext JavaDoc;
21 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /** Abstract base class for the logging tags which log a message to a
27   * commons.logging category.
28   *
29   * @author <a HREF="mailto:joeo@epesh.com">Joseph Ottinger</a>
30   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
31   */

32
33 public abstract class LoggerTag extends BodyTagSupport JavaDoc {
34     
35     private String JavaDoc category;
36     private String JavaDoc message;
37
38     
39     public void setCategory(String JavaDoc category) {
40         this.category = category;
41     }
42     
43     public void setMessage(String JavaDoc message) {
44         this.message = message;
45     }
46     
47     // Tag interface
48
//-------------------------------------------------------------------------
49
public int doStartTag() throws JspException JavaDoc {
50         if ( message != null ) {
51             Log logCategory = getLoggingCategory();
52             if ( isEnabled( logCategory ) ) {
53                 // Log now as doAfterBody() may not be called for an empty tag
54
log( logCategory, message );
55             }
56             return SKIP_BODY;
57         }
58         return EVAL_BODY_TAG;
59     }
60     
61     public int doAfterBody() throws JspException JavaDoc {
62         if (message == null) {
63             Log logCategory = getLoggingCategory();
64             if ( isEnabled( logCategory ) ) {
65                 log( logCategory, getBodyContent().getString().trim() );
66             }
67         }
68         return SKIP_BODY;
69     }
70     
71     // Implementation methods
72
//-------------------------------------------------------------------------
73
protected abstract boolean isEnabled(Log logCategory);
74     protected abstract void log(Log logCategory, String JavaDoc message);
75     
76     protected Log getLoggingCategory() {
77         if ( category == null ) {
78             // used to be Category.getRoot();
79
return LogFactory.getLog("");
80         }
81         else {
82             return LogFactory.getLog( category );
83         }
84     }
85 }
86
87
Popular Tags