001 package railo.runtime.cache; 002 003 import java.io.IOException; 004 005 import railo.commons.io.cache.Cache; 006 import railo.commons.io.cache.exp.CacheException; 007 import railo.commons.lang.ClassUtil; 008 import railo.commons.net.JarLoader; 009 import railo.runtime.config.Config; 010 import railo.runtime.config.ConfigWeb; 011 import railo.runtime.reflection.Reflector; 012 import railo.runtime.tag.Admin; 013 import railo.runtime.type.Struct; 014 015 016 public class CacheConnectionImpl implements CacheConnection { 017 018 019 020 private String name; 021 private Class clazz; 022 private Struct custom; 023 private Cache cache; 024 private boolean readOnly; 025 private boolean storage; 026 027 public CacheConnectionImpl(Config config,String name, Class clazz, Struct custom, boolean readOnly, boolean storage) throws CacheException { 028 this.name=name; 029 this.clazz=clazz; 030 if(!Reflector.isInstaneOf(clazz, Cache.class)) 031 throw new CacheException("class ["+clazz.getName()+"] does not implement interface ["+Cache.class.getName()+"]"); 032 this.custom=custom; 033 this.readOnly=readOnly; 034 this.storage=storage; 035 } 036 037 @Override 038 public Cache getInstance(Config config) throws IOException { 039 if(cache==null){ 040 try{ 041 cache=(Cache) ClassUtil.loadInstance(clazz); 042 } 043 catch(NoClassDefFoundError e){ 044 if(!(config instanceof ConfigWeb)) throw e; 045 if(JarLoader.changed((ConfigWeb)config, Admin.CACHE_JARS)) 046 throw new IOException( 047 "cannot initialize Cache ["+clazz.getName()+"], make sure you have added all the required jar files. "+ 048 "GO to the Railo Server Administrator and on the page Services/Update, click on \"Update JARs\"."); 049 throw new IOException( 050 "cannot initialize Cache ["+clazz.getName()+"], make sure you have added all the required jar files. "+ 051 "if you have updated the JARs in the Railo Administrator, please restart your Servlet Engine."); 052 } 053 cache.init(config,getName(), getCustom()); 054 } 055 return cache; 056 } 057 058 059 @Override 060 public String getName() { 061 return name; 062 } 063 064 @Override 065 public Class getClazz() { 066 return clazz; 067 } 068 069 @Override 070 public Struct getCustom() { 071 return custom; 072 } 073 074 075 public String toString(){ 076 return "name:"+this.name+";class:"+this.clazz.getName()+";custom:"+custom+";"; 077 } 078 079 080 @Override 081 public CacheConnection duplicate(Config config) throws IOException { 082 return new CacheConnectionImpl(config,name,clazz,custom,readOnly,storage); 083 } 084 085 086 @Override 087 public boolean isReadOnly() { 088 return readOnly; 089 } 090 public boolean isStorage() { 091 return storage; 092 } 093 }