KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > logic > GlobalPropertyTag


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.logic;
17
18 import com.blandware.atleap.webapp.util.core.GlobalProperties;
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.PageContext JavaDoc;
25 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
26
27 /**
28  * <p>Retrieves value of global property (either static or dynamic) and exports
29  * it to the specified scope.</p>
30  * <p>
31  * If no value could be found for global property with given name, then, if
32  * <code>defaultValue</code> is specified, it will be used instead, otherwise
33  * (nothing found and no default value) the result will be <code>null</code>.
34  * If default value is used, firstly its string value is obtained through its
35  * <code>toString()</code> method. If <code>type</code> is <em>integer</em>,
36  * string value is parsed to get <code>Integer</code> value.
37  * </p>
38  * <p>
39  * Allowed attributes are:
40  * <ul>
41  * <li>
42  * <b>name</b> - this is required; it's the name of global property to export
43  * </li>
44  * <li>
45  * <b>defaultValue</b> - default value to use when no global property value was
46  * found. Its <code>toString()</code> method is called and for <em>integer</em>
47  * type it's parsed to get <code>Integer</code> value
48  * </li>
49  * <li>
50  * <b>type</b> - type of property: <em>string</em> or <em>integer</em>.
51  * If it's neither <em>string</em> nor <em>integer</em>, it's treated as
52  * <em>string</em>. This attribute is case-insensitive.
53  * </li>
54  * <li>
55  * <b>var</b> - this is required; it's name of variable to export result
56  * </li>
57  * <li>
58  * <b>scope</b> - scope of variable to export result
59  * </li>
60  * </ul>
61  * <p><a HREF="GlobalPropertyTag.java.htm"><i>View Source</i></a></p>
62  *
63  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
64  * @version $Revision: 1.1 $ $Date: 2005/10/20 07:18:50 $
65  * @jsp.tag name="globalProperty"
66  * body-content="empty"
67  */

68 public class GlobalPropertyTag extends SimpleTagSupport JavaDoc {
69
70     protected transient final Log log = LogFactory.getLog(GlobalPropertyTag.class);
71
72     /**
73      * Name of property to retrieve value of
74      */

75     protected String JavaDoc name;
76
77     /**
78      * Default value to use
79      */

80     protected Object JavaDoc defaultValue;
81
82     /**
83      * Type of property: <code>string<code> or <code>integer</code>.
84      * Default is <code>string</code>
85      */

86     protected String JavaDoc type;
87
88     /**
89      * Name of variable to export value to
90      */

91     protected String JavaDoc var;
92
93     /**
94      * Scope to export variable to
95      */

96     protected String JavaDoc scope;
97
98     /**
99      * Returns name of the global property
100      *
101      * @return name
102      * @see #name
103      * @jsp.attribute required="true"
104      * rtexprvalue="true"
105      * type="java.lang.String"
106      * description="Name of property to retrieve value of"
107      */

108     public String JavaDoc getName() {
109         return name;
110     }
111
112     /**
113      * Sets name of the global property
114      *
115      * @param name name to set
116      * @see #name
117      */

118     public void setName(String JavaDoc name) {
119         this.name = name;
120     }
121
122     /**
123      * Returns default value
124      *
125      * @return default value
126      * @see #defaultValue
127      * @jsp.attribute required="false"
128      * rtexprvalue="true"
129      * type="java.lang.String"
130      * description="Default value to use"
131      */

132     public Object JavaDoc getDefaultValue() {
133         return defaultValue;
134     }
135
136     /**
137      * Sets default value
138      *
139      * @param defaultValue default value to set
140      */

141     public void setDefaultValue(Object JavaDoc defaultValue) {
142         this.defaultValue = defaultValue;
143     }
144
145     /**
146      * Returns type of global property
147      *
148      * @return type
149      * @see #type
150      * @jsp.attribute required="false"
151      * rtexprvalue="true"
152      * type="java.lang.String"
153      * description="Type of property: string or integer. Default is integer"
154      */

155     public String JavaDoc getType() {
156         return type;
157     }
158
159     /**
160      * Sets type of global property
161      *
162      * @param type type to set
163      * @see #type
164      */

165     public void setType(String JavaDoc type) {
166         this.type = type;
167     }
168
169     /**
170      * Returns name of variable
171      *
172      * @return name of variable
173      * @see #var
174      * @jsp.attribute required="true"
175      * rtexprvalue="true"
176      * type="java.lang.String"
177      * description="Name of variable to export formatted date"
178      */

179     public String JavaDoc getVar() {
180         return var;
181     }
182
183     /**
184      * Sets name of variable
185      *
186      * @param var name of variable to set
187      * @see #var
188      */

189     public void setVar(String JavaDoc var) {
190         this.var = var;
191     }
192
193     /**
194      * Returns variable scope
195      *
196      * @return variable scope
197      * @see #scope
198      * @jsp.attribute required="false"
199      * rtexprvalue="true"
200      * type="java.lang.String"
201      * description="Scope to export variable to"
202      */

203     public String JavaDoc getScope() {
204         return scope;
205     }
206
207     /**
208      * Sets variable scope
209      *
210      * @param scope variable scope to set
211      * @see #scope
212      */

213     public void setScope(String JavaDoc scope) {
214         this.scope = scope;
215     }
216
217     /**
218      * Processes the tag
219      *
220      * @throws JspException
221      */

222     public void doTag() throws JspException JavaDoc {
223
224         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
225
226         if ( !"string".equalsIgnoreCase(type) && !"integer".equalsIgnoreCase(type) ) {
227             type = "string";
228         }
229
230         GlobalProperties globalProperties = GlobalProperties.getInstance(pageContext.getServletContext());
231
232         Object JavaDoc property = null;
233
234         if ( type.equalsIgnoreCase("string") ) {
235             property = globalProperties.getString(name, defaultValue != null ? defaultValue.toString() : null);
236         } else {
237             String JavaDoc s = defaultValue != null ? defaultValue.toString() : null;
238             Integer JavaDoc value = null;
239             if ( s != null && s.length() > 0 ) {
240                 try {
241                     value = Integer.valueOf(s);
242                 } catch ( NumberFormatException JavaDoc e ) {
243                     throw new JspException JavaDoc("Value '" + s + "' cannot be converted to java.lang.Integer");
244                 }
245             }
246             property = globalProperties.getInteger(name, value);
247         }
248
249         int varScope = PageContext.PAGE_SCOPE;
250         if ( scope != null ) {
251             varScope = TagUtils.getInstance().getScope(scope);
252         }
253         pageContext.setAttribute(var, property, varScope);
254     }
255
256 }
257
Popular Tags