KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > acl > resolver > ResolverBase


1 package de.webman.acl.resolver;
2
3 import java.util.Hashtable JavaDoc;
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 /**
15  * Base class for access right resolvers providing core functionality
16  * for subclasses implementing the <CODE>Resolver</CODE> interface.
17  *
18  * @version 1.0
19  * @since 1.0
20  * @author &copy; 2001 Webman AG
21  */

22 public abstract class ResolverBase
23     implements Resolver
24 {
25
26     // $Header: /cvsroot/webman-cms/source/webman/de/webman/acl/resolver/ResolverBase.java,v 1.1.6.1 2002/05/30 10:23:55 uli Exp $
27

28     // Constants
29

30     /**
31      * Dummy object serving as value for hashtable entries.
32      */

33     private static final Object JavaDoc DUMMY = new Object JavaDoc();
34
35     /**
36      * Empty vector serving as value for cache entries.
37      */

38     private static final TKVector EMPTY = new TKVector(0);
39
40
41     // Attributes
42

43     /**
44      * The login object.
45      */

46     private Login login = null;
47
48     /**
49      * The hash code of the resolver.
50      */

51     private int hash = 0;
52
53     /**
54      * The resolution results cache.
55      */

56     private Hashtable JavaDoc resolutionCache = null;
57
58     /**
59      * The checking results cache.
60      */

61     private Hashtable JavaDoc checkingCache = null;
62
63     /**
64      * The string representation of the resolver.
65      */

66     private String JavaDoc string = null;
67
68
69     // Constructors
70

71     /**
72      * Provide instantion only to package classes or subclasses.
73      *
74      * @param login the initial login object.
75      */

76     protected ResolverBase (Login login)
77     {
78         this.login = login;
79     }
80
81
82     // Standard methods
83

84     /**
85      * Checks wether the resolver and the given object can be treated as equal.
86      *
87      * @param object the object to be proved upon equality.
88      * @return <CODE>true</CODE> if both objects can be treated as equal,
89      * otherwise <CODE>false</CODE>.
90      */

91     public final boolean equals (Object JavaDoc 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     /**
107      * Returns the hash code of the resolver.
108      *
109      * @return the hash code of the resolver.
110      */

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     /**
122      * Returns the string representation of the resolver.
123      *
124      * @return the string representation of the resolver.
125      */

126     public final String JavaDoc toString ()
127     {
128         if (this.string == null)
129         {
130             this.string = (new StringBuffer JavaDoc(getClass().getName())).append('.')
131                                                                   .append(login.getID())
132                                                                   .toString();
133         }
134
135         return this.string;
136     }
137
138
139     // Implementation of 'com.teamkonzept.lib.ConfigurationListener'.
140

141     /**
142      * Informs the resolver about changes in its configuration.
143      *
144      * @exception com.teamkonzept.lib.TKException if an error occured during configuration.
145      */

146     public final void configurationChanged ()
147         throws TKException
148     {
149         // Configuration value initialization.
150
boolean cacheResolution = false;
151         boolean cacheChecking = false;
152
153         // Configuration value loading.
154
try
155         {
156             // Obtain property manager.
157
PropertyManager manager = PropertyManager.getPropertyManager(PROPERTY_GROUP_NAME);
158
159             // Get resolution and checking cache properties.
160
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             // Fall back to default.
166
cacheResolution = Boolean.valueOf(DEFAULT_CACHE_RESOLUTION_RESULTS).booleanValue();
167             cacheChecking = Boolean.valueOf(DEFAULT_CACHE_CHECKING_RESULTS).booleanValue();
168         }
169
170         // Configuration value evaluation.
171
if (cacheResolution && resolutionCache == null)
172         {
173             // Initialize resolution cache.
174
resolutionCache = new Hashtable JavaDoc();
175         }
176
177         if (! cacheResolution && resolutionCache != null)
178         {
179             // Destroy resolution cache.
180
resolutionCache = null;
181         }
182
183         if (Checker.class.isAssignableFrom(this.getClass()))
184         {
185             if (cacheChecking && checkingCache == null)
186             {
187                 // Initialize checking cache.
188
checkingCache = new Hashtable JavaDoc();
189             }
190
191             if (! cacheChecking && checkingCache != null)
192             {
193                 // Destroy checking cache.
194
checkingCache = null;
195             }
196         }
197     }
198
199
200     // Implementation of 'com.teamkonzept.webman.accesscontrol.resolver.Resolver'.
201

202     /**
203      * Assigns the login object of the resolver.
204      *
205      * @param login the login object to be assigned.
206      */

207     public final void setLogin (Login login)
208     {
209         this.login = login;
210     }
211
212     /**
213      * Returns the assigned login object.
214      *
215      * @return the assigned login object.
216      */

217     public final Login getLogin ()
218     {
219         return this.login;
220     }
221
222     /**
223      * This method is provided for the implementation of the actual
224      * access right resolution algorithm, i.e. the calculation of all
225      * allowed events for a login (user or group) in the current application
226      * state. The application state is determined by the context, an object
227      * type and an object reference.
228      * <P>
229      * If the optional arguments are omitted, only the context-wide
230      * access rights are resolved, otherwise the context-wide as well
231      * as the object-specific access rights are resolved.
232      * <P>
233      * The hashed collection is passed recursively to ensure the uniqueness
234      * of the calculated events conveniently.
235      *
236      * @param collection the distinct collection of permitted events.
237      * @param context the ID if the current context (<I>required</I>).
238      * @param type the current object type (<I>optional</I>).
239      * @param reference the current object reference (<I>optional</I>).
240      * @exception com.teamkonzept.lib.TKException if an error occured during
241      * access right resolution.
242      */

243     public abstract void resolve (Hashtable JavaDoc collection,
244                                   Integer JavaDoc context,
245                                   Integer JavaDoc type,
246                                   Integer JavaDoc reference)
247         throws TKException;
248
249
250     // Convenience methods
251

252     /**
253      * Performs access right resolution for all parents of the assigned
254      * login object.
255      *
256      * @param collection the distinct collection of allowed events.
257      * @param context the ID if the current context (<I>required</I>).
258      * @param type the current object type (<I>optional</I>).
259      * @param reference the current object reference (<I>optional</I>).
260      * @exception com.teamkonzept.lib.TKException if an error occured during
261      * access right resolution.
262      */

263     protected final void resolveParents (Hashtable JavaDoc collection,
264                                          Integer JavaDoc context,
265                                          Integer JavaDoc type,
266                                          Integer JavaDoc reference)
267         throws TKException
268     {
269         try
270         {
271             // Get parents.
272
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                     // Call each parent's resolver.
282
ResolverFactory.getInstance()
283                                    .getResolver((Login) parents.elementAt(index++))
284                                    .resolve(collection, context, type, reference);
285                 }
286             }
287         }
288         catch (Exception JavaDoc x)
289         {
290             throw WebmanExceptionHandler.getException(x);
291         }
292     }
293
294     /**
295      * Performs access right checking for all parents of the assigned
296      * login object.
297      *
298      * @param event the ID if the event to be checked (<I>required</I>).
299      * @param context the ID if the current context (<I>required</I>).
300      * @param type the current object type (<I>optional</I>).
301      * @param reference the current object reference (<I>optional</I>).
302      * @exception com.teamkonzept.lib.TKException if an error occured during
303      * access right checking.
304      */

