001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Maxime Hilaire
018 *
019 */
020package org.nuxeo.ecm.core.cache;
021
022import java.util.HashMap;
023import java.util.Map;
024
025import org.nuxeo.common.xmap.annotation.XNode;
026import org.nuxeo.common.xmap.annotation.XNodeMap;
027import org.nuxeo.common.xmap.annotation.XObject;
028import org.nuxeo.runtime.model.Descriptor;
029
030/**
031 * Descriptor of cache contrib
032 *
033 * @since 6.0
034 */
035@XObject("cache")
036public class CacheDescriptor implements Descriptor {
037
038    /** Default TTL in minutes. */
039    public static final long DEFAULT_TTL = 1;
040
041    /**
042     * Default max size
043     *
044     * @since 9.3
045     */
046    public static final long DEFAULT_MAX_SIZE = 100;
047
048    /**
049     * Maximum number of entries the cache may contain.
050     *
051     * @since 9.3
052     */
053    public static final String OPTION_MAX_SIZE = "maxSize";
054
055    /** @since 9.3 */
056    public static final String OPTION_CONCURRENCY_LEVEL = "concurrencyLevel";
057
058    @XNode("@name")
059    public String name;
060
061    @XNode("@remove")
062    public boolean remove;
063
064    @XNode("@class")
065    protected Class<? extends CacheManagement> klass;
066
067    @XNode("ttl")
068    private Long ttl;
069
070    @XNodeMap(value = "option", key = "@name", type = HashMap.class, componentType = String.class)
071    public Map<String, String> options = new HashMap<>();
072
073    @Override
074    public String getId() {
075        return name;
076    }
077
078    public long getTTL() {
079        return ttl == null ? DEFAULT_TTL : ttl.longValue();
080    }
081
082    public void setTTL(Long value) {
083        ttl = value;
084    }
085
086    @Override
087    public Descriptor merge(Descriptor o) {
088        CacheDescriptor other = (CacheDescriptor) o;
089        CacheDescriptor merged = new CacheDescriptor();
090        merged.name = name;
091        merged.remove = other.remove;
092        merged.ttl = other.ttl != null ? other.ttl : ttl;
093        merged.klass = other.klass != null ? other.klass : klass;
094        merged.options.putAll(options);
095        merged.options.putAll(other.options);
096        return merged;
097    }
098
099    @Override
100    public boolean doesRemove() {
101        return remove;
102    }
103
104}