]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/ant/AntRegistryUtil.java
Simplify defaults
[gpl/argeo-slc.git] / org.argeo.slc.core / 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.BuildException;
12 import org.apache.tools.ant.Project;
13 import org.apache.tools.ant.ProjectHelper;
14
15 import org.argeo.slc.core.structure.StructurePath;
16 import org.argeo.slc.core.structure.StructureRegistry;
17
18 /** Utilities to manipulate the structure registry in SLC Ant. */
19 public class AntRegistryUtil {
20 private static Log log = LogFactory.getLog(AntRegistryUtil.class);
21
22 /** Reads a structure registry from an Ant file without executing it. */
23 public static StructureRegistry readRegistry(File antFile) {
24 if (log.isDebugEnabled())
25 log.debug("Reads registry for Ant file " + antFile);
26 Project p = new Project();
27 p.setUserProperty("ant.file", antFile.getAbsolutePath());
28 p.setBaseDir(antFile.getParentFile());
29 p.init();
30 ProjectHelper helper = new SlcProjectHelper();
31 p.addReference(ProjectHelper.PROJECTHELPER_REFERENCE, helper);
32 helper.parse(p, antFile);
33
34 StructureRegistry registry = (StructureRegistry) p
35 .getReference(SlcProjectHelper.REF_STRUCTURE_REGISTRY);
36 registry.setMode(StructureRegistry.READ);
37
38 p.executeTarget(p.getDefaultTarget());
39 return registry;
40 }
41
42 /** Executes only the active paths of the Ant file. */
43 public static Project runActive(File antFile,
44 List<StructurePath> activePaths) {
45 if (log.isDebugEnabled())
46 log.debug("Runs the " + activePaths.size()
47 + " provided active paths of Ant file " + antFile);
48 Project p = new Project();
49 p.setUserProperty("ant.file", antFile.getAbsolutePath());
50 p.setBaseDir(antFile.getParentFile());
51 p.init();
52 ProjectHelper helper = new SlcProjectHelper();
53 p.addReference(ProjectHelper.PROJECTHELPER_REFERENCE, helper);
54 helper.parse(p, antFile);
55
56 StructureRegistry registry = (StructureRegistry) p
57 .getReference(SlcProjectHelper.REF_STRUCTURE_REGISTRY);
58 registry.setMode(StructureRegistry.ACTIVE);
59 registry.setActivePaths(activePaths);
60
61 runProject(p, null);
62 return p;
63 }
64
65 /** Executes all paths of the provided target of the Ant file. */
66 public static Project runAll(File antFile, String target) {
67 if (log.isDebugEnabled())
68 log.debug("Runs all paths of Ant file " + antFile);
69 Project p = new Project();
70 p.setUserProperty("ant.file", antFile.getAbsolutePath());
71 p.setBaseDir(antFile.getParentFile());
72 p.init();
73 ProjectHelper helper = new SlcProjectHelper();
74 p.addReference(ProjectHelper.PROJECTHELPER_REFERENCE, helper);
75 helper.parse(p, antFile);
76
77 runProject(p, target);
78 return p;
79 }
80
81 /** Executes all paths of the provided target of the Ant URL. */
82 public static Project runAll(URL url, String target, Properties properties) {
83 if (log.isDebugEnabled())
84 log.debug("Runs all paths of Ant URL " + url);
85 Project p = new Project();
86 p.setUserProperty("ant.file", url.toString());
87 // p.setBaseDir(url.toString());
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 } finally {
117 p.fireBuildFinished(exception);
118 }
119 }
120 }