View Javadoc

1   package com.philemonworks.selfdiagnose;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.xml.sax.Attributes;
7   
8   public class CollectionIteratorTask extends DiagnosticTask {
9       private static final long serialVersionUID = -6424385910785821011L;
10  
11      private ArrayList tasks = new ArrayList();
12      
13      private String expression = "";
14      
15      /**
16       * This method is sent from the SelfDiagnoseHandler when a configuration is being processed. Use the passed
17       * attributes to initialize the receiver.
18       * If a variable parameter is passed then store it.
19       * 
20       * @param attributes
21       *        org.xml.sax.Attributes
22       */
23      public void initializeFromAttributes(Attributes attributes){
24          super.initializeFromAttributes(attributes);  
25          this.setExpression(attributes.getValue("value"));
26      }
27  
28      public String getDescription() {
29          return "Iterates over elements from the collection value";
30      }
31      public DiagnosticTaskResult createResult(){
32          return new CompositeDiagnosticTaskResult(this);
33      }
34      public void run(ExecutionContext ctx, DiagnosticTaskResult result) throws DiagnoseException {
35          Object collection = ctx.resolveValue(expression);
36          if (collection == null) {
37              result.setErrorMessage("Expression ["+expression+"] does not evaluate to a collection;  found null instead");
38              return;            
39          }
40          if (!canBeIterated(collection)) {
41              result.setErrorMessage("Expression does not evaluate to a collection;  found ["+collection+"] instead");
42              return;
43          }
44          int count = 0;
45          if (!(result instanceof CompositeDiagnosticTaskResult)) throw new RuntimeException("bug"); 
46          CompositeDiagnosticTaskResult composedResult = (CompositeDiagnosticTaskResult)result;
47          // either a List or an Array
48          if (collection.getClass().isArray()) {
49              Object[] array = (Object[])collection;
50              for (int i=0;i<array.length;i++) {                
51                  this.iterateUsingEach(ctx, composedResult, array[i]);
52                  count = count + tasks.size();
53              }
54          } else if (collection instanceof List) {
55              List list = (List)collection;
56              for (int i=0;i<list.size();i++) {                
57                  this.iterateUsingEach(ctx, composedResult, list.get(i));
58                  count = count + tasks.size();
59              }
60          }
61          int failed = composedResult.howManyErrors();
62          if (failed > 0) {
63              result.setFailedMessage(failed + " out of " + count + " checks failed.");
64          } else {
65              result.setPassedMessage("All " + count + " checks passed");
66          }        
67      }
68      private void iterateUsingEach(ExecutionContext ctx,CompositeDiagnosticTaskResult composedResult, Object each) throws DiagnoseException {
69          for (int t=0;t<tasks.size();t++) {
70              DiagnosticTask eachTask = (DiagnosticTask)tasks.get(t);
71              ctx.setValue(this.getVariableName(), each);
72              DiagnosticTaskResult eachResult = eachTask.createResult();
73              eachTask.run(ctx, eachResult);
74              composedResult.addResult(eachResult);
75          }
76      }
77      
78      private boolean canBeIterated(Object value) {
79          return value.getClass().isArray() || value instanceof java.util.List;
80      }
81      
82      public void register(DiagnosticTask task) {
83          tasks.add(task);        
84      }
85      
86      public List getTasks() { return tasks; }
87  
88      public String getExpression() {
89          return expression;
90      }
91  
92      public void setExpression(String expression) {
93          this.expression = expression;
94      }
95  }