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
;
8 import org
.argeo
.slc
.ant
.AntConstants
;
9 import org
.argeo
.slc
.ant
.spring
.AbstractSpringTask
;
10 import org
.argeo
.slc
.ant
.spring
.SpringArg
;
11 import org
.argeo
.slc
.core
.SlcException
;
12 import org
.argeo
.slc
.core
.structure
.SimpleSElement
;
13 import org
.argeo
.slc
.core
.structure
.StructureAware
;
14 import org
.argeo
.slc
.core
.structure
.StructureElement
;
15 import org
.argeo
.slc
.core
.structure
.StructureRegistry
;
16 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
18 /** Ant task that can be registered within a structure. */
19 public abstract class SAwareTask
extends AbstractSpringTask
{
21 private TreeSPath treeSPath
;
22 private final List
<SpringArg
> sAwareArgs
= new Vector
<SpringArg
>();
24 private StructureElementArg structureElementArg
;
27 public void init() throws BuildException
{
28 StructureRegistry
<TreeSPath
> registry
= getRegistry();
29 Target target
= getOwningTarget();
31 TreeSPath targetPath
= createTargetPath(target
);
32 SimpleSElement targetElement
= (SimpleSElement
) registry
33 .getElement(createTargetPath(target
));
35 if (targetElement
== null) {
36 targetElement
= new SimpleSElement(target
.getDescription(),
38 registry
.register(targetPath
, targetElement
);
43 * Includes this arg in the checks for propagation of structure related
46 protected void addSAwareArg(SpringArg arg
) {
52 * Called by Ant at runtime. Decides whether to call the actions depending
53 * of the mode of the underlying structure registry.
55 * @see #executeActions
56 * @see StructureRegistry
58 public final void execute() throws BuildException
{
60 // register the task in the structure
61 TreeSPath targetPath
= createTargetPath(getOwningTarget());
62 TreeSPath taskPath
= targetPath
.createChild(getTaskName()
63 + targetPath
.listChildren(getRegistry()).size());
67 treeSPath
= new TreeSPath(path
);
70 if (getRegistry().getElement(treeSPath
) == null) {
71 // No structure element registered.
72 if (structureElementArg
!= null) {
73 getRegistry().register(treeSPath
,
74 structureElementArg
.getStructureElement());
76 if (getDescription() != null) {
77 getRegistry().register(treeSPath
,
78 new SimpleSElement(getDescription()));
83 // notify registered args
84 for (SpringArg arg
: sAwareArgs
) {
85 Object obj
= arg
.getBeanInstance();
87 if (obj
instanceof StructureAware
) {
88 StructureAware
<TreeSPath
> sAwareT
= (StructureAware
<TreeSPath
>) obj
;
89 sAwareT
.notifyCurrentPath(getRegistry(), treeSPath
);
93 // execute depending on the registry mode
94 String mode
= getRegistry().getMode();
95 if (mode
.equals(StructureRegistry
.ALL
)) {
97 } else if (mode
.equals(StructureRegistry
.ACTIVE
)) {
98 List
<TreeSPath
> activePaths
= getRegistry().getActivePaths();
100 if (activePaths
.contains(treeSPath
)) {
101 if (activePaths
.contains(treeSPath
)) {
102 executeActions(mode
);
109 /** Actions to be executed by the implementor. */
110 protected abstract void executeActions(String mode
);
112 public <T
> T
getBean(String beanName
) {
113 return (T
) getContext().getBean(beanName
);
116 /** Create a reference to an external structure element. */
117 public StructureElementArg
createStructureElement() {
118 if (structureElementArg
!= null)
119 throw new SlcException("Arg already set.");
120 structureElementArg
= new StructureElementArg();
121 return structureElementArg
;
124 /** Gets the underlying structure registry. */
125 protected StructureRegistry
<TreeSPath
> getRegistry() {
126 return (StructureRegistry
<TreeSPath
>) getProject().getReference(
127 AntConstants
.REF_STRUCTURE_REGISTRY
);
130 /** Creates the treeSPath for a given Ant target. */
131 protected static TreeSPath
createTargetPath(Target target
) {
132 TreeSPath projectPath
= (TreeSPath
) target
.getProject().getReference(
133 AntConstants
.REF_PROJECT_PATH
);
134 return projectPath
.createChild(target
.getName());
137 /** Gets the treeSPath under which this task is registered. */
138 public TreeSPath
getTreeSPath() {
142 public String
getLabel() {
143 String description
= super.getDescription();
144 if (description
== null) {
145 return "<no task def>";
151 public void setPath(String path
) {
156 class StructureElementArg
extends SpringArg
{
157 public StructureElement
getStructureElement() {
158 return (StructureElement
) getBeanInstance();