Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 
 package org.apache.tomcat.util.modeler;
 
 
 import static org.jboss.web.CoyoteMessages.MESSAGES;
 
 import java.util.Map;
 
 //import javax.management.modelmbean.InvalidTargetObjectTypeException;
 

Internal configuration information for a managed bean (MBean) descriptor.

Author(s):
Craig R. McClanahan
Version:
$Revision: 1237 $ $Date: 2009-11-03 02:55:48 +0100 (Tue, 03 Nov 2009) $
 
 
 public class ManagedBean implements java.io.Serializable
 {
     private static final String BASE_MBEAN = "org.apache.tomcat.util.modeler.BaseModelMBean";
     // ----------------------------------------------------- Instance Variables
     static final Object[] NO_ARGS_PARAM=new Object[0];
     static final Class[] NO_ARGS_PARAM_SIG=new Class[0];


    
The ModelMBeanInfo object that corresponds to this ManagedBean instance.
 
     transient MBeanInfo info = null;
     // Map<AttributeInfo>
     private Map attributes = new HashMap();
     //Map<OperationInfo>
     private Map operations = new HashMap();
     
     protected String className = ;
     //protected ConstructorInfo constructors[] = new ConstructorInfo[0];
     protected String description = null;
     protected String domain = null;
     protected String group = null;
     protected String name = null;
 
     //protected List fields = new ArrayList();
     protected NotificationInfo notifications[] = new NotificationInfo[0];
     protected String type = null;

    
Constructor. Will add default attributes.
  
     public ManagedBean() {
         AttributeInfo ai=new AttributeInfo();
         ai.setName("modelerType");
         ai.setDescription("Type of the modeled resource. Can be set only once");
         ai.setType("java.lang.String");
         ai.setWriteable(false);
         addAttribute(ai);
     }
     
     // ------------------------------------------------------------- Properties
 

    
The collection of attributes for this MBean.
 
     public AttributeInfo[] getAttributes() {
         AttributeInfo result[] = new AttributeInfo[.size()];
        .values().toArray(result);
        return result;
    }


    
The fully qualified name of the Java class of the MBean described by this descriptor. If not specified, the standard JMX class (javax.management.modelmbean.RequiredModeLMBean) will be utilized.
    public String getClassName() {
        return (this.);
    }
    public void setClassName(String className) {
        this. = className;
        this. = null;
    }
//    /**
//     * The collection of constructors for this MBean.
//     */
//    public ConstructorInfo[] getConstructors() {
//        return (this.constructors);
//    }


    
The human-readable description of this MBean.
    public String getDescription() {
        return (this.);
    }
    public void setDescription(String description) {
        this. = description;
        this. = null;
    }


    
The (optional) ObjectName domain in which this MBean should be registered in the MBeanServer.
    public String getDomain() {
        return (this.);
    }
    public void setDomain(String domain) {
        this. = domain;
    }


    

Return a List of the FieldInfo objects for the name/value pairs that should be added to the Descriptor created from this metadata.

//    public List getFields() {
//        return (this.fields);
//    }
//

    
The (optional) group to which this MBean belongs.
    public String getGroup() {
        return (this.);
    }
    public void setGroup(String group) {
        this. = group;
    }


    
The name of this managed bean, which must be unique among all MBeans managed by a particular MBeans server.
    public String getName() {
        return (this.);
    }
    public void setName(String name) {
        this. = name;
        this. = null;
    }


    
The collection of notifications for this MBean.
    public NotificationInfo[] getNotifications() {
        return (this.);
    }


    
The collection of operations for this MBean.
    public OperationInfo[] getOperations() {
        OperationInfo[] result = new OperationInfo[.size()];
        .values().toArray(result);
        return result;
    }


    
The fully qualified name of the Java class of the resource implementation class described by the managed bean described by this descriptor.
    public String getType() {
        return (this.);
    }
    public void setType(String type) {
        this. = type;
        this. = null;
    }
    // --------------------------------------------------------- Public Methods


    
Add a new attribute to the set of attributes for this MBean.

Parameters:
attribute The new attribute descriptor
    public void addAttribute(AttributeInfo attribute) {
        .put(attribute.getName(), attribute);
    }


    
Add a new constructor to the set of constructors for this MBean.

Parameters:
constructor The new constructor descriptor
//    public void addConstructor(ConstructorInfo constructor) {
//
//        synchronized (constructors) {
//            ConstructorInfo results[] =
//                new ConstructorInfo[constructors.length + 1];
//            System.arraycopy(constructors, 0, results, 0, constructors.length);
//            results[constructors.length] = constructor;
//            constructors = results;
//            this.info = null;
//        }
//
//    }


    

Add a new field to the fields associated with the Descriptor that will be created from this metadata.

Parameters:
field The field to be added
//    public void addField(FieldInfo field) {
//        fields.add(field);
//    }


    
Add a new notification to the set of notifications for this MBean.

Parameters:
notification The new notification descriptor
    public void addNotification(NotificationInfo notification) {
        synchronized () {
            NotificationInfo results[] =
                new NotificationInfo[. + 1];
            System.arraycopy(, 0, results, 0,
                             .);
            results[.] = notification;
             = results;
            this. = null;
        }
    }


    
Add a new operation to the set of operations for this MBean.

Parameters:
operation The new operation descriptor
    public void addOperation(OperationInfo operation) {
        .put(operation.getName(), operation);
    }


    
Create and return a ModelMBean that has been preconfigured with the ModelMBeanInfo information for this managed bean, but is not associated with any particular managed resource. The returned ModelMBean will NOT have been registered with our MBeanServer.

Throws:
javax.management.InstanceNotFoundException if the managed resource object cannot be found
InvalidTargetObjectTypeException if our MBean cannot handle object references (should never happen)
javax.management.MBeanException if a problem occurs instantiating the ModelMBean instance
javax.management.RuntimeOperationsException if a JMX runtime error occurs
    public DynamicMBean createMBean()
        throws InstanceNotFoundException,
        return (createMBean(null));
    }


    
