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.lang.reflect.Method;
023import java.util.ArrayList;
024import java.util.Iterator;
025import java.util.List;
026
027import lucee.commons.lang.ExceptionUtil;
028import lucee.runtime.PageContext;
029import lucee.runtime.config.ConfigServer;
030import lucee.runtime.config.ConfigWeb;
031
032public class ActionMonitorCollectorRefImpl implements ActionMonitorCollector {
033        
034        private List<Object> monitors=new ArrayList<Object>();
035        private Method init;
036        private Method logc;
037        private Method getName;
038        private Method logpc;
039        
040        @Override
041        public void addMonitor(ConfigServer cs,Object monitor, String name, boolean log) throws IOException {
042                monitor=init(monitor,cs,name,log);
043                if(monitor!=null)monitors.add(monitor);
044        }
045
046        @Override
047        public void log(PageContext pc, String type, String label, long executionTime, Object data) {
048                
049                Iterator<Object> it = monitors.iterator();
050                while(it.hasNext()){
051                        log(it.next(),pc, type, label, executionTime, data);
052                }
053        }
054
055        @Override
056        public void log(ConfigWeb config, String type, String label, long executionTime, Object data) {
057                
058                Iterator<Object> it = monitors.iterator();
059                while(it.hasNext()){
060                        log(it.next(),config, type, label, executionTime, data);
061                }
062        }
063        
064        @Override
065        public Object getActionMonitor(String name) {
066                Iterator<Object> it = monitors.iterator();
067                Object am;
068                while(it.hasNext()){
069                        am = it.next();
070                        if(name.equalsIgnoreCase(getName(am))) return am;
071                }
072                return null;
073        }
074
075
076
077
078        private String getName(Object am) {
079                if(getName==null){
080                        try {
081                                getName=am.getClass().getMethod("getName", new Class[]{});
082                        } 
083                        catch(Throwable t) {
084                                ExceptionUtil.rethrowIfNecessary(t);
085                                t.printStackTrace();
086                                return null;
087                        }
088                }
089                
090                try {
091                        return (String) getName.invoke(am, new Object[]{});
092                }
093                catch (Throwable t) {
094                        ExceptionUtil.rethrowIfNecessary(t);
095                        t.printStackTrace();
096                }
097                return null;
098        }
099        
100        private void log(Object monitor, PageContext pc, String type, String label, long executionTime, Object data) {
101                if(logpc==null){
102                        try {
103                                logpc=monitor.getClass().getMethod("log", new Class[]{PageContext.class,String.class,String.class,long.class,Object.class});
104                        } 
105                        catch (Throwable t) {
106                                ExceptionUtil.rethrowIfNecessary(t);
107                                t.printStackTrace();
108                                return;
109                        }
110                }
111                
112                try {
113                        logpc.invoke(monitor, new Object[]{pc,type,label,executionTime,data});
114                }
115                catch (Throwable t) {
116                        ExceptionUtil.rethrowIfNecessary(t);
117                        t.printStackTrace();
118                }
119        }
120        
121        private void log(Object monitor, ConfigWeb config, String type, String label, long executionTime, Object data) {
122                if(logc==null){
123                        try {
124                                logc=monitor.getClass().getMethod("log", new Class[]{ConfigWeb.class,String.class,String.class,long.class,Object.class});
125                        } 
126                        catch (Throwable t) {
127                                ExceptionUtil.rethrowIfNecessary(t);
128                                t.printStackTrace();
129                                return;
130                        }
131                }
132                
133                try {
134                        logc.invoke(monitor, new Object[]{config,type,label,executionTime,data});
135                }
136                catch (Throwable t) {
137                        ExceptionUtil.rethrowIfNecessary(t);
138                        t.printStackTrace();
139                }
140        }
141        
142
143        private Object init(Object monitor, ConfigServer cs, String name, boolean log) {
144                if(init==null){
145                        try {
146                                init=monitor.getClass().getMethod("init", new Class[]{ConfigServer.class,String.class,boolean.class});
147                        } 
148                        catch (Throwable t) {
149                                ExceptionUtil.rethrowIfNecessary(t);
150                                t.printStackTrace();
151                                return null;
152                        }
153                }
154                
155                try {
156                        return init.invoke(monitor, new Object[]{cs,name,log});
157                }
158                catch (Throwable t) {
159                        ExceptionUtil.rethrowIfNecessary(t);
160                        t.printStackTrace();
161                        return null;
162                }
163        }
164}