-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
-import org.argeo.slc.ant.AntConstants;\r
-import org.argeo.slc.ant.spring.AbstractSpringTask;\r
-import org.argeo.slc.ant.spring.SpringArg;\r
-import org.argeo.slc.core.SlcException;\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<SpringArg> sAwareArgs = new Vector<SpringArg>();\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(SpringArg 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 (SpringArg arg : sAwareArgs) {\r
- Object obj = arg.getInstance();\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
- public <T> T getBean(String beanName) {\r
- return (T) getContext().getBean(beanName);\r
- }\r
-\r
- /** Create a reference to an external structure element. */\r
- public StructureElementArg createStructureElement() {\r
- if (structureElementArg != null)\r
- throw new SlcException("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
- AntConstants.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
- AntConstants.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 SpringArg {\r
- public StructureElement getStructureElement() {\r
- return (StructureElement) getInstance();\r
- }\r
-}
\ No newline at end of file