X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fant%2Fstructure%2FSAwareTask.java;h=d248a38705378bfa318781385bf1a5400362e48d;hb=58db7291c1746d4e7623926f34108c07e2ccae4f;hp=3f4d35864abd8017f3ac0629caff3ec8fd9ae99c;hpb=faf680e212bf3e18837c4f798587856e061273b3;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.core/src/main/java/org/argeo/slc/ant/structure/SAwareTask.java b/org.argeo.slc.core/src/main/java/org/argeo/slc/ant/structure/SAwareTask.java index 3f4d35864..d248a3870 100644 --- a/org.argeo.slc.core/src/main/java/org/argeo/slc/ant/structure/SAwareTask.java +++ b/org.argeo.slc.core/src/main/java/org/argeo/slc/ant/structure/SAwareTask.java @@ -6,25 +6,27 @@ import java.util.Vector; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Target; +import org.argeo.slc.ant.SlcAntException; import org.argeo.slc.ant.SlcProjectHelper; import org.argeo.slc.ant.spring.AbstractSpringArg; import org.argeo.slc.ant.spring.AbstractSpringTask; import org.argeo.slc.core.structure.SimpleSElement; import org.argeo.slc.core.structure.StructureAware; import org.argeo.slc.core.structure.StructureElement; -import org.argeo.slc.core.structure.StructurePath; import org.argeo.slc.core.structure.StructureRegistry; import org.argeo.slc.core.structure.tree.TreeSPath; /** Ant task that can be registered within a structure. */ -public abstract class SAwareTask extends AbstractSpringTask implements - StructureElement { - private TreeSPath path; +public abstract class SAwareTask extends AbstractSpringTask { + private String path; + private TreeSPath treeSPath; private final List sAwareArgs = new Vector(); + private StructureElementArg structureElementArg; + @Override public void init() throws BuildException { - StructureRegistry registry = getRegistry(); + StructureRegistry registry = getRegistry(); Target target = getOwningTarget(); TreeSPath targetPath = createTargetPath(target); @@ -39,7 +41,7 @@ public abstract class SAwareTask extends AbstractSpringTask implements } /** - * Includes this arg in the checks for propagation of sstructure related + * Includes this arg in the checks for propagation of structure related * information. */ protected void addSAwareArg(AbstractSpringArg arg) { @@ -55,20 +57,37 @@ public abstract class SAwareTask extends AbstractSpringTask implements * @see StructureRegistry */ public final void execute() throws BuildException { - // register the task in the structure - TreeSPath targetPath = createTargetPath(getOwningTarget()); - TreeSPath taskPath = targetPath.createChild(getTaskName() - + targetPath.listChildren(getRegistry()).size()); - getRegistry().register(taskPath, this); - path = taskPath; + if (path == null) { + // register the task in the structure + TreeSPath targetPath = createTargetPath(getOwningTarget()); + TreeSPath taskPath = targetPath.createChild(getTaskName() + + targetPath.listChildren(getRegistry()).size()); + + treeSPath = taskPath; + } else { + treeSPath = new TreeSPath(path); + } + + if (getRegistry().getElement(treeSPath) == null) { + // No structure element registered. + if (structureElementArg != null) { + getRegistry().register(treeSPath, + structureElementArg.getStructureElement()); + } else { + if (getDescription() != null) { + getRegistry().register(treeSPath, + new SimpleSElement(getDescription())); + } + } + } // notify registered args for (AbstractSpringArg arg : sAwareArgs) { Object obj = arg.getBeanInstance(); if (obj instanceof StructureAware) { - StructureAware sAwareT = (StructureAware) obj; - sAwareT.notifyCurrentPath(getRegistry(), taskPath); + StructureAware sAwareT = (StructureAware) obj; + sAwareT.notifyCurrentPath(getRegistry(), treeSPath); } } @@ -77,10 +96,10 @@ public abstract class SAwareTask extends AbstractSpringTask implements if (mode.equals(StructureRegistry.ALL)) { executeActions(mode); } else if (mode.equals(StructureRegistry.ACTIVE)) { - List activePaths = getRegistry().getActivePaths(); + List activePaths = getRegistry().getActivePaths(); - if (activePaths.contains(targetPath)) { - if (activePaths.contains(taskPath)) { + if (activePaths.contains(treeSPath)) { + if (activePaths.contains(treeSPath)) { executeActions(mode); } } @@ -91,26 +110,33 @@ public abstract class SAwareTask extends AbstractSpringTask implements /** Actions to be executed by the implementor. */ protected abstract void executeActions(String mode); + /** Create a reference to an external structure element. */ + public StructureElementArg createStructureElement() { + if (structureElementArg != null) + throw new SlcAntException("Arg already set."); + structureElementArg = new StructureElementArg(); + return structureElementArg; + } + /** Gets the underlying structure registry. */ - protected StructureRegistry getRegistry() { - return (StructureRegistry) getProject().getReference( + protected StructureRegistry getRegistry() { + return (StructureRegistry) getProject().getReference( SlcProjectHelper.REF_STRUCTURE_REGISTRY); } - /** Creates the path for a given Ant target. */ + /** Creates the treeSPath for a given Ant target. */ protected static TreeSPath createTargetPath(Target target) { - TreeSPath projectPath = SlcProjectHelper.getProjectPath(target - .getProject()); + TreeSPath projectPath = (TreeSPath) target.getProject().getReference( + SlcProjectHelper.REF_PROJECT_PATH); return projectPath.createChild(target.getName()); } - /** Gets the path under which this task is registered. */ - public TreeSPath getPath() { - return path; + /** Gets the treeSPath under which this task is registered. */ + public TreeSPath getTreeSPath() { + return treeSPath; } - @Override - public String getDescription() { + public String getLabel() { String description = super.getDescription(); if (description == null) { return ""; @@ -119,4 +145,13 @@ public abstract class SAwareTask extends AbstractSpringTask implements } } + public void setPath(String path) { + this.path = path; + } } + +class StructureElementArg extends AbstractSpringArg { + public StructureElement getStructureElement() { + return (StructureElement) getBeanInstance(); + } +} \ No newline at end of file