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    protected int ttl = 1;
050
051    @XNodeMap(value = "option", key = "@name", type = HashMap.class, componentType = String.class)
052    protected 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    @Override
068    public CacheDescriptor clone() {
069        return new CacheDescriptor(name, implClass, ttl, options);
070    }
071
072    public Class<?> getImplClass() {
073        return implClass;
074    }
075
076    public void setImplClass(Class<Cache> implClass) {
077        this.implClass = implClass;
078    }
079
080    @Override
081    public String toString() {
082        return name + ": " + implClass + ": " + ttl + ": " + options;
083    }
084
085    public void start() {
086        try {
087            cacheChecker = new CacheAttributesChecker(this);
088            cacheChecker.setCache(implClass.getConstructor(CacheDescriptor.class).newInstance(this));
089        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
090                | NoSuchMethodException | SecurityException e) {
091            throw new NuxeoException("Failed to instantiate class " + implClass, e);
092        }
093    }
094
095    public void stop() {
096        if (cacheChecker == null) {
097            return;
098        }
099        cacheChecker.cache = null;
100        cacheChecker = null;
101    }
102
103}