001/* 002 * (C) Copyright 2014 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Maxime Hilaire 016 * 017 */ 018package org.nuxeo.ecm.core.cache; 019 020import java.lang.reflect.InvocationTargetException; 021import java.util.HashMap; 022import java.util.Map; 023 024import org.nuxeo.common.xmap.annotation.XNode; 025import org.nuxeo.common.xmap.annotation.XNodeMap; 026import org.nuxeo.common.xmap.annotation.XObject; 027import org.nuxeo.ecm.core.api.NuxeoException; 028 029/** 030 * Descriptor of cache contrib 031 * 032 * @since 6.0 033 */ 034@XObject("cache") 035public class CacheDescriptor { 036 037 @XNode("@name") 038 public String name; 039 040 @XNode("@remove") 041 public boolean remove = false; 042 043 @XNode("@class") 044 protected Class<? extends Cache> implClass = InMemoryCacheImpl.class; 045 046 @XNode("ttl") 047 protected int ttl = 1; 048 049 @XNodeMap(value = "option", key = "@name", type = HashMap.class, componentType = String.class) 050 protected Map<String, String> options = new HashMap<String, String>(); 051 052 protected CacheAttributesChecker cacheChecker; 053 054 public CacheDescriptor() { 055 super(); 056 } 057 058 protected CacheDescriptor(String name, Class<? extends Cache> implClass, Integer ttl, Map<String, String> options) { 059 this.name = name; 060 this.implClass = implClass; 061 this.ttl = ttl; 062 this.options.putAll(options); 063 } 064 065 @Override 066 public CacheDescriptor clone() { 067 return new CacheDescriptor(name, implClass, ttl, options); 068 } 069 070 public Class<?> getImplClass() { 071 return implClass; 072 } 073 074 public void setImplClass(Class<Cache> implClass) { 075 this.implClass = implClass; 076 } 077 078 @Override 079 public String toString() { 080 return name + ": " + implClass + ": " + ttl + ": " + options; 081 } 082 083 public void start() { 084 try { 085 cacheChecker = new CacheAttributesChecker(this); 086 cacheChecker.setCache(implClass.getConstructor(CacheDescriptor.class).newInstance(this)); 087 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException 088 | NoSuchMethodException | SecurityException e) { 089 throw new NuxeoException("Failed to instantiate class " + implClass, e); 090 } 091 } 092 093 public void stop() { 094 if (cacheChecker == null) { 095 return; 096 } 097 cacheChecker.cache = null; 098 cacheChecker = null; 099 } 100 101}