]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc/src/main/java/org/argeo/slc/ant/structure/SAwareTask.java
Rename doc directory.
[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.AbstractSpringTask;
11 import org.argeo.slc.core.structure.StructurePath;
12 import org.argeo.slc.core.structure.StructureRegistry;
13 import org.argeo.slc.core.structure.tree.DefaultTreeSAware;
14 import org.argeo.slc.core.structure.tree.TreeSAware;
15 import org.argeo.slc.core.structure.tree.TreeSElement;
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 protected final TreeSAware sAware = new DefaultTreeSAware();
21 protected final List<SAwareArg> sAwareArgs = new Vector<SAwareArg>();
22
23 @Override
24 public void init() throws BuildException {
25 StructureRegistry registry = getRegistry();
26 Target target = getOwningTarget();
27 TreeSElement projectElement = (TreeSElement) registry
28 .getElement(SlcProjectHelper.getProjectPath(getProject()));
29 TreeSElement targetElement = (TreeSElement) registry
30 .getElement(createTargetPath(target));
31
32 if (targetElement == null) {
33 // create target element
34 targetElement = projectElement.createChild(target.getName(), target
35 .getDescription() != null ? target.getDescription()
36 : "<no target>");
37 registry.register(targetElement);
38 }
39
40 TreeSElement taskElement = targetElement.createChild(getTaskName()
41 + targetElement.getChildren().size(),
42 getDescription() != null ? getDescription() : "<no task desc>");
43 sAware.setElement(taskElement);
44 }
45
46 @Override
47 /**
48 * Called by Ant at runtime. Decides whether to call the actions depending
49 * of the mode of the underlying structure registry.
50 *
51 * @see #executeActions
52 * @see StructureRegistry
53 */
54 public final void execute() throws BuildException {
55 for(SAwareArg arg : sAwareArgs){
56 arg.init(sAware);
57 }
58
59 getRegistry().register(sAware);
60
61 String mode = getRegistry().getMode();
62 if (mode.equals(StructureRegistry.ALL)) {
63 executeActions(mode);
64 } else if (mode.equals(StructureRegistry.ACTIVE)) {
65 List<StructurePath> activePaths = getRegistry().getActivePaths();
66
67 StructurePath targetPath = createTargetPath(getOwningTarget());
68 if(activePaths.contains(targetPath)){
69 if (activePaths.contains(sAware.getElement().getPath())) {
70 executeActions(mode);
71 }
72 }
73 }
74
75 }
76
77 /** Actions to be executed by the implementor. */
78 protected abstract void executeActions(String mode);
79
80 /** Gets the underlying structure registry. */
81 protected StructureRegistry getRegistry() {
82 return (StructureRegistry) getProject().getReference(
83 SlcProjectHelper.REF_STRUCTURE_REGISTRY);
84 }
85
86 /** Creates the path for a given Ant target. */
87 protected static StructurePath createTargetPath(Target target) {
88 TreeSPath projectPath = SlcProjectHelper.getProjectPath(target
89 .getProject());
90 return TreeSPath.createChild(projectPath, target.getName());
91 }
92 }