001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.cache;
020
021import java.io.IOException;
022
023import lucee.commons.io.cache.Cache;
024import lucee.commons.io.cache.exp.CacheException;
025import lucee.commons.lang.ClassUtil;
026import lucee.commons.net.JarLoader;
027import lucee.runtime.config.Config;
028import lucee.runtime.config.ConfigWeb;
029import lucee.runtime.reflection.Reflector;
030import lucee.runtime.tag.Admin;
031import lucee.runtime.type.Struct;
032
033
034public class CacheConnectionImpl implements CacheConnection  {
035
036
037
038                private String name;
039                private Class clazz;
040                private Struct custom;
041                private Cache cache;
042                private boolean readOnly;
043                private boolean storage;
044
045                public CacheConnectionImpl(Config config,String name, Class clazz, Struct custom, boolean readOnly, boolean storage) throws CacheException {
046                        this.name=name;
047                        this.clazz=clazz;
048                        if(!Reflector.isInstaneOf(clazz, Cache.class))
049                                throw new CacheException("class ["+clazz.getName()+"] does not implement interface ["+Cache.class.getName()+"]");
050                        this.custom=custom;
051                        this.readOnly=readOnly;
052                        this.storage=storage;
053                }
054
055                @Override
056                public Cache getInstance(Config config) throws IOException  {
057                        if(cache==null){
058                                try{
059                                cache=(Cache) ClassUtil.loadInstance(clazz);
060                                }
061                                catch(NoClassDefFoundError e){
062                                        if(!(config instanceof ConfigWeb)) throw e;
063                                        if(JarLoader.changed((ConfigWeb)config, Admin.CACHE_JARS))
064                                                throw new IOException(
065                                                        "cannot initialize Cache ["+clazz.getName()+"], make sure you have added all the required jar files. "+
066                                                        "GO to the Lucee Server Administrator and on the page Services/Update, click on \"Update JARs\".");
067                                        throw new IOException(
068                                                                "cannot initialize Cache ["+clazz.getName()+"], make sure you have added all the required jar files. "+
069                                                                "if you have updated the JARs in the Lucee Administrator, please restart your Servlet Engine.");
070                                }
071                                cache.init(config,getName(), getCustom());
072                        }
073                        return cache;
074                }
075
076
077                @Override
078                public String getName() {
079                        return name;
080                }
081
082                @Override
083                public Class getClazz() {
084                        return clazz;
085                }
086
087                @Override
088                public Struct getCustom() {
089                        return custom;
090                }
091
092                
093                public String toString(){
094                        return "name:"+this.name+";class:"+this.clazz.getName()+";custom:"+custom+";";
095                }
096
097
098                @Override
099                public CacheConnection duplicate(Config config) throws IOException {
100                        return new CacheConnectionImpl(config,name,clazz,custom,readOnly,storage);
101                }
102
103
104                        @Override
105                        public boolean isReadOnly() {
106                                return readOnly;
107                        }
108                        public boolean isStorage() {
109                                return storage;
110                        }
111        }