--- /dev/null
+package org.argeo.slc.ant.structure;\r
+\r
+import java.util.List;\r
+import java.util.Vector;\r
+\r
+import org.apache.tools.ant.BuildException;\r
+import org.apache.tools.ant.Target;\r
+\r
+import org.argeo.slc.ant.SlcAntException;\r
+import org.argeo.slc.ant.SlcProjectHelper;\r
+import org.argeo.slc.ant.spring.AbstractSpringArg;\r
+import org.argeo.slc.ant.spring.AbstractSpringTask;\r
+import org.argeo.slc.core.structure.SimpleSElement;\r
+import org.argeo.slc.core.structure.StructureAware;\r
+import org.argeo.slc.core.structure.StructureElement;\r
+import org.argeo.slc.core.structure.StructureRegistry;\r
+import org.argeo.slc.core.structure.tree.TreeSPath;\r
+\r
+/** Ant task that can be registered within a structure. */\r
+public abstract class SAwareTask extends AbstractSpringTask {\r
+ private String path;\r
+ private TreeSPath treeSPath;\r
+ private final List<AbstractSpringArg> sAwareArgs = new Vector<AbstractSpringArg>();\r
+\r
+ private StructureElementArg structureElementArg;\r
+\r
+ @Override\r
+ public void init() throws BuildException {\r
+ StructureRegistry<TreeSPath> registry = getRegistry();\r
+ Target target = getOwningTarget();\r
+\r
+ TreeSPath targetPath = createTargetPath(target);\r
+ SimpleSElement targetElement = (SimpleSElement) registry\r
+ .getElement(createTargetPath(target));\r
+\r
+ if (targetElement == null) {\r
+ targetElement = new SimpleSElement(target.getDescription(),\r
+ "<no target desc>");\r
+ registry.register(targetPath, targetElement);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Includes this arg in the checks for propagation of structure related\r
+ * information.\r
+ */\r
+ protected void addSAwareArg(AbstractSpringArg arg) {\r
+ sAwareArgs.add(arg);\r
+ }\r
+\r
+ @Override\r
+ /**\r
+ * Called by Ant at runtime. Decides whether to call the actions depending\r
+ * of the mode of the underlying structure registry.\r
+ * \r
+ * @see #executeActions\r
+ * @see StructureRegistry\r
+ */\r
+ public final void execute() throws BuildException {\r
+ if (path == null) {\r
+ // register the task in the structure\r
+ TreeSPath targetPath = createTargetPath(getOwningTarget());\r
+ TreeSPath taskPath = targetPath.createChild(getTaskName()\r
+ + targetPath.listChildren(getRegistry()).size());\r
+\r
+ treeSPath = taskPath;\r
+ } else {\r
+ treeSPath = new TreeSPath(path);\r
+ }\r
+\r
+ if (getRegistry().getElement(treeSPath) == null) {\r
+ // No structure element registered.\r
+ if (structureElementArg != null) {\r
+ getRegistry().register(treeSPath,\r
+ structureElementArg.getStructureElement());\r
+ } else {\r
+ if (getDescription() != null) {\r
+ getRegistry().register(treeSPath,\r
+ new SimpleSElement(getDescription()));\r
+ }\r
+ }\r
+ }\r
+\r
+ // notify registered args\r
+ for (AbstractSpringArg arg : sAwareArgs) {\r
+ Object obj = arg.getBeanInstance();\r
+\r
+ if (obj instanceof StructureAware) {\r
+ StructureAware<TreeSPath> sAwareT = (StructureAware<TreeSPath>) obj;\r
+ sAwareT.notifyCurrentPath(getRegistry(), treeSPath);\r
+ }\r
+ }\r
+\r
+ // execute depending on the registry mode\r
+ String mode = getRegistry().getMode();\r
+ if (mode.equals(StructureRegistry.ALL)) {\r
+ executeActions(mode);\r
+ } else if (mode.equals(StructureRegistry.ACTIVE)) {\r
+ List<TreeSPath> activePaths = getRegistry().getActivePaths();\r
+\r
+ if (activePaths.contains(treeSPath)) {\r
+ if (activePaths.contains(treeSPath)) {\r
+ executeActions(mode);\r
+ }\r
+ }\r
+ }\r
+\r
+ }\r
+\r
+ /** Actions to be executed by the implementor. */\r
+ protected abstract void executeActions(String mode);\r
+\r
+ /** Create a reference to an external structure element. */\r
+ public StructureElementArg createStructureElement() {\r
+ if (structureElementArg != null)\r
+ throw new SlcAntException("Arg already set.");\r
+ structureElementArg = new StructureElementArg();\r
+ return structureElementArg;\r
+ }\r
+\r
+ /** Gets the underlying structure registry. */\r
+ protected StructureRegistry<TreeSPath> getRegistry() {\r
+ return (StructureRegistry<TreeSPath>) getProject().getReference(\r
+ SlcProjectHelper.REF_STRUCTURE_REGISTRY);\r
+ }\r
+\r
+ /** Creates the treeSPath for a given Ant target. */\r
+ protected static TreeSPath createTargetPath(Target target) {\r
+ TreeSPath projectPath = (TreeSPath) target.getProject().getReference(\r
+ SlcProjectHelper.REF_PROJECT_PATH);\r
+ return projectPath.createChild(target.getName());\r
+ }\r
+\r
+ /** Gets the treeSPath under which this task is registered. */\r
+ public TreeSPath getTreeSPath() {\r
+ return treeSPath;\r
+ }\r
+\r
+ public String getLabel() {\r
+ String description = super.getDescription();\r
+ if (description == null) {\r
+ return "<no task def>";\r
+ } else {\r
+ return description;\r
+ }\r
+ }\r
+\r
+ public void setPath(String path) {\r
+ this.path = path;\r
+ }\r
+}\r
+\r
+class StructureElementArg extends AbstractSpringArg {\r
+ public StructureElement getStructureElement() {\r
+ return (StructureElement) getBeanInstance();\r
+ }\r
+}
\ No newline at end of file