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