]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.core/src/main/java/org/argeo/slc/ant/structure/SAwareTask.java
Simplify ANt-Spring bridge
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / ant / structure / SAwareTask.java
index 136bd5201487e8ac8711971b576327fa625de7f9..9db63f141dcc4437c2ba481466f8a41b8c0e2a74 100644 (file)
@@ -6,25 +6,28 @@ import java.util.Vector;
 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.StructurePath;\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 implements\r
                StructureElement {\r
-       private TreeSPath path;\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 registry = getRegistry();\r
+               StructureRegistry<TreeSPath> registry = getRegistry();\r
                Target target = getOwningTarget();\r
 \r
                TreeSPath targetPath = createTargetPath(target);\r
@@ -39,7 +42,7 @@ public abstract class SAwareTask extends AbstractSpringTask implements
        }\r
 \r
        /**\r
-        * Includes this arg in the checks for propagation of sstructure related\r
+        * Includes this arg in the checks for propagation of structure related\r
         * information.\r
         */\r
        protected void addSAwareArg(AbstractSpringArg arg) {\r
@@ -55,20 +58,30 @@ public abstract class SAwareTask extends AbstractSpringTask implements
         * @see StructureRegistry\r
         */\r
        public final void execute() throws BuildException {\r
-               // register the task in the structure\r
-               TreeSPath targetPath = createTargetPath(getOwningTarget());\r
-               TreeSPath taskPath = targetPath.createChild(getTaskName()\r
-                               + targetPath.listChildren(getRegistry()).size());\r
-               getRegistry().register(taskPath, this);\r
-               path = taskPath;\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 = TreeSPath.parseToCreatePath(path);\r
+               }\r
+\r
+               if (structureElementArg != null)\r
+                       getRegistry().register(treeSPath,\r
+                                       structureElementArg.getStructureElement());\r
+               else\r
+                       getRegistry().register(treeSPath, this);\r
 \r
                // notify registered args\r
                for (AbstractSpringArg arg : sAwareArgs) {\r
                        Object obj = arg.getBeanInstance();\r
 \r
                        if (obj instanceof StructureAware) {\r
-                               StructureAware sAwareT = (StructureAware) obj;\r
-                               sAwareT.notifyCurrentPath(getRegistry(), taskPath);\r
+                               StructureAware<TreeSPath> sAwareT = (StructureAware<TreeSPath>) obj;\r
+                               sAwareT.notifyCurrentPath(getRegistry(), treeSPath);\r
                        }\r
                }\r
 \r
@@ -77,10 +90,10 @@ public abstract class SAwareTask extends AbstractSpringTask implements
                if (mode.equals(StructureRegistry.ALL)) {\r
                        executeActions(mode);\r
                } else if (mode.equals(StructureRegistry.ACTIVE)) {\r
-                       List<StructurePath> activePaths = getRegistry().getActivePaths();\r
+                       List<TreeSPath> activePaths = getRegistry().getActivePaths();\r
 \r
-                       if (activePaths.contains(targetPath)) {\r
-                               if (activePaths.contains(taskPath)) {\r
+                       if (activePaths.contains(treeSPath)) {\r
+                               if (activePaths.contains(treeSPath)) {\r
                                        executeActions(mode);\r
                                }\r
                        }\r
@@ -91,22 +104,30 @@ public abstract class SAwareTask extends AbstractSpringTask implements
        /** 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 getRegistry() {\r
-               return (StructureRegistry) getProject().getReference(\r
+       protected StructureRegistry<TreeSPath> getRegistry() {\r
+               return (StructureRegistry<TreeSPath>) getProject().getReference(\r
                                SlcProjectHelper.REF_STRUCTURE_REGISTRY);\r
        }\r
 \r
-       /** Creates the path for a given Ant target. */\r
+       /** Creates the treeSPath for a given Ant target. */\r
        protected static TreeSPath createTargetPath(Target target) {\r
-               TreeSPath projectPath = SlcProjectHelper.getProjectPath(target\r
-                               .getProject());\r
+               TreeSPath projectPath = (TreeSPath) target.getProject().getReference(\r
+                               SlcProjectHelper.REF_PROJECT_PATH);\r
                return projectPath.createChild(target.getName());\r
        }\r
 \r
-       /** Gets the path under which this task is registered. */\r
-       public TreeSPath getPath() {\r
-               return path;\r
+       /** Gets the treeSPath under which this task is registered. */\r
+       public TreeSPath getTreeSPath() {\r
+               return treeSPath;\r
        }\r
 \r
        public String getLabel() {\r
@@ -118,4 +139,13 @@ public abstract class SAwareTask extends AbstractSpringTask implements
                }\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