001    package railo.runtime.cache;
002    
003    import java.io.IOException;
004    import java.lang.reflect.InvocationTargetException;
005    import java.lang.reflect.Method;
006    
007    import railo.commons.io.cache.Cache;
008    import railo.commons.io.cache.exp.CacheException;
009    import railo.commons.lang.ClassUtil;
010    import railo.commons.net.JarLoader;
011    import railo.runtime.config.Config;
012    import railo.runtime.config.ConfigWeb;
013    import railo.runtime.op.Caster;
014    import railo.runtime.reflection.Reflector;
015    import railo.runtime.tag.Admin;
016    import railo.runtime.type.Struct;
017    
018    
019    public class CacheConnectionImpl implements CacheConnection  {
020    
021    
022    
023                    private String name;
024                    private Class clazz;
025                    private Struct custom;
026                    private Cache cache;
027                    private boolean readOnly;
028                    private boolean storage;
029    
030                    public CacheConnectionImpl(Config config,String name, Class clazz, Struct custom, boolean readOnly, boolean storage) throws CacheException {
031                            this.name=name;
032                            this.clazz=clazz;
033                            if(!Reflector.isInstaneOf(clazz, Cache.class))
034                                    throw new CacheException("class ["+clazz.getName()+"] does not implement interface ["+Cache.class.getName()+"]");
035                            this.custom=custom;
036                            this.readOnly=readOnly;
037                            this.storage=storage;
038                    }
039    
040                    /**
041                     * @see railo.runtime.cache.X#getInstance(railo.runtime.config.ConfigWeb)
042                     */
043                    public Cache getInstance(Config config) throws IOException  {
044                            if(cache==null){
045                                    try{
046                                    cache=(Cache) ClassUtil.loadInstance(clazz);
047                                    }
048                                    catch(NoClassDefFoundError e){
049                                            if(!(config instanceof ConfigWeb)) throw e;
050                                            if(JarLoader.changed((ConfigWeb)config, Admin.CACHE_JARS))
051                                                    throw new IOException(
052                                                            "cannot initilaize Cache ["+clazz.getName()+"], make sure you have added all the required jars files. "+
053                                                            "GO to the Railo Server Administrator and on the page Services/Update, click on \"Update JAR's\".");
054                                            else 
055                                                    throw new IOException(
056                                                                    "cannot initilaize Cache ["+clazz.getName()+"], make sure you have added all the required jars files. "+
057                                                                    "if you have updated the JAR's in the Railo Administrator, please restart your Servlet Engine.");
058                                    }
059                                    
060                                    try {
061                                            // FUTURE Workaround to provide config oject, add to interface
062                                            Method m = clazz.getMethod("init", new Class[]{Config.class,String.class,Struct.class});
063                                            m.invoke(cache, new Object[]{config,getName(), getCustom()});
064                                            
065                                    } catch (InvocationTargetException e) {
066                                            Throwable target = e.getTargetException();
067                                            if(target instanceof IOException) throw ((IOException)target);
068                                            target.printStackTrace();
069                                            IOException ioe = new IOException(Caster.toClassName(target)+":"+target.getMessage());
070                                            ioe.setStackTrace(target.getStackTrace());
071                                            throw ioe;
072                                    } 
073                                    catch (Throwable e) {
074                                            
075                                            cache.init(getName(), getCustom());
076                                    }
077                                    
078                                    
079                                    
080                                    //ConfigWeb config,String cacheName, Struct arguments
081                            }
082                            return cache;
083                    }
084    
085    
086                    /**
087                     * @see railo.runtime.cache.X#getName()
088                     */
089                    public String getName() {
090                            return name;
091                    }
092    
093                    /**
094                     * @see railo.runtime.cache.X#getClazz()
095                     */
096                    public Class getClazz() {
097                            return clazz;
098                    }
099    
100                    /**
101                     * @see railo.runtime.cache.X#getCustom()
102                     */
103                    public Struct getCustom() {
104                            return custom;
105                    }
106    
107                    
108                    public String toString(){
109                            return "name:"+this.name+";class:"+this.clazz.getName()+";custom:"+custom+";";
110                    }
111    
112    
113                    /**
114                     * @see railo.runtime.cache.X#duplicate(railo.runtime.config.Config, boolean)
115                     */
116                    public CacheConnection duplicate(Config config) throws IOException {
117                            return new CacheConnectionImpl(config,name,clazz,custom,readOnly,storage);
118                    }
119    
120    
121                            /**
122                             * @see railo.runtime.cache.X#isReadOnly()
123                             */
124                            public boolean isReadOnly() {
125                                    return readOnly;
126                            }
127                            public boolean isStorage() {
128                                    return storage;
129                            }
130            }