1 package org
.argeo
.slc
.ant
.structure
;
4 import java
.util
.Vector
;
6 import org
.apache
.tools
.ant
.BuildException
;
7 import org
.apache
.tools
.ant
.Target
;
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
.StructurePath
;
17 import org
.argeo
.slc
.core
.structure
.StructureRegistry
;
18 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
20 /** Ant task that can be registered within a structure. */
21 public abstract class SAwareTask
extends AbstractSpringTask
implements
23 private TreeSPath path
;
24 private final List
<AbstractSpringArg
> sAwareArgs
= new Vector
<AbstractSpringArg
>();
26 private StructureElementArg structureElementArg
;
29 public void init() throws BuildException
{
30 StructureRegistry registry
= getRegistry();
31 Target target
= getOwningTarget();
33 TreeSPath targetPath
= createTargetPath(target
);
34 SimpleSElement targetElement
= (SimpleSElement
) registry
35 .getElement(createTargetPath(target
));
37 if (targetElement
== null) {
38 targetElement
= new SimpleSElement(target
.getDescription(),
40 registry
.register(targetPath
, targetElement
);
45 * Includes this arg in the checks for propagation of structure related
48 protected void addSAwareArg(AbstractSpringArg arg
) {
54 * Called by Ant at runtime. Decides whether to call the actions depending
55 * of the mode of the underlying structure registry.
57 * @see #executeActions
58 * @see StructureRegistry
60 public final void execute() throws BuildException
{
61 // register the task in the structure
62 TreeSPath targetPath
= createTargetPath(getOwningTarget());
63 TreeSPath taskPath
= targetPath
.createChild(getTaskName()
64 + targetPath
.listChildren(getRegistry()).size());
65 if (structureElementArg
!= null)
66 getRegistry().register(taskPath
,
67 structureElementArg
.getStructureElement());
69 getRegistry().register(taskPath
, this);
73 // notify registered args
74 for (AbstractSpringArg arg
: sAwareArgs
) {
75 Object obj
= arg
.getBeanInstance();
77 if (obj
instanceof StructureAware
) {
78 StructureAware sAwareT
= (StructureAware
) obj
;
79 sAwareT
.notifyCurrentPath(getRegistry(), taskPath
);
83 // execute depending on the registry mode
84 String mode
= getRegistry().getMode();
85 if (mode
.equals(StructureRegistry
.ALL
)) {
87 } else if (mode
.equals(StructureRegistry
.ACTIVE
)) {
88 List
<StructurePath
> activePaths
= getRegistry().getActivePaths();
90 if (activePaths
.contains(targetPath
)) {
91 if (activePaths
.contains(taskPath
)) {
99 /** Actions to be executed by the implementor. */
100 protected abstract void executeActions(String mode
);
102 /** Create a reference to an external structure element. */
103 public StructureElementArg
createStructureElement() {
104 if (structureElementArg
!= null)
105 throw new SlcAntException("Arg already set.");
106 structureElementArg
= new StructureElementArg();
107 return structureElementArg
;
110 /** Gets the underlying structure registry. */
111 protected StructureRegistry
getRegistry() {
112 return (StructureRegistry
) getProject().getReference(
113 SlcProjectHelper
.REF_STRUCTURE_REGISTRY
);
116 /** Creates the path for a given Ant target. */
117 protected static TreeSPath
createTargetPath(Target target
) {
118 TreeSPath projectPath
= SlcProjectHelper
.getProjectPath(target
120 return projectPath
.createChild(target
.getName());
123 /** Gets the path under which this task is registered. */
124 public TreeSPath
getPath() {
128 public String
getLabel() {
129 String description
= super.getDescription();
130 if (description
== null) {
131 return "<no task def>";
139 class StructureElementArg
extends AbstractSpringArg
{
140 public StructureElement
getStructureElement() {
141 return (StructureElement
) getBeanInstance();