1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jdiagnose;
17
18 import java.lang.reflect.InvocationTargetException;
19 import java.lang.reflect.Method;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24
25 /***
26 * Used by DiagnosticUnit
27 *
28 * @see org.jdiagnose.DiagnosticUnit
29 *
30 * User: jamie Date: May 29, 2004 Time: 11:14:24 PM
31 */
32 public class DelegatingDiagnosticUnit implements DiagnosticContainer, Diagnostic {
33
34 private DiagnosticUnitLifeCycle delegate;
35
36 private List diagnostics;
37
38 private String methodPrefix = "diagnose";
39
40 public DelegatingDiagnosticUnit(DiagnosticUnitLifeCycle delegate) {
41 this.delegate = delegate;
42 this.diagnostics = getReflectedDiagnostics();
43 }
44
45 public String getName() {
46 String className = delegate.getClass().getName();
47 return className.substring(className.lastIndexOf('.') + 1);
48 }
49
50 protected List getReflectedDiagnostics() {
51 List diagnostics = new ArrayList();
52 Method[] methods = delegate.getClass().getMethods();
53 for (int i = 0; i < methods.length; i++) {
54 final Method method = methods[i];
55 final String methodName = method.getName();
56 if (methodName.startsWith(methodPrefix)
57 && !methodName.equals(methodPrefix)) {
58 diagnostics.add(new Diagnostic() {
59 public String getName() {
60 return delegate.getName() + '.'
61 + methodName.substring(methodPrefix.length());
62 }
63
64 public void diagnose() throws DiagnosticException {
65 try {
66 delegate.setUp();
67 method.invoke(delegate, null);
68 delegate.tearDown();
69 } catch (IllegalAccessException e) {
70 throw new DiagnosticException(e);
71 } catch (InvocationTargetException e) {
72 Throwable t = e.getTargetException();
73 if (t instanceof DiagnosticException) {
74 throw (DiagnosticException) t;
75 } else {
76 throw new DiagnosticException(e
77 .getTargetException());
78 }
79 } catch (Exception e) {
80 throw new DiagnosticException(e);
81 }
82 }
83
84 public List getDiagnostics() {
85 return Collections.EMPTY_LIST;
86 }
87
88 });
89 }
90 }
91 return diagnostics;
92 }
93
94 public List getDiagnostics() {
95 return diagnostics;
96 }
97
98
99
100
101 public List getDiagnosticContainers() {
102 return Collections.EMPTY_LIST;
103 }
104
105
106
107
108 public void diagnose() throws DiagnosticException {
109 for (Iterator diagnosticIterator = diagnostics.iterator(); diagnosticIterator.hasNext();) {
110 Diagnostic diagnostic = (Diagnostic) diagnosticIterator.next();
111 diagnostic.diagnose();
112 }
113 }
114
115 }