1/* 2 * Copyright (c) 2003, 2015, 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 24import java.security.AccessControlContext; 25import java.security.AccessController; 26import javax.security.auth.Subject; 27import java.security.Principal; 28import java.util.Iterator; 29import java.util.Set; 30 31import javax.management.MBeanRegistration ; 32import javax.management.MBeanServer ; 33import javax.management.ObjectName ; 34import javax.management.NotificationBroadcasterSupport; 35import javax.management.NotificationListener; 36import javax.management.Notification; 37 38public class MBS_Light extends NotificationBroadcasterSupport 39 implements MBS_LightMBean, MBeanRegistration, NotificationListener 40{ 41 private RjmxMBeanParameter param = null ; 42 private String aString = "notset" ; 43 private int anInt = 0 ; 44 private MBeanServer mbs = null ; 45 private ObjectName objname = null ; 46 private Exception anException = null ; 47 private Error anError = null ; 48 private int count = 0; 49 private SimpleListener listener = new SimpleListener(); 50 51 @SqeDescriptorKey("NO PARAMETER CONSTRUCTOR MBS_Light") 52 public MBS_Light() { 53 } 54 55 @SqeDescriptorKey("ONE RjmxMBeanParameter PARAMETER CONSTRUCTOR MBS_Light") 56 public MBS_Light(@SqeDescriptorKey("CONSTRUCTOR PARAMETER param") 57 RjmxMBeanParameter param) { 58 this.param = param ; 59 } 60 61 @SqeDescriptorKey("ONE String PARAMETER CONSTRUCTOR MBS_Light") 62 public MBS_Light(@SqeDescriptorKey("CONSTRUCTOR PARAMETER param")String param) { 63 this.aString = param ; 64 } 65 66 // Getter for property param 67 public RjmxMBeanParameter getParam() { 68 return this.param ; 69 } 70 71 // Setter for property param 72 public void setParam(RjmxMBeanParameter param) { 73 this.param = param ; 74 } 75 76 // Getter for property aString 77 public String getAstring() { 78 return this.aString ; 79 } 80 81 // Setter for property aString 82 public void setAstring(String aString) { 83 this.aString = aString ; 84 } 85 86 // Getter for property anInt 87 public int getAnInt() { 88 return this.anInt ; 89 } 90 91 // Setter for property anInt 92 public void setAnInt(int anInt) { 93 this.anInt = anInt ; 94 } 95 96 // Getter for property anException 97 public Exception getAnException() { 98 return this.anException ; 99 } 100 101 // Setter for property anException 102 public void setAnException(Exception anException) { 103 this.anException = anException ; 104 } 105 106 // Getter for property anError 107 public Error getAnError() { 108 return this.anError ; 109 } 110 111 // Setter for property anError 112 public void setAnError(Error anError) { 113 this.anError = anError ; 114 } 115 116 // An operation 117 public RjmxMBeanParameter operate1(String name) { 118 return new RjmxMBeanParameter(name) ; 119 } 120 121 // An operation 122 public String operate2(RjmxMBeanParameter param) { 123 return param.name ; 124 } 125 126 // An operation 127 public void throwError() { 128 throw new Error("JSR-160-ERROR"); 129 } 130 131 // An operation 132 public void throwException() throws Exception { 133 throw new Exception("JSR-160-EXCEPTION"); 134 } 135 136 // MBeanRegistration method 137 public void postDeregister() { 138 } 139 140 // MBeanRegistration method 141 public void postRegister(Boolean registrationDone) { 142 } 143 144 // MBeanRegistration method 145 public void preDeregister() 146 throws Exception 147 { 148 } 149 150 // MBeanRegistration method 151 public ObjectName preRegister(MBeanServer server, ObjectName name) 152 throws Exception 153 { 154 this.mbs = server ; 155 if ( name == null ) { 156 this.objname = new ObjectName("protocol:class=MBS_Light") ; 157 } 158 else { 159 this.objname = name ; 160 } 161 return this.objname ; 162 } 163 164 public synchronized void handleNotification(Notification notification, 165 Object handback) { 166 Utils.debug(Utils.DEBUG_STANDARD, 167 "MBS_Light::handleNotification: " + notification); 168 listener.handleNotification(notification, handback); 169 } 170 171 // Send a notification 172 public void sendNotification() { 173 Notification notification = 174 new Notification("JSR160-TCK-NOTIFICATION", this, count++); 175 sendNotification(notification); 176 } 177 178 public Object waitForNotificationHB() { 179 return listener.waitForNotificationHB(); 180 } 181 182 // Receive multi notifications and send back handbacks 183 public synchronized Object[] waitForMultiNotifications(String nb) { 184 return listener.waitForMultiNotifications(Integer.valueOf(nb).intValue()); 185 } 186 187 // Receive a notification 188 public synchronized String waitForNotification() { 189 return listener.waitForNotification(); 190 } 191 192 // Is the notification received 193 public synchronized Boolean notificationReceived() { 194 return Boolean.valueOf(listener.isNotificationReceived()); 195 } 196 197 // The authorization Id 198 public String getAuthorizationId() { 199 AccessControlContext acc = AccessController.getContext(); 200 Subject subject = Subject.getSubject(acc); 201 Set<Principal> principals = subject.getPrincipals(); 202 Iterator<Principal> i = principals.iterator(); 203 StringBuffer buffer = new StringBuffer(); 204 while(i.hasNext()) { 205 Principal p = i.next(); 206 buffer.append(p.getName()); 207 if(i.hasNext()) 208 buffer.append(" "); 209 } 210 211 return buffer.toString(); 212 } 213} 214