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