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.ArrayList;
023import java.util.Iterator;
024import java.util.List;
025
026import lucee.commons.lang.ExceptionUtil;
027import lucee.runtime.PageContext;
028import lucee.runtime.config.ConfigServer;
029import lucee.runtime.config.ConfigWeb;
030
031public class ActionMonitorCollectorImpl implements ActionMonitorCollector {
032        
033        private List<ActionMonitor> monitors;
034        
035        @Override
036        public void addMonitor(ConfigServer cs,Object oMonitor, String name, boolean log) throws IOException {
037                ActionMonitor monitor;
038                try {
039                        monitor = (ActionMonitor) oMonitor;
040                } catch (Throwable t) {
041                        throw ExceptionUtil.toIOException(t);
042                }
043                monitor.init(cs,name,log);
044                if(monitors==null) monitors=new ArrayList<ActionMonitor>();
045                monitors.add(monitor);
046        }
047
048        /**
049         *  logs certain action within a Request
050         * @param pc
051         * @param ar
052         * @throws IOException
053         */
054        public void log(PageContext pc, String type, String label, long executionTime, Object data) {
055                if(monitors==null) return ;
056                
057                Iterator<ActionMonitor> it = monitors.iterator();
058                while(it.hasNext()){
059                        try {
060                                it.next().log(pc, type, label, executionTime, data);
061                        } catch (Throwable t) {
062                                ExceptionUtil.rethrowIfNecessary(t);
063                                t.printStackTrace();
064                        }
065                }
066        }
067        public void log(ConfigWeb config, String type, String label, long executionTime, Object data) {
068                if(monitors==null) return ;
069                
070                Iterator<ActionMonitor> it = monitors.iterator();
071                while(it.hasNext()){
072                        try {
073                                it.next().log(config, type, label, executionTime, data);
074                        } catch (Throwable t) {
075                                ExceptionUtil.rethrowIfNecessary(t);
076                                t.printStackTrace();
077                        }
078                }
079        }
080
081        @Override
082        public Object getActionMonitor(String name) {
083                Iterator<ActionMonitor> it = monitors.iterator();
084                ActionMonitor am;
085                while(it.hasNext()){
086                        am = it.next();
087                        if(name.equalsIgnoreCase(am.getName())) return am;
088                }
089                return null;
090        }
091        
092}