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.createRequestCache(__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 } 069 070 @Override 071 public void dispose() 072 { 073 _getInstanceCache().invalidateAll(); 074 } 075 076 private Cache<String, SearchServiceInstance> _getInstanceCache() 077 { 078 return _cacheManager.get(__INSTANCE_CACHE); 079 } 080 081 /** 082 * Returns <code>true</code> if the given id of a {@link ZoneItem} is an instance of a {@link SearchService} 083 * @param zoneItemId the id of the {@link ZoneItem} 084 * @return <code>true</code> if the given id of a {@link ZoneItem} is an instance of a {@link SearchService} 085 */ 086 public boolean isSearchServiceInstance(String zoneItemId) 087 { 088 ZoneItem zoneItem; 089 try 090 { 091 zoneItem = _resolver.resolveById(zoneItemId); 092 } 093 catch (AmetysRepositoryException e) 094 { 095 return false; 096 } 097 return zoneItem != null && zoneItem.getType() == ZoneType.SERVICE && SearchService.ROLE.equals(zoneItem.getServiceId()); 098 } 099 100 /** 101 * Gets, or creates if it does not exist yet, the {@link SearchServiceInstance} which is placed at the given {@link ZoneItem}. 102 * <br>You must call {@link SearchServiceInstanceManager#isSearchServiceInstance(String) isSearchServiceInstance} before to ensure that the given id is an instance of {@link SearchService} 103 * (this check is not done in this method) 104 * @param zoneItemId the id of the {@link ZoneItem} 105 * @return the {@link SearchServiceInstance} which is placed at the given {@link ZoneItem} 106 */ 107 public synchronized SearchServiceInstance get(String zoneItemId) 108 { 109 return _getInstanceCache().get(zoneItemId, __ -> _factory.createSearchServiceInstance(zoneItemId)); 110 } 111 112 /** 113 * Removes the {@link SearchServiceInstance} which is placed at the given {@link ZoneItem}. 114 * @param zoneItemId the id of the {@link ZoneItem} 115 */ 116 public synchronized void remove(String zoneItemId) 117 { 118 _getInstanceCache().invalidate(zoneItemId); 119 } 120}