001 /*
002 Licensed to the Apache Software Foundation (ASF) under one
003 or more contributor license agreements. See the NOTICE file
004 distributed with this work for additional information
005 regarding copyright ownership. The ASF licenses this file
006 to you under the Apache License, Version 2.0 (the
007 "License"); you may not use this file except in compliance
008 with the License. You may obtain a copy of the License at
009
010 http://www.apache.org/licenses/LICENSE-2.0
011
012 Unless required by applicable law or agreed to in writing,
013 software distributed under the License is distributed on an
014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 KIND, either express or implied. See the License for the
016 specific language governing permissions and limitations
017 under the License.
018 */
019 package org.apache.wiki.ui.admin;
020
021 import javax.management.*;
022
023 import org.apache.commons.lang.StringUtils;
024
025 import org.apache.wiki.WikiContext;
026 import org.apache.wiki.WikiEngine;
027 import org.apache.wiki.management.SimpleMBean;
028
029 /**
030 * Provides an easy-to-use interface for JSPWiki AdminBeans, which also
031 * are JMX MBeans. This class provides a default interface for the doGet()
032 * and doPost() interfaces by using the introspection capabilities of the
033 * SimpleMBean.
034 *
035 * @since 2.5.52
036 */
037 public abstract class SimpleAdminBean extends SimpleMBean implements AdminBean
038 {
039 /**
040 * Provides access to a WikiEngine instance to which this AdminBean
041 * belongs to.
042 */
043 protected WikiEngine m_engine;
044
045 /**
046 * Constructor reserved for subclasses only.
047 *
048 * @throws NotCompliantMBeanException
049 */
050 protected SimpleAdminBean() throws NotCompliantMBeanException
051 {
052 super();
053 }
054
055 /**
056 * Initialize the AdminBean by setting up a WikiEngine instance internally.
057 */
058 public void initialize( WikiEngine engine )
059 {
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 {
071 MBeanInfo info = getMBeanInfo();
072 MBeanAttributeInfo[] attributes = info.getAttributes();
073 StringBuffer sb = new StringBuffer();
074
075 for( int i = 0; i < attributes.length; i++ )
076 {
077 sb.append("<div class='block'>\n");
078
079 sb.append( "<label>"+StringUtils.capitalize( attributes[i].getName() )+"</label>\n");
080
081 try
082 {
083 Object value = getAttribute( attributes[i].getName() );
084 if( attributes[i].isWritable() )
085 {
086 sb.append( "<input type='text' name='question' size='30' value='"+value+"' />\n" );
087 }
088 else
089 {
090 sb.append( "<input type='text' class='readonly' readonly='true' size='30' value='"+value+"' />\n" );
091 }
092 }
093 catch( Exception e )
094 {
095 sb.append("Exception: "+e.getMessage());
096 }
097
098 sb.append( "<div class='description'>"+attributes[i].getDescription()+"</div>\n");
099
100 sb.append("</div>\n");
101 }
102 return sb.toString();
103 }
104
105 /**
106 * Not implemented yet.
107 */
108 public String doPost(WikiContext context)
109 {
110 // TODO Auto-generated method stub
111 return null;
112 }
113
114 /**
115 * By default, this method returns the class name of the bean. This is
116 * suitable, if you have a singleton bean.
117 */
118 public String getId()
119 {
120 return getClass().getName();
121 }
122 }