001/* 002 * Copyright 2018 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.web.frontoffice.search.instance; 017 018import org.apache.avalon.framework.activity.Disposable; 019import org.apache.avalon.framework.activity.Initializable; 020import org.apache.avalon.framework.component.Component; 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.avalon.framework.service.Serviceable; 024 025import org.ametys.core.cache.AbstractCacheManager; 026import org.ametys.core.cache.Cache; 027import org.ametys.plugins.repository.AmetysObjectResolver; 028import org.ametys.plugins.repository.AmetysRepositoryException; 029import org.ametys.runtime.i18n.I18nizableText; 030import org.ametys.web.frontoffice.search.SearchService; 031import org.ametys.web.repository.page.ZoneItem; 032import org.ametys.web.repository.page.ZoneItem.ZoneType; 033 034/** 035 * The component managing all {@link SearchServiceInstance}s. 036 */ 037public class SearchServiceInstanceManager implements Component, Serviceable, Disposable, Initializable 038{ 039 /** Avalon Role */ 040 public static final String ROLE = SearchServiceInstanceManager.class.getName(); 041 042 private static final String __INSTANCE_CACHE = SearchServiceInstanceManager.class.getName() + "$instance.cache"; 043 044 private AmetysObjectResolver _resolver; 045 private SearchServiceInstanceFactory _factory; 046 047 private AbstractCacheManager _cacheManager; 048 049 public void service(ServiceManager manager) throws ServiceException 050 { 051 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 052 _factory = (SearchServiceInstanceFactory) manager.lookup(SearchServiceInstanceFactory.ROLE); 053 054 _cacheManager = (AbstractCacheManager) manager.lookup(AbstractCacheManager.ROLE); 055 } 056 057 public void initialize() throws Exception 058 { 059 _createCaches(); 060 } 061 062 private void _createCaches() 063 { 064 _cacheManager.createMemoryCache(__INSTANCE_CACHE, 065 new I18nizableText("plugin.web", "PLUGINS_WEB_SEARCH_SERVICE_INSTANCE_CACHE_LABEL"), 066 new I18nizableText("plugin.web", "PLUGINS_WEB_SEARCH_SERVICE_INSTANCE_CACHE_DESCRIPTION"), 067 false, 068 null); 069 } 070 071 @Override 072 public void dispose() 073 { 074 _getInstanceCache().invalidateAll(); 075 } 076 077 private Cache<String, SearchServiceInstance> _getInstanceCache() 078 { 079 return _cacheManager.get(__INSTANCE_CACHE); 080 } 081 082 /** 083 * Returns <code>true</code> if the given id of a {@link ZoneItem} is an instance of a {@link SearchService} 084 * @param zoneItemId the id of the {@link ZoneItem} 085 * @return <code>true</code> if the given id of a {@link ZoneItem} is an instance of a {@link SearchService} 086 */ 087 public boolean isSearchServiceInstance(String zoneItemId) 088 { 089 ZoneItem zoneItem; 090 try 091 { 092 zoneItem = _resolver.resolveById(zoneItemId); 093 } 094 catch (AmetysRepositoryException e) 095 { 096 return false; 097 } 098 return zoneItem != null && zoneItem.getType() == ZoneType.SERVICE && SearchService.ROLE.equals(zoneItem.getServiceId()); 099 } 100 101 /** 102 * Gets, or creates if it does not exist yet, the {@link SearchServiceInstance} which is placed at the given {@link ZoneItem}. 103 * <br>You must call {@link SearchServiceInstanceManager#isSearchServiceInstance(String) isSearchServiceInstance} before to ensure that the given id is an instance of {@link SearchService} 104 * (this check is not done in this method) 105 * @param zoneItemId the id of the {@link ZoneItem} 106 * @return the {@link SearchServiceInstance} which is placed at the given {@link ZoneItem} 107 */ 108 public synchronized SearchServiceInstance get(String zoneItemId) 109 { 110 return _getInstanceCache().get(zoneItemId, __ -> _factory.createSearchServiceInstance(zoneItemId)); 111 } 112 113 /** 114 * Removes the {@link SearchServiceInstance} which is placed at the given {@link ZoneItem}. 115 * @param zoneItemId the id of the {@link ZoneItem} 116 */ 117 public synchronized void remove(String zoneItemId) 118 { 119 _getInstanceCache().invalidate(zoneItemId); 120 } 121}