1 package de.webman.acl.resolver; 2 3 import java.util.Hashtable ; 4 import com.teamkonzept.lib.PropertyManager; 5 import com.teamkonzept.lib.TKConfigurationException; 6 import com.teamkonzept.lib.TKException; 7 import com.teamkonzept.lib.TKVector; 8 import de.webman.acl.EventFactory; 9 import de.webman.acl.Login; 10 import de.webman.acl.Policy; 11 import de.webman.acl.PolicyFactory; 12 import com.teamkonzept.webman.mainint.WebmanExceptionHandler; 13 14 22 public abstract class ResolverBase 23 implements Resolver 24 { 25 26 28 30 33 private static final Object DUMMY = new Object (); 34 35 38 private static final TKVector EMPTY = new TKVector(0); 39 40 41 43 46 private Login login = null; 47 48 51 private int hash = 0; 52 53 56 private Hashtable resolutionCache = null; 57 58 61 private Hashtable checkingCache = null; 62 63 66 private String string = null; 67 68 69 71 76 protected ResolverBase (Login login) 77 { 78 this.login = login; 79 } 80 81 82 84 91 public final boolean equals (Object object) 92 { 93 if (object == null) 94 { 95 return false; 96 } 97 98 if (object.getClass() != getClass()) 99 { 100 return false; 101 } 102 103 return ((ResolverBase) object).login.getID().equals(this.login.getID()); 104 } 105 106 111 public final int hashCode () 112 { 113 if (this.hash == 0) 114 { 115 this.hash = this.login.getID().intValue(); 116 } 117 118 return this.hash; 119 } 120 121 126 public final String toString () 127 { 128 if (this.string == null) 129 { 130 this.string = (new StringBuffer (getClass().getName())).append('.') 131 .append(login.getID()) 132 .toString(); 133 } 134 135 return this.string; 136 } 137 138 139 141 146 public final void configurationChanged () 147 throws TKException 148 { 149 boolean cacheResolution = false; 151 boolean cacheChecking = false; 152 153 try 155 { 156 PropertyManager manager = PropertyManager.getPropertyManager(PROPERTY_GROUP_NAME); 158 159 cacheResolution = Boolean.valueOf(manager.getValue(PROPERTY_CACHE_RESOLUTION_RESULTS, DEFAULT_CACHE_RESOLUTION_RESULTS)).booleanValue(); 161 cacheChecking = Boolean.valueOf(manager.getValue(PROPERTY_CACHE_CHECKING_RESULTS, DEFAULT_CACHE_CHECKING_RESULTS)).booleanValue(); 162 } 163 catch (TKConfigurationException tkce) 164 { 165 cacheResolution = Boolean.valueOf(DEFAULT_CACHE_RESOLUTION_RESULTS).booleanValue(); 167 cacheChecking = Boolean.valueOf(DEFAULT_CACHE_CHECKING_RESULTS).booleanValue(); 168 } 169 170 if (cacheResolution && resolutionCache == null) 172 { 173 resolutionCache = new Hashtable (); 175 } 176 177 if (! cacheResolution && resolutionCache != null) 178 { 179 resolutionCache = null; 181 } 182 183 if (Checker.class.isAssignableFrom(this.getClass())) 184 { 185 if (cacheChecking && checkingCache == null) 186 { 187 checkingCache = new Hashtable (); 189 } 190 191 if (! cacheChecking && checkingCache != null) 192 { 193 checkingCache = null; 195 } 196 } 197 } 198 199 200 202 207 public final void setLogin (Login login) 208 { 209 this.login = login; 210 } 211 212 217 public final Login getLogin () 218 { 219 return this.login; 220 } 221 222 243 public abstract void resolve (Hashtable collection, 244 Integer context, 245 Integer type, 246 Integer reference) 247 throws TKException; 248 249 250 252 263 protected final void resolveParents (Hashtable collection, 264 Integer context, 265 Integer type, 266 Integer reference) 267 throws TKException 268 { 269 try 270 { 271 TKVector parents = this.login.getParents(); 273 274 if (parents != null) 275 { 276 int index = 0; 277 int size = parents.size(); 278 279 while (index < size) 280 { 281 ResolverFactory.getInstance() 283 .getResolver((Login) parents.elementAt(index++)) 284 .resolve(collection, context, type, reference); 285 } 286 } 287 } 288 catch (Exception x) 289 { 290 throw WebmanExceptionHandler.getException(x); 291 } 292 } 293 294 305 protected final boolean checkParents (Integer event, 306 Integer context, 307 Integer type, 308 Integer reference) 309 throws TKException 310 { 311 boolean value = false; 312 313 try 314 { 315 TKVector parents = this.login.getParents(); 317 318 if (parents != null) 319 { 320 int index = 0; 321 int size = parents.size(); 322 323 while (index < size && 324 ! value) 325 { 326 value = ResolverFactory.getInstance() 328 .getChecker((Login) parents.elementAt(index++)) 329 .check(event, context, type, reference); 330 } 331 } 332 } 333 catch (Exception x) 334 { 335 throw WebmanExceptionHandler.getException(x); 336 } 337 338 return value; 339 } 340 341 348 protected final void processPolicies (Hashtable collection, 349 TKVector policies) 350 throws TKException 351 { 352 try 353 { 354 if (policies != null) 355 { 356 int index = 0; 357 int size = policies.size(); 358 359 while (index < size) 360 { 361 Policy policy = PolicyFactory.getInstance() 363 .getPolicy((Integer ) policies.elementAt(index++)); 364 365 processEvents(collection, 367 EventFactory.getInstance() 368 .getEventProxies(policy.getID()), 369 policy.isAllowed()); 370 } 371 } 372 } 373 catch (Exception x) 374 { 375 throw WebmanExceptionHandler.getException(x); 376 } 377 } 378 379 388 protected final void processEvents (Hashtable collection, 389 TKVector events, 390 boolean access) 391 throws TKException 392 { 393 try 394 { 395 if (events != null) 396 { 397 int index = 0; 398 int size = events.size(); 399 400 if (access) 401 { 402 while (index < size) 404 { 405 collection.put(events.elementAt(index++), DUMMY); 406 } 407 } 408 else 409 { 410 while (index < size) 412 { 413 collection.remove(events.elementAt(index++)); 414 } 415 } 416 } 417 } 418 catch (Exception x) 419 { 420 throw WebmanExceptionHandler.getException(x); 421 } 422 } 423 424 425 427 438 protected final void resolutionCacheWrite (Integer context, 439 Integer type, 440 Integer reference, 441 Boolean access, 442 TKVector result) 443 { 444 if (resolutionCache != null) 445 { 446 resolutionCache.put(toKey(null, context, type, reference, access), 447 result != null 448 ? result 449 : EMPTY); 450 } 451 } 452 453 464 protected final void checkingCacheWrite (Integer event, 465 Integer context, 466 Integer type, 467 Integer reference, 468 Boolean result) 469 { 470 if (checkingCache != null) 471 { 472 checkingCache.put(toKey(event, context, type, reference, null), 473 result != null 474 ? result 475 : Boolean.FALSE); 476 } 477 } 478 479 490 protected final TKVector resolutionCacheRead (Integer context, 491 Integer type, 492 Integer reference, 493 Boolean access) 494 { 495 return resolutionCache != null 496 ? (TKVector) resolutionCache.get(toKey(null, context, type, reference, access)) 497 : (TKVector) null; 498 } 499 500 511 protected final Boolean checkingCacheRead (Integer event, 512 Integer context, 513 Integer type, 514 Integer reference) 515 { 516 return checkingCache != null 517 ? (Boolean ) checkingCache.get(toKey(event, context, type, reference, null)) 518 : (Boolean ) null; 519 } 520 521 533 protected final Object toKey (Integer event, 534 Integer context, 535 Integer type, 536 Integer reference, 537 Boolean access) 538 { 539 return new Integer ((new StringBuffer ()).append(event) 540 .append('.') 541 .append(context) 542 .append('.') 543 .append(type) 544 .append('.') 545 .append(reference) 546 .append('.') 547 .append(access) 548 .toString() 549 .hashCode()); 550 } 551 552 } 553 | Popular Tags |