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.services; 020 021import java.io.IOException; 022import java.sql.SQLException; 023 024import lucee.commons.io.res.Resource; 025import lucee.commons.io.res.filter.ExtensionResourceFilter; 026import lucee.commons.io.res.util.ResourceUtil; 027import lucee.commons.lang.ExceptionUtil; 028import lucee.runtime.PageContext; 029import lucee.runtime.config.ConfigWebAdmin; 030import lucee.runtime.config.ConfigWebUtil; 031import lucee.runtime.db.DataSourceManager; 032import lucee.runtime.exp.ExpressionException; 033import lucee.runtime.exp.PageException; 034import lucee.runtime.exp.PageRuntimeException; 035import lucee.runtime.exp.SecurityException; 036import lucee.runtime.functions.list.ListFirst; 037import lucee.runtime.type.Array; 038import lucee.runtime.type.ArrayImpl; 039import lucee.runtime.type.Struct; 040import lucee.runtime.type.StructImpl; 041import lucee.runtime.type.util.KeyConstants; 042import coldfusion.server.DataSourceService; 043import coldfusion.server.ServiceException; 044import coldfusion.sql.DataSource; 045 046public class DataSourceServiceImpl extends ServiceSupport implements DataSourceService { 047 048 private Number maxQueryCount=new Double(500); 049 050 051 052 public Struct getDefaults() { 053 Struct sct=new StructImpl(); 054 sct.setEL("alter",Boolean.TRUE); 055 sct.setEL("blob_buffer",new Double(64000)); 056 sct.setEL("buffer",new Double(64000)); 057 sct.setEL("create",Boolean.TRUE); 058 sct.setEL("delete",Boolean.TRUE); 059 sct.setEL("disable",Boolean.FALSE); 060 sct.setEL("disable_blob",Boolean.TRUE); 061 sct.setEL("disable_clob",Boolean.TRUE); 062 sct.setEL("drop",Boolean.TRUE); 063 sct.setEL("grant",Boolean.TRUE); 064 sct.setEL("insert",Boolean.TRUE); 065 sct.setEL("pooling",Boolean.TRUE); 066 sct.setEL("revoke",Boolean.TRUE); 067 sct.setEL("select",Boolean.TRUE); 068 sct.setEL("storedproc",Boolean.TRUE); 069 sct.setEL("update",Boolean.TRUE); 070 sct.setEL("",Boolean.TRUE); 071 sct.setEL("",Boolean.TRUE); 072 sct.setEL("",Boolean.TRUE); 073 sct.setEL("interval",new Double(420)); 074 sct.setEL("login_timeout",new Double(30)); 075 sct.setEL("timeout",new Double(1200)); 076 077 return sct; 078 } 079 080 public Number getMaxQueryCount() { 081 return maxQueryCount; 082 } 083 084 public void setMaxQueryCount(Number maxQueryCount) { 085 this.maxQueryCount=maxQueryCount; 086 } 087 088 public String encryptPassword(String pass) { 089 throw new PageRuntimeException(new ServiceException("method [encryptPassword] is not supported for datasource service")); 090 //return pass; 091 } 092 093 @Override 094 public String getDbdir() { 095 Resource db = config().getConfigDir().getRealResource("db"); 096 if(!db.exists())db.createNewFile(); 097 return db.getPath(); 098 } 099 100 @Override 101 public Object getCachedQuery(String key) { 102 throw new PageRuntimeException(new ServiceException("method [getQueryCache] is not supported for datasource service")); 103 } 104 105 @Override 106 public void setCachedQuery(String arg0, Object arg1) { 107 throw new PageRuntimeException(new ServiceException("method [setQueryCache] is not supported for datasource service")); 108 } 109 110 @Override 111 public void purgeQueryCache() throws IOException { 112 PageContext pc = pc(); 113 if(pc!=null) 114 try { 115 ConfigWebUtil.getCacheHandlerFactories(pc.getConfig()).query.clean(pc); 116 } 117 catch (PageException e) { 118 throw ExceptionUtil.toIOException(e); 119 } 120 //if(pc!=null)pc.getQueryCache().clearUnused(pc); 121 122 } 123 124 @Override 125 public boolean disableConnection(String name) {return false;} 126 127 @Override 128 public boolean isJadoZoomLoaded() {return false;} 129 130 131 132 public Struct getDrivers() throws ServiceException, SecurityException { 133 checkReadAccess(); 134 Struct rtn=new StructImpl(); 135 Struct driver; 136 137 try { 138 Resource luceeContext = ResourceUtil.toResourceExisting(pc() ,"/lucee/admin/dbdriver/"); 139 Resource[] children = luceeContext.listResources(new ExtensionResourceFilter("cfc")); 140 141 String name; 142 for(int i=0;i<children.length;i++) { 143 driver=new StructImpl(); 144 name=ListFirst.call(pc(),children[i].getName(),"."); 145 driver.setEL(KeyConstants._name,name); 146 driver.setEL("handler",children[i].getName()); 147 rtn.setEL(name,driver); 148 } 149 150 151 } catch (ExpressionException e) { 152 throw new ServiceException(e.getMessage()); 153 } 154 155 156 157 return rtn; 158 } 159 160 public Struct getDatasources() throws SecurityException {// MUST muss struct of struct zurueckgeben!!! 161 checkReadAccess(); 162 lucee.runtime.db.DataSource[] sources = config().getDataSources(); 163 Struct rtn=new StructImpl(); 164 for(int i=0;i<sources.length;i++) { 165 rtn.setEL(sources[i].getName(),new DataSourceImpl(sources[i])); 166 } 167 return rtn; 168 } 169 170 public Array getNames() throws SecurityException { 171 checkReadAccess(); 172 lucee.runtime.db.DataSource[] sources = config().getDataSources(); 173 Array names=new ArrayImpl(); 174 for(int i=0;i<sources.length;i++) { 175 names.appendEL(sources[i].getName()); 176 } 177 return names; 178 } 179 180 public void removeDatasource(String name) throws SQLException, SecurityException { 181 checkWriteAccess(); 182 try { 183 ConfigWebAdmin admin = ConfigWebAdmin.newInstance(config(),""); 184 admin.removeDataSource(name); 185 } catch (Exception e) { 186 // ignoriert wenn die db nicht existiert 187 } 188 } 189 190 191 192 public boolean verifyDatasource(String name) throws SQLException, SecurityException { 193 checkReadAccess(); 194 lucee.runtime.db.DataSource d = _getDatasource(name); 195 PageContext pc = pc(); 196 DataSourceManager manager = pc.getDataSourceManager(); 197 try { 198 manager.releaseConnection(pc,manager.getConnection(pc,name, d.getUsername(), d.getPassword())); 199 return true; 200 } catch (PageException e) { 201 return false; 202 } 203 } 204 205 public DataSource getDatasource(String name) throws SQLException, SecurityException { 206 return new DataSourceImpl(_getDatasource(name)); 207 } 208 209 private lucee.runtime.db.DataSource _getDatasource(String name) throws SQLException, SecurityException { 210 checkReadAccess(); 211 name=name.trim(); 212 lucee.runtime.db.DataSource[] sources = config().getDataSources(); 213 for(int i=0;i<sources.length;i++) { 214 if(sources[i].getName().equalsIgnoreCase(name))return sources[i]; 215 } 216 throw new SQLException("no datasource with name ["+name+"] found"); 217 } 218}