Create and return a ModelMBean that has been preconfigured with the ModelMBeanInfo information for this managed bean, and is associated with the specified managed object instance. The returned ModelMBean will NOT have been registered with our MBeanServer.

Parameters:
instance Instanced of the managed object, or null for no associated instance
Throws:
javax.management.InstanceNotFoundException if the managed resource object cannot be found
InvalidTargetObjectTypeException if our MBean cannot handle object references (should never happen)
javax.management.MBeanException if a problem occurs instantiating the ModelMBean instance
javax.management.RuntimeOperationsException if a JMX runtime error occurs
    public DynamicMBean createMBean(Object instance)
        throws InstanceNotFoundException,
        BaseModelMBean mbean = null;
        // Load the ModelMBean implementation class
        if(getClassName().equals()) {
            // Skip introspection
            mbean = new BaseModelMBean();
        } else {
            Class clazz = null;
            Exception ex = null;
            try {
                clazz = Class.forName(getClassName());
            } catch (Exception e) {
            }
          
            ifclazz==null ) {  
                try {
                    ClassLoader cl= Thread.currentThread().getContextClassLoader();
                    if ( cl != null)
                        clazzcl.loadClass(getClassName());
                } catch (Exception e) {
                    ex=e;
                }
            }
    
            ifclazz==null) { 
                throw new MBeanException
                    (ex.errorLoadingModelMbean(getClassName()));
            }
            try {
                // Stupid - this will set the default minfo first....
                mbean = (BaseModelMBeanclazz.newInstance();
            } catch (RuntimeOperationsException e) {
                throw e;
            } catch (Exception e) {
                throw new MBeanException
                    (e.errorInstantiatingModelMbean(getClassName()));
            }
        }
        
        mbean.setManagedBean(this);
        
        // Set the managed resource (if any)
        try {
            if (instance != null)
                mbean.setManagedResource(instance"ObjectReference");
        } catch (InstanceNotFoundException e) {
            throw e;
        }
        return (mbean);
    }


    
Create and return a ModelMBeanInfo object that describes this entire managed bean.
        // Return our cached information (if any)
        if ( != null)
            return ();
        // Create subordinate information descriptors as required
        AttributeInfo attrs[] = getAttributes();
        MBeanAttributeInfo attributes[] =
            new MBeanAttributeInfo[attrs.length];
        for (int i = 0; i < attrs.lengthi++)
            attributes[i] = attrs[i].createAttributeInfo();
        OperationInfo opers[] = getOperations();
        MBeanOperationInfo operations[] =
            new MBeanOperationInfo[opers.length];
        for (int i = 0; i < opers.lengthi++)
            operations[i] = opers[i].createOperationInfo();
//        ConstructorInfo consts[] = getConstructors();
//        ModelMBeanConstructorInfo constructors[] =
//            new ModelMBeanConstructorInfo[consts.length];
//        for (int i = 0; i < consts.length; i++)
//            constructors[i] = consts[i].createConstructorInfo();
        
        NotificationInfo notifs[] = getNotifications();
        MBeanNotificationInfo notifications[] =
            new MBeanNotificationInfo[notifs.length];
        for (int i = 0; i < notifs.lengthi++)
            notifications[i] = notifs[i].createNotificationInfo();
        
        // Construct and return a new ModelMBeanInfo object
         = new MBeanInfo(getClassName(), 
                             getDescription(),
                             attributes
                             new MBeanConstructorInfo[] {}, 
                             operations
                             notifications);
//        try {
//            Descriptor descriptor = info.getMBeanDescriptor();
//            Iterator fields = getFields().iterator();
//            while (fields.hasNext()) {
//                FieldInfo field = (FieldInfo) fields.next();
//                descriptor.setField(field.getName(), field.getValue());
//            }
//            info.setMBeanDescriptor(descriptor);
//        } catch (MBeanException e) {
//            ;
//        }
        return ();
    }


    
