1 /*
2  * Copyright (c) 2003, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * build     @BUILD_TAG_PLACEHOLDER@
26  *
27  * @COPYRIGHT_MINI_LEGAL_NOTICE_PLACEHOLDER@
28  */
29 
30 /**
31  * Simple definition of a standard MBean, named "SimpleStandard".
32  *
33  * The "SimpleStandard" standard MBean shows how to expose attributes
34  * and operations for management by implementing its corresponding
35  * "SimpleStandardMBean" management interface.
36  *
37  * This MBean has two attributes and one operation exposed
38  * for management by a JMX agent:
39  *      - the read/write "State" attribute,
40  *      - the read only "NbChanges" attribute,
41  *      - the "reset()" operation.
42  *
43  * This object also has one property and one method not exposed
44  * for management by a JMX agent:
45  *      - the "NbResets" property,
46  *      - the "getNbResets()" method.
47  */
48 
49 import javax.management.AttributeChangeNotification;
50 import javax.management.NotificationBroadcasterSupport;
51 
52 public class SimpleStandard
53     extends NotificationBroadcasterSupport
54     implements SimpleStandardMBean {
55 
56     /*
57      * -----------------------------------------------------
58      * CONSTRUCTORS
59      * -----------------------------------------------------
60      */
61 
62     /* "SimpleStandard" does not provide any specific constructors.
63      * However, "SimpleStandard" is JMX compliant with regards to
64      * contructors because the default contructor SimpleStandard()
65      * provided by the Java compiler is public.
66      */
67 
68     /*
69      * -----------------------------------------------------
70      * IMPLEMENTATION OF THE SimpleStandardMBean INTERFACE
71      * -----------------------------------------------------
72      */
73 
74     /**
75      * Getter: get the "State" attribute of the "SimpleStandard" standard MBean.
76      *
77      * @return the current value of the "State" attribute.
78      */
getState()79     public String getState() {
80         return state;
81     }
82 
83     /**
84      * Setter: set the "State" attribute of the "SimpleStandard" standard MBean.
85      *
86      * @param <VAR>s</VAR> the new value of the "State" attribute.
87      */
setState(String s)88     public void setState(String s) {
89         state = s;
90         nbChanges++;
91     }
92 
93     /**
94      * Getter: get the "NbChanges" attribute of the "SimpleStandard" standard
95      * MBean.
96      *
97      * @return the current value of the "NbChanges" attribute.
98      */
getNbChanges()99     public int getNbChanges() {
100         return nbChanges;
101     }
102 
103     /**
104      * Operation: reset to their initial values the "State" and "NbChanges"
105      * attributes of the "SimpleStandard" standard MBean.
106      */
reset()107     public void reset() {
108         AttributeChangeNotification acn =
109             new AttributeChangeNotification(this,
110                                             0,
111                                             0,
112                                             "NbChanges reset",
113                                             "NbChanges",
114                                             "Integer",
115                                             new Integer(nbChanges),
116                                             new Integer(0));
117         state = "initial state";
118         nbChanges = 0;
119         nbResets++;
120         sendNotification(acn);
121     }
122 
123     /*
124      * -----------------------------------------------------
125      * METHOD NOT EXPOSED FOR MANAGEMENT BY A JMX AGENT
126      * -----------------------------------------------------
127      */
128 
129     /**
130      * Return the "NbResets" property.
131      * This method is not a Getter in the JMX sense because it
132      * is not exposed in the "SimpleStandardMBean" interface.
133      *
134      * @return the current value of the "NbResets" property.
135      */
getNbResets()136     public int getNbResets() {
137         return nbResets;
138     }
139 
140     /*
141      * -----------------------------------------------------
142      * ATTRIBUTES ACCESSIBLE FOR MANAGEMENT BY A JMX AGENT
143      * -----------------------------------------------------
144      */
145 
146     private String state = "initial state";
147     private int nbChanges = 0;
148 
149     /*
150      * -----------------------------------------------------
151      * PROPERTY NOT ACCESSIBLE FOR MANAGEMENT BY A JMX AGENT
152      * -----------------------------------------------------
153      */
154 
155     private int nbResets = 0;
156 }
157