View Javadoc

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   * @author Johan Lindquist
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          // Process the specified class for annotations
26          processAnnotations(clazz, taskConfiguration);
27  
28          return taskConfiguration;
29          
30      }
31  
32      private TaskConfiguration createDefaultTaskConfiguration()
33      {
34          // FIXME: this should really have a 'default' task configuration being provided to it
35          return new TaskConfiguration();
36      }
37  
38      private void processAnnotations(final Class<? extends Object> taskInstanceClass, final TaskConfiguration taskConfiguration)
39      {
40          // First examine super classes for annotations
41          // By doing depth-first here, we allow sub-classes to override super classes annotations
42          final Class superClass = taskInstanceClass.getSuperclass();
43          if (!superClass.equals(Object.class))
44          {
45              processAnnotations(superClass, taskConfiguration);
46          }
47  
48          // Determine whether the task is timed or not
49          if (taskInstanceClass.isAnnotationPresent(BasherTimedTask.class))
50          {
51              BasherTimedTask annotation = taskInstanceClass.getAnnotation(BasherTimedTask.class);
52              // taskConfiguration.setInterval(annotation.interval());
53              // taskConfiguration.setTimed(true);
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 }