1 package org.tanukisoftware.wrapper.security; 2 3 27 28 import java.security.Permission ; 29 import java.security.PermissionCollection ; 30 import java.util.Enumeration ; 31 import java.util.Vector ; 32 import java.util.StringTokenizer ; 33 34 122 public class WrapperServicePermission 123 extends Permission 124 { 125 public static String ACTION_START = "start"; 126 public static String ACTION_STOP = "stop"; 127 public static String ACTION_PAUSE = "pause"; 128 public static String ACTION_CONTINUE = "continue"; 129 public static String ACTION_INTERROGATE = "interrogate"; 130 public static String ACTION_USER_CODE = "userCode"; 131 132 private static int MASK_START = 1; 133 private static int MASK_STOP = 2; 134 private static int MASK_PAUSE = 4; 135 private static int MASK_CONTINUE = 8; 136 private static int MASK_INTERROGATE = 16; 137 private static int MASK_USER_CODE = 32; 138 private static int MASK_ALL = 139 MASK_START | MASK_STOP | MASK_PAUSE | MASK_CONTINUE | MASK_INTERROGATE | MASK_USER_CODE; 140 141 private int m_actionMask; 142 143 146 153 public WrapperServicePermission( String serviceName, String actions ) 154 { 155 super( serviceName ); 156 m_actionMask = buildActionMask( actions ); 157 } 158 159 166 public WrapperServicePermission( String serviceName ) 167 { 168 this( serviceName, "*" ); 169 } 170 171 174 184 public boolean equals( Object obj ) 185 { 186 if ( obj == this ) 187 { 188 return true; 189 } 190 191 if ( !( obj instanceof WrapperServicePermission ) ) 192 { 193 return false; 194 } 195 196 WrapperServicePermission wsp = (WrapperServicePermission)obj; 197 198 return ( m_actionMask == wsp.m_actionMask ) && 199 getName().equals( wsp.getName() ); 200 } 201 202 209 public String getActions() 210 { 211 StringBuffer sb = new StringBuffer (); 212 boolean first = true; 213 214 if ( ( m_actionMask & MASK_START ) != 0 ) 215 { 216 if ( first ) 217 { 218 sb.append( ',' ); 219 } 220 else 221 { 222 first = false; 223 } 224 sb.append( ACTION_START ); 225 } 226 if ( ( m_actionMask & MASK_STOP ) != 0 ) 227 { 228 if ( first ) 229 { 230 sb.append( ',' ); 231 } 232 else 233 { 234 first = false; 235 } 236 sb.append( ACTION_STOP ); 237 } 238 if ( ( m_actionMask & MASK_PAUSE ) != 0 ) 239 { 240 if ( first ) 241 { 242 sb.append( ',' ); 243 } 244 else 245 { 246 first = false; 247 } 248 sb.append( ACTION_CONTINUE ); 249 } 250 if ( ( m_actionMask & MASK_CONTINUE ) != 0 ) 251 { 252 if ( first ) 253 { 254 sb.append( ',' ); 255 } 256 else 257 { 258 first = false; 259 } 260 sb.append( ACTION_CONTINUE ); 261 } 262 if ( ( m_actionMask & MASK_INTERROGATE ) != 0 ) 263 { 264 if ( first ) 265 { 266 sb.append( ',' ); 267 } 268 else 269 { 270 first = false; 271 } 272 sb.append( ACTION_INTERROGATE ); 273 } 274 if ( ( m_actionMask & MASK_USER_CODE ) != 0 ) 275 { 276 if ( first ) 277 { 278 sb.append( ',' ); 279 } 280 else 281 { 282 first = false; 283 } 284 sb.append( ACTION_USER_CODE ); 285 } 286 287 return sb.toString(); 288 } 289 290 307 public boolean implies( Permission p2 ) 308 { 309 if ( !( p2 instanceof WrapperServicePermission ) ) 310 { 311 return false; 312 } 313 314 WrapperServicePermission wsp = (WrapperServicePermission)p2; 315 316 319 return ( ( m_actionMask & wsp.m_actionMask ) == wsp.m_actionMask ) && 320 impliesIgnoreActionMask( wsp ); 321 } 322 323 326 public PermissionCollection newPermissionCollection() 327 { 328 return new WSCollection(); 329 } 330 331 336 public int hashCode() 337 { 338 return getName().hashCode(); 339 } 340 341 344 347 int getActionMask() 348 { 349 return m_actionMask; 350 } 351 352 356 boolean impliesIgnoreActionMask( WrapperServicePermission p2 ) 357 { 358 if ( getName().equals( p2.getName() ) ) 359 { 360 return true; 361 } 362 363 if ( p2.getName().endsWith( "*" ) ) 364 { 365 if ( getName().startsWith( p2.getName().substring( 0, p2.getName().length() - 1 ) ) ) 366 { 367 return true; 368 } 369 } 370 return false; 371 } 372 373 376 private int buildActionMask( String actions ) 377 { 378 if ( actions == ACTION_START ) 380 { 381 return MASK_START; 382 } 383 else if ( actions == ACTION_STOP ) 384 { 385 return MASK_STOP; 386 } 387 else if ( actions == ACTION_PAUSE ) 388 { 389 return MASK_PAUSE; 390 } 391 else if ( actions == ACTION_CONTINUE ) 392 { 393 return MASK_CONTINUE; 394 } 395 else if ( actions == ACTION_INTERROGATE ) 396 { 397 return MASK_INTERROGATE; 398 } 399 else if ( actions == ACTION_USER_CODE ) 400 { 401 return MASK_USER_CODE; 402 } 403 else if ( actions.equals( "*" ) ) 404 { 405 return MASK_ALL; 406 } 407 408 int mask = 0; 409 StringTokenizer st = new StringTokenizer ( actions, "," ); 410 while ( st.hasMoreTokens() ) 411 { 412 String action = st.nextToken(); 413 if ( action.equals( ACTION_START ) ) 414 { 415 mask |= MASK_START; 416 } 417 else if ( action.equals( ACTION_STOP ) ) 418 { 419 mask |= MASK_STOP; 420 } 421 else if ( action.equals( ACTION_PAUSE ) ) 422 { 423 mask |= MASK_PAUSE; 424 } 425 else if ( action.equals( ACTION_CONTINUE ) ) 426 { 427 mask |= MASK_CONTINUE; 428 } 429 else if ( action.equals( ACTION_INTERROGATE ) ) 430 { 431 mask |= MASK_INTERROGATE; 432 } 433 else if ( action.equals( ACTION_USER_CODE ) ) 434 { 435 mask |= MASK_USER_CODE; 436 } 437 else 438 { 439 throw new IllegalArgumentException ( 440 "Invalid permission action: \"" + action + "\"" ); 441 } 442 } 443 444 return mask; 445 } 446 } 447 448 final class WSCollection 449 extends PermissionCollection 450 { 451 private Vector m_permissions = new Vector (); 452 453 456 459 public WSCollection() 460 { 461 } 462 463 466 478 public void add( Permission permission ) 479 { 480 if ( !( permission instanceof WrapperServicePermission ) ) 481 { 482 throw new IllegalArgumentException ( "invalid permission: " + permission ); 483 } 484 485 if ( isReadOnly() ) 486 { 487 throw new SecurityException ( "Collection is read-only."); 488 } 489 490 m_permissions.add( permission ); 491 } 492 493 502 public boolean implies( Permission permission ) 503 { 504 if ( !( permission instanceof WrapperServicePermission ) ) 505 { 506 return false; 507 } 508 509 WrapperServicePermission wsp = (WrapperServicePermission)permission; 510 511 int desiredMask = wsp.getActionMask(); 512 int pendingMask = desiredMask; 513 int foundMask = 0; 514 515 for ( Enumeration en = m_permissions.elements(); en.hasMoreElements(); ) 516 { 517 WrapperServicePermission p2 = 518 (WrapperServicePermission)en.nextElement(); 519 if ( ( pendingMask & p2.getActionMask() ) != 0 ) 520 { 521 if ( wsp.impliesIgnoreActionMask( p2 ) ) 523 { 524 foundMask |= desiredMask & p2.getActionMask(); 525 if ( foundMask == desiredMask ) 526 { 527 return true; 528 } 529 pendingMask = desiredMask ^ foundMask; 530 } 531 } 532 } 533 534 return false; 535 } 536 537 544 public Enumeration elements() 545 { 546 return m_permissions.elements(); 547 } 548 } 549 | Popular Tags |