001/*
002 *  Copyright 2019 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.cache;
017
018import java.time.Duration;
019
020import org.openjdk.jol.vm.VM;
021
022import org.ametys.core.cache.AbstractCacheManager;
023import org.ametys.core.cache.Cache;
024import org.ametys.runtime.i18n.I18nizableText;
025
026/**
027 * Implementation of AbstractCacheManager to create new Cache using GuavaCache implementation
028 */
029public class GuavaCacheManager extends AbstractCacheManager
030{
031    /** Is size computable on this configuration */
032    protected boolean _canComputeSize;
033    
034    @Override
035    public void initialize() throws Exception
036    {
037        super.initialize();
038        try
039        {
040            VM.current();
041            _canComputeSize = true;
042        }
043        catch (IllegalStateException e)
044        {
045            _canComputeSize = false;
046            getLogger().warn("JOL is not supported for this JVM. Caches sizes won't be computed.", e);
047        }
048    }
049    
050    @SuppressWarnings("unchecked")
051    @Override
052    protected Cache _createCache(String id, I18nizableText name, I18nizableText description, boolean computableSize, Duration duration, boolean isDispatchable)
053    {
054        return new GuavaCache(id, name, description, Long.MAX_VALUE, _canComputeSize && computableSize, duration, isDispatchable);
055    }
056}