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
.SpringArg
;
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
{
22 private TreeSPath treeSPath
;
23 private final List
<SpringArg
> sAwareArgs
= new Vector
<SpringArg
>();
25 private StructureElementArg structureElementArg
;
28 public void init() throws BuildException
{
29 StructureRegistry
<TreeSPath
> registry
= getRegistry();
30 Target target
= getOwningTarget();
32 TreeSPath targetPath
= createTargetPath(target
);
33 SimpleSElement targetElement
= (SimpleSElement
) registry
34 .getElement(createTargetPath(target
));
36 if (targetElement
== null) {
37 targetElement
= new SimpleSElement(target
.getDescription(),
39 registry
.register(targetPath
, targetElement
);
44 * Includes this arg in the checks for propagation of structure related
47 protected void addSAwareArg(SpringArg arg
) {
53 * Called by Ant at runtime. Decides whether to call the actions depending
54 * of the mode of the underlying structure registry.
56 * @see #executeActions
57 * @see StructureRegistry
59 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());
68 treeSPath
= new TreeSPath(path
);
71 if (getRegistry().getElement(treeSPath
) == null) {
72 // No structure element registered.
73 if (structureElementArg
!= null) {
74 getRegistry().register(treeSPath
,
75 structureElementArg
.getStructureElement());
77 if (getDescription() != null) {
78 getRegistry().register(treeSPath
,
79 new SimpleSElement(getDescription()));
84 // notify registered args
85 for (SpringArg arg
: sAwareArgs
) {
86 Object obj
= arg
.getBeanInstance();
88 if (obj
instanceof StructureAware
) {
89 StructureAware
<TreeSPath
> sAwareT
= (StructureAware
<TreeSPath
>) obj
;
90 sAwareT
.notifyCurrentPath(getRegistry(), treeSPath
);
94 // execute depending on the registry mode
95 String mode
= getRegistry().getMode();
96 if (mode
.equals(StructureRegistry
.ALL
)) {
98 } else if (mode
.equals(StructureRegistry
.ACTIVE
)) {
99 List
<TreeSPath
> activePaths
= getRegistry().getActivePaths();
101 if (activePaths
.contains(treeSPath
)) {
102 if (activePaths
.contains(treeSPath
)) {
103 executeActions(mode
);
110 /** Actions to be executed by the implementor. */
111 protected abstract void executeActions(String mode
);
113 public <T
> T
getBean(String beanName
) {
114 return (T
) getContext().getBean(beanName
);
117 /** Create a reference to an external structure element. */
118 public StructureElementArg
createStructureElement() {
119 if (structureElementArg
!= null)
120 throw new SlcAntException("Arg already set.");
121 structureElementArg
= new StructureElementArg();
122 return structureElementArg
;
125 /** Gets the underlying structure registry. */
126 protected StructureRegistry
<TreeSPath
> getRegistry() {
127 return (StructureRegistry
<TreeSPath
>) getProject().getReference(
128 SlcProjectHelper
.REF_STRUCTURE_REGISTRY
);
131 /** Creates the treeSPath for a given Ant target. */
132 protected static TreeSPath
createTargetPath(Target target
) {
133 TreeSPath projectPath
= (TreeSPath
) target
.getProject().getReference(
134 SlcProjectHelper
.REF_PROJECT_PATH
);
135 return projectPath
.createChild(target
.getName());
138 /** Gets the treeSPath under which this task is registered. */
139 public TreeSPath
getTreeSPath() {
143 public String
getLabel() {
144 String description
= super.getDescription();
145 if (description
== null) {
146 return "<no task def>";
152 public void setPath(String path
) {
157 class StructureElementArg
extends SpringArg
{
158 public StructureElement
getStructureElement() {
159 return (StructureElement
) getBeanInstance();