]> git.argeo.org Git - gpl/argeo-slc.git/blob - SlcProjectHelper.java
07678c6ab1d16f2a4b6b9f619138605a076deab7
[gpl/argeo-slc.git] / SlcProjectHelper.java
1 package org.argeo.slc.ant;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.List;
7 import java.util.Properties;
8 import java.util.Vector;
9
10 import org.springframework.context.support.AbstractApplicationContext;
11 import org.springframework.context.support.FileSystemXmlApplicationContext;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.apache.tools.ant.BuildException;
16 import org.apache.tools.ant.Project;
17 import org.apache.tools.ant.helper.ProjectHelperImpl;
18
19 import org.argeo.slc.core.structure.DefaultSRegistry;
20 import org.argeo.slc.core.structure.SimpleSElement;
21 import org.argeo.slc.core.structure.StructureRegistry;
22 import org.argeo.slc.core.structure.tree.TreeSPath;
23
24 /**
25 * Custom implementation of a <code>ProjectHelper</code> binding a Spring
26 * application context and a structure registry with the Ant project.
27 */
28 public class SlcProjectHelper extends ProjectHelperImpl {
29 private static Log log ;
30
31 public static String REF_ROOT_CONTEXT = "slcApplicationContext";
32 public static String REF_STRUCTURE_REGISTRY = "slcStructureRegistry";
33 public static String REF_PROJECT_PATH = "slcProjectPath";
34
35 private String slcRootFileName = "slcRoot.properties";
36 private String slcLocalFileName = "slcLocal.properties";
37
38 @Override
39 public void parse(Project project, Object source) throws BuildException {
40
41 // look for root file
42 File projectBaseDir = project.getBaseDir();
43 File slcRootFile = findSlcRootFile(projectBaseDir);
44 if (slcRootFile == null) {
45 throw new SlcAntException("Cannot find SLC root file");
46 }
47 SlcAntConfig.initProject(project, slcRootFile);
48
49 if(log == null){
50 // log4j is initialized only now
51 log = LogFactory.getLog(SlcProjectHelper.class);
52 }
53 log.debug("SLC properties are set, starting initialization..");
54
55 // init Spring application context
56 initSpringContext(project);
57
58 // init structure registry
59 DefaultSRegistry registry = new DefaultSRegistry();
60 project.addReference(REF_STRUCTURE_REGISTRY, registry);
61
62 // call the underlying implementation to do the actual work
63 super.parse(project, source);
64
65 // create structure root
66 registerProjectAndParents(project);
67
68 addSlcTasks(project);
69
70 }
71
72 private void registerProjectAndParents(Project project) {
73 StructureRegistry registry = (StructureRegistry) project
74 .getReference(REF_STRUCTURE_REGISTRY);
75 File rootDir = new File(project
76 .getUserProperty(SlcAntConfig.ROOT_DIR_PROPERTY))
77 .getAbsoluteFile();
78 File baseDir = project.getBaseDir().getAbsoluteFile();
79
80 List<File> dirs = new Vector<File>();
81 File currentDir = baseDir;
82 do {
83 dirs.add(currentDir);
84 currentDir = currentDir.getParentFile();
85 log.trace("List " + currentDir);
86 } while (!currentDir.equals(rootDir.getParentFile()));
87
88 TreeSPath currPath = null;
89 for (int i = dirs.size() - 1; i >= 0; i--) {
90 File dir = dirs.get(i);
91
92 String description = dir.getName();
93 File slcLocal = new File(dir.getPath() + File.separator
94 + slcLocalFileName);
95 if (slcLocal.exists()) {
96 Properties properties = SlcAntConfig.loadFile(slcLocal
97 .getAbsolutePath());
98 description = properties
99 .getProperty(SlcAntConfig.DIR_DESCRIPTION_PROPERTY);
100 } else {
101 if (i == 0) {// project it self
102 description = project.getDescription() != null ? project
103 .getDescription() : "";
104 }
105 }
106 SimpleSElement element = new SimpleSElement(description);
107
108 if (dir.equals(rootDir)) {
109 currPath = TreeSPath.createRootPath(rootDir.getName());
110 } else {
111 currPath = currPath.createChild(dir.getName());
112 }
113 registry.register(currPath, element);
114 }
115 project.addReference(REF_PROJECT_PATH, currPath);
116 }
117
118 /** Get the path of a project (root). */
119 public static TreeSPath getProjectPath(Project project) {
120 return (TreeSPath) project.getReference(REF_PROJECT_PATH);
121 }
122
123 private File findSlcRootFile(File dir) {
124 for (File file : dir.listFiles()) {
125 if (!file.isDirectory() && file.getName().equals(slcRootFileName)) {
126 return file;
127 }
128 }
129
130 File parentDir = dir.getParentFile();
131 if (parentDir == null) {
132 return null;// stop condition: not found
133 } else {
134 return findSlcRootFile(parentDir);
135 }
136 }
137
138 private void initSpringContext(Project project) {
139 System.getProperties().putAll(project.getProperties());
140 String acPath = project
141 .getUserProperty(SlcAntConfig.APPLICATION_CONTEXT_PROPERTY);
142 AbstractApplicationContext context = new FileSystemXmlApplicationContext(acPath);
143 context.registerShutdownHook();
144 project.addReference(REF_ROOT_CONTEXT, context);
145 }
146
147 private void addSlcTasks(Project project) {
148 Properties taskdefs = new Properties();
149 try {
150 InputStream in = project.getClass().getResourceAsStream(
151 "/org/argeo/slc/ant/taskdefs.properties");
152 taskdefs.load(in);
153 in.close();
154 } catch (IOException e) {
155 throw new SlcAntException("Cannot load task definitions", e);
156 }
157
158 for (Object o : taskdefs.keySet()) {
159 String name = o.toString();
160 try {
161 project.addTaskDefinition(name, Class.forName(taskdefs
162 .getProperty(name)));
163 } catch (ClassNotFoundException e) {
164 log.error("Unknown class for task " + name, e);
165 }
166 }
167 }
168 }