blob: 329453912091fba51846125d61783e1a2d2a69fe [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2004 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/**
25 * Simple definition of a standard MBean, named "SimpleStandard".
26 *
27 * The "SimpleStandard" standard MBean shows how to expose attributes and
28 * operations for management by implementing its corresponding
29 * "SimpleStandardMBean" management interface.
30 *
31 * This MBean has two attributes and one operation exposed
32 * for management by a JMX agent:
33 * - the read/write "State" attribute,
34 * - the read only "NbChanges" attribute,
35 * - the "reset()" operation.
36 *
37 * This object also has one property and one method not exposed
38 * for management by a JMX agent:
39 * - the "NbResets" property,
40 * - the "getNbResets()" method.
41 */
42
43import java.security.AccessControlContext;
44import java.security.AccessController;
45import java.security.Principal;
46import java.util.Set;
47import javax.management.AttributeChangeNotification;
48import javax.management.NotificationBroadcasterSupport;
49import javax.management.remote.JMXPrincipal;
50import javax.security.auth.Subject;
51
52public 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 */
79 public String getState() {
80 checkSubject();
81 return state;
82 }
83
84 /**
85 * Setter: set the "State" attribute of the "SimpleStandard" standard MBean.
86 *
87 * @param <VAR>s</VAR> the new value of the "State" attribute.
88 */
89 public void setState(String s) {
90 checkSubject();
91 state = s;
92 nbChanges++;
93 }
94
95 /**
96 * Getter: get the "NbChanges" attribute of the "SimpleStandard" standard
97 * MBean.
98 *
99 * @return the current value of the "NbChanges" attribute.
100 */
101 public int getNbChanges() {
102 checkSubject();
103 return nbChanges;
104 }
105
106 /**
107 * Operation: reset to their initial values the "State" and "NbChanges"
108 * attributes of the "SimpleStandard" standard MBean.
109 */
110 public void reset() {
111 checkSubject();
112 AttributeChangeNotification acn =
113 new AttributeChangeNotification(this,
114 0,
115 0,
116 "NbChanges reset",
117 "NbChanges",
118 "Integer",
119 new Integer(nbChanges),
120 new Integer(0));
121 state = "initial state";
122 nbChanges = 0;
123 nbResets++;
124 sendNotification(acn);
125 }
126
127 /*
128 * -----------------------------------------------------
129 * METHOD NOT EXPOSED FOR MANAGEMENT BY A JMX AGENT
130 * -----------------------------------------------------
131 */
132
133 /**
134 * Return the "NbResets" property.
135 * This method is not a Getter in the JMX sense because
136 * it is not exposed in the "SimpleStandardMBean" interface.
137 *
138 * @return the current value of the "NbResets" property.
139 */
140 public int getNbResets() {
141 return nbResets;
142 }
143
144 /*
145 * ---------------
146 * PRIVATE METHODS
147 * ---------------
148 */
149
150 /**
151 * Check that the principal contained in the Subject is of
152 * type JMXPrincipal and refers to the "monitorRole" identity.
153 */
154 private void checkSubject() {
155 AccessControlContext acc = AccessController.getContext();
156 Subject subject = Subject.getSubject(acc);
157 Set principals = subject.getPrincipals();
158 Principal principal = (Principal) principals.iterator().next();
159 if (!(principal instanceof JMXPrincipal))
160 throw new SecurityException("Authenticated subject contains " +
161 "invalid principal type = " +
162 principal.getClass().getName());
163 String identity = principal.getName();
164 if (!identity.equals("monitorRole"))
165 throw new SecurityException("Authenticated subject contains " +
166 "invalid principal name = " + identity);
167 }
168
169 /*
170 * -----------------------------------------------------
171 * ATTRIBUTES ACCESSIBLE FOR MANAGEMENT BY A JMX AGENT
172 * -----------------------------------------------------
173 */
174
175 private String state = "initial state";
176 private int nbChanges = 0;
177
178 /*
179 * -----------------------------------------------------
180 * PROPERTY NOT ACCESSIBLE FOR MANAGEMENT BY A JMX AGENT
181 * -----------------------------------------------------
182 */
183
184 private int nbResets = 0;
185}