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.db;
020
021import java.util.Map;
022import java.util.TimeZone;
023
024import lucee.commons.lang.ClassException;
025import lucee.commons.lang.ClassUtil;
026import lucee.runtime.config.Config;
027import lucee.runtime.engine.ThreadLocalPageContext;
028import lucee.runtime.exp.ApplicationException;
029import lucee.runtime.exp.PageRuntimeException;
030
031import org.apache.commons.collections.map.ReferenceMap;
032
033public abstract class DataSourceSupport implements DataSourcePro, Cloneable {
034
035    private final Class clazz;
036        private final boolean blob;
037    private final boolean clob;
038    private final int connectionLimit;
039    private final int connectionTimeout;
040        private final long metaCacheTimeout;
041        private final TimeZone timezone;
042    private final String name;
043        private final boolean storage;
044    protected final int allow;
045    private final boolean readOnly;
046        private final String username;
047        private final String password;
048    
049        
050        private Map<String,ProcMetaCollection> procedureColumnCache;
051
052
053        public DataSourceSupport(String name, Class clazz,String username, String password, boolean blob,boolean clob,int connectionLimit, int connectionTimeout, long metaCacheTimeout, TimeZone timezone, int allow, boolean storage, boolean readOnly){
054                this.name=name;
055        this.clazz=clazz;
056                this.blob=blob;
057                this.clob=clob;
058                this.connectionLimit=connectionLimit;
059                this.connectionTimeout=connectionTimeout;
060                this.metaCacheTimeout=metaCacheTimeout;
061        this.timezone=timezone;
062        this.allow=allow;
063        this.storage=storage;
064        this.readOnly=readOnly;
065        this.username=username;
066        this.password=password;
067        }
068        
069        @Override
070        public Object clone() {
071                return cloneReadOnly();
072        }
073        
074
075        public Map<String,ProcMetaCollection> getProcedureColumnCache() {
076                if(procedureColumnCache==null)
077                        procedureColumnCache=new ReferenceMap();
078                return procedureColumnCache;
079        }
080        
081
082
083        @Override
084    public final boolean isBlob() {
085        return blob;
086    }
087
088    @Override
089    public final boolean isClob() {
090        return clob;
091    }
092
093    @Override
094    public final int getConnectionLimit() {
095        return connectionLimit;
096    }
097
098    @Override
099    public final int getConnectionTimeout() {
100        return connectionTimeout;
101    }
102
103    @Override
104    public final long getMetaCacheTimeout() {
105                return metaCacheTimeout;
106        } 
107        
108        @Override
109        public final TimeZone getTimeZone() {
110                return timezone;
111        } 
112    
113        @Override
114        public final Class getClazz() {
115        return clazz;
116    }
117
118        @Override
119        public final void setClazz(Class clazz) {
120                throw new PageRuntimeException(new ApplicationException("this method is no longer supported")); 
121        //this.clazz = clazz;
122    }
123
124        @Override
125        public final String getName() {
126        return name;
127    }
128
129        @Override
130        public final boolean isStorage() {
131                return storage;
132        }
133        
134        @Override
135        public final boolean hasAllow(int allow) {
136        return (this.allow&allow)>0;
137    }
138
139        @Override
140        public final boolean hasSQLRestriction() {
141        return this.allow!=DataSource.ALLOW_ALL;
142    }
143    
144        @Override
145        public final boolean isReadOnly() {
146        return readOnly;
147    }
148    
149        @Override
150        public String getPassword() {
151        return password;
152    }
153        
154        @Override
155        public String getUsername() {
156        return username;
157    }
158
159        @Override
160        public boolean equals(Object obj) {
161                if(this==obj)return true;
162                if(!(obj instanceof DataSourcePro)) return false;
163                DataSourcePro ds = (DataSourcePro)obj;
164                return id().equals(ds.id());
165        } 
166        
167
168        @Override
169        public int hashCode() {
170                return id().hashCode();
171        }
172        
173
174        public String id() {
175                
176                return new StringBuilder(getConnectionStringTranslated())
177                .append(':')
178                .append(getConnectionLimit())
179                .append(':')
180                .append(getConnectionTimeout())
181                .append(':')
182                .append(getMetaCacheTimeout())
183                .append(':')
184                .append(getName().toLowerCase())
185                .append(':')
186                .append(getUsername())
187                .append(':')
188                .append(getPassword())
189                .append(':')
190                .append(getClazz().getName())
191                .append(':')
192                .append((getTimeZone()==null?"null":getTimeZone().getID()))
193                .append(':')
194                .append(isBlob())
195                .append(':')
196                .append(isClob())
197                .append(':')
198                .append(isReadOnly())
199                .append(':')
200                .append(isStorage()).toString();
201                
202        } 
203
204    @Override
205    public String toString() {
206                return id();
207        }
208    
209    public static Class toClass(String className) throws ClassException {
210        try {
211                        return Class.forName(className);
212                } 
213                catch (ClassNotFoundException e) {
214                        Config config = ThreadLocalPageContext.getConfig();
215                        if(config!=null) return ClassUtil.loadClass(config.getClassLoader(),className);
216                        return ClassUtil.loadClass(className);
217                }
218        }
219
220}