001    package org.shiftone.jrat.api;
002    
003    
004    import org.shiftone.jrat.core.HandlerFactory;
005    import org.shiftone.jrat.core.spi.MethodHandler;
006    import org.shiftone.jrat.util.time.Clock;
007    
008    import java.lang.reflect.Method;
009    
010    
011    /**
012     * @author jeff@shiftone.org (Jeff Drost)
013     */
014    public class Monitor {
015    
016        private final MethodHandler handler;
017    
018        public Monitor(MethodHandler handler) {
019            this.handler = handler;
020        }
021    
022    //    public Monitor(Method method) {
023    //        this.handler = HandlerFactory.getMethodHandler(method);
024    //    }
025    
026    
027        public Monitor(String className, String methodName, String signature) {
028            this.handler = HandlerFactory.getMethodHandler(className, methodName, signature);
029        }
030    
031    
032        public Monitor(Class klass, String methodName, String signature) {
033            this.handler = HandlerFactory.getMethodHandler(klass.getName(), methodName, signature);
034        }
035    
036    
037        public Monitor(Class klass, String methodName) {
038            this.handler = HandlerFactory.getMethodHandler(klass.getName(), methodName, "()V");
039        }
040    
041    
042        public Object execute(Object instance, Command command) throws Throwable {
043    
044            try {
045                return execute(handler, instance, command);
046            }
047            catch (RuntimeException e) {
048                throw e;
049            }
050            catch (Throwable e) {
051                throw new RuntimeException(e);
052            }
053        }
054    
055    
056        public Object execute(Command command) {
057    
058            try {
059                return execute(handler, null, command);
060            }
061            catch (RuntimeException e) {
062                throw e;
063            }
064            catch (Throwable e) {
065                throw new RuntimeException(e);
066            }
067        }
068    
069        public static Object execute(Method method, Object instance, Command command) throws Throwable {
070            // return execute(HandlerFactory.getMethodHandler(method), instance, command);
071            return null;
072        }
073    
074    
075        public static Object execute(MethodHandler handler, Object instance, Command command) throws Throwable {
076    
077            handler.onMethodStart(instance);
078    
079            long startTime = Clock.currentTimeMillis();
080    
081            try {
082                Object result = command.execute();
083    
084                handler.onMethodFinish(instance, Clock.currentTimeMillis() - startTime, null);
085    
086                return result;
087            }
088            catch (Throwable throwable) {
089                handler.onMethodFinish(instance, Clock.currentTimeMillis() - startTime, throwable);
090    
091                throw throwable;
092            }
093        }
094    
095    
096        private static void main(String[] args) {
097    
098            Monitor monitor = new Monitor(Monitor.class, "foo");
099    
100            monitor.execute(new Callable() {
101    
102                public void run() {
103    
104                    // do some work here...
105                }
106            });
107        }
108    }