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 89 public class WrapperEventPermission 90 extends Permission 91 { 92 public static String EVENT_TYPE_SERVICE = "service"; 93 public static String EVENT_TYPE_CONTROL = "control"; 94 public static String EVENT_TYPE_CORE = "core"; 95 96 private static int MASK_SERVICE = 1; 97 private static int MASK_CONTROL = 2; 98 private static int MASK_CORE = 65536; 99 private static int MASK_ALL = MASK_SERVICE | MASK_CONTROL | MASK_CORE; 100 101 private int m_eventTypeMask; 102 103 106 111 public WrapperEventPermission( String eventTypes ) 112 { 113 super( "*" ); 114 m_eventTypeMask = buildEventTypeMask( eventTypes ); 115 } 116 117 120 130 public boolean equals( Object obj ) 131 { 132 if ( obj == this ) 133 { 134 return true; 135 } 136 137 if ( !( obj instanceof WrapperEventPermission ) ) 138 { 139 return false; 140 } 141 142 WrapperEventPermission wsp = (WrapperEventPermission)obj; 143 144 return ( m_eventTypeMask == wsp.m_eventTypeMask ) && 145 getName().equals( wsp.getName() ); 146 } 147 148 155 public String getActions() 156 { 157 StringBuffer sb = new StringBuffer (); 158 boolean first = true; 159 160 if ( ( m_eventTypeMask & MASK_SERVICE ) != 0 ) 161 { 162 if ( first ) 163 { 164 sb.append( ',' ); 165 } 166 else 167 { 168 first = false; 169 } 170 sb.append( EVENT_TYPE_SERVICE ); 171 } 172 if ( ( m_eventTypeMask & MASK_CONTROL ) != 0 ) 173 { 174 if ( first ) 175 { 176 sb.append( ',' ); 177 } 178 else 179 { 180 first = false; 181 } 182 sb.append( EVENT_TYPE_CONTROL ); 183 } 184 if ( ( m_eventTypeMask & MASK_CORE ) != 0 ) 185 { 186 if ( first ) 187 { 188 sb.append( ',' ); 189 } 190 else 191 { 192 first = false; 193 } 194 sb.append( EVENT_TYPE_CORE ); 195 } 196 197 return sb.toString(); 198 } 199 200 217 public boolean implies( Permission p2 ) 218 { 219 if ( !( p2 instanceof WrapperEventPermission ) ) 220 { 221 return false; 222 } 223 224 WrapperEventPermission wsp = (WrapperEventPermission)p2; 225 226 229 return ( ( m_eventTypeMask & wsp.m_eventTypeMask ) == wsp.m_eventTypeMask ) && 230 impliesIgnoreEventTypeMask( wsp ); 231 } 232 233 236 public PermissionCollection newPermissionCollection() 237 { 238 return new WECollection(); 239 } 240 241 246 public int hashCode() 247 { 248 return getName().hashCode(); 249 } 250 251 254 257 int getActionMask() 258 { 259 return m_eventTypeMask; 260 } 261 262 266 boolean impliesIgnoreEventTypeMask( WrapperEventPermission p2 ) 267 { 268 if ( getName().equals( p2.getName() ) ) 269 { 270 return true; 271 } 272 273 if ( p2.getName().endsWith( "*" ) ) 274 { 275 if ( getName().startsWith( p2.getName().substring( 0, p2.getName().length() - 1 ) ) ) 276 { 277 return true; 278 } 279 } 280 return false; 281 } 282 283 286 private int buildEventTypeMask( String eventTypes ) 287 { 288 if ( eventTypes == EVENT_TYPE_SERVICE ) 290 { 291 return MASK_SERVICE; 292 } 293 else if ( eventTypes == EVENT_TYPE_CONTROL ) 294 { 295 return MASK_CONTROL; 296 } 297 else if ( eventTypes == EVENT_TYPE_CORE ) 298 { 299 return MASK_CORE; 300 } 301 else if ( eventTypes.equals( "*" ) ) 302 { 303 return MASK_ALL; 304 } 305 306 int mask = 0; 307 StringTokenizer st = new StringTokenizer ( eventTypes, "," ); 308 while ( st.hasMoreTokens() ) 309 { 310 String eventType = st.nextToken(); 311 if ( eventType.equals( EVENT_TYPE_SERVICE ) ) 312 { 313 mask |= MASK_SERVICE; 314 } 315 else if ( eventType.equals( EVENT_TYPE_CONTROL ) ) 316 { 317 mask |= MASK_CONTROL; 318 } 319 else if ( eventType.equals( EVENT_TYPE_CORE ) ) 320 { 321 mask |= MASK_CORE; 322 } 323 else 324 { 325 throw new IllegalArgumentException ( 326 "Invalid permission eventType: \"" + eventType + "\"" ); 327 } 328 } 329 330 return mask; 331 } 332 } 333 334 final class WECollection 335 extends PermissionCollection 336 { 337 private Vector m_permissions = new Vector (); 338 339 342 345 public WECollection() 346 { 347 } 348 349 352 364 public void add( Permission permission ) 365 { 366 if ( !( permission instanceof WrapperEventPermission ) ) 367 { 368 throw new IllegalArgumentException ( "invalid permission: " + permission ); 369 } 370 371 if ( isReadOnly() ) 372 { 373 throw new SecurityException ( "Collection is read-only."); 374 } 375 376 m_permissions.add( permission ); 377 } 378 379 388 public boolean implies( Permission permission ) 389 { 390 if ( !( permission instanceof WrapperEventPermission ) ) 391 { 392 return false; 393 } 394 395 WrapperEventPermission wsp = (WrapperEventPermission)permission; 396 397 int desiredMask = wsp.getActionMask(); 398 int pendingMask = desiredMask; 399 int foundMask = 0; 400 401 for ( Enumeration en = m_permissions.elements(); en.hasMoreElements(); ) 402 { 403 WrapperEventPermission p2 = 404 (WrapperEventPermission)en.nextElement(); 405 if ( ( pendingMask & p2.getActionMask() ) != 0 ) 406 { 407 if ( wsp.impliesIgnoreEventTypeMask( p2 ) ) 409 { 410 foundMask |= desiredMask & p2.getActionMask(); 411 if ( foundMask == desiredMask ) 412 { 413 return true; 414 } 415 pendingMask = desiredMask ^ foundMask; 416 } 417 } 418 } 419 420 return false; 421 } 422 423 430 public Enumeration elements() 431 { 432 return m_permissions.elements(); 433 } 434 } 435 | Popular Tags |