KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > AttributeTag


1 /*
2  * Copyright 2002,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
18 package org.apache.webapp.admin;
19
20
21 import java.io.IOException JavaDoc;
22 import javax.management.MBeanServer JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.JspWriter JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
28 import org.apache.commons.beanutils.PropertyUtils;
29
30
31
32 /**
33  * Custom tag that retrieves a JMX MBean attribute value, and writes it
34  * out to the current output stream.
35  *
36  * @author Craig R. McClanahan
37  * @version $Revision: 1.3 $ $Date: 2004/02/27 14:59:01 $
38  */

39
40 public class AttributeTag extends TagSupport JavaDoc {
41
42
43     // ------------------------------------------------------------- Properties
44

45
46     /**
47      * The attribute name on the JMX MBean to be retrieved.
48      */

49     protected String JavaDoc attribute = null;
50
51     public String JavaDoc getAttribute() {
52         return (this.attribute);
53     }
54
55     public void setAttribute(String JavaDoc attribute) {
56         this.attribute = attribute;
57     }
58
59
60     /**
61      * The bean name to be retrieved.
62      */

63     protected String JavaDoc name = null;
64
65     public String JavaDoc getName() {
66         return (this.name);
67     }
68
69     public void setName(String JavaDoc name) {
70         this.name = name;
71     }
72
73
74     /**
75      * The property name to be retrieved.
76      */

77     protected String JavaDoc property = null;
78
79     public String JavaDoc getProperty() {
80         return (this.property);
81     }
82
83     public void setProperty(String JavaDoc property) {
84         this.property = property;
85     }
86
87
88     /**
89      * The scope in which the bean should be searched.
90      */

91     protected String JavaDoc scope = null;
92
93     public String JavaDoc getScope() {
94         return (this.scope);
95     }
96
97     public void setScope(String JavaDoc scope) {
98         this.scope = scope;
99     }
100
101
102     // --------------------------------------------------------- Public Methods
103

104
105     /**
106      * Render the JMX MBean attribute identified by this tag
107      *
108      * @exception JspException if a processing exception occurs
109      */

110     public int doEndTag() throws JspException JavaDoc {
111
112         // Retrieve the object name identified by our attributes
113
Object JavaDoc bean = null;
114         if (scope == null) {
115             bean = pageContext.findAttribute(name);
116         } else if ("page".equalsIgnoreCase(scope)) {
117             bean = pageContext.getAttribute(name, PageContext.PAGE_SCOPE);
118         } else if ("request".equalsIgnoreCase(scope)) {
119             bean = pageContext.getAttribute(name, PageContext.REQUEST_SCOPE);
120         } else if ("session".equalsIgnoreCase(scope)) {
121             bean = pageContext.getAttribute(name, PageContext.SESSION_SCOPE);
122         } else if ("application".equalsIgnoreCase(scope)) {
123             bean = pageContext.getAttribute(name,
124                                             PageContext.APPLICATION_SCOPE);
125         } else {
126             throw new JspException JavaDoc("Invalid scope value '" + scope + "'");
127         }
128         if (bean == null) {
129             throw new JspException JavaDoc("No bean '" + name + "' found");
130         }
131         if (property != null) {
132             try {
133                 bean = PropertyUtils.getProperty(bean, property);
134             } catch (Throwable JavaDoc t) {
135                 throw new JspException JavaDoc
136                     ("Exception retrieving property '" + property + "': " + t);
137             }
138             if (bean == null) {
139                 throw new JspException JavaDoc("No property '" + property + "' found");
140             }
141         }
142
143         // Convert to an object name as necessary
144
ObjectName JavaDoc oname = null;
145         try {
146             if (bean instanceof ObjectName JavaDoc) {
147                 oname = (ObjectName JavaDoc) bean;
148             } else if (bean instanceof String JavaDoc) {
149                 oname = new ObjectName JavaDoc((String JavaDoc) bean);
150             } else {
151                 oname = new ObjectName JavaDoc(bean.toString());
152             }
153         } catch (Throwable JavaDoc t) {
154             throw new JspException JavaDoc("Exception creating object name for '" +
155                                    bean + "': " + t);
156         }
157
158         // Acquire a reference to our MBeanServer
159
MBeanServer JavaDoc mserver =
160             (MBeanServer JavaDoc) pageContext.getAttribute
161             ("org.apache.catalina.MBeanServer", PageContext.APPLICATION_SCOPE);
162         if (mserver == null)
163             throw new JspException JavaDoc("MBeanServer is not available");
164
165         // Retrieve the specified attribute from the specified MBean
166
Object JavaDoc value = null;
167         try {
168             value = mserver.getAttribute(oname, attribute);
169         } catch (Throwable JavaDoc t) {
170             throw new JspException JavaDoc("Exception retrieving attribute '" +
171                                    attribute + "'");
172         }
173
174         // Render this value to our current output writer
175
if (value != null) {
176             JspWriter JavaDoc out = pageContext.getOut();
177             try {
178                 out.print(value);
179             } catch (IOException JavaDoc e) {
180                 throw new JspException JavaDoc("IOException: " + e);
181             }
182         }
183
184         // Evaluate the remainder of this page
185
return (EVAL_PAGE);
186
187     }
188
189
190     /**
191      * Release all current state.
192      */

193     public void release() {
194
195         attribute = null;
196         name = null;
197         property = null;
198         scope = null;
199
200     }
201
202
203 }
204
Popular Tags