Return a string representation of this managed bean.
    public String toString() {
        StringBuilder sb = new StringBuilder("ManagedBean[");
        sb.append("name=");
        sb.append();
        sb.append(", className=");
        sb.append();
        sb.append(", description=");
        sb.append();
        if ( != null) {
            sb.append(", group=");
            sb.append();
        }
        sb.append(", type=");
        sb.append();
        sb.append("]");
        return (sb.toString());
    }
    Method getGetter(String anameBaseModelMBean mbeanObject resource
        // TODO: do we need caching ? JMX is for management, it's not supposed to require lots of performance.
        Method m=null// (Method)getAttMap.get( name );
        ifm==null ) {
            AttributeInfo attrInfo = (AttributeInfo).get(aname);
            // Look up the actual operation to be used
            if (attrInfo == null)
                throw new AttributeNotFoundException(.errorGettingAttribute(aname));
            
            String getMethod = attrInfo.getGetMethod();
            if (getMethod == null)
                throw new AttributeNotFoundException(.errorGettingAttribute(aname));
            Object object = null;
            NoSuchMethodException exception = null;
            try {
                object = mbean;
                m = object.getClass().getMethod(getMethod);
            } catch (NoSuchMethodException e) {
                exception = e;;
            }
            ifm== null && resource != null ) {
                try {
                    object = resource;
                    m = object.getClass().getMethod(getMethod);
                    exception=null;
                } catch (NoSuchMethodException e) {
                    exception = e;
                }
            }
            ifexception != null )
                throw new ReflectionException(exception,
                        .errorGettingAttribute(aname));
            //getAttMap.put( name, m );
        }
        return m;
    }
    public Method getSetter(String anameBaseModelMBean beanObject resource
        // Cache may be needed for getters, but it is a really bad idea for setters, this is far 
        // less frequent.
        Method m=null;//(Method)setAttMap.get( name );
        ifm==null ) {
            AttributeInfo attrInfo = (AttributeInfo).get(aname);
            if (attrInfo == null)
                throw new AttributeNotFoundException(.errorSettingAttribute(aname));
            // Look up the actual operation to be used
            String setMethod = attrInfo.getSetMethod();
            if (setMethod == null)
                throw new AttributeNotFoundException(.errorSettingAttribute(aname));
            String argType=attrInfo.getType();
            Class signature[] = new Class[] { BaseModelMBean.getAttributeClassargType ) };
            Object object = null;
            NoSuchMethodException exception = null;
            try {
                object = bean;
                m = object.getClass().getMethod(setMethodsignature);
            } catch (NoSuchMethodException e) {
                exception = e;;
            }
            ifm== null && resource != null ) {
                try {
                    object = resource;
                    m = object.getClass().getMethod(setMethodsignature);
                    exception=null;
                } catch (NoSuchMethodException e) {
                    exception = e;
                }
            }
            ifexception != null )
                throw new ReflectionException(exception,
                        .errorSettingAttribute(aname));
            //setAttMap.put( name, m );
        }
        return m;
    }
    public Method getInvoke(String anameObject[] paramsString[] signatureBaseModelMBean beanObject resource
            throws MBeanExceptionReflectionException {
        Method method = null;
        if (method == null) {
            if (params == null)
                params = new Object[0];
            if (signature == null)
                signature = new String[0];
            if (params.length != signature.length)
                throw new RuntimeOperationsException(
                        new IllegalArgumentException(
                                .errorInvokingMethod(aname)),
                                .errorInvokingMethod(aname));
            // Acquire the ModelMBeanOperationInfo information for
            // the requested operation
            OperationInfo opInfo = (OperationInfo).get(aname);
            if (opInfo == null)
                throw new MBeanException(new ServiceNotFoundException(
                        .errorInvokingMethod(aname)),
                        .errorInvokingMethod(aname));
            // Prepare the signature required by Java reflection APIs
            // FIXME - should we use the signature from opInfo?
            Class types[] = new Class[signature.length];
            for (int i = 0; i < signature.lengthi++) {
                types[i] = BaseModelMBean.getAttributeClass(signature[i]);
            }
            // Locate the method to be invoked, either in this MBean itself
            // or in the corresponding managed resource
            // FIXME - Accessible methods in superinterfaces?
            Object object = null;
            Exception exception = null;
            try {
                object = bean;
                method = object.getClass().getMethod(anametypes);
            } catch (NoSuchMethodException e) {
                exception = e;
                ;
            }
            try {
                if ((method == null) && (resource != null)) {
                    object = resource;
                    method = object.getClass().getMethod(anametypes);
                }
            } catch (NoSuchMethodException e) {
                exception = e;
            }
            if (method == null) {
                throw new ReflectionException(exception.errorInvokingMethod(aname));
            }
            // invokeAttMap.put(mkey, method);
        }
        return method;
    }
New to GrepCode? Check out our FAQ X