View Javadoc

1   /*
2    * Copyright 2004-2005 Germinus XXI
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.germinus.easyconf.taglib;
17  
18  import com.germinus.easyconf.EasyConf;
19  import com.germinus.easyconf.ComponentProperties;
20  import com.germinus.easyconf.Filter;
21  
22  import javax.servlet.jsp.tagext.BodyTagSupport;
23  import javax.servlet.jsp.JspException;
24  import org.apache.struts.util.RequestUtils;
25  import org.apache.commons.lang.StringUtils;
26  
27  import java.util.List;
28  import java.util.ArrayList;
29  
30  
31  /***
32   * Read a configuration property and expose it as a page variable and attribute
33   * Examples of use:
34   *
35   * >%@ taglib uri="/WEB-INF/tld/easyconf.tld" prefix="easyconf" %>
36   *
37   * >easyconf:property id="registration_list"
38   *                  component="registration"
39   *                  property="registration.list"
40   *                  type="java.util.List"/>
41   * >logic:iterate id="item" name="registration_list">
42   *   >bean:write name="item"/>    >br/>
43   * >/logic:iterate>
44   *
45   * >easyconf:property id="registration_disabled"
46   *                  component="registration"
47   *                  property="registration.disabled"/>
48   * >logic:equal name="registration_disabled" value="true">
49   *   The registration is disabled
50   * >/logic:equal>
51   * 
52   * @jsp.tag name="property" body-content="empty" tei-class="com.germinus.easyconf.taglib.PropertyTei"
53   */
54  public class PropertyTag extends BodyTagSupport {
55  	private static final long serialVersionUID = 3546082471134573881L;
56  
57  	private static final String DEFAULT_TYPE = "java.lang.String";
58  
59      protected String id = null;
60      protected String component = null;
61      protected String property = null;
62      protected String type = DEFAULT_TYPE;
63      protected String selector1 = "";
64      protected String selector2 = "";
65      protected String selector3 = "";
66      protected String selector4 = "";
67      protected String selector5 = "";    
68      protected String defaultValue;
69      private static final List EMPTY_LIST = new ArrayList();
70  
71  	
72      public PropertyTag() {
73  		super();
74  		release();
75  	}
76  
77  	/***
78       * @jsp.attribute required="true" rtexprvalue="true"
79       */
80      public String getId() {
81          return (this.id);
82      }
83  
84      public void setId(String id) {
85          this.id = id;
86      }
87  
88      /***
89       * @jsp.attribute required="true" rtexprvalue="true"
90       */
91      public String getComponent() {
92          return component;
93      }
94  
95      public void setComponent(String component) {
96          this.component = component;
97      }
98  
99      /***
100      * @jsp.attribute required="true" rtexprvalue="true"
101      */
102     public String getProperty() {
103         return (this.property);
104     }
105 
106     public void setProperty(String property) {
107         this.property = property;
108     }
109 
110     /***
111      * @jsp.attribute required="false" rtexprvalue="true"
112      */
113     public String getType() {
114         if (StringUtils.isEmpty(type)) {
115             type = DEFAULT_TYPE;
116         }
117         return (this.type);
118     }
119 
120     /***
121      * @jsp.attribute required="false" rtexprvalue="true"
122      */
123     public void setType(String type) {
124         if (StringUtils.isNotEmpty(type)) {
125             this.type = type;
126         }
127     }
128 
129 
130     /***
131      * @jsp.attribute required="false" rtexprvalue="true"
132      */
133     public String getDefaultValue() {
134         return (this.defaultValue);
135     }
136 
137     /***
138      * Note: currently this is only used if type is String
139      * @param defaultValue
140      */
141     public void setDefaultValue(String defaultValue) {
142         this.defaultValue = defaultValue;
143     }
144 
145     
146     /***
147      * @jsp.attribute required="false" rtexprvalue="true"
148      */
149     public String getSelector1() {
150         return selector1;
151     }
152     public void setSelector1(String selector1) {
153         this.selector1 = selector1;
154     }
155     
156     /***
157      * @jsp.attribute required="false" rtexprvalue="true"
158      */
159     public String getSelector2() {
160         return selector2;
161     }
162     public void setSelector2(String selector2) {
163         this.selector2 = selector2;
164     }
165 
166     /***
167      * @jsp.attribute required="false" rtexprvalue="true"
168      */
169     public String getSelector3() {
170         return selector3;
171     }
172     public void setSelector3(String selector3) {
173         this.selector3 = selector3;
174     }
175 
176     /***
177      * @jsp.attribute required="false" rtexprvalue="true"
178      */
179     public String getSelector4() {
180         return selector4;
181     }
182     public void setSelector4(String selector4) {
183         this.selector4 = selector4;
184     }
185 
186     /***
187      * @jsp.attribute required="false" rtexprvalue="true"
188      */
189     public String getSelector5() {
190         return selector5;
191     }
192     public void setSelector5(String selector5) {
193         this.selector5 = selector5;
194     }
195     
196     private String[] getSelectorArray() {
197         List selectors = new ArrayList();
198         if (StringUtils.isNotEmpty(selector1)) {
199             selectors.add(selector1);
200         }
201         if (StringUtils.isNotEmpty(selector2)) {
202             selectors.add(selector2);
203         }
204         if (StringUtils.isNotEmpty(selector3)) {
205             selectors.add(selector3);
206         }
207         if (StringUtils.isNotEmpty(selector4)) {
208             selectors.add(selector4);
209         }
210         if (StringUtils.isNotEmpty(selector5)) {
211             selectors.add(selector5);
212         }
213         return (String[]) selectors.toArray(new String[0]);
214     }
215     // .................. Taglib methods ..................
216     
217     /***
218      * Check if we need to evaluate the body of the tag
219      *
220      * @exception javax.servlet.jsp.JspException if a JSP exception has occurred
221      */
222     public int doStartTag() throws JspException {
223         return (EVAL_BODY_BUFFERED);
224 
225     }
226 
227 
228     /***
229      * Save the body content of this tag (if any), or throw a JspException
230      * if the value was already defined.
231      *
232      * @exception JspException if value was defined by an attribute
233      */
234     public int doAfterBody() throws JspException {
235         return (SKIP_BODY);
236 
237     }
238 
239 
240     /***
241      * Retrieve the required property and expose it as a scripting variable.
242      *
243      * @exception JspException if a JSP exception has occurred
244      */
245     public int doEndTag() throws JspException {
246         Object value = null;
247         
248         ComponentProperties conf = EasyConf.getConfiguration(component).
249         	getProperties();
250         value = readProperty(conf);
251 
252        
253         if (value == null) {
254             JspException e = new JspException("The value of the property is null");
255             RequestUtils.saveException(pageContext, e);
256             throw e;
257         }
258         pageContext.setAttribute(id, value);
259         return (EVAL_PAGE);
260 
261     }
262 
263     private Object readProperty(ComponentProperties conf) throws JspException {
264         Object value;
265         if (getType().equals("java.util.List")) {
266             value = conf.getList(property, getPropertyFilter(), EMPTY_LIST);
267         } else if (getType().equals("java.lang.Integer")) {
268             value = conf.getInteger(property, getPropertyFilter(), new Integer(0));
269         } else if (getType().equals("java.lang.String[]")) {
270             value = conf.getStringArray(property, getPropertyFilter(), new String[0]);
271         } else if (getType().equals("java.lang.String")) {
272             if (defaultValue != null) {
273                 value = conf.getString(property, getPropertyFilter(), defaultValue);
274             } else {
275                 value = conf.getString(property, getPropertyFilter());
276             }
277         } else if (getType().equals("java.lang.Double")) {
278             value = new Double(conf.getDouble(property, getPropertyFilter()));
279         } else if (getType().equals("java.lang.Float")) {
280             value = new Float(conf.getFloat(property, getPropertyFilter()));
281         } else if (getType().equals("java.lang.Byte")) {
282             value = new Byte(conf.getByte(property, getPropertyFilter()));
283         } else if (getType().equals("java.math.BigDecimal")) {
284             value = conf.getBigDecimal(property, getPropertyFilter());
285         } else if (getType().equals("java.lang.BigInteger")) {
286             value = conf.getBigInteger(property, getPropertyFilter());
287         } else if (getType().equals("java.lang.Boolean")) {
288             value = new Boolean(conf.getBoolean(property, getPropertyFilter()));
289         } else if (getType().equals("java.lang.Short")) {
290             value = new Short(conf.getShort(property, getPropertyFilter()));
291         } else if (getType().equals("java.lang.Long")) {
292             value = new Long(conf.getLong(property, getPropertyFilter()));
293         } else {
294                 JspException e = new JspException("Unsupported type: " +type);
295                 RequestUtils.saveException(pageContext, e);
296                 throw e;
297         }
298         return value;
299     }
300 
301     private Filter getPropertyFilter() {
302         return Filter.by(getSelectorArray());
303     }
304 
305     public void release() {
306         super.release();
307         id = null;
308         component = null;
309         property = null;
310         type = null;
311         defaultValue = null;
312     }
313 
314 
315 }