User: juhalindfors
  Date: 01/12/05 06:20:39

  Added:       src/main/test BroadcastingUser.java
                        BroadcastingUserMBean.java TestMX.java User.java
                        UserMBean.java
  Log:
  some test classes
  
  Revision  Changes    Path
  1.1                  jmx/src/main/test/BroadcastingUser.java
  
  Index: BroadcastingUser.java
  ===================================================================
  
  package test;
  
  import javax.management.*;
  
  public class BroadcastingUser extends User implements
        BroadcastingUserMBean, NotificationBroadcaster  {
  
           
     // broadcaster support class
     private NotificationBroadcasterSupport broadcaster = 
                 new NotificationBroadcasterSupport();
     
     // sequence number for notifications
     private long notificationSequence = 0;
     
     // override the 'Name' management attribute
     public void setName(String name) {
         String oldValue = super.getName();
         String newValue = name;
         String attrType = String.class.getName();
         String attrName = "Name";
         
         super.setName(name);
         
         broadcaster.sendNotification(
           new AttributeChangeNotification(
             this,                            // source
             ++notificationSequence,          // seq. number
             System.currentTimeMillis(),      // time stamp
             "User's name has been changed.", // message
             attrName, attrType,
             oldValue, newValue
           )
         );
     }
     
     // management operations
     public void remove() {
          
        broadcaster.sendNotification(
           new Notification(
              "example.user.remove",          // type
              this,                           // source
              ++notificationSequence,         // seq. number
              "User " +getName()+ " removed." // message
           )
        );
     }
           
           
     // notification broadcaster implementation      
     
     public void addNotificationListener(
              NotificationListener listener, 
              NotificationFilter filter,
              Object handback) {
           
        broadcaster.addNotificationListener(
                 listener, filter, handback);   
     }
                                      
     public void removeNotificationListener(
              NotificationListener listener)
              throws ListenerNotFoundException {
        
        broadcaster.removeNotificationListener(listener);         
     }
                                  
     public MBeanNotificationInfo[] getNotificationInfo() {
        return new MBeanNotificationInfo[] {
            
          new MBeanNotificationInfo(
            new String[] 
              { "example.user.remove" },  // notif. types
            Notification.class.getName(), // notif. class
            "User Notifications."         // description
          ),
          
          // attribute change notification type
          new MBeanNotificationInfo(
            new String[] {
              AttributeChangeNotification.ATTRIBUTE_CHANGE
            },
            AttributeChangeNotification.class.getName(),
            "User attribute change notification."
          )
        };
     }
  
  }
  
  
  
  
  1.1                  jmx/src/main/test/BroadcastingUserMBean.java
  
  Index: BroadcastingUserMBean.java
  ===================================================================
  package test;
  
  public interface BroadcastingUserMBean extends UserMBean {
  
      public void remove();
      
  }
  
  
  
  1.1                  jmx/src/main/test/TestMX.java
  
  Index: TestMX.java
  ===================================================================
  package test;
  
  import javax.management.*;
  import javax.management.loading.*;
  import java.util.*;
  
  public class TestMX {
  
     public static void main(String[] args) throws Exception {
  
        MBeanServer server = MBeanServerFactory.createMBeanServer();
        
        NotificationListener regListener = new NotificationListener() {
           public void handleNotification(Notification notif, Object handback) {
              
              System.out.println(notif);
              System.out.println(((MBeanServerNotification)notif).getMBeanName());
              System.out.println("handback : " + handback);
              System.out.println("type: " + notif.getType());
              System.out.println("source: " + notif.getSource());
              System.out.println("seq #: " + notif.getSequenceNumber());
              System.out.println("timestamp: " + notif.getTimeStamp());
              System.out.println("userdata: "  + notif.getUserData());
              System.out.println("message: " + notif.getMessage());
           }
        };
        
        server.createMBean("test.User", new ObjectName("standard:name=user"));
        server.createMBean("test.BroadcastingUser", new 
ObjectName("standard:name=buser"));
        
        System.out.println(server.invoke(new ObjectName("standard:name=user"), 
"printInfo", null, null));
        System.out.println(server.invoke(new ObjectName("standard:name=buser"), 
"printInfo", new Object[0], new String[0]));
  
  
        
        
        // simple speed test, invoke empty methodInvocation 1 million times
        ObjectName oname = new ObjectName("Speed:name=mymbean,class=Dynamic");
        System.out.println(oname.getCanonicalName());
        System.out.println(oname.getDomain());
        System.out.println(oname.getKeyPropertyListString());
        ObjectInstance oi = server.registerMBean(new Dynamic(), oname);
        
        long time = System.currentTimeMillis();
        for (int i = 0; i < 1000000; ++i)
           server.invoke(oname, "methodInvocation", null, null);
        long end = System.currentTimeMillis();
        
        System.out.println("1M invocations, DYNAMIC, no args, null return: " + (end - 
time));
  
  
        
        server.registerMBean(new DynamicUser(), new ObjectName("Test:test=dynamic"));
  
        String name = (String)server.getAttribute(new ObjectName("Test:test=dynamic"), 
"Name");
        System.out.println("Name: " + name);
  
        server.setAttribute(new ObjectName("Test:test=dynamic"), new Attribute("Name", 
"Juha"));
        name = (String)server.getAttribute(new ObjectName("Test:test=dynamic"), 
"Name");
        System.out.println("Name: " + name);
  
        oname = new ObjectName("Speed:name=mymbean");
        server.registerMBean(new DynamicUser(), oname);
        String info = (String)server.invoke(oname, "printInfo", null, null);
        System.out.println(info);
  
  
        // simple speed test, invoke empty methodInvocation 1 million times
        oname = new ObjectName("Speed:name=mymbean,class=Dynamic");
        
        time = System.currentTimeMillis();
        for (int i = 0; i < 1000000; ++i)
           server.invoke(oname, "methodInvocation", null, null);
        end = System.currentTimeMillis();
        
        System.out.println("1M invocations, DYNAMIC, no args, null return: " + (end - 
time));
  
  
  
        // simple speed test, invoke printInfo 1 million times
        oname = new ObjectName("Speed:name=mymbean");
        time = System.currentTimeMillis();
        for (int i = 0; i < 100000; ++i)
           server.invoke(oname, "printInfo", null, null);
        end = System.currentTimeMillis();
        System.out.println("1M invocations, DYNAMIC, no args, return printInfo string: 
" + (end - time));
        
        
        server.registerMBean(new SuperStandardDynamic(), new 
ObjectName("standard:name=superStandardDynamic"));
        server.registerMBean(new StandardDynamic(), new 
ObjectName("standard:name=standardDynamic"));
        server.registerMBean(new RealDynamic(), new ObjectName(":name=realDynamic"));
        
        
        // simple speed test, invoke printInfo million times (STANDARD MBEAN)
        oname = new ObjectName("standard:name=user");
        time = System.currentTimeMillis();
        for (int i = 0; i < 1000000; ++i)
           server.invoke(oname, "printInfo", null, null);
        end = System.currentTimeMillis();
        System.out.println("1M invocations, STANDARD, no args, return printInfo 
string: " + (end - time));
  
        // simple speed test, invoke no args, no return methodInvocation 1 million 
times (STANDARD MBEAN)
        oname = new ObjectName("standard:name=superStandardDynamic");
        time = System.currentTimeMillis();
        for (int i = 0; i < 1000000; ++i)
           server.invoke(oname, "methodInvocation", null, null);
        end = System.currentTimeMillis();
        System.out.println("1M invocations, STANDARD, no args, no return: " + (end - 
time));
        
     }
  
     public static interface MyRealInterface extends DynamicMBean {}
     
     public static class RealDynamic extends SuperStandardDynamic implements 
MyRealInterface {
        
     }
     
     public static interface SuperStandardDynamicMBean {
       void methodInvocation();
     }
     
     public static interface MyInterface extends SuperStandardDynamicMBean {
        
     }
     
     public static class SuperStandardDynamic extends Dynamic implements MyInterface {
        public void methodInvocation() {}
     }
     
     static interface StandardDynamicMBean { }
     
     static class StandardDynamic extends Dynamic implements StandardDynamicMBean {
        
     }
     
     static class Dynamic implements DynamicMBean {
  
        public java.lang.Object getAttribute(java.lang.String attribute)
        throws AttributeNotFoundException,
                 MBeanException,
           ReflectionException {
  
           return null;
        }
  
        public void setAttribute(Attribute attribute)
        throws AttributeNotFoundException,
                 InvalidAttributeValueException,
                 MBeanException,
           ReflectionException {}
  
        public AttributeList getAttributes(java.lang.String[] attributes) {
           return null;
        }
  
        public AttributeList setAttributes(AttributeList attributes) {
           return null;
        }
  
        public java.lang.Object invoke(java.lang.String actionName,
                                       java.lang.Object[] params, java.lang.String[] 
signature)
        throws MBeanException,
           ReflectionException {
  
           if (actionName.equals("methodInvocation")) {
              methodInvocation();
              return null;
           }
  
           return null;
        }
  
        public MBeanInfo getMBeanInfo() {
  
           return new MBeanInfo("jmx.book.examples.Dynamic", "", null,
                                new MBeanConstructorInfo[] { new 
MBeanConstructorInfo("default", "", null) },
                                new MBeanOperationInfo[] { new 
MBeanOperationInfo("methodInvocation", "", null, void.class.getName(), 0) },
                                null);
        }
  
        private void methodInvocation() {}
     }
  
     static class DynamicUser implements DynamicMBean {
  
  
        // mgmt attribute name constants
        final static String ID      = "ID";
        final static String NAME    = "Name";
        final static String ADDRESS = "Address";
        final static String NUMBERS = "PhoneNumbers";
        final static String PASSWD  = "Password";
  
  
        // mgmt operation name constants
        final static String PRINT_INFO    = "printInfo";
        final static String ADD_NUMBER    = "addPhoneNumber";
        final static String REMOVE_NUMBER = "removePhoneNumber";
  
  
        // fields for attribute values
        private long id           = System.currentTimeMillis();
        private String name       = "";
        private String address    = "";
        private String passwd     = null;
        private String[] numbers  = new String[3];
  
  
        // getAttribute implementation
  
        public Object getAttribute(String attribute) throws
                 AttributeNotFoundException,
                 MBeanException,
           ReflectionException {
  
           if (attribute == null || attribute.equals(""))
              throw new IllegalArgumentException(
                 "null or empty attribute name"
              );
  
           // map the named attributes to fields
           if (attribute.equals(NAME))
              return name;
  
           if (attribute.equals(ADDRESS))
              return address;
  
           if (attribute.equals(NUMBERS))
              return numbers;
  
           if (attribute.equals(ID))
              return new Long(id);
  
           throw new AttributeNotFoundException(
              "Attribute " + attribute + " not found."
           );
        }
  
  
        // getAttributes 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;
        }
  
  
        // setAttribute implementation
  
        public void setAttribute(Attribute attribute) throws
                 AttributeNotFoundException,
                 InvalidAttributeValueException,
                 MBeanException,
           ReflectionException {
  
           if (attribute == null)
              throw new
              AttributeNotFoundException("null attribute");
  
           // map attributes to fields
           try {
              if (attribute.getName().equals(NAME))
                 this.name = (String)attribute.getValue();
  
              else if (attribute.getName().equals(ADDRESS))
                 this.address = (String)attribute.getValue();
  
              else if (attribute.getName().equals(NUMBERS))
                 this.numbers = (String[])attribute.getValue();
  
              else if (attribute.getName().equals(PASSWD))
                 this.passwd = (String)attribute.getValue();
  
              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) {
  
           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 {
  
           if (actionName == null || actionName.equals(""))
              throw new IllegalArgumentException("no operation");
  
           // map operation names to methods
           if (actionName.equals(PRINT_INFO))
              return printInfo();
  
           else if (actionName.equals(ADD_NUMBER)) {
              addPhoneNumber((String)params[0]);
              return null;
           }
           else if (actionName.equals(REMOVE_NUMBER)) {
              removePhoneNumber(((Integer)params[0]).intValue());
              return null;
           }
           else
              throw new UnsupportedOperationException(
                 "unknown operation " + actionName
              );
        }
  
  
        // getMBeanInfo implementation
  
        public MBeanInfo 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 dynamic management interface";
  
           // meta data for 'ID' attribute.
           MBeanAttributeInfo id   = new MBeanAttributeInfo(
                                        ID,                                   // name
                                        long.class.getName(),                 // type
                                        "Unique identifier of user.",         // 
description
                                        READABLE, !WRITABLE, !IS_GETTER       // access
                                     );
  
           // meta data for 'Name' attribute.
           MBeanAttributeInfo name = new MBeanAttributeInfo(
                                        NAME,                                 // name
                                        String.class.getName(),               // type
                                        "User's name.",                       // 
description
                                        READABLE, WRITABLE, !IS_GETTER        // access
                                     );
  
           // meta data for 'Address' attribute.
           MBeanAttributeInfo address = new MBeanAttributeInfo(
                                           ADDRESS,                              // 
name
                                           String.class.getName(),               // 
type
                                           "User's address.",                    // 
description
                                           READABLE, WRITABLE, !IS_GETTER        // 
access
                                        );
  
           // meta data for 'PhoneNumbers' attribute.
           MBeanAttributeInfo numbers = new MBeanAttributeInfo(
                                           NUMBERS,                              // 
name
                                           String[].class.getName(),             // 
type
                                           "User's phone numbers.",              // 
description
                                           READABLE, WRITABLE, !IS_GETTER        // 
access
                                        );
  
           // meta data for 'Password' attribute.
           MBeanAttributeInfo passwd  = new MBeanAttributeInfo(
                                           PASSWD,                               // 
name
                                           String.class.getName(),               // 
type
                                           "User's password.",                   // 
description
                                           !READABLE, WRITABLE, !IS_GETTER       // 
access
                                        );
  
  
           // meta data for 'printInfo' operation
           MBeanOperationInfo printInfo = new MBeanOperationInfo(
                                             PRINT_INFO,                           // 
name
                                             "String representation of the user.", // 
description
                                             null,                                 // 
signature
                                             String.class.getName(),               // 
return type
                                             MBeanOperationInfo.INFO               // 
impact
                                          );
  
           // meta data for 'addPhoneNumber' operation.
           MBeanOperationInfo addPhoneNumber =
              new MBeanOperationInfo(
                 ADD_NUMBER,                         // name
                 "Adds phone number for the user.",  // description
  
                 new MBeanParameterInfo[] {          // signature
                    new MBeanParameterInfo(
                       "number",
                       String.class.getName(),
                       "The number to add."
                    )
                 },
  
                 void.class.getName(),               // return type
                 MBeanOperationInfo.ACTION           // impact
              );
  
           // meta data for 'removePhoneNumber' operation.
           MBeanOperationInfo removePhoneNumber =
              new MBeanOperationInfo(
                 REMOVE_NUMBER,                      // name
                 "Removes phone number from user.",  // description
  
                 new MBeanParameterInfo[] {          // signature
                    new MBeanParameterInfo(
                       "index",
                       int.class.getName(),
                       "The index number."
                    )
                 },
  
                 void.class.getName(),               // return type
                 MBeanOperationInfo.ACTION           // impact
              );
  
           // mbean constructors
           MBeanConstructorInfo defaultConstructor =
              new MBeanConstructorInfo(
                 "Default Constructor",
                 "Creates a new user instance.", null
              );
  
           // attribute, constructor and operation lists
           MBeanAttributeInfo[] attributes =
              new MBeanAttributeInfo[] {
                 id, name, address, numbers, passwd
              };
  
           MBeanConstructorInfo[] constructors =
              new MBeanConstructorInfo[] {
                 defaultConstructor
              };
  
           MBeanOperationInfo[] operations =
              new MBeanOperationInfo[] {
                 printInfo, addPhoneNumber, removePhoneNumber
              };
  
           // return the MBeanInfo
           return new MBeanInfo(
                     className, description, attributes,
                     constructors, operations, null
                  );
        }
  
        // management operation implementations
        public String printInfo() {
           return
              "User: " + name +"\n"+
              "Address: " + address +"\n"+
              "Phone #: " + numbers[0] +"\n"+
              "Phone #: " + numbers[1] +"\n"+
              "Phone #: " + numbers[2] +"\n";
        }
  
        public void addPhoneNumber(String number) {
           for (int i = 0; i < numbers.length; ++i)
              if (numbers[i] == null) {
                 numbers[i] = number;
                 break;
              }
        }
  
        public void removePhoneNumber(int index) {
           if (index < 0 || index >= numbers.length)
              return;
  
           numbers[index] = null;
        }
  
     }
  
  
  }
  
  
  
  1.1                  jmx/src/main/test/User.java
  
  Index: User.java
  ===================================================================
  
  package test;
  
  public class User implements UserMBean  {
  
      private long id          = System.currentTimeMillis();
      private String name      = "";
      private String address   = "";
      private String password  = null;
      private String[] numbers = new String[3];
      
  
      // read-only attribute 'ID'
      public long getID() { 
          return id;
      }
  
  
      // application method, not exposed to management
      public void setID(long id) {
          this.id = id;
      }
      
  
      // read-write attribute 'Name'
      public String getName() {
          return name;
      }
      public void setName(String name) {
          
  //System.out.println("SetNAME");
  
          this.name = name;
      }
  
      
      // read-write attribute 'Address'
      public String getAddress() {
          return address;
      }
      public void setAddress(String address) {
          this.address = address;
      }
  
      
      // read-write array attribute 'PhoneNumbers'
      public String[] getPhoneNumbers() {
          return numbers;
      }
      public void setPhoneNumbers(String[] numbers) {
          this.numbers = numbers;
      }
  
      
      // write-only attribute 'Password'
      public void setPassword(String passwd) {
          this.password = passwd;
      }
  
      
      // management operations
      public String printInfo() {
          return 
            "User: " + getName() +"\n"+
            "Address: " + getAddress() +"\n"+
            "Phone #: " + getPhoneNumbers()[0] +"\n"+
            "Phone #: " + getPhoneNumbers()[1] +"\n"+
            "Phone #: " + getPhoneNumbers()[2] +"\n";
      }
      
      public void addPhoneNumber(String number) {
          for (int i = 0; i < numbers.length; ++i)
              if (numbers[i] == null) {
                  numbers[i] = number;
                  break;
              }
      }
  
      public void removePhoneNumber(int index) {
          if (index < 0 || index >= numbers.length)
              return;
              
          numbers[index] = null;
      }
  }
  
  
  
  
  
  1.1                  jmx/src/main/test/UserMBean.java
  
  Index: UserMBean.java
  ===================================================================
  
  package test;
  
  public interface UserMBean {
  
      // read-only attribute 'ID'
      public long getID();
      
      // read-write attribute 'Name'
      public String getName();
      public void setName(String name);
      
      // read-write attribute 'Address'
      public String getAddress();
      public void setAddress(String address);
      
      // read-write array attribute 'PhoneNumbers'
      public String[] getPhoneNumbers();
      public void setPhoneNumbers(String[] numbers);
      
      // write-only attribute 'Password'
      public void setPassword(String passwd);
      
      // management operations
      public String printInfo();
      public void addPhoneNumber(String number);
      public void removePhoneNumber(int index);
  }
  
  
  
  
  

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

Reply via email to