View Javadoc

1   /*
2    * Copyright 2007-2010 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This is a description of what this class does - jjhanso don't forget to fill this in. 
36   * 
37   * @author Kuali Rice Team (rice.collab@kuali.org)
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();//Collections.synchronizedMap(new HashMap<List<PermissionBo>, MaxAgeSoftReference<List<String>>>());
53  
54      // Not ThreadLocal or time limited- should not change during the life of the system
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  	 * @return the permissionTypeServiceByNameCache
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 }