]> git.argeo.org Git - gpl/argeo-slc.git/blob - SAwareTask.java
830c13ae3cfeb0f8fffcfb66d32d718786401165
[gpl/argeo-slc.git] / 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.SpringArg;
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<SpringArg> sAwareArgs = new Vector<SpringArg>();
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(SpringArg 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 (SpringArg 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 public <T> T getBean(String beanName) {
114 return (T) getContext().getBean(beanName);
115 }
116
117 /** Create a reference to an external structure element. */
118 public StructureElementArg createStructureElement() {
119 if (structureElementArg != null)
120 throw new SlcAntException("Arg already set.");
121 structureElementArg = new StructureElementArg();
122 return structureElementArg;
123 }
124
125 /** Gets the underlying structure registry. */
126 protected StructureRegistry<TreeSPath> getRegistry() {
127 return (StructureRegistry<TreeSPath>) getProject().getReference(
128 SlcProjectHelper.REF_STRUCTURE_REGISTRY);
129 }
130
131 /** Creates the treeSPath for a given Ant target. */
132 protected static TreeSPath createTargetPath(Target target) {
133 TreeSPath projectPath = (TreeSPath) target.getProject().getReference(
134 SlcProjectHelper.REF_PROJECT_PATH);
135 return projectPath.createChild(target.getName());
136 }
137
138 /** Gets the treeSPath under which this task is registered. */
139 public TreeSPath getTreeSPath() {
140 return treeSPath;
141 }
142
143 public String getLabel() {
144 String description = super.getDescription();
145 if (description == null) {
146 return "<no task def>";
147 } else {
148 return description;
149 }
150 }
151
152 public void setPath(String path) {
153 this.path = path;
154 }
155 }
156
157 class StructureElementArg extends SpringArg {
158 public StructureElement getStructureElement() {
159 return (StructureElement) getBeanInstance();
160 }
161 }