QpidEntity.java
001 package org.apache.qpid.management.domain.model;
002 
003 import java.util.HashMap;
004 import java.util.Map;
005 
006 import javax.management.Attribute;
007 import javax.management.AttributeList;
008 import javax.management.DynamicMBean;
009 import javax.management.MBeanInfo;
010 import javax.management.NotificationBroadcasterSupport;
011 import javax.management.ObjectName;
012 import javax.management.RuntimeOperationsException;
013 
014 import org.apache.qpid.management.Messages;
015 import org.apache.qpid.management.Names;
016 import org.apache.qpid.management.domain.model.type.Binary;
017 import org.apache.qpid.management.domain.services.QpidService;
018 import org.apache.qpid.management.jmx.EntityLifecycleNotification;
019 import org.apache.qpid.transport.util.Logger;
020 
021 /**
022  * Layer supertype for QMan entities.
023  */
024 public abstract class QpidEntity extends NotificationBroadcasterSupport
025 {  
026   /**
027    * Layer supertype for QMan managed bean entities. 
028    */
029   abstract class QManManagedEntity implements DynamicMBean
030   {
031         // After mbean is registered with the MBean server this collection holds the mbean attribute values.
032         Map<String,Object> _attributes = new HashMap<String, Object>();
033         
034         /**
035          * Creates or replace the given attribute.
036          * Note that this is not part of the management interface of this object instance and therefore will be accessible only
037          * from within this class.
038          * It is used to update directly the object attributes bypassing jmx interface.
039          
040          @param attributeName the name of the attribute.
041          @param property newValue the new value of the attribute.
042          */
043         void createOrReplaceAttributeValue(String attributeName, Object newValue
044         {
045           _attributes.put(attributeName, newValue);
046         }
047         
048         /**
049          * Get the values of several attributes of the Dynamic MBean.
050          *
051          @param attributes A list of the attributes to be retrieved.
052          *
053          @return  The list of attributes retrieved.
054          */
055          public AttributeList getAttributes (String[] attributes)
056          {
057              if (attributes == null
058              {
059                  throw new RuntimeOperationsException(new IllegalArgumentException("Attributes array must not be null"));
060              }
061              
062              AttributeList result = new AttributeList(attributes.length);
063              for (int i = 0; i < attributes.length; i++)
064              {
065                  String attributeName = attributes[i];
066                  try 
067                  {
068                      result.add(new Attribute(attributeName,getAttribute(attributeName)));
069                  catch(Exception exception
070                  {
071                      // Already logged.
072                  }
073              
074              return result;
075          }
076          
077          /**
078           * Returns metadata for this object instance.
079           */
080          // Developer Note : note that this metadata is a member of the outer class definition : in that way we create 
081          // that metadata only once and then it will be shared between all object instances (it's a readonly object)
082          public MBeanInfo getMBeanInfo ()
083          {
084              return _metadata;
085          }         
086   };
087   
088     final Logger _logger = Logger.get(getClass());
089     final static JmxService JMX_SERVICE = new JmxService();
090     
091     final String _name;
092     final Binary _hash;
093     
094     final QpidPackage _parent;
095     MBeanInfo _metadata;
096     final QpidService _service;
097 
098     protected ObjectName _objectName;
099     
100     private final String _type;
101     
102     /**
103      * Builds a new class with the given name and package as parent.
104      
105      @param className the name of the class.
106      @param hash the class schema hash.
107      @param parentPackage the parent of this class.
108      */
109     QpidEntity(String className, Binary hash, QpidPackage parentPackage,String type)
110     {
111         this._name = className;
112         this._parent = parentPackage;
113         this._hash = hash;
114         this._type = type;
115         this._service = new QpidService(_parent.getOwnerId());
116         
117         _logger.debug(
118                 Messages.QMAN_200020_ENTITY_DEFINITION_HAS_BEEN_BUILT, 
119                 _parent.getOwnerId(),
120                 _parent.getName(),
121                 _name);        
122     }
123     
124     public String getName()
125     {
126       return _name;
127     }
128     
129     public String getPackageName()
130     {
131       return _parent.getName();
132     }
133     
134     /**
135      * Internal method used to send a schema request for this entity.
136      
137      @throws Exception when the request cannot be sent.
138      */
139     void requestSchema() throws Exception
140     {     
141 
142          _objectName = JMX_SERVICE.createEntityDefinitionName(_parent.getName(), _name,_type);
143         JMX_SERVICE.registerEntityDefinition(_objectName,this,_parent.getName(),_name);
144 
145       try
146         {
147             _service.connect();
148            _service.requestSchema(_parent.getName(), _name, _hash);
149             _service.sync();
150         finally
151         {
152             _service.close();
153         }                
154         
155          EntityLifecycleNotification notification = new EntityLifecycleNotification(
156                EntityLifecycleNotification.SCHEMA_REQUESTED_NOTIFICATION_TYPE,
157                _parent.getName()
158                _name, 
159                Names.CLASS,
160                _objectName);
161            sendNotification(notification);
162     }    
163 }