001    package railo.runtime.monitor;
002    
003    import java.io.IOException;
004    import java.lang.reflect.Method;
005    import java.util.ArrayList;
006    import java.util.Iterator;
007    import java.util.List;
008    
009    import railo.runtime.PageContext;
010    import railo.runtime.config.ConfigServer;
011    import railo.runtime.config.ConfigWeb;
012    
013    public class ActionMonitorCollectorRefImpl implements ActionMonitorCollector {
014            
015            private List<Object> monitors=new ArrayList<Object>();
016            private Method init;
017            private Method logc;
018            private Method getName;
019            private Method logpc;
020            
021            @Override
022            public void addMonitor(ConfigServer cs,Object monitor, String name, boolean log) throws IOException {
023                    monitor=init(monitor,cs,name,log);
024                    if(monitor!=null)monitors.add(monitor);
025            }
026    
027            @Override
028            public void log(PageContext pc, String type, String label, long executionTime, Object data) {
029                    
030                    Iterator<Object> it = monitors.iterator();
031                    while(it.hasNext()){
032                            log(it.next(),pc, type, label, executionTime, data);
033                    }
034            }
035    
036            @Override
037            public void log(ConfigWeb config, String type, String label, long executionTime, Object data) {
038                    
039                    Iterator<Object> it = monitors.iterator();
040                    while(it.hasNext()){
041                            log(it.next(),config, type, label, executionTime, data);
042                    }
043            }
044            
045            @Override
046            public Object getActionMonitor(String name) {
047                    Iterator<Object> it = monitors.iterator();
048                    Object am;
049                    while(it.hasNext()){
050                            am = it.next();
051                            if(name.equalsIgnoreCase(getName(am))) return am;
052                    }
053                    return null;
054            }
055    
056    
057    
058    
059            private String getName(Object am) {
060                    if(getName==null){
061                            try {
062                                    getName=am.getClass().getMethod("getName", new Class[]{});
063                            } 
064                            catch(Throwable t) {
065                                    t.printStackTrace();
066                                    return null;
067                            }
068                    }
069                    
070                    try {
071                            return (String) getName.invoke(am, new Object[]{});
072                    }
073                    catch (Throwable t) {
074                            t.printStackTrace();
075                    }
076                    return null;
077            }
078            
079            private void log(Object monitor, PageContext pc, String type, String label, long executionTime, Object data) {
080                    if(logpc==null){
081                            try {
082                                    logpc=monitor.getClass().getMethod("log", new Class[]{PageContext.class,String.class,String.class,long.class,Object.class});
083                            } 
084                            catch (Throwable t) {
085                                    t.printStackTrace();
086                                    return;
087                            }
088                    }
089                    
090                    try {
091                            logpc.invoke(monitor, new Object[]{pc,type,label,executionTime,data});
092                    }
093                    catch (Throwable t) {
094                            t.printStackTrace();
095                    }
096            }
097            
098            private void log(Object monitor, ConfigWeb config, String type, String label, long executionTime, Object data) {
099                    if(logc==null){
100                            try {
101                                    logc=monitor.getClass().getMethod("log", new Class[]{ConfigWeb.class,String.class,String.class,long.class,Object.class});
102                            } 
103                            catch (Throwable t) {
104                                    t.printStackTrace();
105                                    return;
106                            }
107                    }
108                    
109                    try {
110                            logc.invoke(monitor, new Object[]{config,type,label,executionTime,data});
111                    }
112                    catch (Throwable t) {
113                            t.printStackTrace();
114                    }
115            }
116            
117    
118            private Object init(Object monitor, ConfigServer cs, String name, boolean log) {
119                    if(init==null){
120                            try {
121                                    init=monitor.getClass().getMethod("init", new Class[]{ConfigServer.class,String.class,boolean.class});
122                            } 
123                            catch (Throwable t) {
124                                    t.printStackTrace();
125                                    return null;
126                            }
127                    }
128                    
129                    try {
130                            return init.invoke(monitor, new Object[]{cs,name,log});
131                    }
132                    catch (Throwable t) {
133                            t.printStackTrace();
134                            return null;
135                    }
136            }
137    }