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