001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2007 JSPWiki development group
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.ui.admin;
021:
022: import javax.management.*;
023:
024: import org.apache.commons.lang.StringUtils;
025:
026: import com.ecyrd.jspwiki.WikiContext;
027: import com.ecyrd.jspwiki.WikiEngine;
028: import com.ecyrd.management.SimpleMBean;
029:
030: /**
031: * Provides an easy-to-use interface for JSPWiki AdminBeans, which also
032: * are JMX MBeans. This class provides a default interface for the doGet()
033: * and doPost() interfaces by using the introspection capabilities of the
034: * SimpleMBean.
035: *
036: * @author Janne Jalkanen
037: * @since 2.5.52
038: */
039: public abstract class SimpleAdminBean extends SimpleMBean implements
040: AdminBean {
041: /**
042: * Provides access to a WikiEngine instance to which this AdminBean
043: * belongs to.
044: */
045: protected WikiEngine m_engine;
046:
047: /**
048: * Constructor reserved for subclasses only.
049: *
050: * @throws NotCompliantMBeanException
051: */
052: protected SimpleAdminBean() throws NotCompliantMBeanException {
053: super ();
054: }
055:
056: /**
057: * Initialize the AdminBean by setting up a WikiEngine instance internally.
058: */
059: public void initialize(WikiEngine engine) {
060: m_engine = engine;
061: }
062:
063: /**
064: * By default, this method creates a blob of HTML, listing
065: * all the attributes which can be read or written to. If the
066: * attribute is read-only, a readonly input widget is created.
067: * The value is determined by the toString() method of the attribute.
068: */
069: public String doGet(WikiContext context) {
070: MBeanInfo info = getMBeanInfo();
071: MBeanAttributeInfo[] attributes = info.getAttributes();
072: StringBuffer sb = new StringBuffer();
073:
074: for (int i = 0; i < attributes.length; i++) {
075: sb.append("<div class='block'>\n");
076:
077: sb.append("<label>"
078: + StringUtils.capitalize(attributes[i].getName())
079: + "</label>\n");
080:
081: try {
082: Object value = getAttribute(attributes[i].getName());
083: if (attributes[i].isWritable()) {
084: sb
085: .append("<input type='text' name='question' size='30' value='"
086: + value + "' />\n");
087: } else {
088: sb
089: .append("<input type='text' class='readonly' readonly='true' size='30' value='"
090: + value + "' />\n");
091: }
092: } catch (Exception e) {
093: sb.append("Exception: " + e.getMessage());
094: }
095:
096: sb.append("<div class='description'>"
097: + attributes[i].getDescription() + "</div>\n");
098:
099: sb.append("</div>\n");
100: }
101: return sb.toString();
102: }
103:
104: /**
105: * Not implemented yet.
106: */
107: public String doPost(WikiContext context) {
108: // TODO Auto-generated method stub
109: return null;
110: }
111:
112: /**
113: * By default, this method returns the class name of the bean. This is
114: * suitable, if you have a singleton bean.
115: */
116: public String getId() {
117: return getClass().getName();
118: }
119: }
|