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
.SlcProjectHelper
;
10 import org
.argeo
.slc
.ant
.spring
.AbstractSpringArg
;
11 import org
.argeo
.slc
.ant
.spring
.AbstractSpringTask
;
12 import org
.argeo
.slc
.core
.structure
.StructureAware
;
13 import org
.argeo
.slc
.core
.structure
.StructurePath
;
14 import org
.argeo
.slc
.core
.structure
.StructureRegistry
;
15 import org
.argeo
.slc
.core
.structure
.tree
.DefaultTreeSAware
;
16 import org
.argeo
.slc
.core
.structure
.tree
.TreeSElement
;
17 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
19 /** Ant task that can be registered within a structure. */
20 public abstract class SAwareTask
extends AbstractSpringTask
{
21 private final DefaultTreeSAware sAware
= new DefaultTreeSAware();
22 private final List
<AbstractSpringArg
> sAwareArgs
= new Vector
<AbstractSpringArg
>();
25 public void init() throws BuildException
{
26 StructureRegistry registry
= getRegistry();
27 Target target
= getOwningTarget();
29 TreeSPath targetPath
= createTargetPath(target
);
30 TreeSElement targetElement
= (TreeSElement
) registry
31 .getElement(createTargetPath(target
));
33 if (targetElement
== null) {
34 targetElement
= new TreeSElement(target
.getDescription(),
36 registry
.register(targetPath
, targetElement
);
39 TreeSElement taskElement
= new TreeSElement(getDescription(),
41 sAware
.setElement(taskElement
);
44 protected void addSAwareArg(AbstractSpringArg arg
) {
50 * Called by Ant at runtime. Decides whether to call the actions depending
51 * of the mode of the underlying structure registry.
53 * @see #executeActions
54 * @see StructureRegistry
56 public final void execute() throws BuildException
{
57 // init registered args
58 for (AbstractSpringArg arg
: sAwareArgs
) {
59 Object obj
= arg
.getBeanInstance();
61 if (obj
instanceof StructureAware
&& sAware
!= null) {
62 StructureAware sAwareT
= (StructureAware
) obj
;
63 sAware
.addToPropagationList(arg
.getBean(), sAwareT
);
67 // register the task in the structure
68 TreeSPath targetPath
= createTargetPath(getOwningTarget());
69 TreeSPath taskPath
= targetPath
.createChild(getTaskName()
70 + targetPath
.listChildren(getRegistry()).size());
71 getRegistry().register(taskPath
, sAware
);
73 // execute depending on the registry mode
74 String mode
= getRegistry().getMode();
75 if (mode
.equals(StructureRegistry
.ALL
)) {
77 } else if (mode
.equals(StructureRegistry
.ACTIVE
)) {
78 List
<StructurePath
> activePaths
= getRegistry().getActivePaths();
80 if (activePaths
.contains(targetPath
)) {
81 if (activePaths
.contains(taskPath
)) {
89 /** Actions to be executed by the implementor. */
90 protected abstract void executeActions(String mode
);
92 /** Gets the underlying structure registry. */
93 protected StructureRegistry
getRegistry() {
94 return (StructureRegistry
) getProject().getReference(
95 SlcProjectHelper
.REF_STRUCTURE_REGISTRY
);
98 /** Creates the path for a given Ant target. */
99 protected static TreeSPath
createTargetPath(Target target
) {
100 TreeSPath projectPath
= SlcProjectHelper
.getProjectPath(target
102 return projectPath
.createChild(target
.getName());