001    package railo.runtime.type.scope.storage.clean;
002    
003    import java.sql.Types;
004    
005    import railo.runtime.config.ConfigWeb;
006    import railo.runtime.config.ConfigWebImpl;
007    import railo.runtime.db.DataSource;
008    import railo.runtime.db.DataSourceImpl;
009    import railo.runtime.db.DatasourceConnection;
010    import railo.runtime.db.DatasourceConnectionPool;
011    import railo.runtime.db.SQL;
012    import railo.runtime.db.SQLImpl;
013    import railo.runtime.db.SQLItem;
014    import railo.runtime.db.SQLItemImpl;
015    import railo.runtime.exp.PageException;
016    import railo.runtime.op.Caster;
017    import railo.runtime.type.KeyImpl;
018    import railo.runtime.type.Query;
019    import railo.runtime.type.QueryImpl;
020    import railo.runtime.type.scope.storage.StorageScopeDatasource;
021    import railo.runtime.type.scope.storage.StorageScopeEngine;
022    import railo.runtime.type.scope.storage.StorageScopeListener;
023    
024    public class DatasourceStorageScopeCleaner extends StorageScopeCleanerSupport {
025            
026            //private String strType;
027            
028            public DatasourceStorageScopeCleaner(int type,StorageScopeListener listener) {
029                    super(type,listener,INTERVALL_HOUR);
030                    //this.strType=VariableInterpreter.scopeInt2String(type);
031            }
032            
033            public void init(StorageScopeEngine engine) {
034                    super.init(engine);
035            }
036    
037            protected void _clean() {
038                    ConfigWeb config = engine.getFactory().getConfig();
039                    DataSource[] datasources = config.getDataSources();
040                    for(int i=0;i<datasources.length;i++){
041                            
042                            if(((DataSourceImpl)datasources[i]).isStorage()) {
043                                    try {
044                                            clean(config,datasources[i]);
045                                    } catch (PageException e) {
046                                            error(e);
047                                    }
048                            }
049                    }
050            }
051    
052            private void clean(ConfigWeb config, DataSource dataSource) throws PageException        {
053                    ConfigWebImpl cwi=(ConfigWebImpl) config;
054                    DatasourceConnection dc=null;
055                    Query query=null;
056                
057                    //      executeUpdate(config,dc.getConnection(),"insert into "+PREFIX+"_"+getTypeAsString()+"_data (expires,data,cfid,name) values(?,?,?,?)",false);
058                       
059                // select
060                SQL sqlSelect=new SQLImpl("select cfid,name from "+StorageScopeDatasource.PREFIX+"_"+strType+"_data where expires<=?"
061                                                    ,new SQLItem[]{
062                                            new SQLItemImpl(System.currentTimeMillis(),Types.VARCHAR)
063                                    });
064                //print.o(sqlSelect);
065                    
066                    DatasourceConnectionPool pool = cwi.getDatasourceConnectionPool();
067                    try {
068                            dc=pool.getDatasourceConnection(null,dataSource,null,null);
069                            query = new QueryImpl(dc,sqlSelect,-1,-1,-1,"query");
070                            int recordcount=query.getRecordcount();
071                            //print.o("recordcount:"+recordcount);
072                            
073                            String cfid,name;
074                            for(int row=1;row<=recordcount;row++){
075                                    cfid=Caster.toString(query.getAt(KeyImpl.CFID, row, null),null);
076                                    name=Caster.toString(query.getAt(KeyImpl.NAME, row, null),null);
077                                    
078                                    if(listener!=null)listener.doEnd(engine, this,name, cfid);
079                                    
080                                    
081                                    info("remove "+strType+"/"+name+"/"+cfid+" from datasource "+dataSource.getName());
082                                    engine.remove(type,name,cfid);
083                                    SQLImpl sql = new SQLImpl("delete from "+StorageScopeDatasource.PREFIX+"_"+strType+"_data where cfid=? and name=?",new SQLItem[]{
084                                                    new SQLItemImpl(cfid,Types.VARCHAR),
085                                                    new SQLItemImpl(name,Types.VARCHAR)
086                                                    });
087                                    new QueryImpl(dc,sql,-1,-1,-1,"query");
088                                    
089                                    
090                                    
091                            }
092                            
093                            
094                    }
095                finally {
096                    if(dc!=null) pool.releaseDatasourceConnection(dc);
097                }
098                
099                
100                
101                
102                
103                //long expires=Caster.toLongValue(query.get(StorageScopeDatasource.EXPIRES));
104                
105                
106            }
107    }