]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/ant/structure/SAwareTask.java
Introduce Context Unit Tests.
[gpl/argeo-slc.git] / org.argeo.slc.core / 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.SlcAntException;
10 import org.argeo.slc.ant.SlcProjectHelper;
11 import org.argeo.slc.ant.spring.AbstractSpringArg;
12 import org.argeo.slc.ant.spring.AbstractSpringTask;
13 import org.argeo.slc.core.structure.SimpleSElement;
14 import org.argeo.slc.core.structure.StructureAware;
15 import org.argeo.slc.core.structure.StructureElement;
16 import org.argeo.slc.core.structure.StructurePath;
17 import org.argeo.slc.core.structure.StructureRegistry;
18 import org.argeo.slc.core.structure.tree.TreeSPath;
19
20 /** Ant task that can be registered within a structure. */
21 public abstract class SAwareTask extends AbstractSpringTask implements
22 StructureElement {
23 private TreeSPath path;
24 private final List<AbstractSpringArg> sAwareArgs = new Vector<AbstractSpringArg>();
25
26 private StructureElementArg structureElementArg;
27
28 @Override
29 public void init() throws BuildException {
30 StructureRegistry registry = getRegistry();
31 Target target = getOwningTarget();
32
33 TreeSPath targetPath = createTargetPath(target);
34 SimpleSElement targetElement = (SimpleSElement) registry
35 .getElement(createTargetPath(target));
36
37 if (targetElement == null) {
38 targetElement = new SimpleSElement(target.getDescription(),
39 "<no target desc>");
40 registry.register(targetPath, targetElement);
41 }
42 }
43
44 /**
45 * Includes this arg in the checks for propagation of structure related
46 * information.
47 */
48 protected void addSAwareArg(AbstractSpringArg arg) {
49 sAwareArgs.add(arg);
50 }
51
52 @Override
53 /**
54 * Called by Ant at runtime. Decides whether to call the actions depending
55 * of the mode of the underlying structure registry.
56 *
57 * @see #executeActions
58 * @see StructureRegistry
59 */
60 public final void execute() throws BuildException {
61 // register the task in the structure
62 TreeSPath targetPath = createTargetPath(getOwningTarget());
63 TreeSPath taskPath = targetPath.createChild(getTaskName()
64 + targetPath.listChildren(getRegistry()).size());
65 if (structureElementArg != null)
66 getRegistry().register(taskPath,
67 structureElementArg.getStructureElement());
68 else
69 getRegistry().register(taskPath, this);
70
71 path = taskPath;
72
73 // notify registered args
74 for (AbstractSpringArg arg : sAwareArgs) {
75 Object obj = arg.getBeanInstance();
76
77 if (obj instanceof StructureAware) {
78 StructureAware sAwareT = (StructureAware) obj;
79 sAwareT.notifyCurrentPath(getRegistry(), taskPath);
80 }
81 }
82
83 // execute depending on the registry mode
84 String mode = getRegistry().getMode();
85 if (mode.equals(StructureRegistry.ALL)) {
86 executeActions(mode);
87 } else if (mode.equals(StructureRegistry.ACTIVE)) {
88 List<StructurePath> activePaths = getRegistry().getActivePaths();
89
90 if (activePaths.contains(targetPath)) {
91 if (activePaths.contains(taskPath)) {
92 executeActions(mode);
93 }
94 }
95 }
96
97 }
98
99 /** Actions to be executed by the implementor. */
100 protected abstract void executeActions(String mode);
101
102 /** Create a reference to an external structure element. */
103 public StructureElementArg createStructureElement() {
104 if (structureElementArg != null)
105 throw new SlcAntException("Arg already set.");
106 structureElementArg = new StructureElementArg();
107 return structureElementArg;
108 }
109
110 /** Gets the underlying structure registry. */
111 protected StructureRegistry getRegistry() {
112 return (StructureRegistry) getProject().getReference(
113 SlcProjectHelper.REF_STRUCTURE_REGISTRY);
114 }
115
116 /** Creates the path for a given Ant target. */
117 protected static TreeSPath createTargetPath(Target target) {
118 TreeSPath projectPath = SlcProjectHelper.getProjectPath(target
119 .getProject());
120 return projectPath.createChild(target.getName());
121 }
122
123 /** Gets the path under which this task is registered. */
124 public TreeSPath getPath() {
125 return path;
126 }
127
128 public String getLabel() {
129 String description = super.getDescription();
130 if (description == null) {
131 return "<no task def>";
132 } else {
133 return description;
134 }
135 }
136
137 }
138
139 class StructureElementArg extends AbstractSpringArg {
140 public StructureElement getStructureElement() {
141 return (StructureElement) getBeanInstance();
142 }
143 }