Path: blob/master/test/jdk/javax/management/remote/mandatory/subjectDelegation/SimpleStandard.java
41159 views
/*1* Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* Simple definition of a standard MBean, named "SimpleStandard".25*26* The "SimpleStandard" standard MBean shows how to expose attributes and27* operations for management by implementing its corresponding28* "SimpleStandardMBean" management interface.29*30* This MBean has two attributes and one operation exposed31* for management by a JMX agent:32* - the read/write "State" attribute,33* - the read only "NbChanges" attribute,34* - the "reset()" operation.35*36* This object also has one property and one method not exposed37* for management by a JMX agent:38* - the "NbResets" property,39* - the "getNbResets()" method.40*/4142import java.security.AccessControlContext;43import java.security.AccessController;44import java.security.Principal;45import java.util.Set;46import javax.management.AttributeChangeNotification;47import javax.management.NotificationBroadcasterSupport;48import javax.management.remote.JMXPrincipal;49import javax.security.auth.Subject;5051public class SimpleStandard52extends NotificationBroadcasterSupport53implements SimpleStandardMBean {5455/*56* -----------------------------------------------------57* CONSTRUCTORS58* -----------------------------------------------------59*/6061public SimpleStandard(String principalName) {62this.principalName = principalName;63}6465/*66* -----------------------------------------------------67* IMPLEMENTATION OF THE SimpleStandardMBean INTERFACE68* -----------------------------------------------------69*/7071/**72* Getter: get the "State" attribute of the "SimpleStandard" standard MBean.73*74* @return the current value of the "State" attribute.75*/76public String getState() {77checkSubject("getState");78return state;79}8081/**82* Setter: set the "State" attribute of the "SimpleStandard" standard MBean.83*84* @param <VAR>s</VAR> the new value of the "State" attribute.85*/86public void setState(String s) {87checkSubject("setState");88state = s;89nbChanges++;90}9192/**93* Getter: get the "NbChanges" attribute of the "SimpleStandard" standard94* MBean.95*96* @return the current value of the "NbChanges" attribute.97*/98public int getNbChanges() {99checkSubject("getNbChanges");100return nbChanges;101}102103/**104* Operation: reset to their initial values the "State" and "NbChanges"105* attributes of the "SimpleStandard" standard MBean.106*/107public void reset() {108checkSubject("reset");109AttributeChangeNotification acn =110new AttributeChangeNotification(this,1110,1120,113"NbChanges reset",114"NbChanges",115"Integer",116new Integer(nbChanges),117new Integer(0));118state = "initial state";119nbChanges = 0;120nbResets++;121sendNotification(acn);122}123124/*125* -----------------------------------------------------126* METHOD NOT EXPOSED FOR MANAGEMENT BY A JMX AGENT127* -----------------------------------------------------128*/129130/**131* Return the "NbResets" property.132* This method is not a Getter in the JMX sense because133* it is not exposed in the "SimpleStandardMBean" interface.134*135* @return the current value of the "NbResets" property.136*/137public int getNbResets() {138return nbResets;139}140141/*142* ---------------143* PRIVATE METHODS144* ---------------145*/146147/**148* Check that the principal contained in the Subject is of149* type JMXPrincipal and refers to the principalName identity.150*/151private void checkSubject(String op) {152AccessControlContext acc = AccessController.getContext();153Subject subject = Subject.getSubject(acc);154Set principals = subject.getPrincipals();155Principal principal = (Principal) principals.iterator().next();156if (!(principal instanceof JMXPrincipal))157throw new SecurityException(op+": Authenticated subject contains " +158"invalid principal type = " +159principal.getClass().getName());160String identity = principal.getName();161if (!identity.equals(principalName))162throw new SecurityException(op+": Authenticated subject contains " +163"invalid principal name = " + identity);164}165166/*167* -----------------------------------------------------168* ATTRIBUTES ACCESSIBLE FOR MANAGEMENT BY A JMX AGENT169* -----------------------------------------------------170*/171172private String state = "initial state";173private int nbChanges = 0;174175/*176* -----------------------------------------------------177* PROPERTY NOT ACCESSIBLE FOR MANAGEMENT BY A JMX AGENT178* -----------------------------------------------------179*/180181private int nbResets = 0;182private String principalName;183}184185186