This project has retired. For details please refer to its
Attic page.
CachedRbacManager xref
1 package org.apache.archiva.redback.rbac.cached;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.redback.components.cache.Cache;
23 import org.apache.archiva.redback.rbac.Operation;
24 import org.apache.archiva.redback.rbac.Permission;
25 import org.apache.archiva.redback.rbac.RBACManager;
26 import org.apache.archiva.redback.rbac.RBACManagerListener;
27 import org.apache.archiva.redback.rbac.RbacManagerException;
28 import org.apache.archiva.redback.rbac.RbacObjectInvalidException;
29 import org.apache.archiva.redback.rbac.RbacObjectNotFoundException;
30 import org.apache.archiva.redback.rbac.Resource;
31 import org.apache.archiva.redback.rbac.Role;
32 import org.apache.archiva.redback.rbac.UserAssignment;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.stereotype.Service;
36
37 import javax.inject.Inject;
38 import javax.inject.Named;
39 import java.util.Collection;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Set;
43
44
45
46
47
48
49 @Service( "rbacManager#cached" )
50 public class CachedRbacManager
51 implements RBACManager, RBACManagerListener
52 {
53
54 private Logger log = LoggerFactory.getLogger( getClass() );
55
56 @Inject
57 @Named( value = "rbacManager#jdo" )
58 private RBACManager rbacImpl;
59
60 @Inject
61 @Named( value = "cache#operations" )
62 private Cache<String, Operation> operationsCache;
63
64 @Inject
65 @Named( value = "cache#permissions" )
66 private Cache<String, Permission> permissionsCache;
67
68 @Inject
69 @Named( value = "cache#resources" )
70 private Cache<String, Resource> resourcesCache;
71
72 @Inject
73 @Named( value = "cache#roles" )
74 private Cache<String, Role> rolesCache;
75
76 @Inject
77 @Named( value = "cache#userAssignments" )
78 private Cache<String, UserAssignment> userAssignmentsCache;
79
80 @Inject
81 @Named( value = "cache#userPermissions" )
82 private Cache<String, Map<String, List<Permission>>> userPermissionsCache;
83
84 @Inject
85 @Named( value = "cache#effectiveRoleSet" )
86 private Cache<String, Set<Role>> effectiveRoleSetCache;
87
88 public void initialize()
89 {
90
91 }
92
93 public void addChildRole( Role role, Role childRole )
94 throws RbacObjectInvalidException, RbacManagerException
95 {
96 try
97 {
98 this.rbacImpl.addChildRole( role, childRole );
99 }
100 finally
101 {
102 invalidateCachedRole( role );
103 invalidateCachedRole( childRole );
104 }
105 }
106
107 public void addListener( RBACManagerListener listener )
108 {
109 this.rbacImpl.addListener( listener );
110 }
111
112 public Operation createOperation( String name )
113 throws RbacManagerException
114 {
115 operationsCache.remove( name );
116 return this.rbacImpl.createOperation( name );
117 }
118
119 public Permission createPermission( String name )
120 throws RbacManagerException
121 {
122 permissionsCache.remove( name );
123 return this.rbacImpl.createPermission( name );
124 }
125
126 public Permission createPermission( String name, String operationName, String resourceIdentifier )
127 throws RbacManagerException
128 {
129 permissionsCache.remove( name );
130 return this.rbacImpl.createPermission( name, operationName, resourceIdentifier );
131 }
132
133 public Resource createResource( String identifier )
134 throws RbacManagerException
135 {
136 resourcesCache.remove( identifier );
137 return this.rbacImpl.createResource( identifier );
138 }
139
140 public Role createRole( String name )
141 {
142 rolesCache.remove( name );
143 return this.rbacImpl.createRole( name );
144 }
145
146 public UserAssignment createUserAssignment( String principal )
147 throws RbacManagerException
148 {
149 invalidateCachedUserAssignment( principal );
150 return this.rbacImpl.createUserAssignment( principal );
151 }
152
153 public void eraseDatabase()
154 {
155 try
156 {
157 this.rbacImpl.eraseDatabase();
158 }
159 finally
160 {
161
162
163 }
164 }
165
166
167
168
169 public List<Role> getAllAssignableRoles()
170 throws RbacManagerException, RbacObjectNotFoundException
171 {
172 log.debug( "NOT CACHED - .getAllAssignableRoles()" );
173 return this.rbacImpl.getAllAssignableRoles();
174 }
175
176 public List<Operation> getAllOperations()
177 throws RbacManagerException
178 {
179 log.debug( "NOT CACHED - .getAllOperations()" );
180 return this.rbacImpl.getAllOperations();
181 }
182
183 public List<Permission> getAllPermissions()
184 throws RbacManagerException
185 {
186 log.debug( "NOT CACHED - .getAllPermissions()" );
187 return this.rbacImpl.getAllPermissions();
188 }
189
190 public List<Resource> getAllResources()
191 throws RbacManagerException
192 {
193 log.debug( "NOT CACHED - .getAllResources()" );
194 return this.rbacImpl.getAllResources();
195 }
196
197 public List<Role> getAllRoles()
198 throws RbacManagerException
199 {
200 log.debug( "NOT CACHED - .getAllRoles()" );
201 return this.rbacImpl.getAllRoles();
202 }
203
204 public List<UserAssignment> getAllUserAssignments()
205 throws RbacManagerException
206 {
207 log.debug( "NOT CACHED - .getAllUserAssignments()" );
208 return this.rbacImpl.getAllUserAssignments();
209 }
210
211
212
213
214 @SuppressWarnings( "unchecked" )
215 public Map<String, List<Permission>> getAssignedPermissionMap( String principal )
216 throws RbacObjectNotFoundException, RbacManagerException
217 {
218 Map<String, List<Permission>> el = userPermissionsCache.get( principal );
219
220 if ( el != null )
221 {
222 log.debug( "using cached user permission map" );
223 return el;
224 }
225
226 log.debug( "building user permission map" );
227 Map<String, List<Permission>> userPermMap = this.rbacImpl.getAssignedPermissionMap( principal );
228 userPermissionsCache.put( principal, userPermMap );
229 return userPermMap;
230
231 }
232
233 public Set<Permission> getAssignedPermissions( String principal )
234 throws RbacObjectNotFoundException, RbacManagerException
235 {
236 log.debug( "NOT CACHED - .getAssignedPermissions(String)" );
237 return this.rbacImpl.getAssignedPermissions( principal );
238 }
239
240 public Collection<Role> getAssignedRoles( String principal )
241 throws RbacObjectNotFoundException, RbacManagerException
242 {
243 log.debug( "NOT CACHED - .getAssignedRoles(String)" );
244 return this.rbacImpl.getAssignedRoles( principal );
245 }
246
247 public Collection<Role> getAssignedRoles( UserAssignment userAssignment )
248 throws RbacObjectNotFoundException, RbacManagerException
249 {
250 log.debug( "NOT CACHED - .getAssignedRoles(UserAssignment)" );
251 return this.rbacImpl.getAssignedRoles( userAssignment );
252 }
253
254 public Map<String, Role> getChildRoles( Role role )
255 throws RbacManagerException
256 {
257 log.debug( "NOT CACHED - .getChildRoles(Role)" );
258 return this.rbacImpl.getChildRoles( role );
259 }
260
261 public Map<String, Role> getParentRoles( Role role )
262 throws RbacManagerException
263 {
264 log.debug( "NOT CACHED - .getParentRoles(Role)" );
265 return this.rbacImpl.getParentRoles( role );
266 }
267
268 public Collection<Role> getEffectivelyAssignedRoles( String principal )
269 throws RbacObjectNotFoundException, RbacManagerException
270 {
271 log.debug( "NOT CACHED - .getEffectivelyAssignedRoles(String)" );
272 return this.rbacImpl.getEffectivelyAssignedRoles( principal );
273 }
274
275 public Collection<Role> getEffectivelyUnassignedRoles( String principal )
276 throws RbacManagerException, RbacObjectNotFoundException
277 {
278 log.debug( "NOT CACHED - .getEffectivelyUnassignedRoles(String)" );
279 return this.rbacImpl.getEffectivelyUnassignedRoles( principal );
280 }
281
282 @SuppressWarnings( "unchecked" )
283 public Set<Role> getEffectiveRoles( Role role )
284 throws RbacObjectNotFoundException, RbacManagerException
285 {
286 Set<Role> el = effectiveRoleSetCache.get( role.getName() );
287
288 if ( el != null )
289 {
290 log.debug( "using cached effective role set" );
291 return el;
292 }
293 else
294 {
295 log.debug( "building effective role set" );
296 Set<Role> effectiveRoleSet = this.rbacImpl.getEffectiveRoles( role );
297 effectiveRoleSetCache.put( role.getName(), effectiveRoleSet );
298 return effectiveRoleSet;
299 }
300 }
301
302 public Resource getGlobalResource()
303 throws RbacManagerException
304 {
305
306 log.debug( "NOT CACHED - .getGlobalResource()" );
307 return this.rbacImpl.getGlobalResource();
308 }
309
310 public Operation getOperation( String operationName )
311 throws RbacObjectNotFoundException, RbacManagerException
312 {
313 Operation el = operationsCache.get( operationName );
314 if ( el != null )
315 {
316 return el;
317 }
318 else
319 {
320 Operation operation = this.rbacImpl.getOperation( operationName );
321 operationsCache.put( operationName, operation );
322 return operation;
323 }
324 }
325
326 public Permission getPermission( String permissionName )
327 throws RbacObjectNotFoundException, RbacManagerException
328 {
329 Permission el = permissionsCache.get( permissionName );
330 if ( el != null )
331 {
332 return el;
333 }
334 else
335 {
336 Permission permission = this.rbacImpl.getPermission( permissionName );
337 permissionsCache.put( permissionName, permission );
338 return permission;
339 }
340 }
341
342 public Resource getResource( String resourceIdentifier )
343 throws RbacObjectNotFoundException, RbacManagerException
344 {
345 Resource el = resourcesCache.get( resourceIdentifier );
346 if ( el != null )
347 {
348 return el;
349 }
350 else
351 {
352 Resource resource = this.rbacImpl.getResource( resourceIdentifier );
353 resourcesCache.put( resourceIdentifier, resource );
354 return resource;
355 }
356 }
357
358 public Role getRole( String roleName )
359 throws RbacObjectNotFoundException, RbacManagerException
360 {
361 Role el = rolesCache.get( roleName );
362 if ( el != null )
363 {
364 return el;
365 }
366 else
367 {
368 Role role = this.rbacImpl.getRole( roleName );
369 rolesCache.put( roleName, role );
370 return role;
371 }
372 }
373
374 public Map<String, Role> getRoles( Collection<String> roleNames )
375 throws RbacObjectNotFoundException, RbacManagerException
376 {
377 log.debug( "NOT CACHED - .getRoles(Collection)" );
378 return this.rbacImpl.getRoles( roleNames );
379 }
380
381 public Collection<Role> getUnassignedRoles( String principal )
382 throws RbacManagerException, RbacObjectNotFoundException
383 {
384 log.debug( "NOT CACHED - .getUnassignedRoles(String)" );
385 return this.rbacImpl.getUnassignedRoles( principal );
386 }
387
388 public UserAssignment getUserAssignment( String principal )
389 throws RbacObjectNotFoundException, RbacManagerException
390 {
391 UserAssignment el = userAssignmentsCache.get( principal );
392 if ( el != null )
393 {
394 return el;
395 }
396 else
397 {
398 UserAssignment userAssignment = this.rbacImpl.getUserAssignment( principal );
399 userAssignmentsCache.put( principal, userAssignment );
400 return userAssignment;
401 }
402 }
403
404 public List<UserAssignment> getUserAssignmentsForRoles( Collection<String> roleNames )
405 throws RbacManagerException
406 {
407 log.debug( "NOT CACHED - .getUserAssignmentsForRoles(Collection)" );
408 return this.rbacImpl.getUserAssignmentsForRoles( roleNames );
409 }
410
411 public boolean operationExists( Operation operation )
412 {
413 if ( operation == null )
414 {
415 return false;
416 }
417
418 if ( operationsCache.hasKey( operation.getName() ) )
419 {
420 return true;
421 }
422
423 return this.rbacImpl.operationExists( operation );
424 }
425
426 public boolean operationExists( String name )
427 {
428 if ( operationsCache.hasKey( name ) )
429 {
430 return true;
431 }
432
433 return this.rbacImpl.operationExists( name );
434 }
435
436 public boolean permissionExists( Permission permission )
437 {
438 if ( permission == null )
439 {
440 return false;
441 }
442
443 if ( permissionsCache.hasKey( permission.getName() ) )
444 {
445 return true;
446 }
447
448 return this.rbacImpl.permissionExists( permission );
449 }
450
451 public boolean permissionExists( String name )
452 {
453 if ( permissionsCache.hasKey( name ) )
454 {
455 return true;
456 }
457
458 return this.rbacImpl.permissionExists( name );
459 }
460
461 public void rbacInit( boolean freshdb )
462 {
463 if ( rbacImpl instanceof RBACManagerListener )
464 {
465 ( (RBACManagerListener) this.rbacImpl ).rbacInit( freshdb );
466 }
467
468 this.resourcesCache.clear();
469 this.operationsCache.clear();
470 this.permissionsCache.clear();
471 this.rolesCache.clear();
472 this.userAssignmentsCache.clear();
473 this.userPermissionsCache.clear();
474 }
475
476 public void rbacPermissionRemoved( Permission permission )
477 {
478 if ( rbacImpl instanceof RBACManagerListener )
479 {
480 ( (RBACManagerListener) this.rbacImpl ).rbacPermissionRemoved( permission );
481 }
482
483 invalidateCachedPermission( permission );
484 }
485
486 public void rbacPermissionSaved( Permission permission )
487 {
488 if ( rbacImpl instanceof RBACManagerListener )
489 {
490 ( (RBACManagerListener) this.rbacImpl ).rbacPermissionSaved( permission );
491 }
492
493 invalidateCachedPermission( permission );
494 }
495
496 public void rbacRoleRemoved( Role role )
497 {
498 if ( rbacImpl instanceof RBACManagerListener )
499 {
500 ( (RBACManagerListener) this.rbacImpl ).rbacRoleRemoved( role );
501 }
502
503 invalidateCachedRole( role );
504 }
505
506 public void rbacRoleSaved( Role role )
507 {
508 if ( rbacImpl instanceof RBACManagerListener )
509 {
510 ( (RBACManagerListener) this.rbacImpl ).rbacRoleSaved( role );
511 }
512
513 invalidateCachedRole( role );
514 }
515
516 public void rbacUserAssignmentRemoved( UserAssignment userAssignment )
517 {
518 if ( rbacImpl instanceof RBACManagerListener )
519 {
520 ( (RBACManagerListener) this.rbacImpl ).rbacUserAssignmentRemoved( userAssignment );
521 }
522
523 invalidateCachedUserAssignment( userAssignment );
524 }
525
526 public void rbacUserAssignmentSaved( UserAssignment userAssignment )
527 {
528 if ( rbacImpl instanceof RBACManagerListener )
529 {
530 ( (RBACManagerListener) this.rbacImpl ).rbacUserAssignmentSaved( userAssignment );
531 }
532
533 invalidateCachedUserAssignment( userAssignment );
534 }
535
536 public void removeListener( RBACManagerListener listener )
537 {
538 this.rbacImpl.removeListener( listener );
539 }
540
541 public void removeOperation( Operation operation )
542 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
543 {
544 invalidateCachedOperation( operation );
545 this.rbacImpl.removeOperation( operation );
546 }
547
548 public void removeOperation( String operationName )
549 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
550 {
551 operationsCache.remove( operationName );
552 this.rbacImpl.removeOperation( operationName );
553 }
554
555 public void removePermission( Permission permission )
556 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
557 {
558 invalidateCachedPermission( permission );
559 this.rbacImpl.removePermission( permission );
560 }
561
562 public void removePermission( String permissionName )
563 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
564 {
565 permissionsCache.remove( permissionName );
566 this.rbacImpl.removePermission( permissionName );
567 }
568
569 public void removeResource( Resource resource )
570 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
571 {
572 invalidateCachedResource( resource );
573 this.rbacImpl.removeResource( resource );
574 }
575
576 public void removeResource( String resourceIdentifier )
577 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
578 {
579 resourcesCache.remove( resourceIdentifier );
580 this.rbacImpl.removeResource( resourceIdentifier );
581 }
582
583 public void removeRole( Role role )
584 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
585 {
586 invalidateCachedRole( role );
587 this.rbacImpl.removeRole( role );
588 }
589
590 public void removeRole( String roleName )
591 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
592 {
593 rolesCache.remove( roleName );
594 this.rbacImpl.removeRole( roleName );
595 }
596
597 public void removeUserAssignment( String principal )
598 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
599 {
600 invalidateCachedUserAssignment( principal );
601 this.rbacImpl.removeUserAssignment( principal );
602 }
603
604 public void removeUserAssignment( UserAssignment userAssignment )
605 throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
606 {
607 invalidateCachedUserAssignment( userAssignment );
608 this.rbacImpl.removeUserAssignment( userAssignment );
609 }
610
611 public boolean resourceExists( Resource resource )
612 {
613 if ( resourcesCache.hasKey( resource.getIdentifier() ) )
614 {
615 return true;
616 }
617
618 return this.rbacImpl.resourceExists( resource );
619 }
620
621 public boolean resourceExists( String identifier )
622 {
623 if ( resourcesCache.hasKey( identifier ) )
624 {
625 return true;
626 }
627
628 return this.rbacImpl.resourceExists( identifier );
629 }
630
631 public boolean roleExists( Role role )
632 throws RbacManagerException
633 {
634 if ( rolesCache.hasKey( role.getName() ) )
635 {
636 return true;
637 }
638
639 return this.rbacImpl.roleExists( role );
640 }
641
642 public boolean roleExists( String name )
643 throws RbacManagerException
644 {
645 if ( rolesCache.hasKey( name ) )
646 {
647 return true;
648 }
649
650 return this.rbacImpl.roleExists( name );
651 }
652
653 public Operation saveOperation( Operation operation )
654 throws RbacObjectInvalidException, RbacManagerException
655 {
656 invalidateCachedOperation( operation );
657 return this.rbacImpl.saveOperation( operation );
658 }
659
660 public Permission savePermission( Permission permission )
661 throws RbacObjectInvalidException, RbacManagerException
662 {
663 invalidateCachedPermission( permission );
664 return this.rbacImpl.savePermission( permission );
665 }
666
667 public Resource saveResource( Resource resource )
668 throws RbacObjectInvalidException, RbacManagerException
669 {
670 invalidateCachedResource( resource );
671 return this.rbacImpl.saveResource( resource );
672 }
673
674 public synchronized Role saveRole( Role role )
675 throws RbacObjectInvalidException, RbacManagerException
676 {
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695 invalidateAllCachedUserAssignments();
696 invalidateCachedRole( role );
697 return this.rbacImpl.saveRole( role );
698 }
699
700 public synchronized void saveRoles( Collection<Role> roles )
701 throws RbacObjectInvalidException, RbacManagerException
702 {
703
704 for ( Role role : roles )
705 {
706 invalidateCachedRole( role );
707 }
708
709
710
711
712
713
714
715
716
717
718 invalidateAllCachedUserAssignments();
719 this.rbacImpl.saveRoles( roles );
720 }
721
722 public UserAssignment saveUserAssignment( UserAssignment userAssignment )
723 throws RbacObjectInvalidException, RbacManagerException
724 {
725 invalidateCachedUserAssignment( userAssignment );
726 return this.rbacImpl.saveUserAssignment( userAssignment );
727 }
728
729 public boolean userAssignmentExists( String principal )
730 {
731 if ( userAssignmentsCache.hasKey( principal ) )
732 {
733 return true;
734 }
735
736 return this.rbacImpl.userAssignmentExists( principal );
737 }
738
739 public boolean userAssignmentExists( UserAssignment assignment )
740 {
741 if ( userAssignmentsCache.hasKey( assignment.getPrincipal() ) )
742 {
743 return true;
744 }
745
746 return this.rbacImpl.userAssignmentExists( assignment );
747 }
748
749 private void invalidateCachedRole( Role role )
750 {
751 if ( role != null )
752 {
753 rolesCache.remove( role.getName() );
754
755
756 effectiveRoleSetCache.clear();
757 }
758
759 }
760
761 private void invalidateCachedOperation( Operation operation )
762 {
763 if ( operation != null )
764 {
765 operationsCache.remove( operation.getName() );
766 }
767 }
768
769 private void invalidateCachedPermission( Permission permission )
770 {
771 if ( permission != null )
772 {
773 permissionsCache.remove( permission.getName() );
774 }
775 }
776
777 private void invalidateCachedResource( Resource resource )
778 {
779 if ( resource != null )
780 {
781 resourcesCache.remove( resource.getIdentifier() );
782 }
783 }
784
785 private void invalidateCachedUserAssignment( UserAssignment userAssignment )
786 {
787 if ( userAssignment != null )
788 {
789 userAssignmentsCache.remove( userAssignment.getPrincipal() );
790 userPermissionsCache.remove( userAssignment.getPrincipal() );
791 }
792 }
793
794 private void invalidateCachedUserAssignment( String principal )
795 {
796 userAssignmentsCache.remove( principal );
797 userPermissionsCache.remove( principal );
798 }
799
800 private void invalidateAllCachedUserAssignments()
801 {
802 userAssignmentsCache.clear();
803 userPermissionsCache.clear();
804 }
805
806 public Cache getOperationsCache()
807 {
808 return operationsCache;
809 }
810
811 public void setOperationsCache( Cache operationsCache )
812 {
813 this.operationsCache = operationsCache;
814 }
815
816 public Cache getPermissionsCache()
817 {
818 return permissionsCache;
819 }
820
821 public void setPermissionsCache( Cache permissionsCache )
822 {
823 this.permissionsCache = permissionsCache;
824 }
825
826 public Cache getResourcesCache()
827 {
828 return resourcesCache;
829 }
830
831 public void setResourcesCache( Cache resourcesCache )
832 {
833 this.resourcesCache = resourcesCache;
834 }
835
836 public Cache getRolesCache()
837 {
838 return rolesCache;
839 }
840
841 public void setRolesCache( Cache rolesCache )
842 {
843 this.rolesCache = rolesCache;
844 }
845
846 public Cache getUserAssignmentsCache()
847 {
848 return userAssignmentsCache;
849 }
850
851 public void setUserAssignmentsCache( Cache userAssignmentsCache )
852 {
853 this.userAssignmentsCache = userAssignmentsCache;
854 }
855
856 public Cache getUserPermissionsCache()
857 {
858 return userPermissionsCache;
859 }
860
861 public void setUserPermissionsCache( Cache userPermissionsCache )
862 {
863 this.userPermissionsCache = userPermissionsCache;
864 }
865
866 public Cache getEffectiveRoleSetCache()
867 {
868 return effectiveRoleSetCache;
869 }
870
871 public void setEffectiveRoleSetCache( Cache effectiveRoleSetCache )
872 {
873 this.effectiveRoleSetCache = effectiveRoleSetCache;
874 }
875
876 public RBACManager getRbacImpl()
877 {
878 return rbacImpl;
879 }
880
881 public void setRbacImpl( RBACManager rbacImpl )
882 {
883 this.rbacImpl = rbacImpl;
884 }
885
886
887 public boolean isFinalImplementation()
888 {
889 return false;
890 }
891
892 public String getDescriptionKey()
893 {
894 return "archiva.redback.rbacmanager.cached";
895 }
896
897 public boolean isReadOnly()
898 {
899 return false;
900 }
901 }