1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.service.impl;
17
18 import com.google.common.collect.MapMaker;
19 import org.kuali.rice.kim.impl.permission.PermissionBo;
20 import org.kuali.rice.kim.impl.role.RoleMemberAttributeDataBo;
21 import org.kuali.rice.kim.service.support.KimPermissionTypeService;
22 import org.kuali.rice.kim.util.KimConstants;
23 import org.kuali.rice.krad.service.BusinessObjectService;
24 import org.kuali.rice.krad.service.KRADServiceLocator;
25 import org.kuali.rice.krad.service.SequenceAccessorService;
26 import org.kuali.rice.ksb.api.KsbApiServiceLocator;
27 import org.kuali.rice.ksb.api.cache.RiceCacheAdministrator;
28
29 import java.util.List;
30 import java.util.Map;
31 import java.util.concurrent.ConcurrentMap;
32 import java.util.concurrent.TimeUnit;
33
34
35
36
37
38
39
40 public class PermissionServiceBase {
41 protected static final String PERMISSION_IMPL_CACHE_PREFIX = "PermissionImpl-Template-";
42 protected static final String PERMISSION_IMPL_NAME_CACHE_PREFIX = "PermissionImpl-Name-";
43 protected static final String PERMISSION_IMPL_ID_CACHE_PREFIX = "PermissionImpl-Id-";
44 protected static final String PERMISSION_IMPL_CACHE_GROUP = "PermissionImpl";
45
46 protected static final String DEFAULT_PERMISSION_TYPE_SERVICE = "defaultPermissionTypeService";
47
48 private BusinessObjectService businessObjectService;
49 private SequenceAccessorService sequenceAccessorService;
50 private RiceCacheAdministrator cacheAdministrator;
51
52 private ConcurrentMap<List<PermissionBo>, List<String>> permissionToRoleCache = new MapMaker().expireAfterAccess(CACHE_MAX_AGE_SECONDS, TimeUnit.SECONDS).softValues().makeMap();
53
54
55 private ConcurrentMap<String,KimPermissionTypeService> permissionTypeServiceByNameCache = new MapMaker().makeMap();
56
57
58 private static final long CACHE_MAX_AGE_SECONDS = 60L;
59
60 protected BusinessObjectService getBusinessObjectService() {
61 if ( businessObjectService == null ) {
62 businessObjectService = KRADServiceLocator.getBusinessObjectService();
63 }
64 return businessObjectService;
65 }
66
67 public void flushPermissionImplCache() {
68 getCacheAdministrator().flushGroup(PERMISSION_IMPL_CACHE_GROUP);
69 }
70
71
72
73
74 protected Map<String, KimPermissionTypeService> getPermissionTypeServiceByNameCache() {
75 return this.permissionTypeServiceByNameCache;
76 }
77
78 protected void addRolesForPermissionsToCache( List<PermissionBo> key, List<String> roleIds ) {
79 permissionToRoleCache.put(key, roleIds);
80 }
81
82 protected List<String> getRolesForPermissionsFromCache( List<PermissionBo> key ) {
83 return permissionToRoleCache.get( key );
84 }
85
86 protected String getPermissionImplByTemplateNameCacheKey( String namespaceCode, String permissionTemplateName ) {
87 return PERMISSION_IMPL_CACHE_PREFIX + namespaceCode + "-" + permissionTemplateName;
88 }
89 protected String getPermissionImplByNameCacheKey( String namespaceCode, String permissionName ) {
90 return PERMISSION_IMPL_NAME_CACHE_PREFIX + namespaceCode + "-" + permissionName;
91 }
92 protected String getPermissionImplByIdCacheKey( String permissionId ) {
93 return PERMISSION_IMPL_ID_CACHE_PREFIX + permissionId;
94 }
95
96 protected String getNewAttributeDataId(){
97 SequenceAccessorService sas = getSequenceAccessorService();
98 Long nextSeq = sas.getNextAvailableSequenceNumber(
99 KimConstants.SequenceNames.KRIM_ATTR_DATA_ID_S,
100 RoleMemberAttributeDataBo.class );
101 return nextSeq.toString();
102 }
103
104 protected SequenceAccessorService getSequenceAccessorService() {
105 if ( sequenceAccessorService == null ) {
106 sequenceAccessorService = KRADServiceLocator.getSequenceAccessorService();
107 }
108 return sequenceAccessorService;
109 }
110
111 protected RiceCacheAdministrator getCacheAdministrator() {
112 if ( cacheAdministrator == null ) {
113 cacheAdministrator = KsbApiServiceLocator.getCacheAdministrator();
114 }
115 return cacheAdministrator;
116 }
117 }