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.monitor;
020
021import java.io.IOException;
022import java.util.Map;
023
024import lucee.commons.io.log.Log;
025import lucee.commons.io.log.LogUtil;
026import lucee.runtime.PageContext;
027import lucee.runtime.config.ConfigImpl;
028import lucee.runtime.config.ConfigServer;
029import lucee.runtime.config.ConfigWeb;
030import lucee.runtime.engine.ThreadLocalPageContext;
031import lucee.runtime.exp.PageException;
032import lucee.runtime.type.Query;
033
034public class AsyncRequestMonitor implements RequestMonitor {
035        
036        private RequestMonitor monitor;
037        private boolean logEnabled;
038
039        public AsyncRequestMonitor(RequestMonitor monitor){
040                this.monitor=monitor;
041        }
042        
043        @Override
044        public void init(ConfigServer configServer, String name, boolean logEnabled) {
045                monitor.init(configServer, name, logEnabled);
046                this.logEnabled=logEnabled;
047        }
048
049        @Override
050        public short getType() {
051                return monitor.getType();
052        }
053
054        @Override
055        public String getName() {
056                return monitor.getName();
057        }
058
059        @Override
060        public Class getClazz() {
061                return monitor.getClazz();
062        }
063
064        @Override
065        public boolean isLogEnabled() {
066                return monitor.isLogEnabled();
067        }
068
069        @Override
070        public Query getData(ConfigWeb config, Map<String, Object> arguments) throws PageException {
071                return monitor.getData(config, arguments);
072        }
073
074        @Override
075        public void log(PageContext pc, boolean error) throws IOException {
076                new _Log(monitor,pc,error,logEnabled).start();
077        }
078
079        static class _Log extends Thread {
080                private RequestMonitor monitor;
081                private PageContext pc;
082                private boolean error;
083                private boolean logEnabled;
084
085                public _Log(RequestMonitor monitor, PageContext pc, boolean error, boolean logEnabled) {
086                        this.monitor=monitor;
087                        this.pc=pc;
088                        this.error=error;
089                        this.logEnabled=logEnabled;
090                }
091
092                public void run(){
093                        try{
094                                ThreadLocalPageContext.register(pc);
095                                try {
096                                        monitor.log(pc, error);
097                                }
098                                catch (IOException e) {
099                                        if(logEnabled) {
100                                                Log log=((ConfigImpl)pc.getConfig()).getLog("io");
101                                                if(log!=null) LogUtil.log(log, Log.LEVEL_ERROR, "io", e);
102                                        }
103                                }
104                        }
105                        finally{
106                                ThreadLocalPageContext.release();
107                        }
108                }
109        }
110}