+package org.argeo.slc.ant;\r
+\r
+import java.io.File;\r
+import java.net.URL;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Properties;\r
+\r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
+import org.apache.tools.ant.Project;\r
+import org.apache.tools.ant.ProjectHelper;\r
+import org.apache.tools.ant.listener.CommonsLoggingListener;\r
+import org.argeo.slc.core.structure.StructurePath;\r
+import org.argeo.slc.core.structure.StructureRegistry;\r
+\r
+/** Utilities to manipulate the structure registry in SLC Ant. */\r
+public class AntRegistryUtil {\r
+ private static Log log = LogFactory.getLog(AntRegistryUtil.class);\r
+\r
+ /** Reads a structure registry from an Ant file without executing it. */\r
+ public static StructureRegistry readRegistry(File antFile) {\r
+ if (log.isDebugEnabled())\r
+ log.debug("Reads registry for Ant file " + antFile);\r
+ Project p = new Project();\r
+ p.setUserProperty("ant.file", antFile.getAbsolutePath());\r
+ p.setBaseDir(antFile.getParentFile());\r
+ p.init();\r
+ ProjectHelper helper = new SlcProjectHelper();\r
+ p.addReference(ProjectHelper.PROJECTHELPER_REFERENCE, helper);\r
+ helper.parse(p, antFile);\r
+\r
+ StructureRegistry registry = (StructureRegistry) p\r
+ .getReference(SlcProjectHelper.REF_STRUCTURE_REGISTRY);\r
+ registry.setMode(StructureRegistry.READ);\r
+\r
+ p.executeTarget(p.getDefaultTarget());\r
+ return registry;\r
+ }\r
+\r
+ /** Executes only the active paths of the Ant file. */\r
+ public static Project runActive(File antFile,\r
+ List<StructurePath> activePaths) {\r
+ if (log.isDebugEnabled())\r
+ log.debug("Runs the " + activePaths.size()\r
+ + " provided active paths of Ant file " + antFile);\r
+ Project p = new Project();\r
+ p.setUserProperty("ant.file", antFile.getAbsolutePath());\r
+ p.setBaseDir(antFile.getParentFile());\r
+ p.init();\r
+ ProjectHelper helper = new SlcProjectHelper();\r
+ p.addReference(ProjectHelper.PROJECTHELPER_REFERENCE, helper);\r
+ helper.parse(p, antFile);\r
+\r
+ StructureRegistry registry = (StructureRegistry) p\r
+ .getReference(SlcProjectHelper.REF_STRUCTURE_REGISTRY);\r
+ registry.setMode(StructureRegistry.ACTIVE);\r
+ registry.setActivePaths(activePaths);\r
+\r
+ runProject(p, null);\r
+ return p;\r
+ }\r
+\r
+ /** Executes all paths of the provided target of the Ant file. */\r
+ public static Project runAll(File antFile, String target) {\r
+ if (log.isDebugEnabled())\r
+ log.debug("Runs all paths of Ant file " + antFile);\r
+ Project p = new Project();\r
+ p.setUserProperty("ant.file", antFile.getAbsolutePath());\r
+ p.setBaseDir(antFile.getParentFile());\r
+ p.init();\r
+ ProjectHelper helper = new SlcProjectHelper();\r
+ p.addReference(ProjectHelper.PROJECTHELPER_REFERENCE, helper);\r
+ helper.parse(p, antFile);\r
+\r
+ runProject(p, target);\r
+ return p;\r
+ }\r
+\r
+ /** Executes all paths of the provided target of the Ant URL. */\r
+ public static Project runAll(URL url, String target, Properties properties) {\r
+ if (log.isDebugEnabled())\r
+ log.debug("Runs all paths of Ant URL " + url);\r
+ Project p = new Project();\r
+ p.setUserProperty("ant.file", url.toString());\r
+ // p.setBaseDir(url.toString());\r
+ p.addBuildListener(new CommonsLoggingListener());\r
+ p.init();\r
+ ProjectHelper helper = new SlcProjectHelper();\r
+ p.addReference(ProjectHelper.PROJECTHELPER_REFERENCE, helper);\r
+ helper.parse(p, url);\r
+\r
+ if (properties != null) {\r
+ for (Map.Entry<Object, Object> entry : properties.entrySet()) {\r
+ p.setUserProperty(entry.getKey().toString(), entry.getValue()\r
+ .toString());\r
+ }\r
+ }\r
+\r
+ runProject(p, target);\r
+ return p;\r
+ }\r
+\r
+ /** Executes all paths of the default target of the Ant file. */\r
+ public static Project runAll(File antFile) {\r
+ return runAll(antFile, null);\r
+ }\r
+\r
+ protected static void runProject(Project p, String target) {\r
+ p.fireBuildStarted();\r
+ Throwable exception = null;\r
+ try {\r
+ p.executeTarget(target != null ? target : p.getDefaultTarget());\r
+ } catch (Throwable e) {\r
+ exception = e;\r
+ log.error("Exception when running Ant: ",e);\r
+ } finally {\r
+ p.fireBuildFinished(exception);\r
+ }\r
+ }\r
+}\r