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 javax.servlet.jsp.tagext.BodyTagSupport;
20  import javax.servlet.jsp.JspException;
21  import org.apache.struts.util.RequestUtils;
22  
23  
24  
25  /***
26   * Read a configuration property and expose it as a page variable and attribute
27   * Examples of use:
28   *
29   * >%@ taglib uri="/WEB-INF/tld/easyconf.tld" prefix="easyconf" %>
30   *
31   * >easyconf:configurationObject id="dbConf"
32   *                  component="test_module"
33   *                  type="com.germinus.easyconf.example.DatabaseConf"/>
34   * >bean:write name="dbConf" property="tables"/>
35   * 
36   * @jsp.tag name="configurationObject" body-content="empty" 
37   *          tei-class="com.germinus.easyconf.taglib.ConfigurationObjectTei"
38   */
39  public class ConfigurationObjectTag extends BodyTagSupport {
40  
41      protected String id = null;
42      protected String component = null;
43      protected String type = "java.lang.Object";
44  
45      /***
46       * @jsp.attribute required="true" rtexprvalue="true"
47       */
48      public String getId() {
49          return (this.id);
50      }
51  
52      public void setId(String id) {
53          this.id = id;
54      }
55  
56      /***
57       * @jsp.attribute required="true" rtexprvalue="true"
58       */
59      public String getComponent() {
60          return component;
61      }
62  
63      public void setComponent(String component) {
64          this.component = component;
65      }
66  
67      /***
68       * @jsp.attribute required="false" rtexprvalue="true"
69       */
70      public String getType() {
71          return (this.type);
72      }
73  
74      public void setType(String type) {
75          this.type = type;
76      }
77  
78      /***
79       * Check if we need to evaluate the body of the tag
80       *
81       * @exception javax.servlet.jsp.JspException if a JSP exception has occurred
82       */
83      public int doStartTag() throws JspException {
84          return (EVAL_BODY_BUFFERED);
85  
86      }
87  
88  
89      /***
90       * Save the body content of this tag (if any), or throw a JspException
91       * if the value was already defined.
92       *
93       * @exception JspException if value was defined by an attribute
94       */
95      public int doAfterBody() throws JspException {
96          return (SKIP_BODY);
97  
98      }
99  
100 
101     /***
102      * Retrieve the required property and expose it as a scripting variable.
103      *
104      * @exception JspException if a JSP exception has occurred
105      */
106     public int doEndTag() throws JspException {
107         Object confObj = EasyConf.getConfiguration(component).
108         	getConfigurationObject();
109        
110         if (confObj == null) {
111             JspException e = new JspException("The value of the configuration object is null. " +
112             		"Check the configuration files");
113             RequestUtils.saveException(pageContext, e);
114             throw e;
115         }
116         pageContext.setAttribute(id, confObj);
117         return (EVAL_PAGE);
118     }
119 
120     public void release() {
121         super.release();
122         id = null;
123         component = null;
124         type = null;
125     }
126 
127 
128 }