1 16 17 18 package org.apache.webapp.admin; 19 20 21 import java.io.IOException ; 22 import javax.management.MBeanServer ; 23 import javax.management.ObjectName ; 24 import javax.servlet.jsp.JspException ; 25 import javax.servlet.jsp.JspWriter ; 26 import javax.servlet.jsp.PageContext ; 27 import javax.servlet.jsp.tagext.TagSupport ; 28 import org.apache.commons.beanutils.PropertyUtils; 29 30 31 32 39 40 public class AttributeTag extends TagSupport { 41 42 43 45 46 49 protected String attribute = null; 50 51 public String getAttribute() { 52 return (this.attribute); 53 } 54 55 public void setAttribute(String attribute) { 56 this.attribute = attribute; 57 } 58 59 60 63 protected String name = null; 64 65 public String getName() { 66 return (this.name); 67 } 68 69 public void setName(String name) { 70 this.name = name; 71 } 72 73 74 77 protected String property = null; 78 79 public String getProperty() { 80 return (this.property); 81 } 82 83 public void setProperty(String property) { 84 this.property = property; 85 } 86 87 88 91 protected String scope = null; 92 93 public String getScope() { 94 return (this.scope); 95 } 96 97 public void setScope(String scope) { 98 this.scope = scope; 99 } 100 101 102 104 105 110 public int doEndTag() throws JspException { 111 112 Object 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 ("Invalid scope value '" + scope + "'"); 127 } 128 if (bean == null) { 129 throw new JspException ("No bean '" + name + "' found"); 130 } 131 if (property != null) { 132 try { 133 bean = PropertyUtils.getProperty(bean, property); 134 } catch (Throwable t) { 135 throw new JspException 136 ("Exception retrieving property '" + property + "': " + t); 137 } 138 if (bean == null) { 139 throw new JspException ("No property '" + property + "' found"); 140 } 141 } 142 143 ObjectName oname = null; 145 try { 146 if (bean instanceof ObjectName ) { 147 oname = (ObjectName ) bean; 148 } else if (bean instanceof String ) { 149 oname = new ObjectName ((String ) bean); 150 } else { 151 oname = new ObjectName (bean.toString()); 152 } 153 } catch (Throwable t) { 154 throw new JspException ("Exception creating object name for '" + 155 bean + "': " + t); 156 } 157 158 MBeanServer mserver = 160 (MBeanServer ) pageContext.getAttribute 161 ("org.apache.catalina.MBeanServer", PageContext.APPLICATION_SCOPE); 162 if (mserver == null) 163 throw new JspException ("MBeanServer is not available"); 164 165 Object value = null; 167 try { 168 value = mserver.getAttribute(oname, attribute); 169 } catch (Throwable t) { 170 throw new JspException ("Exception retrieving attribute '" + 171 attribute + "'"); 172 } 173 174 if (value != null) { 176 JspWriter out = pageContext.getOut(); 177 try { 178 out.print(value); 179 } catch (IOException e) { 180 throw new JspException ("IOException: " + e); 181 } 182 } 183 184 return (EVAL_PAGE); 186 187 } 188 189 190 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 |