KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > ConstantsTag


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 package com.blandware.atleap.webapp.taglib.core;
17
18 import com.blandware.atleap.common.Constants;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.struts.taglib.TagUtils;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.JspTagException JavaDoc;
25 import javax.servlet.jsp.PageContext JavaDoc;
26 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
27 import java.lang.reflect.AccessibleObject JavaDoc;
28 import java.lang.reflect.Field JavaDoc;
29 import java.lang.reflect.Modifier JavaDoc;
30
31
32 /**
33  * <p>This class is designed to put all the public static variables (or only one
34  * of them) in a class to a specified scope - designed for exporting constants
35  * to Tag Libraries.
36  * </p>
37  * <p>In order to prevent <code>java.lang.ClassCastException</code> to be thrown during execution,
38  * all variables are cast to <code>java.lang.String</code> using String.valueOf() method
39  * before being exported to specified scope.<br />
40  * </p>
41  * <p>It is designed to be used as follows:
42  * <pre>&lt;atleap:constants className="fully.qualyfied.ClassName" /&gt;</pre>
43  * or
44  * <pre>&lt;atleap:constants className="fully.qualyfied.ClassName" var="SPECIFIC_CONST" /&gt;</pre>
45  * </p>
46  * <p>
47  * Allowed attributes are:
48  * <ul>
49  * <li>
50  * <b>className</b> - name of class from which to export constants (this should
51  * be fully-qualified). If not specified, constants are exported from
52  * <code>com.blandware.atleap.common.Constants</code>.
53  * </li>
54  * <li>
55  * <b>var</b> - if specified, only constant with given name will be exported.
56  * Otherwise, all constants will be exported.
57  * </li>
58  * <li>
59  * <b>scope</b> - scope to which value(s) will be exported
60  * </li>
61  * </ul>
62  * </p>
63  * <p><a HREF="ConstantsTag.java.htm"><i>View Source</i></a>
64  * </p>
65  *
66  * @author Matt Raible <a HREF="mailto:matt@raibledesigns.com">&lt;matt@raibledesigns.com&gt;</a>
67  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
68  * @version $Revision: 1.12 $ $Date: 2005/10/12 13:34:52 $
69  * @jsp.tag name="constants" body-content="empty"
70  * @see java.lang.ClassCastException
71  * @see java.lang.String#valueOf(java.lang.Object)
72  */

73 public class ConstantsTag extends SimpleTagSupport JavaDoc {
74     protected transient final Log log = LogFactory.getLog(ConstantsTag.class);
75
76     /**
77      * The class to expose the variables from.
78      */

79     public String JavaDoc className = Constants.class.getName();
80
81     /**
82      * The scope to put the variable in.
83      */

84     protected String JavaDoc scope = null;
85
86     /**
87      * The single variable to expose.
88      */

89     protected String JavaDoc var = null;
90
91     /**
92      * Processes the tag
93      *
94      * @throws JspException
95      */

96     public void doTag() throws JspException JavaDoc {
97
98         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
99
100         // Using reflection, get the available field names in the class
101
Class JavaDoc c = null;
102         Object JavaDoc obj = null;
103         int toScope = PageContext.PAGE_SCOPE;
104
105         if ( scope != null ) {
106             // default to pageScope
107
toScope = TagUtils.getInstance().getScope(scope);
108         }
109
110         try {
111             c = Class.forName(className);
112         } catch ( ClassNotFoundException JavaDoc cnf ) {
113             if ( log.isErrorEnabled() ) {
114                 log.error("Class " + className + " not found - maybe a typo?");
115             }
116             throw new JspTagException JavaDoc(cnf.getMessage());
117         }
118
119         try {
120             // if var is null, expose all variables
121
if ( var == null ) {
122
123                 // only public static fields will be exported
124

125                 Field JavaDoc[] fields = c.getFields();
126
127                 AccessibleObject.setAccessible(fields, true);
128
129                 for ( int i = 0; i < fields.length; i++ ) {
130                     if ( Modifier.isStatic(fields[i].getModifiers()) ) {
131                         if ( log.isDebugEnabled() ) {
132                             log.debug("putting '" + fields[i].getName() + "=" +
133                                     fields[i].get(obj) + "' into " + scope +
134                                     " scope");
135                         }
136                         pageContext.setAttribute(fields[i].getName(),
137                                 String.valueOf(fields[i].get(obj)), toScope);
138                     }
139                 }
140
141             } else {
142                 try {
143                     Field JavaDoc field = c.getField(var);
144                     if ( Modifier.isStatic(field.getModifiers()) ) {
145                         String JavaDoc value = String.valueOf(field.get(null));
146                         pageContext.setAttribute(field.getName(), value,
147                                 toScope);
148                     } else {
149                         String JavaDoc errorMessage = "Field '" + var + "' is not static and will not be exported";
150                         if ( log.isDebugEnabled() ) {
151                             log.debug(errorMessage);
152                         }
153                         throw new JspTagException JavaDoc(errorMessage);
154                     }
155                 } catch ( NoSuchFieldException JavaDoc nsf ) {
156                     log.error(nsf.getMessage());
157                     throw new JspTagException JavaDoc(nsf);
158                 }
159             }
160         } catch ( IllegalAccessException JavaDoc iae ) {
161             log.error("Illegal Access Exception - maybe a classloader issue?");
162             throw new JspTagException JavaDoc(iae);
163         }
164
165         if ( log.isDebugEnabled() ) {
166             log.debug("Continue processing this page");
167         }
168
169     }
170
171     /**
172      * Sets name of class to expose constants from
173      *
174      * @param clazz name of class to set
175      * @jsp.attribute
176      */

177     public void setClassName(String JavaDoc clazz) {
178         this.className = clazz;
179     }
180
181     /**
182      * Returns name of class to expose constants from
183      *
184      * @return name of class
185      */

186     public String JavaDoc getClassName() {
187         return this.className;
188     }
189
190     /**
191      * Sets scope to which to expose constants
192      *
193      * @param scope scope to set
194      * @jsp.attribute
195      */

196     public void setScope(String JavaDoc scope) {
197         this.scope = scope;
198     }
199
200     /**
201      * Returns scope to which to expose constants
202      *
203      * @return scope
204      */

205     public String JavaDoc getScope() {
206         return (this.scope);
207     }
208
209     /**
210      * Sets name of the concrete constant to expose
211      *
212      * @param var name of constant
213      * @jsp.attribute
214      */

215     public void setVar(String JavaDoc var) {
216         this.var = var;
217     }
218
219     /**
220      * Returns name of the concrete constant to expose
221      *
222      * @return name of constant
223      */

224     public String JavaDoc getVar() {
225         return (this.var);
226     }
227
228 }
229
Popular Tags