]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/ant/AntRegistryUtil.java
SlcExecution hibernate persistence
[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.Project;
12 import org.apache.tools.ant.ProjectHelper;
13
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(SlcProjectHelper.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(SlcProjectHelper.REF_STRUCTURE_REGISTRY);
57 registry.setMode(StructureRegistry.ACTIVE);
58 registry.setActivePaths(activePaths);
59 p.executeTarget(p.getDefaultTarget());
60 return p;
61 }
62
63 /** Executes all paths of the provided target of the Ant file. */
64 public static Project runAll(File antFile, String target) {
65 if (log.isDebugEnabled())
66 log.debug("Runs all paths of Ant file " + antFile);
67 Project p = new Project();
68 p.setUserProperty("ant.file", antFile.getAbsolutePath());
69 p.setBaseDir(antFile.getParentFile());
70 p.init();
71 ProjectHelper helper = new SlcProjectHelper();
72 p.addReference(ProjectHelper.PROJECTHELPER_REFERENCE, helper);
73 helper.parse(p, antFile);
74 p.executeTarget(target != null ? target : p.getDefaultTarget());
75 return p;
76 }
77
78 /** Executes all paths of the provided target of the Ant URL. */
79 public static Project runAll(URL url, String target, Properties properties) {
80 if (log.isDebugEnabled())
81 log.debug("Runs all paths of Ant URL " + url);
82 Project p = new Project();
83 p.setUserProperty("ant.file", url.toString());
84 //p.setBaseDir(url.toString());
85 p.init();
86 ProjectHelper helper = new SlcProjectHelper();
87 p.addReference(ProjectHelper.PROJECTHELPER_REFERENCE, helper);
88 helper.parse(p, url);
89 for(Map.Entry<Object, Object> entry : properties.entrySet()){
90 p.setUserProperty(entry.getKey().toString(), entry.getValue().toString());
91 }
92 p.executeTarget(target != null ? target : p.getDefaultTarget());
93 return p;
94 }
95
96 /** Executes all paths of the default target of the Ant file. */
97 public static Project runAll(File antFile) {
98 return runAll(antFile, null);
99 }
100 }