1 7 8 package javax.management; 9 10 import java.io.IOException ; 11 import java.io.ObjectInputStream ; 12 import java.security.BasicPermission ; 13 import java.security.Permission ; 14 import java.security.PermissionCollection ; 15 import java.util.Collections ; 16 import java.util.Enumeration ; 17 import java.util.Set ; 18 import java.util.StringTokenizer ; 19 20 52 public class MBeanServerPermission extends BasicPermission { 53 private static final long serialVersionUID = -5661980843569388590L; 54 55 private final static int 56 CREATE = 0, 57 FIND = 1, 58 NEW = 2, 59 RELEASE = 3, 60 N_NAMES = 4; 61 62 private final static String [] names = { 63 "createMBeanServer", 64 "findMBeanServer", 65 "newMBeanServer", 66 "releaseMBeanServer", 67 }; 68 69 private final static int 70 CREATE_MASK = 1<<CREATE, 71 FIND_MASK = 1<<FIND, 72 NEW_MASK = 1<<NEW, 73 RELEASE_MASK = 1<<RELEASE, 74 ALL_MASK = CREATE_MASK|FIND_MASK|NEW_MASK|RELEASE_MASK; 75 76 84 private final static String [] canonicalNames = new String [1 << N_NAMES]; 85 86 93 transient int mask; 94 95 106 public MBeanServerPermission(String name) { 107 this(name, null); 108 } 109 110 121 public MBeanServerPermission(String name, String actions) { 122 super(getCanonicalName(parseMask(name)), actions); 123 124 129 mask = parseMask(name); 130 131 132 if (actions != null && actions.length() > 0) 133 throw new IllegalArgumentException ("MBeanServerPermission " + 134 "actions must be null: " + 135 actions); 136 } 137 138 MBeanServerPermission(int mask) { 139 super(getCanonicalName(mask)); 140 this.mask = impliedMask(mask); 141 } 142 143 private void readObject(ObjectInputStream in) 144 throws IOException , ClassNotFoundException { 145 in.defaultReadObject(); 146 mask = parseMask(getName()); 147 } 148 149 static int simplifyMask(int mask) { 150 if ((mask & CREATE_MASK) != 0) 151 mask &= ~NEW_MASK; 152 return mask; 153 } 154 155 static int impliedMask(int mask) { 156 if ((mask & CREATE_MASK) != 0) 157 mask |= NEW_MASK; 158 return mask; 159 } 160 161 static String getCanonicalName(int mask) { 162 if (mask == ALL_MASK) 163 return "*"; 164 165 mask = simplifyMask(mask); 166 167 synchronized (canonicalNames) { 168 if (canonicalNames[mask] == null) 169 canonicalNames[mask] = makeCanonicalName(mask); 170 } 171 172 return canonicalNames[mask]; 173 } 174 175 private static String makeCanonicalName(int mask) { 176 StringBuffer buf = new StringBuffer (); 177 for (int i = 0; i < N_NAMES; i++) { 178 if ((mask & (1<<i)) != 0) { 179 if (buf.length() > 0) 180 buf.append(','); 181 buf.append(names[i]); 182 } 183 } 184 return buf.toString().intern(); 185 188 } 189 190 192 private static int parseMask(String name) { 193 194 if (name == null) { 195 throw new NullPointerException ("MBeanServerPermission: " + 196 "target name can't be null"); 197 } 198 199 name = name.trim(); 200 if (name.equals("*")) 201 return ALL_MASK; 202 203 204 if (name.indexOf(',') < 0) 205 return impliedMask(1 << nameIndex(name.trim())); 206 207 int mask = 0; 208 209 StringTokenizer tok = new StringTokenizer (name, ","); 210 while (tok.hasMoreTokens()) { 211 String action = tok.nextToken(); 212 int i = nameIndex(action.trim()); 213 mask |= (1 << i); 214 } 215 216 return impliedMask(mask); 217 } 218 219 private static int nameIndex(String name) 220 throws IllegalArgumentException { 221 for (int i = 0; i < N_NAMES; i++) { 222 if (names[i].equals(name)) 223 return i; 224 } 225 final String msg = 226 "Invalid MBeanServerPermission name: \"" + name + "\""; 227 throw new IllegalArgumentException (msg); 228 } 229 230 public int hashCode() { 231 return mask; 232 } 233 234 253 public boolean implies(Permission p) { 254 if (!(p instanceof MBeanServerPermission )) 255 return false; 256 257 MBeanServerPermission that = (MBeanServerPermission ) p; 258 259 return ((this.mask & that.mask) == that.mask); 260 } 261 262 270 public boolean equals(Object obj) { 271 if (obj == this) 272 return true; 273 274 if (! (obj instanceof MBeanServerPermission )) 275 return false; 276 277 MBeanServerPermission that = (MBeanServerPermission ) obj; 278 279 return (this.mask == that.mask); 280 } 281 282 public PermissionCollection newPermissionCollection() { 283 return new MBeanServerPermissionCollection(); 284 } 285 } 286 287 292 293 311 class MBeanServerPermissionCollection extends PermissionCollection { 312 315 private MBeanServerPermission collectionPermission; 316 317 private static final long serialVersionUID = -5661980843569388590L; 318 319 public synchronized void add(Permission permission) { 320 if (!(permission instanceof MBeanServerPermission )) { 321 final String msg = 322 "Permission not an MBeanServerPermission: " + permission; 323 throw new IllegalArgumentException (msg); 324 } 325 if (isReadOnly()) 326 throw new SecurityException ("Read-only permission collection"); 327 MBeanServerPermission mbsp = (MBeanServerPermission ) permission; 328 if (collectionPermission == null) 329 collectionPermission = mbsp; 330 else if (!collectionPermission.implies(permission)) { 331 int newmask = collectionPermission.mask | mbsp.mask; 332 collectionPermission = new MBeanServerPermission (newmask); 333 } 334 } 335 336 public synchronized boolean implies(Permission permission) { 337 return (collectionPermission != null && 338 collectionPermission.implies(permission)); 339 } 340 341 public synchronized Enumeration elements() { 342 Set set; 343 if (collectionPermission == null) 344 set = Collections.EMPTY_SET; 345 else 346 set = Collections.singleton(collectionPermission); 347 return Collections.enumeration(set); 348 } 349 } 350 | Popular Tags |