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.type.scope.storage.clean;
020
021import java.sql.SQLException;
022
023import lucee.commons.io.log.Log;
024import lucee.commons.lang.ExceptionUtil;
025import lucee.runtime.config.ConfigImpl;
026import lucee.runtime.config.ConfigWeb;
027import lucee.runtime.config.ConfigWebImpl;
028import lucee.runtime.db.DataSource;
029import lucee.runtime.db.DatasourceConnection;
030import lucee.runtime.db.DatasourceConnectionPool;
031import lucee.runtime.exp.PageException;
032import lucee.runtime.type.scope.storage.StorageScopeEngine;
033import lucee.runtime.type.scope.storage.StorageScopeListener;
034import lucee.runtime.type.scope.storage.db.SQLExecutionFactory;
035import lucee.runtime.type.scope.storage.db.SQLExecutor;
036
037public class DatasourceStorageScopeCleaner extends StorageScopeCleanerSupport {
038        
039        //private String strType;
040        
041        public DatasourceStorageScopeCleaner(int type,StorageScopeListener listener) {
042                super(type,listener,INTERVALL_HOUR);
043                //this.strType=VariableInterpreter.scopeInt2String(type);
044        }
045        
046        public void init(StorageScopeEngine engine) {
047                super.init(engine);
048        }
049
050        protected void _clean() {
051                ConfigWeb config = engine.getFactory().getConfig();
052                DataSource[] datasources = config.getDataSources();
053                for(int i=0;i<datasources.length;i++){
054                        
055                        if((datasources[i]).isStorage()) {
056                                try {
057                                        clean(config,datasources[i]);
058                                } catch (Throwable t) {
059                                        ExceptionUtil.rethrowIfNecessary(t);
060                                        error(t);
061                                }
062                        }
063                }
064        }
065
066        private void clean(ConfigWeb config, DataSource dataSource) throws PageException, SQLException  {
067                ConfigWebImpl cwi=(ConfigWebImpl) config;
068                DatasourceConnection dc=null;
069                DatasourceConnectionPool pool = cwi.getDatasourceConnectionPool();
070                try {
071                        dc=pool.getDatasourceConnection(dataSource,null,null);
072                        Log log=((ConfigImpl)config).getLog("scope");
073                        SQLExecutor executor=SQLExecutionFactory.getInstance(dc);
074                        executor.clean(config, dc, type, engine,this, listener, log);
075                }
076            finally {
077                if(dc!=null) pool.releaseDatasourceConnection(config,dc,true);
078            }
079        }
080}