1 16 17 package org.apache.commons.daemon; 18 19 import java.security.Permission ; 20 import java.util.StringTokenizer ; 21 22 89 public final class DaemonPermission extends Permission { 90 91 92 93 94 98 protected static final String CONTROL = "control"; 99 100 103 protected static final int TYPE_CONTROL = 1; 104 105 109 protected static final String CONTROL_START = "start"; 110 111 115 protected static final String CONTROL_STOP = "stop"; 116 117 121 protected static final String CONTROL_SHUTDOWN = "shutdown"; 122 123 127 protected static final String CONTROL_RELOAD = "reload"; 128 129 133 protected static final int MASK_CONTROL_START = 0x01; 134 135 139 protected static final int MASK_CONTROL_STOP = 0x02; 140 141 145 protected static final int MASK_CONTROL_SHUTDOWN = 0x04; 146 147 151 protected static final int MASK_CONTROL_RELOAD = 0x08; 152 153 157 protected static final String WILDCARD = "*"; 158 159 160 161 162 163 private transient int type = 0; 164 165 private transient int mask = 0; 166 167 private transient String desc = null; 168 169 170 171 172 183 public DaemonPermission (String target) 184 throws IllegalArgumentException { 185 super(target); 187 188 if (target==null) 190 throw new IllegalArgumentException ("Null permission name"); 191 192 if (CONTROL.equalsIgnoreCase(target)) { 194 type=TYPE_CONTROL; 195 return; 196 } 197 198 throw new IllegalArgumentException ("Invalid permission name \""+ 200 target+"\" specified"); 201 } 202 203 215 public DaemonPermission(String target, String actions) 216 throws IllegalArgumentException { 217 this(target); 219 220 if (this.type==TYPE_CONTROL) { 222 this.mask=this.createControlMask(actions); 223 return; 224 } 225 } 226 227 228 229 230 236 public String getActions() { 237 if (this.type==TYPE_CONTROL) { 238 return(this.createControlActions(this.mask)); 239 } 240 return(""); 241 } 242 243 248 public int hashCode() { 249 this.setupDescription(); 250 return(this.desc.hashCode()); 251 } 252 253 259 public boolean equals(Object object) { 260 if (object == this) return(true); 261 262 if (!(object instanceof DaemonPermission)) return false; 263 264 DaemonPermission that = (DaemonPermission)object; 265 266 if (this.type!=that.type) return(false); 267 return(this.mask==that.mask); 268 } 269 270 278 public boolean implies(Permission permission) { 279 if (permission == this) return(true); 280 281 if (!(permission instanceof DaemonPermission)) return false; 282 283 DaemonPermission that = (DaemonPermission)permission; 284 285 if (this.type!=that.type) return(false); 286 return((this.mask&that.mask)==that.mask); 287 } 288 289 295 public String toString() { 296 this.setupDescription(); 297 return(new String (this.desc)); 298 } 299 300 301 302 303 304 private void setupDescription() { 305 if (this.desc!=null) return; 306 307 StringBuffer buf=new StringBuffer (); 308 buf.append(this.getClass().getName()); 309 buf.append('['); 310 switch (this.type) { 311 case (TYPE_CONTROL): { 312 buf.append(CONTROL); 313 break; 314 } 315 default: { 316 buf.append("UNKNOWN"); 317 break; 318 } 319 } 320 buf.append(':'); 321 buf.append(this.getActions()); 322 buf.append(']'); 323 324 this.desc=buf.toString(); 325 } 326 327 328 private int createControlMask(String actions) 329 throws IllegalArgumentException { 330 if (actions==null) return(0); 331 332 int mask=0; 333 StringTokenizer tok=new StringTokenizer (actions,",",false); 334 while (tok.hasMoreTokens()) { 335 String val=tok.nextToken().trim(); 336 337 if (WILDCARD.equals(val)) { 338 return(MASK_CONTROL_START|MASK_CONTROL_STOP| 339 MASK_CONTROL_SHUTDOWN|MASK_CONTROL_RELOAD); 340 } else if (CONTROL_START.equalsIgnoreCase(val)) { 341 mask=mask|MASK_CONTROL_START; 342 } else if (CONTROL_STOP.equalsIgnoreCase(val)) { 343 mask=mask|MASK_CONTROL_STOP; 344 } else if (CONTROL_SHUTDOWN.equalsIgnoreCase(val)) { 345 mask=mask|MASK_CONTROL_SHUTDOWN; 346 } else if (CONTROL_RELOAD.equalsIgnoreCase(val)) { 347 mask=mask|MASK_CONTROL_RELOAD; 348 } else { 349 throw new IllegalArgumentException ("Invalid action name \""+ 350 val+"\" specified"); 351 } 352 } 353 return(mask); 354 } 355 356 357 private String createControlActions(int mask) { 358 StringBuffer buf=new StringBuffer (); 359 boolean sep=false; 360 361 if ((mask&MASK_CONTROL_START)==MASK_CONTROL_START) { 362 sep=true; 363 buf.append(CONTROL_START); 364 } 365 366 if ((mask&MASK_CONTROL_STOP)==MASK_CONTROL_STOP) { 367 if (sep) buf.append(","); 368 else sep=true; 369 buf.append(CONTROL_STOP); 370 } 371 372 if ((mask&MASK_CONTROL_SHUTDOWN)==MASK_CONTROL_SHUTDOWN) { 373 if (sep) buf.append(","); 374 else sep=true; 375 buf.append(CONTROL_SHUTDOWN); 376 } 377 378 if ((mask&MASK_CONTROL_RELOAD)==MASK_CONTROL_RELOAD) { 379 if (sep) buf.append(","); 380 else sep=true; 381 buf.append(CONTROL_RELOAD); 382 } 383 384 return buf.toString(); 385 } 386 } 387 | Popular Tags |