View Javadoc
1 /* ========================================================================= * 2 * * 3 * The Apache Software License, Version 1.1 * 4 * * 5 * Copyright (c) 2001 The Apache Software Foundation. * 6 * All rights reserved. * 7 * * 8 * ========================================================================= * 9 * * 10 * Redistribution and use in source and binary forms, with or without modi- * 11 * fication, are permitted provided that the following conditions are met: * 12 * * 13 * 1. Redistributions of source code must retain the above copyright notice * 14 * notice, this list of conditions and the following disclaimer. * 15 * * 16 * 2. Redistributions in binary form must reproduce the above copyright * 17 * notice, this list of conditions and the following disclaimer in the * 18 * documentation and/or other materials provided with the distribution. * 19 * * 20 * 3. The end-user documentation included with the redistribution, if any, * 21 * must include the following acknowlegement: * 22 * * 23 * "This product includes software developed by the Apache Software * 24 * Foundation <http://www.apache.org/>;." * 25 * * 26 * Alternately, this acknowlegement may appear in the software itself, if * 27 * and wherever such third-party acknowlegements normally appear. * 28 * * 29 * 4. The names "The Jakarta Project", and "Apache Software Foundation" * 30 * must not be used to endorse or promote products derived from this * 31 * software without prior written permission. For written permission, * 32 * please contact <apache@apache.org>. * 33 * * 34 * 5. Products derived from this software may not be called "Apache" nor may * 35 * "Apache" appear in their names without prior written permission of the * 36 * Apache Software Foundation. * 37 * * 38 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES * 39 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * 40 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * 41 * THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY * 42 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * 43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * 44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * 45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * 46 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * 47 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * 48 * POSSIBILITY OF SUCH DAMAGE. * 49 * * 50 * ========================================================================= * 51 * * 52 * This software consists of voluntary contributions made by many indivi- * 53 * duals on behalf of the Apache Software Foundation. For more information * 54 * on the Apache Software Foundation, please see <http://www.apache.org/>;. * 55 * * 56 * ========================================================================= */ 57 58 package org.apache.commons.daemon; 59 60 import java.security.Permission; 61 import java.util.StringTokenizer; 62 63 /*** 64 * This class represents the permissions to control and query the status of 65 * a <code>Daemon</code>. A <code>DaemonPermission</code> consists of a 66 * target name and a list of actions associated with it. 67 * <p> 68 * In this specification version the only available target name for this 69 * permission is "control", but further releases may add more target 70 * names to fine-tune the access that needs to be granted to the caller. 71 * </p> 72 * <p> 73 * Actions are defined by a string of comma-separated values, as shown in the 74 * table below. The empty string implies no permission at all, while the 75 * special "*" value implies all permissions for the given 76 * name: 77 * </p> 78 * <p> 79 * <table width="100%" border="1"> 80 * <tr> 81 * <th>Target"Name</th> 82 * <th>Action</th> 83 * <th>Description</th> 84 * </tr> 85 * <tr> 86 * <td rowspan="5">"control"</td> 87 * <td>"start"</td> 88 * <td> 89 * The permission to call the <code>start()</code> method in an instance 90 * of a <code>DaemonController</code> interface. 91 * </td> 92 * </tr> 93 * <tr> 94 * <td>"stop"</td> 95 * <td> 96 * The permission to call the <code>stop()</code> method in an instance 97 * of a <code>DaemonController</code> interface. 98 * </td> 99 * </tr> 100 * <tr> 101 * <td>"shutdown"</td> 102 * <td> 103 * The permission to call the <code>shutdown()</code> method in an instance 104 * of a <code>DaemonController</code> interface. 105 * </td> 106 * </tr> 107 * <tr> 108 * <td>"reload"</td> 109 * <td> 110 * The permission to call the <code>reload()</code> method in an instance 111 * of a <code>DaemonController</code> interface. 112 * </td> 113 * </tr> 114 * <tr> 115 * <td>"*"</td> 116 * <td> 117 * The special wildcard action implies all above-mentioned action. This is 118 * equal to construct a permission with the "start, stop, shutdown, 119 * reload" list of actions. 120 * </td> 121 * </tr> 122 * </table> 123 * </p> 124 * 125 * @author <a href="mailto:pier.fumagalli@sun.com">Pier Fumagalli</a> 126 * @author Copyright © 2000-2001 <a href="http://www.apache.org/">The 127 * Apache Software Foundation</a>. All rights reserved. 128 * @version 1.0 <i>(CVS $Revision: 1.1.1.1 $)</i> 129 */ 130 public final class DaemonPermission extends Permission { 131 132 /* ==================================================================== */ 133 /* Constants. */ 134 135 /*** 136 * The target name when associated with control actions 137 * ("control"). 138 */ 139 protected static final String CONTROL = "control"; 140 141 /*** 142 * The target type when associated with control actions. 143 */ 144 protected static final int TYPE_CONTROL = 1; 145 146 /*** 147 * The action name associated with the permission to call the 148 * <code>DaemonController.start()</code> method. 149 */ 150 protected static final String CONTROL_START = "start"; 151 152 /*** 153 * The action name associated with the permission to call the 154 * <code>DaemonController.stop()</code> method. 155 */ 156 protected static final String CONTROL_STOP = "stop"; 157 158 /*** 159 * The action name associated with the permission to call the 160 * <code>DaemonController.shutdown()</code> method. 161 */ 162 protected static final String CONTROL_SHUTDOWN = "shutdown"; 163 164 /*** 165 * The action name associated with the permission to call the 166 * <code>DaemonController.reload()</code> method. 167 */ 168 protected static final String CONTROL_RELOAD = "reload"; 169 170 /*** 171 * The action mask associated with the permission to call the 172 * <code>DaemonController.start()</code> method. 173 */ 174 protected static final int MASK_CONTROL_START = 0x01; 175 176 /*** 177 * The action mask associated with the permission to call the 178 * <code>DaemonController.stop()</code> method. 179 */ 180 protected static final int MASK_CONTROL_STOP = 0x02; 181 182 /*** 183 * The action mask associated with the permission to call the 184 * <code>DaemonController.shutdown()</code> method. 185 */ 186 protected static final int MASK_CONTROL_SHUTDOWN = 0x04; 187 188 /*** 189 * The action mask associated with the permission to call the 190 * <code>DaemonController.reload()</code> method. 191 */ 192 protected static final int MASK_CONTROL_RELOAD = 0x08; 193 194 /*** 195 * The "wildcard" action implying all actions for the given 196 * target name. 197 */ 198 protected static final String WILDCARD = "*"; 199 200 /* ==================================================================== */ 201 /* Instance variables */ 202 203 /*** The type of this permission object. */ 204 private transient int type = 0; 205 /*** The permission mask associated with this permission object. */ 206 private transient int mask = 0; 207 /*** The String representation of this permission object. */ 208 private transient String desc = null; 209 210 /* ==================================================================== */ 211 /* Constructors */ 212 213 /*** 214 * Create a new <code>DaemonPermission</code> instance with a specified 215 * permission name. 216 * <p> 217 * This constructor will create a new <code>DaemonPermission</code> 218 * instance that <b>will not</b> grant any permission to the caller. 219 * 220 * @param target The target name of this permission. 221 * @exception IllegalArgumentException If the specified target name is not 222 * supported. 223 */ 224 public DaemonPermission (String target) 225 throws IllegalArgumentException { 226 // Setup the target name of this permission object. 227 super(target); 228 229 // Check if the permission target name was specified 230 if (target==null) 231 throw new IllegalArgumentException("Null permission name"); 232 233 // Check if this is a "control" permission and set up accordingly. 234 if (CONTROL.equalsIgnoreCase(target)) { 235 type=TYPE_CONTROL; 236 return; 237 } 238 239 // If we got here, we have an invalid permission name. 240 throw new IllegalArgumentException("Invalid permission name \""+ 241 target+"\" specified"); 242 } 243 244 /*** 245 * Create a new <code>DaemonPermission</code> instance with a specified 246 * permission name and a specified list of actions. 247 * <p> 248 * </p> 249 * 250 * @param target The target name of this permission. 251 * @param actions The list of actions permitted by this permission. 252 * @exception IllegalArgumentException If the specified target name is not 253 * supported, or the specified list of actions includes an 254 * invalid value. 255 */ 256 public DaemonPermission(String target, String actions) 257 throws IllegalArgumentException { 258 // Setup this instance's target name. 259 this(target); 260 261 // Create the appropriate mask if this is a control permission. 262 if (this.type==TYPE_CONTROL) { 263 this.mask=this.createControlMask(actions); 264 return; 265 } 266 } 267 268 /* ==================================================================== */ 269 /* Public methods */ 270 271 /*** 272 * Return the list of actions permitted by this instance of 273 * <code>DaemonPermission</code> in its canonical form. 274 * 275 * @return The canonicalized list of actions. 276 */ 277 public String getActions() { 278 if (this.type==TYPE_CONTROL) { 279 return(this.createControlActions(this.mask)); 280 } 281 return(""); 282 } 283 284 /*** 285 * Return the hash code for this <code>DaemonPermission</code> instance. 286 * 287 * @return An hash code value. 288 */ 289 public int hashCode() { 290 this.setupDescription(); 291 return(this.desc.hashCode()); 292 } 293 294 /*** 295 * Check if a specified object equals <code>DaemonPermission</code>. 296 * 297 * @return <b>true</b> or <b>false</b> wether the specified object equals 298 * this <code>DaemonPermission</code> instance or not. 299 */ 300 public boolean equals(Object object) { 301 if (object == this) return(true); 302 303 if (!(object instanceof DaemonPermission)) return false; 304 305 DaemonPermission that = (DaemonPermission)object; 306 307 if (this.type!=that.type) return(false); 308 return(this.mask==that.mask); 309 } 310 311 /*** 312 * Check if this <code>DaemonPermission</code> implies another 313 * <code>Permission</code>. 314 * 315 * @return <b>true</b> or <b>false</b> wether the specified permission 316 * is implied by this <code>DaemonPermission</code> instance or 317 * not. 318 */ 319 public boolean implies(Permission permission) { 320 if (permission == this) return(true); 321 322 if (!(permission instanceof DaemonPermission)) return false; 323 324 DaemonPermission that = (DaemonPermission)permission; 325 326 if (this.type!=that.type) return(false); 327 return((this.mask&that.mask)==that.mask); 328 } 329 330 /*** 331 * Return a <code>String</code> representation of this instance. 332 * 333 * @return A <code>String</code> representing this 334 * <code>DaemonPermission</code> instance. 335 */ 336 public String toString() { 337 this.setupDescription(); 338 return(new String(this.desc)); 339 } 340 341 /* ==================================================================== */ 342 /* Private methods */ 343 344 /*** Create a String description for this permission instance. */ 345 private void setupDescription() { 346 if (this.desc!=null) return; 347 348 StringBuffer buf=new StringBuffer(); 349 buf.append(this.getClass().getName()); 350 buf.append('['); 351 switch (this.type) { 352 case (TYPE_CONTROL): { 353 buf.append(CONTROL); 354 break; 355 } 356 default: { 357 buf.append("UNKNOWN"); 358 break; 359 } 360 } 361 buf.append(':'); 362 buf.append(this.getActions()); 363 buf.append(']'); 364 365 this.desc=buf.toString(); 366 } 367 368 /*** Create a permission mask for a given control actions string. */ 369 private int createControlMask(String actions) 370 throws IllegalArgumentException { 371 if (actions==null) return(0); 372 373 int mask=0; 374 StringTokenizer tok=new StringTokenizer(actions,",",false); 375 while (tok.hasMoreTokens()) { 376 String val=tok.nextToken().trim(); 377 378 if (WILDCARD.equals(val)) { 379 return(MASK_CONTROL_START|MASK_CONTROL_STOP| 380 MASK_CONTROL_SHUTDOWN|MASK_CONTROL_RELOAD); 381 } else if (CONTROL_START.equalsIgnoreCase(val)) { 382 mask=mask|MASK_CONTROL_START; 383 } else if (CONTROL_STOP.equalsIgnoreCase(val)) { 384 mask=mask|MASK_CONTROL_STOP; 385 } else if (CONTROL_SHUTDOWN.equalsIgnoreCase(val)) { 386 mask=mask|MASK_CONTROL_SHUTDOWN; 387 } else if (CONTROL_RELOAD.equalsIgnoreCase(val)) { 388 mask=mask|MASK_CONTROL_RELOAD; 389 } else { 390 throw new IllegalArgumentException("Invalid action name \""+ 391 val+"\" specified"); 392 } 393 } 394 return(mask); 395 } 396 397 /*** Create a actions list for a given control permission mask. */ 398 private String createControlActions(int mask) { 399 StringBuffer buf=new StringBuffer(); 400 boolean sep=false; 401 402 if ((mask&MASK_CONTROL_START)==MASK_CONTROL_START) { 403 sep=true; 404 buf.append(CONTROL_START); 405 } 406 407 if ((mask&MASK_CONTROL_STOP)==MASK_CONTROL_STOP) { 408 if (sep) buf.append(","); 409 else sep=true; 410 buf.append(CONTROL_STOP); 411 } 412 413 if ((mask&MASK_CONTROL_SHUTDOWN)==MASK_CONTROL_SHUTDOWN) { 414 if (sep) buf.append(","); 415 else sep=true; 416 buf.append(CONTROL_SHUTDOWN); 417 } 418 419 if ((mask&MASK_CONTROL_RELOAD)==MASK_CONTROL_RELOAD) { 420 if (sep) buf.append(","); 421 else sep=true; 422 buf.append(CONTROL_RELOAD); 423 } 424 425 return buf.toString(); 426 } 427 }

This page automatically generated by Maven