]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/AntRegistryUtil.java
Simplify new runtime
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / ant / AntRegistryUtil.java
1 package org.argeo.slc.ant;
2
3 import java.io.File;
4 import java.net.URL;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Properties;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.apache.tools.ant.Project;
12 import org.apache.tools.ant.ProjectHelper;
13 import org.apache.tools.ant.listener.CommonsLoggingListener;
14 import org.argeo.slc.core.structure.StructurePath;
15 import org.argeo.slc.core.structure.StructureRegistry;
16
17 /** Utilities to manipulate the structure registry in SLC Ant. */
18 public class AntRegistryUtil {
19 private static Log log = LogFactory.getLog(AntRegistryUtil.class);
20
21 /** Reads a structure registry from an Ant file without executing it. */
22 public static StructureRegistry readRegistry(File antFile) {
23 if (log.isDebugEnabled())
24 log.debug("Reads registry for Ant file " + antFile);
25 Project p = new Project();
26 p.setUserProperty("ant.file", antFile.getAbsolutePath());
27 p.setBaseDir(antFile.getParentFile());
28 p.init();
29 ProjectHelper helper = new SlcProjectHelper();
30 p.addReference(ProjectHelper.PROJECTHELPER_REFERENCE, helper);
31 helper.parse(p, antFile);
32
33 StructureRegistry registry = (StructureRegistry) p
34 .getReference(SlcAntConstants.REF_STRUCTURE_REGISTRY);
35 registry.setMode(StructureRegistry.READ);
36
37 p.executeTarget(p.getDefaultTarget());
38 return registry;
39 }
40
41 /** Executes only the active paths of the Ant file. */
42 public static Project runActive(File antFile,
43 List<StructurePath> activePaths) {
44 if (log.isDebugEnabled())
45 log.debug("Runs the " + activePaths.size()
46 + " provided active paths of Ant file " + antFile);
47 Project p = new Project();
48 p.setUserProperty("ant.file", antFile.getAbsolutePath());
49 p.setBaseDir(antFile.getParentFile());
50 p.init();
51 ProjectHelper helper = new SlcProjectHelper();
52 p.addReference(ProjectHelper.PROJECTHELPER_REFERENCE, helper);
53 helper.parse(p, antFile);
54
55 StructureRegistry registry = (StructureRegistry) p
56 .getReference(SlcAntConstants.REF_STRUCTURE_REGISTRY);
57 registry.setMode(StructureRegistry.ACTIVE);
58 registry.setActivePaths(activePaths);
59
60 runProject(p, null);
61 return p;
62 }
63
64 /** Executes all paths of the provided target of the Ant file. */
65 public static Project runAll(File antFile, String target) {
66 if (log.isDebugEnabled())
67 log.debug("Runs all paths of Ant file " + antFile);
68 Project p = new Project();
69 p.setUserProperty("ant.file", antFile.getAbsolutePath());
70 p.setBaseDir(antFile.getParentFile());
71 p.init();
72 ProjectHelper helper = new SlcProjectHelper();
73 p.addReference(ProjectHelper.PROJECTHELPER_REFERENCE, helper);
74 helper.parse(p, antFile);
75
76 runProject(p, target);
77 return p;
78 }
79
80 /** Executes all paths of the provided target of the Ant URL. */
81 public static Project runAll(URL url, String target, Properties properties) {
82 if (log.isDebugEnabled())
83 log.debug("Runs all paths of Ant URL " + url);
84 Project p = new Project();
85 p.setUserProperty("ant.file", url.toString());
86 // p.setBaseDir(url.toString());
87 p.addBuildListener(new CommonsLoggingListener());
88 p.init();
89 ProjectHelper helper = new SlcProjectHelper();
90 p.addReference(ProjectHelper.PROJECTHELPER_REFERENCE, helper);
91 helper.parse(p, url);
92
93 if (properties != null) {
94 for (Map.Entry<Object, Object> entry : properties.entrySet()) {
95 p.setUserProperty(entry.getKey().toString(), entry.getValue()
96 .toString());
97 }
98 }
99
100 runProject(p, target);
101 return p;
102 }
103
104 /** Executes all paths of the default target of the Ant file. */
105 public static Project runAll(File antFile) {
106 return runAll(antFile, null);
107 }
108
109 protected static void runProject(Project p, String target) {
110 p.fireBuildStarted();
111 Throwable exception = null;
112 try {
113 p.executeTarget(target != null ? target : p.getDefaultTarget());
114 } catch (Throwable e) {
115 exception = e;
116 log.error("Exception when running Ant: ", e);
117 } finally {
118 p.fireBuildFinished(exception);
119 }
120 }
121 }