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.core.minimize;
017
018import java.io.IOException;
019import java.util.Map;
020
021import org.apache.avalon.framework.activity.Initializable;
022import org.apache.avalon.framework.component.Component;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.excalibur.source.Source;
027import org.apache.excalibur.source.SourceResolver;
028
029import org.ametys.core.cache.AbstractCacheManager;
030import org.ametys.core.cache.AbstractCacheManager.CacheType;
031import org.ametys.core.cache.Cache;
032import org.ametys.runtime.i18n.I18nizableText;
033import org.ametys.runtime.plugin.component.AbstractLogEnabled;
034
035/**
036 * A simple cache for source maps from files
037 */
038public class SourceMapCache extends AbstractLogEnabled implements Component, Serviceable, Initializable
039{
040    /** The avalon ROLE */
041    public static final String ROLE = SourceMapCache.class.getName();
042
043    /** CacheManager used to create and get cache */
044    protected AbstractCacheManager _cacheManager;
045    /** Excalibur source resolver */
046    protected SourceResolver _sourceResolver;
047
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        _cacheManager = (AbstractCacheManager) manager.lookup(AbstractCacheManager.ROLE);
051        _sourceResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
052    }
053
054    public void initialize() throws Exception
055    {
056        _cacheManager.createCache(ROLE, 
057                new I18nizableText("plugin.core", "PLUGINS_CORE_CACHE_SOURCE_MAP_LABEL"),
058                new I18nizableText("plugin.core", "PLUGINS_CORE_CACHE_SOURCE_MAP_DESCRIPTION"),
059                CacheType.MEMORY,
060                true);
061    }
062    /**
063     * Put a source map into the cache
064     * @param uri The uri of the source map
065     * @param content The content of the source map
066     * @param lastModified The last modified date of the source map
067     * @throws IOException If the uri is invalid
068     */
069    public void put(String uri, String content, Long lastModified) throws IOException
070    {
071        Source sourceMapData = _sourceResolver.resolveURI(SourceMapSourceFactory.SCHEME + "://" + uri, null, Map.of(SourceMapSourceFactory.CONTENT, content, 
072                                                                                                                    SourceMapSourceFactory.LAST_MODIFIED, lastModified));
073        _getCache().put(uri, sourceMapData);
074    }
075    
076    /**
077     * Get a value from the cache
078     * @param uri The uri of the source map
079     * @return The data, or null if the uri is not in the cache
080     */
081    public Source get(String uri)
082    {
083        return _getCache().get(uri);
084    }
085    
086    private Cache<String, Source> _getCache() 
087    {
088        return this._cacheManager.get(ROLE);
089    }    
090}