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