001    package railo.runtime.type.scope.storage.clean;
002    
003    import railo.commons.lang.ExceptionUtil;
004    import railo.runtime.interpreter.VariableInterpreter;
005    import railo.runtime.type.scope.storage.StorageScopeCleaner;
006    import railo.runtime.type.scope.storage.StorageScopeEngine;
007    import railo.runtime.type.scope.storage.StorageScopeListener;
008    
009    public abstract class StorageScopeCleanerSupport implements StorageScopeCleaner {
010            
011    
012            protected static final int INTERVALL_MINUTE = 60*1000; 
013            protected static final int INTERVALL_HOUR = 60*60*1000; 
014            protected static final int INTERVALL_DAY = 24*60*60*1000; 
015            
016            protected StorageScopeEngine engine;
017            protected int type;
018            protected StorageScopeListener listener;
019            private String application;
020            protected String strType;
021            private final int intervall;
022            private long lastClean;
023            
024            public StorageScopeCleanerSupport(int type, StorageScopeListener listener, int intervall) {
025                    this.type=type;
026                    this.listener=listener;
027                    this.strType=VariableInterpreter.scopeInt2String(type);
028                    application=strType+" storage";
029                    this.intervall=intervall;
030                    
031            }
032    
033            @Override
034            public void init(StorageScopeEngine engine){
035                    this.engine=engine;
036            }
037            
038            public final void clean() {
039                    if(lastClean+intervall<System.currentTimeMillis()) {
040                            //info("cleaning "+application);
041                            _clean();
042                            lastClean=System.currentTimeMillis();
043                            //info("next cleaning intervall in "+(intervall/1000)+" seconds");
044                    }
045            }
046            
047            protected abstract void _clean();
048            
049            /**
050             * @return the log
051             */
052            public void info(String msg) {
053                     engine.getFactory().getScopeContext().info(msg);
054            }
055            public void error(String msg) {
056                    engine.getFactory().getScopeContext().error(msg);
057                    engine._getLog().error(application, msg);
058            }
059            
060            public void error(Throwable t) {
061                    engine.getFactory().getScopeContext().error(t);
062                    engine._getLog().error(application,ExceptionUtil.getStacktrace(t, true));
063            }
064    }