001/* 002 * Copyright 2021 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.core.impl.user.directory; 017 018import java.util.Arrays; 019import java.util.Collection; 020import java.util.Map; 021 022import org.apache.avalon.framework.activity.Disposable; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.avalon.framework.service.Serviceable; 026 027import org.ametys.core.cache.AbstractCacheManager; 028import org.ametys.core.cache.Cache; 029import org.ametys.core.user.User; 030import org.ametys.core.user.directory.UserDirectory; 031import org.ametys.core.util.Cacheable; 032import org.ametys.runtime.i18n.I18nizableText; 033import org.ametys.runtime.i18n.I18nizableTextParameter; 034import org.ametys.runtime.plugin.component.AbstractLogEnabled; 035 036/** 037 * Abstract class providing caching features to {@link UserDirectory}. 038 */ 039public abstract class AbstractCachingUserDirectory extends AbstractLogEnabled implements UserDirectory, Serviceable, Cacheable, Disposable 040{ 041 private String _id; 042 private String _label; 043 private String _udModelId; 044 private Map<String, Object> _paramValues; 045 private String _populationId; 046 047 // Cannot use _populationId + "#" + _id as two UserDirectories with same id can co-exist during a short amount of time (during UserPopulationDAO#_readPopulations) 048 private final String _uniqueCacheSuffix = org.ametys.core.util.StringUtils.generateKey(); 049 050 private AbstractCacheManager _cacheManager; 051 052 @Override 053 public void service(ServiceManager manager) throws ServiceException 054 { 055 _cacheManager = (AbstractCacheManager) manager.lookup(AbstractCacheManager.ROLE); 056 } 057 058 public String getId() 059 { 060 return _id; 061 } 062 063 public String getLabel() 064 { 065 return _label; 066 } 067 068 @Override 069 public void setPopulationId(String populationId) 070 { 071 _populationId = populationId; 072 } 073 074 @Override 075 public String getPopulationId() 076 { 077 return _populationId; 078 } 079 080 @Override 081 public Map<String, Object> getParameterValues() 082 { 083 return _paramValues; 084 } 085 086 @Override 087 public String getUserDirectoryModelId() 088 { 089 return _udModelId; 090 } 091 092 public void init(String id, String udModelId, Map<String, Object> paramValues, String label) throws Exception 093 { 094 _id = id; 095 _udModelId = udModelId; 096 _label = label; 097 _paramValues = paramValues; 098 } 099 100 @Override 101 public Collection<SingleCacheConfiguration> getManagedCaches() 102 { 103 return Arrays.asList( 104 SingleCacheConfiguration.of( 105 getClass().getName() + "$by.login$" + _uniqueCacheSuffix, 106 _buildI18n("PLUGINS_CORE_USERS_CACHE_BY_LOGIN_LABEL"), 107 _buildI18n("PLUGINS_CORE_USERS_CACHE_BY_LOGIN_DESC")), 108 SingleCacheConfiguration.of( 109 getClass().getName() + "$by.mail$" + _uniqueCacheSuffix, 110 _buildI18n("PLUGINS_CORE_USERS_CACHE_BY_MAIL_LABEL"), 111 _buildI18n("PLUGINS_CORE_USERS_CACHE_BY_MAIL_DESC")) 112 ); 113 } 114 115 private I18nizableText _buildI18n(String i18Key) 116 { 117 String catalogue = "plugin.core-impl"; 118 I18nizableText userDirectoryId = new I18nizableText(getPopulationId() + "#" + getId()); 119 I18nizableText type = new I18nizableText(getCacheTypeLabel()); 120 Map<String, I18nizableTextParameter> params = Map.of("type", type, "id", userDirectoryId); 121 return new I18nizableText(catalogue, i18Key, params); 122 } 123 124 /** 125 * Returns the user's cache by login 126 * @return the user's cache by login 127 */ 128 protected Cache<String, User> getCacheByLogin() 129 { 130 return getCache(getClass().getName() + "$by.login$" + _uniqueCacheSuffix); 131 } 132 133 /** 134 * Returns the user's cache by email 135 * @return the user's cache by email 136 */ 137 protected Cache<String, User> getCacheByMail() 138 { 139 return getCache(getClass().getName() + "$by.mail$" + _uniqueCacheSuffix); 140 } 141 142 /** 143 * Returns a String identifying the type of this directory, used in the cache screen in _admin, 144 * such as 'SQL' or 'LDAP'. 145 * @return this directory's type 146 */ 147 protected abstract String getCacheTypeLabel(); 148 149 @Override 150 public AbstractCacheManager getCacheManager() 151 { 152 return _cacheManager; 153 } 154 155 @Override 156 public void dispose() 157 { 158 removeCaches(); 159 } 160}