User: juhalindfors
  Date: 01/12/12 16:02:21

  Added:       src/main/org/jboss/mx/modelmbean ModelBase.java
                        ModelMBeanConstants.java XMBean.java
                        XMBeanConstants.java
  Log:
  part of the mmb implementation
  
  Revision  Changes    Path
  1.1                  jmx/src/main/org/jboss/mx/modelmbean/ModelBase.java
  
  Index: ModelBase.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.mx.modelmbean;
  
  import java.util.Iterator;
  
  import javax.management.AttributeChangeNotification;
  import javax.management.AttributeChangeNotificationFilter;
  import javax.management.AttributeList;
  import javax.management.Attribute;
  import javax.management.Descriptor;
  import javax.management.Notification;
  import javax.management.NotificationFilter;
  import javax.management.NotificationListener;
  import javax.management.NotificationBroadcaster;
  import javax.management.NotificationBroadcasterSupport;
  import javax.management.MBeanNotificationInfo;
  import javax.management.JMException;
  import javax.management.MBeanException;
  import javax.management.ListenerNotFoundException;
  import javax.management.InstanceNotFoundException;
  import javax.management.RuntimeOperationsException;
  
  import javax.management.modelmbean.ModelMBean;
  import javax.management.modelmbean.ModelMBeanInfo;
  import javax.management.modelmbean.ModelMBeanInfoSupport;
  import javax.management.modelmbean.ModelMBeanNotificationInfo;
  import javax.management.modelmbean.DescriptorSupport;
  import javax.management.modelmbean.InvalidTargetObjectTypeException;
  
  import org.jboss.mx.server.MBeanInvoker;
  import org.jboss.mx.persistence.NullPersistence;
  import org.jboss.mx.persistence.PersistenceManager;
  
  /**
   * An abstract base class that can be used to implement different
   * Model MBean implementations.
   *
   * @see javax.management.modelmbean.ModelMBean
   * @see org.jboss.mx.modelmbean.ModelMBeanConstants
   *
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Juha Lindfors</a>.
   * @version $Revision: 1.1 $  
   */
  public abstract class ModelBase 
     extends MBeanInvoker
     implements ModelMBean, ModelMBeanConstants
  {
  
     // Attributes ----------------------------------------------------
     protected Object resource                  = null;
     protected String resourceType              = null;
     protected ModelMBeanInfoSupport metadata   = null;
     protected PersistenceManager persistence   = new NullPersistence();
     protected NotificationBroadcasterSupport notifier = new 
NotificationBroadcasterSupport();
     
     // Static --------------------------------------------------------
     protected static long notifierSequence     = 1;
     protected static long attrNotifierSequence = 1;
     
     
     // Constructors --------------------------------------------------
     public ModelBase()
     {}
  
     public ModelBase(ModelMBeanInfo info) throws MBeanException
     {
        setModelMBeanInfo(info);
  
        try
        {
           load();
        }
        catch (InstanceNotFoundException e)
        {
           throw new MBeanException(e);
        }
     }
  
     // Public --------------------------------------------------------
     
     // verify the resource types supported by the concrete MMBean implementation
     public abstract boolean isSupportedResourceType(String resourceType);
  
     
     // ModelMBean implementation -------------------------------------
     public void setModelMBeanInfo(ModelMBeanInfo metadata)
     throws MBeanException, RuntimeOperationsException
     {
        if (metadata == null)
           throw new IllegalArgumentException("MBeanInfo cannot be null.");
  
        this.metadata = new ModelMBeanInfoSupport(metadata);
     }
  
     public void setManagedResource(Object ref, String resourceType) 
     throws MBeanException, InstanceNotFoundException, InvalidTargetObjectTypeException
     {
        if (ref == null)
           throw new IllegalArgumentException("Resource reference cannot be null.");
  
        // check that is a supported resource type (concrete implementations need to 
implement this)
        if (!isSupportedResourceType(resourceType))
           throw new InvalidTargetObjectTypeException("Unsupported resource type: " + 
resourceType);
  
        this.resource = ref;
        this.resourceType = resourceType;
     }
  
     // ModelMBeanNotificationBroadcaster implementation --------------
     public void addNotificationListener(NotificationListener listener,
                                         NotificationFilter filter,
                                         Object handback)
     {
        notifier.addNotificationListener(listener, filter, handback);
     }
  
     public void removeNotificationListener(NotificationListener listener)
     throws ListenerNotFoundException
     {
        notifier.removeNotificationListener(listener);
     }
  
     public void addAttributeChangeNotificationListener(NotificationListener listener,
           String attributeName, Object handback) throws MBeanException
     {
        AttributeChangeNotificationFilter filter = new 
AttributeChangeNotificationFilter();
        filter.enableAttribute(attributeName);
        notifier.addNotificationListener(listener, filter, handback);
     }
  
     public void removeAttributeChangeNotificationListener(NotificationListener 
listener, String attributeName)
     throws MBeanException, ListenerNotFoundException
     {
        notifier.removeNotificationListener(listener);
     }
  
     public void sendNotification(String message) throws MBeanException
     {
        Notification notif = new Notification(
                                GENERIC_MODELMBEAN_NOTIFICATION, // type
                                this,                            // source
                                ++notifierSequence,              // sequence number
                                message                          // message
                             );
  
        sendNotification(notif);
     }
  
     public void sendNotification(Notification notification)
     throws MBeanException
     {
        notifier.sendNotification(notification);
     }
  
     public void sendAttributeChangeNotification(AttributeChangeNotification 
notification) 
     throws MBeanException
     {
        notifier.sendNotification(notification);
     }
  
     public void sendAttributeChangeNotification(Attribute oldValue, Attribute 
newValue) 
     throws MBeanException
     {
        String attr = oldValue.getName();
        String type = oldValue.getClass().getName();
  
        AttributeChangeNotification notif = new AttributeChangeNotification(
                                               this,                          // source
                                               ++attrNotifierSequence,        // seq. #
                                               System.currentTimeMillis(),    // time 
stamp
                                               "" + attr + " changed from " + oldValue 
+ " to " + newValue,
                                               attr, type,                    // name 
& type
                                               oldValue, newValue             // values
                                            );
  
        notifier.sendNotification(notif);
     }
  
     public MBeanNotificationInfo[] getNotificationInfo()
     {
        // FIXME: NYI
        throw new Error("NYI");
     }
  
     // PersistentMBean implementation --------------------------------
     public void load() throws MBeanException, InstanceNotFoundException
     {
        if (metadata == null)
           return;
  
        persistence.load(metadata);
     }
  
     public void store() throws MBeanException, InstanceNotFoundException
     {
        persistence.store(metadata);
     }
  
     // DynamicMBean implementation -----------------------------------
  /*   public AttributeList getAttributes(String[] attributes)
     {
        if (attributes == null)
           throw new IllegalArgumentException("null array");
  
        AttributeList list = new AttributeList();
  
        for (int i = 0; i < attributes.length; ++i)
        {
           try
           {
              list.add(new Attribute(attributes[i], getAttribute(attributes[i])));
           }
           catch (JMException ignored)
           {
              // if the attribute could not be retrieved, skip it
           }
        }
  
        return list;
     }
  
     public AttributeList setAttributes(AttributeList list)
     {
        if (list == null)
           throw new IllegalArgumentException("null list");
  
        AttributeList results = new AttributeList();
        Iterator it           = list.iterator();
  
        while (it.hasNext())
        {
           try
           {
              Attribute attr = (Attribute)it.next();
              setAttribute(attr);
              results.add(attr);
           }
           catch (JMException ignored)
           {
              // if unable to set the attribute, skip it
           }
        }
  
        return results;
     }
  */
  }
  
  
  
  
  1.1                  jmx/src/main/org/jboss/mx/modelmbean/ModelMBeanConstants.java
  
  Index: ModelMBeanConstants.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.mx.modelmbean;
  
  public interface ModelMBeanConstants
  {
     final static String GENERIC_MODELMBEAN_NOTIFICATION = "jmx.modelmbean.generic";
     
     final static String OBJECT_REF = "ObjectReference";
     final static String VALUE = "value";
     final static String GET_METHOD = "getMethod";
     final static String SET_METHOD = "setMethod";
     final static String PERSIST_POLICY = "persistPolicy";
     final static String PERSIST_PERIOD = "persistPeriod";
     final static String PERSIST_NAME = "persistName";
     final static String PERSIST_LOCATION = "persistLocation";
     final static String CURRENCY_TIME_LIMIT = "currencyTimeLimit";
     final static String LAST_UPDATED_TIME_STAMP = "lastUpdatedTimeStamp";
     final static String EXPORT = "export";
     final static String ON_UPDATE = "OnUpdate";
     final static String NO_MORE_OFTEN_THAN = "NoMoreOftenThan";
     final static String NEVER = "Never";
     final static String ON_TIMER = "OnTimer";
  }
  
  
  
  
  1.1                  jmx/src/main/org/jboss/mx/modelmbean/XMBean.java
  
  Index: XMBean.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.mx.modelmbean;
  
  import java.net.URL;
  import java.net.MalformedURLException;
  
  import javax.management.MBeanInfo;
  import javax.management.MBeanRegistration;
  import javax.management.MBeanServer;
  import javax.management.Attribute;
  import javax.management.ObjectName;
  import javax.management.InvalidAttributeValueException;
  import javax.management.NotCompliantMBeanException;
  import javax.management.MBeanException;
  import javax.management.ReflectionException;
  import javax.management.AttributeNotFoundException;
  import javax.management.InstanceNotFoundException;
  
  import javax.management.modelmbean.ModelMBeanInfo;
  import javax.management.modelmbean.ModelMBeanInfoSupport;
  import javax.management.modelmbean.InvalidTargetObjectTypeException;
  
  import org.jboss.mx.metadata.XMLMetaData;
  import org.jboss.mx.metadata.StandardMetaData;
  import org.jboss.mx.interceptor.ModelMBeanInterceptor;
  
  public class XMBean
     extends ModelBase 
     implements MBeanRegistration, XMBeanConstants
  {
  
     // Constructors --------------------------------------------------
     public XMBean()
     {
        super();
     }
  
     public XMBean(ModelMBeanInfo info) throws MBeanException
     {
        super(info);
     }
  
     public XMBean(Object resource, String resourceType) throws MBeanException, 
NotCompliantMBeanException
     {
        try
        {
           setManagedResource(resource, resourceType);   
        
           if (resourceType.equals(STANDARD_INTERFACE))
              metadata = new ModelMBeanInfoSupport((ModelMBeanInfo)new 
StandardMetaData(resource, true).build());
           if (resourceType.endsWith(".xml"))
              metadata = new ModelMBeanInfoSupport((ModelMBeanInfo)new 
XMLMetaData(resource.getClass().getName(), resourceType).build());   
        }
        catch (InstanceNotFoundException e) 
        {
           throw new MBeanException(e);
        }
        catch (InvalidTargetObjectTypeException e)
        {
           if (resourceType.endsWith(".xml"))
              throw new MBeanException(e, "Malformed URL: " + resourceType);
              
           throw new MBeanException(e, "Unsupported resource type: " + resourceType);
        }
        catch (MalformedURLException e)
        {
           throw new MBeanException(e, "Malformed URL: " + resourceType);
        }         
     }
     
     // MBeanRegistration implementation ------------------------------
     public ObjectName preRegister(MBeanServer server, ObjectName name) throws 
Exception
     {
        /*
        // store the server reference
           this.server = server;
           this.name    = name;
       
           // create attribute and operation maps
           operationMap = createOperationMap(metadata.getOperations());
           attributeMap = createAttributeMap(metadata.getAttributes());
      
           return name;
        */
        
        // FIXME: this should be in the base
        this.stack = new ModelMBeanInterceptor(resource, metadata);
        
        return name;
     }
     
     public void postRegister(Boolean registrationSuccessful) { }
     public void preDeregister() throws Exception { }
     public void postDeregister() { }
  
     // DynamicMBean implementation -----------------------------------
  /*
     public Object getAttribute(String attribute)
     throws AttributeNotFoundException, MBeanException, ReflectionException
     {
        /*
         XMBeanAttribute attr = (XMBeanAttribute)attributeMap.get(attribute);
             
         if (attr == null)
           throw new AttributeNotFoundException();
           
         return attr.getValue();
        
     }
  
     public void setAttribute(Attribute attribute)
     throws AttributeNotFoundException, InvalidAttributeValueException, 
MBeanException, ReflectionException
     {
        
         String attrName  = attribute.getName();
         Object attrValue = attribute.getValue();
      
         XMBeanAttribute attr = (XMBeanAttribute)attributeMap.get(attrName);
         
         if (attr == null)
           throw new AttributeNotFoundException();
         
         try {
           attr.setValue(attrValue);
         }
         catch (InstanceNotFoundException e) {
           
           // may be thrown by PersistentMBean.store()
             
           throw new MBeanException(e);
         }              
        
     }
  
     public Object invoke(String actionName, Object[] params, String[] signature)
     throws MBeanException, ReflectionException
     {
         String method = actionName;
         
         if (signature != null) {
           for (int i = 0; i < signature.length; ++i)
             method += signature[i];
         }
         
         
         XMBeanOperation operation =
             (XMBeanOperation)operationMap.get(method);
         
         if (operation == null) {
           throw new ReflectionException(
              new IllegalArgumentException("unknown operation")
           );
         }
           
         return operation.invoke(params);
       
     }
  */
     public MBeanInfo getMBeanInfo()
     {
        return (MBeanInfo)metadata;
     }
  
     
     public boolean isSupportedResourceType(String resourceType)
     {
        if (resourceType == null)
           return false;
           
        if (resourceType.equals(OBJECT_REF))
           return true;
        if (resourceType.equals(STANDARD_INTERFACE))
           return true;
           
        if (resourceType.endsWith(".xml"))
           try
           {
              new URL(resourceType);
              return true;
           }
           catch (MalformedURLException e) 
           {
              return false;
           }
           
        return false;
     }
  
     
  /*
     private Map createAttributeMap(MBeanAttributeInfo[] attributes) throws 
MBeanException, ReflectionException  {
      
        Map attrMap = new HashMap();
      
        for (int i = 0; i < attributes.length; ++i) {
           String name = attributes[i].getName();      
           ModelMBeanAttributeInfo info = (ModelMBeanAttributeInfo)attributes[i];
           attrMap.put(name, new XMBeanAttribute(this, info));        
        }
      
        return attrMap;
     }
  
     private Map createOperationMap(MBeanOperationInfo[] operations) throws 
MBeanException {
        
        Map operMap = new HashMap();
      
        for (int i = 0; i < operations.length; ++i) {
           String name = operations[i].getName();
           MBeanParameterInfo[] params = operations[i].getSignature();
        
           for (int j = 0; j < params.length; ++j)
              name += params[j].getType();
          
           ModelMBeanOperationInfo info = (ModelMBeanOperationInfo)operations[i];
           operMap.put(name, new XMBeanOperation(this, info));
        }
      
        return operMap;
     }
  */
     
  }
  
  
  
  
  1.1                  jmx/src/main/org/jboss/mx/modelmbean/XMBeanConstants.java
  
  Index: XMBeanConstants.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.mx.modelmbean;
  
  public interface XMBeanConstants extends ModelMBeanConstants
  {
  
     //   final static String XML_DEFINITION_URL = "xml.definition.url";
     final static String STANDARD_INTERFACE = OBJECT_REF + "/Standard";
  }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to