]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/ant/structure/SAwareTask.java
Simplify ANt-Spring bridge
[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.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 String path;
23 private TreeSPath treeSPath;
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<TreeSPath> 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 if (path == null) {
62 // register the task in the structure
63 TreeSPath targetPath = createTargetPath(getOwningTarget());
64 TreeSPath taskPath = targetPath.createChild(getTaskName()
65 + targetPath.listChildren(getRegistry()).size());
66
67 treeSPath = taskPath;
68 } else {
69 treeSPath = TreeSPath.parseToCreatePath(path);
70 }
71
72 if (structureElementArg != null)
73 getRegistry().register(treeSPath,
74 structureElementArg.getStructureElement());
75 else
76 getRegistry().register(treeSPath, this);
77
78 // notify registered args
79 for (AbstractSpringArg arg : sAwareArgs) {
80 Object obj = arg.getBeanInstance();
81
82 if (obj instanceof StructureAware) {
83 StructureAware<TreeSPath> sAwareT = (StructureAware<TreeSPath>) obj;
84 sAwareT.notifyCurrentPath(getRegistry(), treeSPath);
85 }
86 }
87
88 // execute depending on the registry mode
89 String mode = getRegistry().getMode();
90 if (mode.equals(StructureRegistry.ALL)) {
91 executeActions(mode);
92 } else if (mode.equals(StructureRegistry.ACTIVE)) {
93 List<TreeSPath> activePaths = getRegistry().getActivePaths();
94
95 if (activePaths.contains(treeSPath)) {
96 if (activePaths.contains(treeSPath)) {
97 executeActions(mode);
98 }
99 }
100 }
101
102 }
103
104 /** Actions to be executed by the implementor. */
105 protected abstract void executeActions(String mode);
106
107 /** Create a reference to an external structure element. */
108 public StructureElementArg createStructureElement() {
109 if (structureElementArg != null)
110 throw new SlcAntException("Arg already set.");
111 structureElementArg = new StructureElementArg();
112 return structureElementArg;
113 }
114
115 /** Gets the underlying structure registry. */
116 protected StructureRegistry<TreeSPath> getRegistry() {
117 return (StructureRegistry<TreeSPath>) getProject().getReference(
118 SlcProjectHelper.REF_STRUCTURE_REGISTRY);
119 }
120
121 /** Creates the treeSPath for a given Ant target. */
122 protected static TreeSPath createTargetPath(Target target) {
123 TreeSPath projectPath = (TreeSPath) target.getProject().getReference(
124 SlcProjectHelper.REF_PROJECT_PATH);
125 return projectPath.createChild(target.getName());
126 }
127
128 /** Gets the treeSPath under which this task is registered. */
129 public TreeSPath getTreeSPath() {
130 return treeSPath;
131 }
132
133 public String getLabel() {
134 String description = super.getDescription();
135 if (description == null) {
136 return "<no task def>";
137 } else {
138 return description;
139 }
140 }
141
142 public void setPath(String path) {
143 this.path = path;
144 }
145 }
146
147 class StructureElementArg extends AbstractSpringArg {
148 public StructureElement getStructureElement() {
149 return (StructureElement) getBeanInstance();
150 }
151 }