Path: blob/master/test/jdk/javax/management/security/MBS_Light.java
41152 views
/*1* Copyright (c) 2003, 2015, 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*/2223import java.security.AccessControlContext;24import java.security.AccessController;25import javax.security.auth.Subject;26import java.security.Principal;27import java.util.Iterator;28import java.util.Set;2930import javax.management.MBeanRegistration ;31import javax.management.MBeanServer ;32import javax.management.ObjectName ;33import javax.management.NotificationBroadcasterSupport;34import javax.management.NotificationListener;35import javax.management.Notification;3637public class MBS_Light extends NotificationBroadcasterSupport38implements MBS_LightMBean, MBeanRegistration, NotificationListener39{40private RjmxMBeanParameter param = null ;41private String aString = "notset" ;42private int anInt = 0 ;43private MBeanServer mbs = null ;44private ObjectName objname = null ;45private Exception anException = null ;46private Error anError = null ;47private int count = 0;48private SimpleListener listener = new SimpleListener();4950@SqeDescriptorKey("NO PARAMETER CONSTRUCTOR MBS_Light")51public MBS_Light() {52}5354@SqeDescriptorKey("ONE RjmxMBeanParameter PARAMETER CONSTRUCTOR MBS_Light")55public MBS_Light(@SqeDescriptorKey("CONSTRUCTOR PARAMETER param")56RjmxMBeanParameter param) {57this.param = param ;58}5960@SqeDescriptorKey("ONE String PARAMETER CONSTRUCTOR MBS_Light")61public MBS_Light(@SqeDescriptorKey("CONSTRUCTOR PARAMETER param")String param) {62this.aString = param ;63}6465// Getter for property param66public RjmxMBeanParameter getParam() {67return this.param ;68}6970// Setter for property param71public void setParam(RjmxMBeanParameter param) {72this.param = param ;73}7475// Getter for property aString76public String getAstring() {77return this.aString ;78}7980// Setter for property aString81public void setAstring(String aString) {82this.aString = aString ;83}8485// Getter for property anInt86public int getAnInt() {87return this.anInt ;88}8990// Setter for property anInt91public void setAnInt(int anInt) {92this.anInt = anInt ;93}9495// Getter for property anException96public Exception getAnException() {97return this.anException ;98}99100// Setter for property anException101public void setAnException(Exception anException) {102this.anException = anException ;103}104105// Getter for property anError106public Error getAnError() {107return this.anError ;108}109110// Setter for property anError111public void setAnError(Error anError) {112this.anError = anError ;113}114115// An operation116public RjmxMBeanParameter operate1(String name) {117return new RjmxMBeanParameter(name) ;118}119120// An operation121public String operate2(RjmxMBeanParameter param) {122return param.name ;123}124125// An operation126public void throwError() {127throw new Error("JSR-160-ERROR");128}129130// An operation131public void throwException() throws Exception {132throw new Exception("JSR-160-EXCEPTION");133}134135// MBeanRegistration method136public void postDeregister() {137}138139// MBeanRegistration method140public void postRegister(Boolean registrationDone) {141}142143// MBeanRegistration method144public void preDeregister()145throws Exception146{147}148149// MBeanRegistration method150public ObjectName preRegister(MBeanServer server, ObjectName name)151throws Exception152{153this.mbs = server ;154if ( name == null ) {155this.objname = new ObjectName("protocol:class=MBS_Light") ;156}157else {158this.objname = name ;159}160return this.objname ;161}162163public synchronized void handleNotification(Notification notification,164Object handback) {165Utils.debug(Utils.DEBUG_STANDARD,166"MBS_Light::handleNotification: " + notification);167listener.handleNotification(notification, handback);168}169170// Send a notification171public void sendNotification() {172Notification notification =173new Notification("JSR160-TCK-NOTIFICATION", this, count++);174sendNotification(notification);175}176177public Object waitForNotificationHB() {178return listener.waitForNotificationHB();179}180181// Receive multi notifications and send back handbacks182public synchronized Object[] waitForMultiNotifications(String nb) {183return listener.waitForMultiNotifications(Integer.valueOf(nb).intValue());184}185186// Receive a notification187public synchronized String waitForNotification() {188return listener.waitForNotification();189}190191// Is the notification received192public synchronized Boolean notificationReceived() {193return Boolean.valueOf(listener.isNotificationReceived());194}195196// The authorization Id197public String getAuthorizationId() {198AccessControlContext acc = AccessController.getContext();199Subject subject = Subject.getSubject(acc);200Set<Principal> principals = subject.getPrincipals();201Iterator<Principal> i = principals.iterator();202StringBuffer buffer = new StringBuffer();203while(i.hasNext()) {204Principal p = i.next();205buffer.append(p.getName());206if(i.hasNext())207buffer.append(" ");208}209210return buffer.toString();211}212}213214215