]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc/src/main/java/org/argeo/slc/ant/structure/SAwareTask.java
Add test result part
[gpl/argeo-slc.git] / org.argeo.slc / src / main / java / org / argeo / slc / ant / structure / SAwareTask.java
1 package org.argeo.slc.ant.structure;
2
3 import java.util.List;
4 import java.util.Vector;
5
6 import org.apache.tools.ant.BuildException;
7 import org.apache.tools.ant.Target;
8
9 import org.argeo.slc.ant.SlcProjectHelper;
10 import org.argeo.slc.ant.spring.AbstractSpringArg;
11 import org.argeo.slc.ant.spring.AbstractSpringTask;
12 import org.argeo.slc.core.structure.StructureAware;
13 import org.argeo.slc.core.structure.StructurePath;
14 import org.argeo.slc.core.structure.StructureRegistry;
15 import org.argeo.slc.core.structure.tree.DefaultTreeSAware;
16 import org.argeo.slc.core.structure.tree.TreeSElement;
17 import org.argeo.slc.core.structure.tree.TreeSPath;
18
19 /** Ant task that can be registered within a structure. */
20 public abstract class SAwareTask extends AbstractSpringTask {
21 private final DefaultTreeSAware sAware = new DefaultTreeSAware();
22 private final List<AbstractSpringArg> sAwareArgs = new Vector<AbstractSpringArg>();
23
24 @Override
25 public void init() throws BuildException {
26 StructureRegistry registry = getRegistry();
27 Target target = getOwningTarget();
28
29 TreeSPath targetPath = createTargetPath(target);
30 TreeSElement targetElement = (TreeSElement) registry
31 .getElement(createTargetPath(target));
32
33 if (targetElement == null) {
34 targetElement = new TreeSElement(target.getDescription(),
35 "<no target desc>");
36 registry.register(targetPath, targetElement);
37 }
38
39 TreeSElement taskElement = new TreeSElement(getDescription(),
40 "<no task desc>");
41 sAware.setElement(taskElement);
42 }
43
44 protected void addSAwareArg(AbstractSpringArg arg) {
45 sAwareArgs.add(arg);
46 }
47
48 @Override
49 /**
50 * Called by Ant at runtime. Decides whether to call the actions depending
51 * of the mode of the underlying structure registry.
52 *
53 * @see #executeActions
54 * @see StructureRegistry
55 */
56 public final void execute() throws BuildException {
57 // init registered args
58 for (AbstractSpringArg arg : sAwareArgs) {
59 Object obj = arg.getBeanInstance();
60
61 if (obj instanceof StructureAware && sAware != null) {
62 StructureAware sAwareT = (StructureAware) obj;
63 sAware.addToPropagationList(arg.getBean(), sAwareT);
64 }
65 }
66
67 // register the task in the structure
68 TreeSPath targetPath = createTargetPath(getOwningTarget());
69 TreeSPath taskPath = targetPath.createChild(getTaskName()
70 + targetPath.listChildren(getRegistry()).size());
71 getRegistry().register(taskPath, sAware);
72
73 // execute depending on the registry mode
74 String mode = getRegistry().getMode();
75 if (mode.equals(StructureRegistry.ALL)) {
76 executeActions(mode);
77 } else if (mode.equals(StructureRegistry.ACTIVE)) {
78 List<StructurePath> activePaths = getRegistry().getActivePaths();
79
80 if (activePaths.contains(targetPath)) {
81 if (activePaths.contains(taskPath)) {
82 executeActions(mode);
83 }
84 }
85 }
86
87 }
88
89 /** Actions to be executed by the implementor. */
90 protected abstract void executeActions(String mode);
91
92 /** Gets the underlying structure registry. */
93 protected StructureRegistry getRegistry() {
94 return (StructureRegistry) getProject().getReference(
95 SlcProjectHelper.REF_STRUCTURE_REGISTRY);
96 }
97
98 /** Creates the path for a given Ant target. */
99 protected static TreeSPath createTargetPath(Target target) {
100 TreeSPath projectPath = SlcProjectHelper.getProjectPath(target
101 .getProject());
102 return projectPath.createChild(target.getName());
103 }
104 }