1
2
3
4 package org.jdiagnose.remote.template;
5
6 import java.util.Collections;
7 import java.util.HashMap;
8 import java.util.Map;
9
10 import org.jdiagnose.MessageFactory;
11 import org.jdiagnose.message.ExceptionMessageFactory;
12 import org.jdiagnose.remote.Agent;
13 import org.jdiagnose.remote.DefaultResultInfo;
14 import org.jdiagnose.remote.RemoteResultListener;
15 import org.jdiagnose.runtime.ResultState;
16
17 /***
18 * @author jamie
19 */
20 public class DiagnosticTemplate {
21
22 private RemoteResultListener listener;
23 private Agent agent;
24 private boolean filterSuccesses = false;
25 private Map previousResults = Collections.synchronizedMap(new HashMap());
26 private ExceptionMatcher exceptionMatcher = DefaultExceptionMatcher.DEFAULT_EXCEPTION_MATCHER;
27 private MessageFactory messageFactory = ExceptionMessageFactory.DEFAULT_EXCEPTION_MESSAGE_FACTORY;
28
29 public DiagnosticTemplate() {
30
31 }
32
33 public DiagnosticTemplate(Agent remoteResultFactory, RemoteResultListener listener) {
34 this.listener = listener;
35 this.agent = remoteResultFactory;
36 }
37
38 public DiagnosticTemplate(Agent remoteResultFactory, RemoteResultListener listener, boolean filterSuccesses) {
39 this.listener = listener;
40 this.agent = remoteResultFactory;
41 this.filterSuccesses = filterSuccesses;
42 }
43
44 public Object execute(String name, DiagnosticCallback callback) throws Throwable {
45 long startTime = System.currentTimeMillis();
46 long finishTime = startTime;
47 Object result = null;
48 Throwable throwable = null;
49 try {
50 return callback.invoke();
51 } catch (Throwable t) {
52 throwable = t;
53 } finally {
54 finishTime = System.currentTimeMillis();
55 }
56 boolean exceptionMatched = throwable == null ? false : exceptionMatcher.match(throwable);
57 if(filterSuccesses) {
58 if(exceptionMatched) {
59 Boolean succeeded = (Boolean) previousResults.get(name);
60 if(succeeded == Boolean.FALSE) {
61 listener.onRemoteResult(
62 agent.createRemoteResult(
63 new DefaultResultInfo(name, ResultState.SUCCEEDED,
64 finishTime - startTime, null,
65 startTime, finishTime)));
66 previousResults.put(name, Boolean.TRUE);
67 }
68 } else {
69 listener.onRemoteResult(
70 agent.createRemoteResult(
71 new DefaultResultInfo(name, ResultState.FAILED,
72 finishTime - startTime,
73 messageFactory.getMessage(throwable),
74 startTime, finishTime)));
75 previousResults.put(name, Boolean.FALSE);
76 }
77 } else {
78 listener.onRemoteResult(
79 agent.createRemoteResult(
80 new DefaultResultInfo(name,
81 exceptionMatched ? ResultState.SUCCEEDED : ResultState.FAILED,
82 finishTime - startTime,
83 throwable != null ? messageFactory.getMessage(throwable) : null,
84 startTime, finishTime)));
85 }
86 if(throwable != null) {
87 throw throwable;
88 }
89 return result;
90 }
91
92 /***
93 * @param exceptionMatcher The exceptionMatcher to set.
94 */
95 public void setExceptionMatcher(ExceptionMatcher exceptionMatcher) {
96 this.exceptionMatcher = exceptionMatcher;
97 }
98
99 /***
100 * @param agent The agent to set.
101 */
102 public void setAgent(Agent agent) {
103 this.agent = agent;
104 }
105 /***
106 * @param filterSuccesses The filterSuccesses to set.
107 */
108 public void setFilterSuccesses(boolean filterSuccesses) {
109 this.filterSuccesses = filterSuccesses;
110 }
111 /***
112 * @param listener The listener to set.
113 */
114 public void setListener(RemoteResultListener listener) {
115 this.listener = listener;
116 }
117 }