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