305     protected final boolean checkParents (Integer JavaDoc event,
306                                           Integer JavaDoc context,
307                                           Integer JavaDoc type,
308                                           Integer JavaDoc reference)
309         throws TKException
310     {
311         boolean value = false;
312
313         try
314         {
315             // Get parents.
316
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                     // Call each parent's checker.
327
value = ResolverFactory.getInstance()
328                                            .getChecker((Login) parents.elementAt(index++))
329                                            .check(event, context, type, reference);
330                 }
331             }
332         }
333         catch (Exception JavaDoc x)
334         {
335             throw WebmanExceptionHandler.getException(x);
336         }
337
338         return value;
339     }
340
341     /**
342      * Retrieves and processes the events associated with the given policies.
343      *
344      * @param collection the distinct collection of allowed events.
345      * @param policies the policies.
346      * @exception com.teamkonzept.lib.TKException if an error occured during event retrieval.
347      */

348     protected final void processPolicies (Hashtable JavaDoc 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                     // Get policy object.
362
Policy policy = PolicyFactory.getInstance()
363                                                      .getPolicy((Integer JavaDoc) policies.elementAt(index++));
364
365                     // Get events of policy.
366
processEvents(collection,
367                                   EventFactory.getInstance()
368                                                 .getEventProxies(policy.getID()),
369                                   policy.isAllowed());
370                 }
371             }
372         }
373         catch (Exception JavaDoc x)
374         {
375             throw WebmanExceptionHandler.getException(x);
376         }
377     }
378
379     /**
380      * Depending on the access flag, the given events are added to the
381      * collection or removed respectively.
382      *
383      * @param collection the distinct collection of allowed events.
384      * @param events the events.
385      * @param access the flag indicating wether the events are allowed or denied.
386      * @exception com.teamkonzept.lib.TKException if an error occured during event retrieval.
387      */

