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