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 lucee.commons.lang.ExceptionUtil;
022import lucee.runtime.interpreter.VariableInterpreter;
023import lucee.runtime.type.scope.storage.StorageScopeCleaner;
024import lucee.runtime.type.scope.storage.StorageScopeEngine;
025import lucee.runtime.type.scope.storage.StorageScopeListener;
026
027public abstract class StorageScopeCleanerSupport implements StorageScopeCleaner {
028        
029
030        protected static final int INTERVALL_MINUTE = 60*1000; 
031        protected static final int INTERVALL_HOUR = 60*60*1000; 
032        protected static final int INTERVALL_DAY = 24*60*60*1000; 
033        
034        protected StorageScopeEngine engine;
035        protected int type;
036        protected StorageScopeListener listener;
037        private String application;
038        protected String strType;
039        private final int intervall;
040        private long lastClean;
041        
042        public StorageScopeCleanerSupport(int type, StorageScopeListener listener, int intervall) {
043                this.type=type;
044                this.listener=listener;
045                this.strType=VariableInterpreter.scopeInt2String(type);
046                application=strType+" storage";
047                this.intervall=intervall;
048                
049        }
050
051        @Override
052        public void init(StorageScopeEngine engine){
053                this.engine=engine;
054        }
055        
056        public final void clean() {
057                if(lastClean+intervall<System.currentTimeMillis()) {
058                        //info("cleaning "+application);
059                        _clean();
060                        lastClean=System.currentTimeMillis();
061                        //info("next cleaning intervall in "+(intervall/1000)+" seconds");
062                }
063        }
064        
065        protected abstract void _clean();
066        
067        /**
068         * @return the log
069         */
070        public void info(String msg) {
071                 engine.getFactory().getScopeContext().info(msg);
072        }
073        public void error(String msg) {
074                engine.getFactory().getScopeContext().error(msg);
075                engine._getLog().error(application, msg);
076        }
077        
078        public void error(Throwable t) {
079                ExceptionUtil.rethrowIfNecessary(t);
080                engine.getFactory().getScopeContext().error(t);
081                engine._getLog().error(application,ExceptionUtil.getStacktrace(t, true));
082        }
083}