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
.StructureRegistry
;
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
implements
23 private TreeSPath treeSPath
;
24 private final List
<AbstractSpringArg
> sAwareArgs
= new Vector
<AbstractSpringArg
>();
26 private StructureElementArg structureElementArg
;
29 public void init() throws BuildException
{
30 StructureRegistry
<TreeSPath
> 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
{
62 // register the task in the structure
63 TreeSPath targetPath
= createTargetPath(getOwningTarget());
64 TreeSPath taskPath
= targetPath
.createChild(getTaskName()
65 + targetPath
.listChildren(getRegistry()).size());
69 treeSPath
= TreeSPath
.parseToCreatePath(path
);
72 if (structureElementArg
!= null)
73 getRegistry().register(treeSPath
,
74 structureElementArg
.getStructureElement());
76 getRegistry().register(treeSPath
, this);
78 // notify registered args
79 for (AbstractSpringArg arg
: sAwareArgs
) {
80 Object obj
= arg
.getBeanInstance();
82 if (obj
instanceof StructureAware
) {
83 StructureAware
<TreeSPath
> sAwareT
= (StructureAware
<TreeSPath
>) obj
;
84 sAwareT
.notifyCurrentPath(getRegistry(), treeSPath
);
88 // execute depending on the registry mode
89 String mode
= getRegistry().getMode();
90 if (mode
.equals(StructureRegistry
.ALL
)) {
92 } else if (mode
.equals(StructureRegistry
.ACTIVE
)) {
93 List
<TreeSPath
> activePaths
= getRegistry().getActivePaths();
95 if (activePaths
.contains(treeSPath
)) {
96 if (activePaths
.contains(treeSPath
)) {
104 /** Actions to be executed by the implementor. */
105 protected abstract void executeActions(String mode
);
107 /** Create a reference to an external structure element. */
108 public StructureElementArg
createStructureElement() {
109 if (structureElementArg
!= null)
110 throw new SlcAntException("Arg already set.");
111 structureElementArg
= new StructureElementArg();
112 return structureElementArg
;
115 /** Gets the underlying structure registry. */
116 protected StructureRegistry
<TreeSPath
> getRegistry() {
117 return (StructureRegistry
<TreeSPath
>) getProject().getReference(
118 SlcProjectHelper
.REF_STRUCTURE_REGISTRY
);
121 /** Creates the treeSPath for a given Ant target. */
122 protected static TreeSPath
createTargetPath(Target target
) {
123 TreeSPath projectPath
= (TreeSPath
) target
.getProject().getReference(
124 SlcProjectHelper
.REF_PROJECT_PATH
);
125 return projectPath
.createChild(target
.getName());
128 /** Gets the treeSPath under which this task is registered. */
129 public TreeSPath
getTreeSPath() {
133 public String
getLabel() {
134 String description
= super.getDescription();
135 if (description
== null) {
136 return "<no task def>";
142 public void setPath(String path
) {
147 class StructureElementArg
extends AbstractSpringArg
{
148 public StructureElement
getStructureElement() {
149 return (StructureElement
) getBeanInstance();