I just finished a small protoype of an JMX MBean wrapper for the
ServicesManager.

It exposes these methods to the JMX world:

- getAllServices()
- enableService(id)
- disableService(id)
- reload()

It was tested on Tomcat 6.0.26 and CAS server 3.4.2 and jconsole. Feel free
to include it in any future releases.

--------- snip -----------------
package org.jasig.cas.support.jmx;

import java.util.Collection;

import org.jasig.cas.services.RegisteredService;
import org.jasig.cas.services.RegisteredServiceImpl;
import org.jasig.cas.services.ReloadableServicesManager;
import org.springframework.util.Assert;

/**
 * MBean wrapper for a {...@link ReloadableServicesManager} of the 
 * CAS server infrastructure.
 * <p/>
 * Basically, the business calls are delegated to the injected
 * services manager and the results are converted to insure they are
 * serializeable. In order to run in any JMX environment
 * only Java internal data types are used.
 * <p/>
 * To use this bean, add the following XML fragment to your
 * Spring configuration (maybe in a separate config file
 * <code>jmxContext.xml</code>):
 * <pre>
 * &lt;bean id="mbeanExporter"
class="org.springframework.jmx.export.MBeanExporter" lazy-init="false"&gt;
 *   &lt;property name="beans"&gt;
 *      &lt;map&gt;
 *         &lt;entry key="CAS:name=Services Manager"&gt;
 *            &lt;bean
class="org.jasig.cas.support.jmx.ServicesManagerMBean"&gt;
 *               &lt;constructor-arg ref="servicesManager" /&gt;
 *            &lt;/bean&gt;
 *         &lt;/entry&gt;
 *         ...
 *       &lt;/map&gt;
 *    &lt;/property&gt;
 * &lt;/bean&gt;
 * </pre>
 * 
 * @author  mailto:tobias.tre...@proximity.de Tobias Trelle 
 */
public class ServicesManagerMBean {

        /** Our delegate, the services manager. */
        protected final ReloadableServicesManager servicesManager;

        /**
         * Creates a new instance of this class.
         * 
         * @param servicesManager
         *        The services manager.
         */
        public ServicesManagerMBean(final ReloadableServicesManager
servicesManager) {
                Assert.notNull(servicesManager, "Services manager is <null>");
                this.servicesManager = servicesManager;
        }

        
        // JMX operations 
.......................................................
        
        /**
         * Retrieve an array of all configured CAS services.
         * 
         * @return A string array representing the services. Each string 
contains
         *                 a comma separated list of key/value pairs with the 
following
         *         fields from a RegisteredService: <code>id, name, enabled,
ssoEnabled, 
         *         serviceId</code>. 
         */
        public String[] getAllServices() {
                String[] result = null;
                Collection<RegisteredService> services =
this.servicesManager.getAllServices();
                
                if ( services != null ) {
                        result = new String[ services.size() ];
                        int i = 0;
                        
                        for (RegisteredService service : services) {
                                result[i++] = new StringBuilder()
                                        .append( "id: " ).append( 
service.getId() )
                                        .append( " name: " ).append( 
service.getName() )
                                        .append( " enabled: " ).append( 
service.isEnabled() )
                                        .append( " ssoEnabled: " ).append( 
service.isSsoEnabled() )
                                        .append( " serviceId: " ).append( 
service.getServiceId() )
                                        .toString();
                        }
                }
                
                return result;
        }
        
        /**
         * Disables the service with the given id.
         * 
         * @param id
         *        Id of the service to disable.
         */
        public void disableService(final long id) {
                updateServiceEnabledState(id, false);
        }

        /**
         * Enables the service with the given id.
         * 
         * @param id
         *        Id of the service to enable.
         */
        public void enableService(final long id) {
                updateServiceEnabledState(id, true);
        }
        
        /**
         * Reloads the service definitions from the persistence store.
         */
        public void reload() {
                this.servicesManager.reload();
        }

        
        // Internals 
............................................................
        
        protected void updateServiceEnabledState(final long id, final boolean
enabled) {
                final RegisteredService service = 
this.servicesManager.findServiceBy(id);
                
                if ( service == null ) {
                        throw new IllegalArgumentException( "Service does not 
exists: " + id );
                }
        
                // save new state ...
                ((RegisteredServiceImpl)service).setEnabled(enabled);
                this.servicesManager.save(service);
                
                // .. and relaod
                reload();
        }
        
}
--------- snap -----------------

-- 
Best wishes,
Tobias Trelle
-- 
View this message in context: 
http://jasig.275507.n4.nabble.com/JMX-support-for-CAS-Server-tp2238284p2238722.html
Sent from the CAS Developers mailing list archive at Nabble.com.

-- 
You are currently subscribed to cas-dev@lists.jasig.org as: 
arch...@mail-archive.com
To unsubscribe, change settings or access archives, see 
http://www.ja-sig.org/wiki/display/JSG/cas-dev

Reply via email to