1 package net.sourceforge.basher.internal.impl;
2
3 import net.sourceforge.basher.*;
4 import net.sourceforge.basher.annotations.*;
5 import net.sourceforge.basher.internal.TaskConfigurationExtractor;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.impl.NoOpLog;
8
9
10
11
12 public class TaskConfigurationExtractorImpl implements TaskConfigurationExtractor
13 {
14 private Log _log;
15
16 public void setLog(final Log log)
17 {
18 _log = log;
19 }
20
21 public TaskConfiguration extractTaskConfiguration(final Class clazz)
22 {
23 TaskConfiguration taskConfiguration = createDefaultTaskConfiguration();
24
25
26 processAnnotations(clazz, taskConfiguration);
27
28 return taskConfiguration;
29
30 }
31
32 private TaskConfiguration createDefaultTaskConfiguration()
33 {
34
35 return new TaskConfiguration();
36 }
37
38 private void processAnnotations(final Class<? extends Object> taskInstanceClass, final TaskConfiguration taskConfiguration)
39 {
40
41
42 final Class superClass = taskInstanceClass.getSuperclass();
43 if (!superClass.equals(Object.class))
44 {
45 processAnnotations(superClass, taskConfiguration);
46 }
47
48
49 if (taskInstanceClass.isAnnotationPresent(BasherTimedTask.class))
50 {
51 BasherTimedTask annotation = taskInstanceClass.getAnnotation(BasherTimedTask.class);
52
53
54 }
55
56 if (taskInstanceClass.isAnnotationPresent(BasherMaxTime.class))
57 {
58 BasherMaxTime annotation = taskInstanceClass.getAnnotation(BasherMaxTime.class);
59 taskConfiguration.setMaxTime(annotation.time());
60 }
61 if (taskInstanceClass.isAnnotationPresent(BasherMaxInvocations.class))
62 {
63 BasherMaxInvocations annotation = taskInstanceClass.getAnnotation(BasherMaxInvocations.class);
64 taskConfiguration.setMaxInvocations(annotation.max());
65 }
66 if (taskInstanceClass.isAnnotationPresent(BasherInertia.class))
67 {
68 BasherInertia annotation = taskInstanceClass.getAnnotation(BasherInertia.class);
69 taskConfiguration.setInertia(annotation.inertia());
70 }
71 if (taskInstanceClass.isAnnotationPresent(BasherName.class))
72 {
73 BasherName annotation = taskInstanceClass.getAnnotation(BasherName.class);
74 taskConfiguration.setTaskName(((BasherName) annotation).name());
75 }
76 if (taskInstanceClass.isAnnotationPresent(BasherStopAfter.class))
77 {
78 BasherStopAfter annotation = taskInstanceClass.getAnnotation(BasherStopAfter.class);
79 taskConfiguration.setStopAfter(((BasherStopAfter) annotation).time());
80 }
81 if (taskInstanceClass.isAnnotationPresent(BasherWeight.class))
82 {
83 BasherWeight annotation = taskInstanceClass.getAnnotation(BasherWeight.class);
84 taskConfiguration.setWeight(((BasherWeight) annotation).weight());
85 }
86 if (taskInstanceClass.isAnnotationPresent(BasherRunFrom.class))
87 {
88 BasherRunFrom annotation = taskInstanceClass.getAnnotation(BasherRunFrom.class);
89 taskConfiguration.setRunFrom(((BasherRunFrom) annotation).time());
90 }
91
92 if (taskInstanceClass.isAnnotationPresent(BasherPhases.class))
93 {
94 taskConfiguration.clearApplicablePhases();
95 Phase[] phases = taskInstanceClass.getAnnotation(BasherPhases.class).phases();
96 if (phases != null)
97 {
98 for (Phase phase : phases)
99 {
100 taskConfiguration.addApplicablePhase(phase);
101 }
102 }
103 }
104
105 if (taskConfiguration.getApplicablePhases().size() == 0)
106 {
107 _log.warn("No applicable phase specified for task");
108 }
109
110 }
111
112
113 }