388     protected final void processEvents (Hashtable JavaDoc 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                     // Add events.
403
while (index < size)
404                     {
405                         collection.put(events.elementAt(index++), DUMMY);
406                     }
407                 }
408                 else
409                 {
410                     // Remove events.
411
while (index < size)
412                     {
413                         collection.remove(events.elementAt(index++));
414                     }
415                 }
416             }
417         }
418         catch (Exception JavaDoc x)
419         {
420             throw WebmanExceptionHandler.getException(x);
421         }
422     }
423
424
425     // Caching methods
426

427     /**
428      * Writes the given result into the cache.
429      * <P>
430      * The other parameters are used to identify the result.
431      *
432      * @param context the ID of the context.
433      * @param type the object type.
434      * @param reference the object reference.
435      * @param access the access mode.
436      * @param result the result to be cached.
437      */

438     protected final void resolutionCacheWrite (Integer JavaDoc context,
439                                                Integer JavaDoc type,
440                                                Integer JavaDoc reference,
441                                                Boolean JavaDoc 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     /**
454      * Writes the given result into the cache.
455      * <P>
456      * The other parameters are used to identify the result.
457      *
458      * @param event the ID of the event.
459      * @param context the ID of the context.
460      * @param type the object type.
461      * @param reference the object reference.
462      * @param result the result to be cached.
463      */

464     protected final void checkingCacheWrite (Integer JavaDoc event,
465                                              Integer JavaDoc context,
466                                              Integer JavaDoc type,
467                                              Integer JavaDoc reference,
468                                              Boolean JavaDoc 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     /**
480      * Attempts to read a result from the cache.
481      * <P>
482      * The parameters are used to identify the result.
483      *
484      * @param context the ID of the context.
485      * @param type the object type.
486      * @param reference the object reference.
487      * @param access the access mode.
488      * @return the result as found in the cache.
489      */

490     protected final TKVector resolutionCacheRead (Integer JavaDoc context,
491                                                   Integer JavaDoc type,
492                                                   Integer JavaDoc reference,
493                                                   Boolean JavaDoc access)
494     {
495         return resolutionCache != null
496                                ? (TKVector) resolutionCache.get(toKey(null, context, type, reference, access))
497                                : (TKVector) null;
498     }
499
500     /**
501      * Attempts to read a result from the cache.
502      * <P>
503      * The parameters are used to identify the result.
504      *
505      * @param event the ID of the event.
506      * @param context the ID of the context.
507      * @param type the object type.
508      * @param reference the object reference.
509      * @return the result as found in the cache.
510      */

511     protected final Boolean JavaDoc checkingCacheRead (Integer JavaDoc event,
512                                                Integer JavaDoc context,
513                                                Integer JavaDoc type,
514                                                Integer JavaDoc reference)
515     {
516         return checkingCache != null
517                              ? (Boolean JavaDoc) checkingCache.get(toKey(event, context, type, reference, null))
518                              : (Boolean JavaDoc) null;
519     }
520
521     /**
522      * Returns an unique caching key.
523      * <P>
524      * The parameters are used to generate the key.
525      *
526      * @param event the ID of the event.
527      * @param context the ID of the context.
528      * @param type the object type.
529      * @param reference the object reference.
530      * @param access the access mode.
531      * @return an unique caching key.
532      */

533     protected final Object JavaDoc toKey (Integer JavaDoc event,
534                                   Integer JavaDoc context,
535                                   Integer JavaDoc type,
536                                   Integer JavaDoc reference,
537                                   Boolean JavaDoc access)
538     {
539         return new Integer JavaDoc((new StringBuffer JavaDoc()).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