JalistoAbstractDynamicMBean.java :  » Database-DBMS » jalisto » org » objectweb » jalisto » se » jmx » Java Open Source

Java Open Source » Database DBMS » jalisto 
jalisto » org » objectweb » jalisto » se » jmx » JalistoAbstractDynamicMBean.java
/*
 * Jalisto - JAva LIght STOrage
 * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 * Xcalia
 * 71, rue Desnouettes
 * 75014 Paris - France
 * http://www.xcalia.com
 */
package org.objectweb.jalisto.se.jmx;

import javax.management.*;
import java.util.Iterator;

public abstract class JalistoAbstractDynamicMBean implements DynamicMBean {

    public Object getAttribute(String attributeName)
            throws AttributeNotFoundException, MBeanException, ReflectionException {

        if (attributeName == null) {
            throw new RuntimeOperationsException(
                    new IllegalArgumentException("Attribute name cannot be null"),
                    "Cannot invoke a getter of " + this.getClass().getName() +
                    " with null attribute name");
        }

        return internalGetAttribute(attributeName);
    }

    public AttributeList getAttributes(String[] attributeNames) {
        // Check attributeNames is not null to avoid NullPointerException later on
        if (attributeNames == null) {
            throw new RuntimeOperationsException(
                    new IllegalArgumentException("attributeNames[] cannot be null"),
                                                 "Cannot invoke a getter of " + this.getClass().getName());
        }
        AttributeList resultList = new AttributeList();

        // if attributeNames is empty, return an empty result list
        if (attributeNames.length == 0)
            return resultList;

        // build the result attribute list
        for (int i = 0; i < attributeNames.length; i++) {
            Object value = null;
            try {
                value = getAttribute(attributeNames[i]);
            } catch (AttributeNotFoundException e) {
                e.printStackTrace();
            } catch (MBeanException e) {
                e.printStackTrace();
            } catch (ReflectionException e) {
                e.printStackTrace();
            }
            resultList.add(new Attribute(attributeNames[i], value));
        }
        return (resultList);
    }

    public Object invoke(String operationName, Object[] params, String[] signature)
            throws MBeanException, ReflectionException {

        if (operationName == null) {
            throw new RuntimeOperationsException(
                    new IllegalArgumentException("Operation name cannot be null"),
                                                 "Cannot invoke a null operation in " + this.getClass().getName());
        }

        return internalInvoke(operationName, params, signature);
    }

    public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
        if (attribute == null) {
            throw new RuntimeOperationsException(
                    new IllegalArgumentException("Attribute cannot be null"),
                    "Cannot invoke a setter of " + this.getClass().getName() +
                    " with null attribute");
        }

        String name = attribute.getName();
        Object value = attribute.getValue();

        if (name == null) {
            throw new RuntimeOperationsException(
                    new IllegalArgumentException(
                            "Attribute name cannot be null"),
                            "Cannot invoke the setter of " + this.getClass().getName() +
                            " with null attribute name");
        }

        if (!internalSetAttribute(name, value)) {
            throw(new AttributeNotFoundException("Attribute " + name +
                                                 " not found in " + this.getClass().getName()));
        }
    }

    public AttributeList setAttributes(AttributeList attributeList) {
         if (attributeList == null) {
            throw new RuntimeOperationsException(
                    new IllegalArgumentException("AttributeList attributes cannot be null"),
                                                 "Cannot invoke a setter of " + this.getClass().getName());
        }

        AttributeList resultList = new AttributeList();

        if (attributeList.isEmpty()) {
            return resultList;
        }

        for (Iterator i = attributeList.iterator(); i.hasNext();) {
            Attribute attr = (Attribute) i.next();
            try {
                setAttribute(attr);
                String name = attr.getName();
                Object value = getAttribute(name);
                resultList.add(new Attribute(name, value));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return resultList;
    }

    abstract public MBeanInfo getMBeanInfo();

    abstract protected Object internalGetAttribute(String attributeName)
            throws AttributeNotFoundException, MBeanException, ReflectionException;

    abstract protected boolean internalSetAttribute(String name, Object value)
            throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException;

    abstract protected Object internalInvoke(String operationName, Object[] params, String[] signature)
            throws MBeanException, ReflectionException;
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.