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.ecm.core.api.NuxeoException;
029
030/**
031 * Descriptor of cache contrib
032 *
033 * @since 6.0
034 */
035@XObject("cache")
036public class CacheDescriptor {
037
038    @XNode("@name")
039    public String name;
040
041    @XNode("@remove")
042    public boolean remove = false;
043
044    @XNode("@class")
045    protected Class<? extends Cache> implClass = InMemoryCacheImpl.class;
046
047    @XNode("ttl")
048    public int ttl = 1;
049
050    @XNodeMap(value = "option", key = "@name", type = HashMap.class, componentType = String.class)
051    public Map<String, String> options = new HashMap<String, String>();
052
053    protected Cache cache;
054
055    public CacheDescriptor() {
056        super();
057    }
058
059    protected CacheDescriptor(String name, Class<? extends Cache> implClass, Integer ttl, Map<String, String> options) {
060        this.name = name;
061        this.implClass = implClass;
062        this.ttl = ttl;
063        this.options.putAll(options);
064    }
065
066    public CacheDescriptor(CacheDescriptor other) {
067        name = other.name;
068        implClass = other.implClass;
069        ttl = other.ttl;
070        options = new HashMap<String, String>(other.options);
071    }
072
073    @Override
074    public CacheDescriptor clone() {
075        return new CacheDescriptor(name, implClass, ttl, options);
076    }
077
078    public Class<?> getImplClass() {
079        return implClass;
080    }
081
082    public void setImplClass(Class<Cache> implClass) {
083        this.implClass = implClass;
084    }
085
086    @Override
087    public String toString() {
088        return name + ": " + implClass + ": " + ttl + ": " + options;
089    }
090
091    protected void invalidateAll() {
092        cache.invalidateAll();
093    }
094
095    protected void start() {
096        try {
097            cache = implClass.getConstructor(CacheDescriptor.class).newInstance(this);
098            cache = new CacheAttributesChecker(this, cache);
099            cache = new CacheMetrics(cache);
100        } catch (ReflectiveOperationException e) {
101            throw new NuxeoException("Failed to instantiate class " + implClass, e);
102        }
103    }
104
105    protected void stop() {
106        if (cache == null) {
107            return;
108        }
109        try {
110            ((CacheWrapper) cache).stop();
111        } finally {
112            cache = null;
113        }
114    }
115
116}