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