jdk/test/javax/management/namespace/Wombat.java
changeset 4156 acaa49a2768a
parent 4155 460e37d40f12
child 4159 9e3aae7675f1
equal deleted inserted replaced
4155:460e37d40f12 4156:acaa49a2768a
     1 /*
       
     2  * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 import java.util.Random;
       
    25 import java.util.Set;
       
    26 import javax.management.AttributeChangeNotification;
       
    27 import javax.management.ListenerNotFoundException;
       
    28 import javax.management.MBeanAttributeInfo;
       
    29 import javax.management.MBeanInfo;
       
    30 import javax.management.MBeanNotificationInfo;
       
    31 import javax.management.MBeanOperationInfo;
       
    32 import javax.management.MBeanParameterInfo;
       
    33 import javax.management.MBeanRegistration;
       
    34 import javax.management.MBeanServer;
       
    35 import javax.management.NotCompliantMBeanException;
       
    36 import javax.management.NotificationBroadcasterSupport;
       
    37 import javax.management.NotificationEmitter;
       
    38 import javax.management.NotificationFilter;
       
    39 import javax.management.NotificationListener;
       
    40 import javax.management.ObjectName;
       
    41 import javax.management.StandardMBean;
       
    42 
       
    43 
       
    44 /**
       
    45  * Dynamic MBean based on StandardMBean
       
    46  * Class Wombat
       
    47  * Wombat Description
       
    48  * @author dfuchs
       
    49  */
       
    50 public class Wombat extends StandardMBean
       
    51         implements WombatMBean, NotificationEmitter, MBeanRegistration {
       
    52 
       
    53     /**
       
    54      * Attribute : Caption
       
    55      */
       
    56     private String caption = "I'm a wombat";
       
    57 
       
    58     private final long MAX_SEED = 36000;
       
    59     private final long seed;
       
    60     private final long period;
       
    61     private volatile int mood = 0;
       
    62 
       
    63     public int getMood() {
       
    64         final long  degree = seed + (System.currentTimeMillis()/period)%MAX_SEED;
       
    65         final double angle = ((double)degree)/100;
       
    66         mood = (int)(100.0*Math.sin(angle));
       
    67         return mood;
       
    68     }
       
    69 
       
    70     public Wombat() throws NotCompliantMBeanException {
       
    71         this(WombatMBean.class);
       
    72     }
       
    73 
       
    74     public Wombat(Class<? extends WombatMBean> clazz)
       
    75             throws NotCompliantMBeanException {
       
    76         super(clazz);
       
    77         final Random r = new Random();
       
    78         seed = ((r.nextLong() % MAX_SEED) + MAX_SEED)%MAX_SEED;
       
    79         period = 200 + (((r.nextLong()%80)+80)%80)*10;
       
    80     }
       
    81 
       
    82     /**
       
    83      * Next are the methods to compute MBeanInfo.
       
    84      * You shouldn't update these methods.
       
    85      */
       
    86     @Override
       
    87     protected String getDescription(MBeanInfo info) {
       
    88         return "Wombats are strange beasts. You will find them down under " +
       
    89                 "and in some computer programms.";
       
    90     }
       
    91 
       
    92     @Override
       
    93     protected String getDescription(MBeanAttributeInfo info) {
       
    94         String description = null;
       
    95         if (info.getName().equals("Caption")) {
       
    96             description = "A simple caption to describe a wombat";
       
    97         }
       
    98         if (info.getName().equals("Mood")) {
       
    99             description = "This Wombat's mood on a [-100,+100] scale."+
       
   100                       " -100 means that this wombat is very angry.";
       
   101         }
       
   102         return description;
       
   103     }
       
   104 
       
   105     @Override
       
   106     protected String getDescription(MBeanOperationInfo op,
       
   107             MBeanParameterInfo param,
       
   108             int sequence) {
       
   109         return null;
       
   110     }
       
   111 
       
   112     @Override
       
   113     protected String getParameterName(MBeanOperationInfo op,
       
   114             MBeanParameterInfo param,
       
   115             int sequence) {
       
   116         return null;
       
   117     }
       
   118 
       
   119     @Override
       
   120     protected String getDescription(MBeanOperationInfo info) {
       
   121         String description = null;
       
   122         return description;
       
   123     }
       
   124 
       
   125     @Override
       
   126     public MBeanInfo getMBeanInfo() {
       
   127         MBeanInfo mbinfo = super.getMBeanInfo();
       
   128         return new MBeanInfo(mbinfo.getClassName(),
       
   129                 mbinfo.getDescription(),
       
   130                 mbinfo.getAttributes(),
       
   131                 mbinfo.getConstructors(),
       
   132                 mbinfo.getOperations(),
       
   133                 getNotificationInfo());
       
   134     }
       
   135 
       
   136     /**
       
   137      * Get A simple caption to describe a wombat
       
   138      */
       
   139     public synchronized String getCaption() {
       
   140         return caption;
       
   141     }
       
   142 
       
   143     /**
       
   144      * Set A simple caption to describe a wombat
       
   145      */
       
   146     public void setCaption(String value) {
       
   147         final String oldValue;
       
   148         synchronized (this) {
       
   149             oldValue = caption;
       
   150             caption = value;
       
   151         }
       
   152         final AttributeChangeNotification notif =
       
   153                 new AttributeChangeNotification(objectName,
       
   154                     getNextSeqNumber(),
       
   155                     System.currentTimeMillis(),
       
   156                     "Caption changed","Caption",
       
   157                     String.class.getName(),oldValue,value);
       
   158         broadcaster.sendNotification(notif);
       
   159     }
       
   160 
       
   161     /**
       
   162      * MBeanNotification support
       
   163      * You shouldn't update these methods
       
   164      */
       
   165     public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws IllegalArgumentException {
       
   166         broadcaster.addNotificationListener(listener, filter, handback);
       
   167     }
       
   168 
       
   169     public MBeanNotificationInfo[] getNotificationInfo() {
       
   170         return new MBeanNotificationInfo[] {
       
   171             new MBeanNotificationInfo(new String[] {
       
   172                 AttributeChangeNotification.ATTRIBUTE_CHANGE},
       
   173                 javax.management.AttributeChangeNotification.class.getName(),
       
   174                 "Sent when the caption changes")
       
   175             };
       
   176     }
       
   177 
       
   178     public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException {
       
   179         broadcaster.removeNotificationListener(listener);
       
   180     }
       
   181 
       
   182     public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException {
       
   183         broadcaster.removeNotificationListener(listener, filter, handback);
       
   184     }
       
   185 
       
   186     private synchronized long getNextSeqNumber() {
       
   187         return seqNumber++;
       
   188     }
       
   189 
       
   190     private long seqNumber;
       
   191 
       
   192     private final NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
       
   193 
       
   194     /**
       
   195      * Allows the MBean to perform any operations it needs before being
       
   196      * registered in the MBean server. If the name of the MBean is not
       
   197      * specified, the MBean can provide a name for its registration. If
       
   198      * any exception is raised, the MBean will not be registered in the
       
   199      * MBean server.
       
   200      * @param server The MBean server in which the MBean will be registered.
       
   201      * @param name The object name of the MBean. This name is null if the
       
   202      * name parameter to one of the createMBean or registerMBean methods in
       
   203      * the MBeanServer interface is null. In that case, this method must
       
   204      * return a non-null ObjectName for the new MBean.
       
   205      * @return The name under which the MBean is to be registered. This value
       
   206      * must not be null. If the name parameter is not null, it will usually
       
   207      * but not necessarily be the returned value.
       
   208      * @throws Exception This exception will be caught by the MBean server and
       
   209      * re-thrown as an MBeanRegistrationException.
       
   210      */
       
   211     @Override
       
   212     public ObjectName preRegister(MBeanServer server, ObjectName name)
       
   213             throws Exception {
       
   214         objectName = name;
       
   215         mbeanServer = server;
       
   216         return super.preRegister(server, name);
       
   217     }
       
   218 
       
   219     /**
       
   220      * Allows the MBean to perform any operations needed after having
       
   221      * been registered in the MBean server or after the registration has
       
   222      * failed.
       
   223      * @param registrationDone Indicates wether or not the MBean has been
       
   224      * successfully registered in the MBean server. The value false means
       
   225      * that the registration has failed.
       
   226      */
       
   227     @Override
       
   228     public void postRegister(Boolean registrationDone) {
       
   229         super.postRegister(registrationDone);
       
   230     }
       
   231 
       
   232     /**
       
   233      * Allows the MBean to perform any operations it needs before being
       
   234      * unregistered by the MBean server.
       
   235      * @throws Exception This exception will be caught by the MBean server and
       
   236      * re-thrown as an MBeanRegistrationException.
       
   237      */
       
   238     @Override
       
   239     public void preDeregister() throws Exception {
       
   240         super.preDeregister();
       
   241     }
       
   242 
       
   243     /**
       
   244      * Allows the MBean to perform any operations needed after having been
       
   245      * unregistered in the MBean server.
       
   246      */
       
   247     @Override
       
   248     public void postDeregister() {
       
   249         super.postDeregister();
       
   250     }
       
   251 
       
   252     public Set<ObjectName> listMatching(ObjectName pattern) {
       
   253         return mbeanServer.queryNames(pattern, null);
       
   254     }
       
   255 
       
   256     private MBeanServer mbeanServer;
       
   257 
       
   258     private ObjectName objectName;
       
   259 }