1 Reply Latest reply on Apr 3, 2003 11:32 AM by arabinow

    PersistPolicy

    arabinow

      I am trying to use PersistPlicy with ModelMBean "OnUpdate". It looks like store method is not called at all upon update.
      When I use other classes - like ModelBase or XMBean, I have NullPointerException in preRergister method.

        • 1. Re: PersistPolicy
          arabinow

          Actually, I have the following output:
          PulsarStandard
          startPulse
          run
          MBeanException: preRegister() failed [ObjectName='DefaultDomain:type=com.peerdirect.rmapplications.master.jmx.PulsarStandard', Class=com.peerdirect.rmapplications.master.jmx.PulsarStandard (com.peerdirect.rmapplications.master.jmx.PulsarStandard@5bdc50)]
          Cause: java.lang.NullPointerException
          at org.jboss.mx.server.registry.BasicMBeanRegistry.registerMBean(BasicMBeanRegistry.java:187)
          at org.jboss.mx.server.MBeanServerImpl.registerMBean(MBeanServerImpl.java:975)
          at org.jboss.mx.server.MBeanServerImpl.registerMBean(MBeanServerImpl.java:302)
          at com.peerdirect.rmapplications.agent.BaseAgent.(BaseAgent.java:67)
          at com.peerdirect.rmapplications.agent.MasterAgent.(MasterAgent.java:5)
          at com.peerdirect.rmapplications.agent.MasterAgent.main(MasterAgent.java:15)
          Instantiating an HTML protocol adaptor with default port...
          Pulsing
          Instantiating an RMI connector server with default port...
          Pulsing
          Pulsing
          Pulsing
          Pulsing

          The code that registers the mbean, is here:
          String pulsarName = "com.peerdirect.rmapplications.master.jmx.PulsarStandard" ;
          Object pulsar = Class.forName (pulsarName).newInstance();
          String name = myMBeanServer.getDefaultDomain() + ":type=" + pulsarName;
          myMBeanServer.registerMBean (pulsar, new ObjectName(name));
          -------------------------------------------------
          The last line is called, and preRegister () is called from inside.

          The source code for PulsarStandard is the following:

          package com.peerdirect.rmapplications.master.jmx;

          import com.peerdirect.rmapplications.master.jms.StatusNotificationClient;

          import javax.management.*;
          import javax.management.modelmbean.*;
          import java.util.Iterator;
          import java.lang.Runnable;
          import java.io.*;
          import org.jboss.mx.modelmbean.*;
          import org.jboss.mx.modelmbean.ModelMBeanConstants.*;

          /*public class PulsarStandard extends NotificationBroadcasterSupport
          implements ModelMBean, Runnable*/
          public class PulsarStandard extends ModelBase
          implements Runnable
          {
          final static String OBJECT_REF = "ObjectReference";
          //final static String PERSIST_NAME = "persistName";
          //final static String PERSIST_LOCATION = "persistLocation";
          // notification types
          final static String GENERIC_MODELMBEAN_NOTIFICATION =
          "jmx.modelmbean.generic";

          // support for generic notification listeners
          private NotificationBroadcasterSupport notifier = new NotificationBroadcasterSupport();

          // sequence numbers for notifications
          protected long notifierSequence = 0;
          protected long attrNotifierSequence = 0;

          // mgmt attribute name constants
          final static String PULSE_PERIOD = "PulsePeriodInMills";
          final static String IF_STARTED = "Started";

          // mgmt operation name constants
          final static String START_PULSE = "startPulse";
          final static String STOP_PULSE = "stopPulse";

          // fields for attribute values
          private static final int DEFAULT_PULSE_PERIOD = 1000;
          private static final boolean DEFAULT_STARTED= false;

          private int mPulsePeriod = 0;
          private boolean mStarted = false;
          private StatusNotificationClient mSNC = null;
          private Thread mThread = null;

          protected ModelMBeanInfo metadata = null;
          protected Object resource = null;
          protected String resourceType = null;

          // ModelMbean interface

          public void setModelMBeanInfo (ModelMBeanInfo metadata)
          throws MBeanException, RuntimeOperationsException {
          System.out.println ("setModelMBeanInfo");
          if (metadata == null)
          throw new IllegalArgumentException ("The Model MBeanInfo cannot be null.");
          this.metadata = metadata;
          }

          public void setManagedResource (Object ref, String resourceType)
          throws MBeanException, InstanceNotFoundException, InvalidTargetObjectTypeException
          {
          System.out.println ("setManagedResource");
          if (ref == null)
          throw new IllegalArgumentException("Resource reference cannot be null.");

          // check that is a supported resource type
          if (!isSupportedResourceType(resourceType))
          throw new InvalidTargetObjectTypeException("Unsupported resource type: " + resourceType);
          this.resource = ref;
          this.resourceType = resourceType;
          }

          public boolean isSupportedResourceType(String resourceType)
          {
          System.out.println("isSupportedResourceType");
          //if (resourceType == null)
          // return false;
          //if (resourceType.equals(OBJECT_REF))
          return true;
          //return false;
          }


          // ModelMBeanNotificationBroadcaster interface

          public void addNotificationListener (NotificationListener l, NotificationFilter filter, Object hback)
          {
          System.out.println ("addNotificationListener");
          notifier.addNotificationListener(l, filter, hback);
          }

          public void removeNotificationListener(NotificationListener l)
          throws ListenerNotFoundException
          {
          System.out.println ("removeNotificationListener");
          notifier.removeNotificationListener(l);
          }

          public void addAttributeChangeNotificationListener (NotificationListener l,
          String attributeName,
          Object hback) throws MBeanException
          {
          System.out.println ("addAttributeChangeNotificationListener");
          AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter();

          filter.enableAttribute(attributeName);
          notifier.addNotificationListener(l, filter,hback);
          }

          public void removeAttributeChangeNotificationListener (NotificationListener l, String attributeName)
          throws MBeanException, ListenerNotFoundException
          {
          System.out.println ("removeAttributeChangeNotificationListener");
          notifier.removeNotificationListener(l);
          }

          public void sendNotification(String message)
          throws MBeanException {
          System.out.println ("sendNotification");
          Notification notif = new Notification (
          GENERIC_MODELMBEAN_NOTIFICATION, // type
          this, // source
          ++notifierSequence, // sequence number
          message // message
          );

          sendNotification(notif);
          }

          public void sendNotification (Notification notif)
          //throws MBeanException
          {
          System.out.println ("sendNotification");
          notifier.sendNotification(notif);
          }

          public void sendAttributeChangeNotification(
          AttributeChangeNotification notif)
          throws MBeanException {
          System.out.println ("sendAttributeChangeNotification");
          notifier.sendNotification(notif);
          }

          public void sendAttributeChangeNotification(
          Attribute oldValue, Attribute newValue)
          throws MBeanException {
          System.out.println ("sendAttributeChangeNotification");
          String attr = oldValue.getName();
          String type = oldValue.getClass().getName();

          AttributeChangeNotification notif =
          new AttributeChangeNotification(
          this, // source
          ++attrNotifierSequence, // seq. #
          System.currentTimeMillis(), // time stamp
          "" + attr + " changed from " // message
          + oldValue + " to " + newValue,
          attr, type, // name & type
          oldValue, newValue // values
          );

          notifier.sendNotification(notif);
          }

          public MBeanNotificationInfo[] getNotificationInfo() {
          System.out.println ("getNotificationInfo");
          int size = metadata.getNotifications().length;
          MBeanNotificationInfo[] notifInfo = metadata.getNotifications();

          MBeanNotificationInfo[] modelInfo =
          new MBeanNotificationInfo[size + 2];

          for (int i = 0; i < size ;++i)
          modelInfo = notifInfo;

          Descriptor descr1 = new DescriptorSupport();
          descr1.setField("name", "generic");
          descr1.setField("descriptorType", "notification");
          descr1.setField("severity", "5");

          ModelMBeanNotificationInfo generic = new ModelMBeanNotificationInfo(
          new String[] { GENERIC_MODELMBEAN_NOTIFICATION },
          "generic",
          "A generic Model MBean notification.",
          descr1
          );

          Descriptor descr2 = new DescriptorSupport();
          descr2.setField("name", AttributeChangeNotification.class.getName());
          descr2.setField("descriptorType", "notification");

          ModelMBeanNotificationInfo attrChange = new ModelMBeanNotificationInfo(
          new String[] { AttributeChangeNotification.ATTRIBUTE_CHANGE },
          AttributeChangeNotification.class.getName(),
          "Notifies a change in attribute's value.",
          descr2
          );

          modelInfo[size-2] = generic;
          modelInfo[size-1] = attrChange;

          return modelInfo;
          }


          // PersistentMBean interface

          public void load() throws MBeanException,
          InstanceNotFoundException {
          System.out.println ("load");
          // throw new UnsupportedOperationException();
          if (metadata == null)
          return;

          Descriptor d = metadata.getMBeanDescriptor();
          String dir = (String)d.getFieldValue(PERSIST_LOCATION);
          System.out.println (dir);
          String file = (String)d.getFieldValue(PERSIST_NAME);

          if (file != null) {
          try {
          File f = new File(dir, file);
          FileInputStream fis = new FileInputStream(f);
          ObjectInputStream ois = new ObjectInputStream(fis);

          metadata = (ModelMBeanInfoSupport)ois.readObject();
          }
          catch (Exception e) {
          System.out.println("Error loading MBean state");
          }
          }
          }

          public void store() throws MBeanException,
          InstanceNotFoundException {
          System.out.println ("store");
          try {
          Descriptor d = metadata.getMBeanDescriptor();
          String dir = (String)d.getFieldValue(PERSIST_LOCATION);
          System.out.println (dir);
          String file = (String)d.getFieldValue(PERSIST_NAME);

          File f = new File(dir, file);
          FileOutputStream fos = new FileOutputStream(f);
          ObjectOutputStream oos = new ObjectOutputStream(fos);

          oos.writeObject(metadata);
          }
          catch (IOException e) {
          throw new MBeanException(e, "Error in persisting MBean.");
          }
          //throw new UnsupportedOperationException();
          }

          // getAttribute implementation

          public PulsarStandard () throws MBeanException
          {
          super ();
          System.out.println ("PulsarStandard");
          mPulsePeriod = DEFAULT_PULSE_PERIOD;
          mStarted = DEFAULT_STARTED;
          mSNC = new StatusNotificationClient ();
          mThread = new Thread (this);
          startPulse () ;
          }

          public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException
          {
          System.out.println ("getAttribute");
          if (attribute == null || attribute.equals(""))
          throw new IllegalArgumentException("null or empty attribute name");

          // map the named attributes to fields

          if (attribute.equals(PULSE_PERIOD))
          return new Integer (mPulsePeriod);
          else if (attribute.equals(IF_STARTED))
          return new Boolean (mStarted);

          throw new AttributeNotFoundException("Attribute " + attribute + " not found.");
          }


          // getAttributes implementation

          public AttributeList getAttributes (String[] attributes)
          {
          System.out.println ("getAttributes");
          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, getAttribute(attributes)));
          }
          catch (JMException ignored)
          {
          // if the attribute could not be retrieved, skip it
          }
          }
          return list;
          }


          // setAttribute implementation

          public void setAttribute (Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException,
          MBeanException, ReflectionException
          {
          System.out.println ("setAttribute");
          if (attribute == null) throw new
          AttributeNotFoundException("null attribute");

          // map attributes to fields
          try
          {
          if (attribute.getName().equals(PULSE_PERIOD))
          this.mPulsePeriod = ((Integer)attribute.getValue()).intValue();
          else if (attribute.getName().equals(IF_STARTED))
          this.mStarted = ((Boolean)attribute.getValue()).booleanValue();
          else
          throw new AttributeNotFoundException("attribute " + attribute.getName() + " not found.");
          }
          catch (ClassCastException e)
          {
          throw new InvalidAttributeValueException("Invalid attribute type " + attribute.getValue().getClass().getName());
          }
          }


          // setAttributes implementation

          public AttributeList setAttributes (AttributeList list)
          {
          System.out.println ("setAttributes");
          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;
          }


          // invoke implementation

          public Object invoke(String actionName,
          Object[] params,
          String[] signature) throws MBeanException, ReflectionException {
          System.out.println ("invoke");
          if (actionName == null || actionName.equals(""))
          throw new IllegalArgumentException("no operation");

          // map operation names to methods
          if (actionName.equals(START_PULSE))
          {
          startPulse();
          return null;
          }
          else if (actionName.equals(STOP_PULSE))
          {
          stopPulse ();
          return null;
          }
          else
          throw new UnsupportedOperationException("unknown operation " + actionName);
          }


          // getMBeanInfo implementation

          public MBeanInfo getMBeanInfo()
          {
          System.out.println ("getMBeanInfo");
          final boolean READABLE = true;
          final boolean WRITABLE = true;
          final boolean IS_GETTER = true;

          // MBean class and description
          String className = getClass().getName();
          String description =
          "User resource with model management interface";

          // build 'RoomName' read-write attribute
          Descriptor descr1 = new DescriptorSupport();
          descr1.setField("name", PULSE_PERIOD);
          descr1.setField("descriptorType", "attribute");
          descr1.setField("displayName", "Room Number");
          descr1.setField("default", "D325");
          descr1.setField(PERSIST_POLICY, "OnUpdate");
          descr1.setField(PERSIST_LOCATION, "c:\\aa");
          descr1.setField(PERSIST_NAME, "storage.txt");
          System.out.println ("Valid " + descr1.isValid());

          // meta data for 'PULSE_PERIOD' attribute.
          ModelMBeanAttributeInfo mPulsePeriod = new ModelMBeanAttributeInfo(
          PULSE_PERIOD, // name
          int.class.getName(), // type
          "Pulse Period", // description
          IS_READABLE, IS_WRITABLE, !IS_IS, // access
          descr1
          );

          // meta data for 'IF_STARTED' attribute.
          ModelMBeanAttributeInfo mStarted = new ModelMBeanAttributeInfo(
          IF_STARTED, // name
          boolean.class.getName(), // type
          "If the Pulser has Started", // description
          IS_READABLE, !IS_WRITABLE, !IS_IS // access
          );

          // meta data for 'startPulse' operation
          ModelMBeanOperationInfo startPulse = new ModelMBeanOperationInfo(
          START_PULSE, // name
          "Start the pulsar", // description
          null, // signature
          void.class.getName(), // return type
          MBeanOperationInfo.ACTION // impact
          );

          // meta data for 'stopPulse' operation
          ModelMBeanOperationInfo stopPulse = new ModelMBeanOperationInfo(
          STOP_PULSE, // name
          "Stop the pulsar", // description
          null, // signature
          void.class.getName(), // return type
          MBeanOperationInfo.ACTION // impact
          );

          // mbean constructors
          ModelMBeanConstructorInfo defaultConstructor =
          new ModelMBeanConstructorInfo(
          "Default Constructor",
          "Creates a new Pulsar", null
          );

          // attribute, constructor and operation lists
          ModelMBeanAttributeInfo[] attributes = new ModelMBeanAttributeInfo[] {mPulsePeriod, mStarted};

          ModelMBeanConstructorInfo[] constructors = new ModelMBeanConstructorInfo[] {defaultConstructor};

          ModelMBeanOperationInfo[] operations = new ModelMBeanOperationInfo[] {startPulse, stopPulse};

          // return the MBeanInfo
          return new ModelMBeanInfoSupport (className, description, attributes, constructors, operations, null);
          }

          // RW attributes
          /*
          public int getPulsePeriodInMills ()
          {
          return mPulsePeriod;
          }

          public void setPulsePeriodInMills (int pPulsePeriod)
          {
          mPulsePeriod = pPulsePeriod ;
          }

          public boolean getStarted ()
          {
          return mStarted;
          }
          */

          public void startPulse ()
          {
          System.out.println ("startPulse");
          mThread.start ();
          try
          {
          mSNC.connect();
          mStarted = true ;
          }
          catch (Exception e)
          {
          mStarted = false ;
          }
          }

          public void stopPulse ()
          {
          System.out.println ("stopPulse");
          mThread.stop ();
          mSNC.disconnect();
          mStarted = false ;
          }

          public void run ()
          {
          System.out.println ("run");
          try
          {
          while (true)
          {
          mThread.sleep (mPulsePeriod);
          if (mStarted)
          {
          System.out.println("Pulsing");
          mSNC.sendPulse();
          }
          }
          }
          catch (Exception ex)
          {
          ex.printStackTrace();
          }
          }
          }

          //////////////////////////////////////////////////////////////////////////////////////
          //////////////////////////////////////////////////////////////////////////////////////
          //////////////////////////////////////////////////////////////////////////////////////