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 */
019package org.apache.wiki.ui.admin;
020
021import org.apache.commons.lang3.StringUtils;
022import org.apache.wiki.WikiContext;
023import org.apache.wiki.WikiEngine;
024import org.apache.wiki.management.SimpleMBean;
025
026import javax.management.MBeanAttributeInfo;
027import javax.management.MBeanInfo;
028import javax.management.NotCompliantMBeanException;
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 *  @since 2.5.52
037 */
038public abstract class SimpleAdminBean extends SimpleMBean implements AdminBean
039{
040    /**
041     *  Provides access to a WikiEngine instance to which this AdminBean
042     *  belongs to.
043     */
044    protected WikiEngine m_engine;
045    
046    /**
047     *  Constructor reserved for subclasses only.
048     *  
049     *  @throws NotCompliantMBeanException
050     */
051    protected SimpleAdminBean() throws NotCompliantMBeanException
052    {
053        super();
054    }
055    
056    /**
057     *  Initialize the AdminBean by setting up a WikiEngine instance internally.
058     */
059    public void initialize( WikiEngine engine )
060    {
061        m_engine = engine;
062    }
063    
064    /**
065     *  By default, this method creates a blob of HTML, listing
066     *  all the attributes which can be read or written to.  If the
067     *  attribute is read-only, a readonly input widget is created.
068     *  The value is determined by the toString() method of the attribute.
069     */
070    public String doGet(WikiContext context)
071    {
072        MBeanInfo info = getMBeanInfo();
073        MBeanAttributeInfo[] attributes = info.getAttributes();
074        StringBuilder sb = new StringBuilder();
075        
076        for( int i = 0; i < attributes.length; i++ )
077        {
078            sb.append("<div class='block'>\n");
079            
080            sb.append( "<label>"+StringUtils.capitalize( attributes[i].getName() )+"</label>\n");
081            
082            try
083            {
084                Object value = getAttribute( attributes[i].getName() );
085                if( attributes[i].isWritable() )
086                {
087                    sb.append( "<input type='text' name='question' size='30' value='"+value+"' />\n" );
088                }
089                else
090                {
091                    sb.append( "<input type='text' class='readonly' readonly='true' size='30' value='"+value+"' />\n" );
092                }
093            }
094            catch( Exception e )
095            {
096                sb.append("Exception: "+e.getMessage());
097            }
098
099            sb.append( "<div class='description'>"+attributes[i].getDescription()+"</div>\n");
100            
101            sb.append("</div>\n");
102        }
103        return sb.toString();
104    }
105
106    /**
107     *  Not implemented yet.
108     */
109    public String doPost(WikiContext context)
110    {
111        // TODO Auto-generated method stub
112        return null;
113    }
114
115    /**
116     *  By default, this method returns the class name of the bean.  This is
117     *  suitable, if you have a singleton bean.
118     */
119    public String getId()
120    {
121        return getClass().getName();
122    }
123}