]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc/src/main/java/org/argeo/slc/ant/structure/SAwareTask.java
Revert HSQL DB changes (still force shutdown, but reuse connection)
[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 protected void addSAwareArg(AbstractSpringArg arg) {
42 sAwareArgs.add(arg);
43 }
44
45 @Override
46 /**
47 * Called by Ant at runtime. Decides whether to call the actions depending
48 * of the mode of the underlying structure registry.
49 *
50 * @see #executeActions
51 * @see StructureRegistry
52 */
53 public final void execute() throws BuildException {
54 // register the task in the structure
55 TreeSPath targetPath = createTargetPath(getOwningTarget());
56 TreeSPath taskPath = targetPath.createChild(getTaskName()
57 + targetPath.listChildren(getRegistry()).size());
58 getRegistry().register(taskPath, this);
59 path = taskPath;
60
61 // notify registered args
62 for (AbstractSpringArg arg : sAwareArgs) {
63 Object obj = arg.getBeanInstance();
64
65 if (obj instanceof StructureAware) {
66 StructureAware sAwareT = (StructureAware) obj;
67 sAwareT.notifyCurrentPath(getRegistry(), taskPath);
68 }
69 }
70
71 // execute depending on the registry mode
72 String mode = getRegistry().getMode();
73 if (mode.equals(StructureRegistry.ALL)) {
74 executeActions(mode);
75 } else if (mode.equals(StructureRegistry.ACTIVE)) {
76 List<StructurePath> activePaths = getRegistry().getActivePaths();
77
78 if (activePaths.contains(targetPath)) {
79 if (activePaths.contains(taskPath)) {
80 executeActions(mode);
81 }
82 }
83 }
84
85 }
86
87 /** Actions to be executed by the implementor. */
88 protected abstract void executeActions(String mode);
89
90 /** Gets the underlying structure registry. */
91 protected StructureRegistry getRegistry() {
92 return (StructureRegistry) getProject().getReference(
93 SlcProjectHelper.REF_STRUCTURE_REGISTRY);
94 }
95
96 /** Creates the path for a given Ant target. */
97 protected static TreeSPath createTargetPath(Target target) {
98 TreeSPath projectPath = SlcProjectHelper.getProjectPath(target
99 .getProject());
100 return projectPath.createChild(target.getName());
101 }
102
103 public TreeSPath getPath() {
104 return path;
105 }
106
107 @Override
108 public String getDescription() {
109 String description = super.getDescription();
110 if (description == null) {
111 return "<no task def>";
112 } else {
113 return description;
114 }
115 }
116
117 }