1 package org
.argeo
.slc
.ant
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
7 import java
.util
.Properties
;
8 import java
.util
.Vector
;
10 import org
.springframework
.beans
.factory
.config
.PropertyPlaceholderConfigurer
;
11 import org
.springframework
.context
.ApplicationContext
;
12 import org
.springframework
.context
.support
.FileSystemXmlApplicationContext
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.apache
.tools
.ant
.BuildException
;
17 import org
.apache
.tools
.ant
.Project
;
18 import org
.apache
.tools
.ant
.helper
.ProjectHelperImpl
;
20 import org
.argeo
.slc
.core
.structure
.DefaultSRegistry
;
21 import org
.argeo
.slc
.core
.structure
.SimpleSElement
;
22 import org
.argeo
.slc
.core
.structure
.StructureRegistry
;
23 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
26 * Custom implementation of a <code>ProjectHelper</code> binding a Spring
27 * application context and a structure registry with the Ant project.
29 public class SlcProjectHelper
extends ProjectHelperImpl
{
30 private static Log log
= LogFactory
.getLog(SlcProjectHelper
.class);
32 public static String REF_ROOT_CONTEXT
= "slcApplicationContext";
33 public static String REF_STRUCTURE_REGISTRY
= "slcStructureRegistry";
34 public static String REF_PROJECT_PATH
= "slcProjectPath";
36 private String slcRootFileName
= "slcRoot.properties";
37 private String slcLocalFileName
= "slcLocal.properties";
40 public void parse(Project project
, Object source
) throws BuildException
{
41 log
.debug("Entered SLC project helper");
44 File projectBaseDir
= project
.getBaseDir();
45 File slcRootFile
= findSlcRootFile(projectBaseDir
);
46 if (slcRootFile
== null) {
47 throw new SlcAntException("Cannot find SLC root file");
49 SlcAntConfig
.initProject(project
, slcRootFile
);
51 // init Spring application context
52 initSpringContext(project
);
54 // init structure registry
55 DefaultSRegistry registry
= new DefaultSRegistry();
56 project
.addReference(REF_STRUCTURE_REGISTRY
, registry
);
58 // call the underlying implementation to do the actual work
59 super.parse(project
, source
);
61 // create structure root
62 registerProjectAndParents(project
);
68 private void registerProjectAndParents(Project project
) {
69 StructureRegistry registry
= (StructureRegistry
) project
70 .getReference(REF_STRUCTURE_REGISTRY
);
71 File rootDir
= new File(project
72 .getUserProperty(SlcAntConfig
.ROOT_DIR_PROPERTY
))
74 File baseDir
= project
.getBaseDir().getAbsoluteFile();
76 List
<File
> dirs
= new Vector
<File
>();
77 File currentDir
= baseDir
;
80 currentDir
= currentDir
.getParentFile();
81 log
.trace("List " + currentDir
);
82 } while (!currentDir
.equals(rootDir
.getParentFile()));
84 TreeSPath currPath
= null;
85 for (int i
= dirs
.size() - 1; i
>= 0; i
--) {
86 File dir
= dirs
.get(i
);
88 String description
= dir
.getName();
89 File slcLocal
= new File(dir
.getPath() + File
.separator
91 if (slcLocal
.exists()) {
92 Properties properties
= SlcAntConfig
.loadFile(slcLocal
94 description
= properties
95 .getProperty(SlcAntConfig
.DIR_DESCRIPTION_PROPERTY
);
97 if (i
== 0) {// project it self
98 description
= project
.getDescription() != null ? project
99 .getDescription() : "";
102 SimpleSElement element
= new SimpleSElement(description
);
104 if (dir
.equals(rootDir
)) {
105 currPath
= TreeSPath
.createRootPath(rootDir
.getName());
107 currPath
= currPath
.createChild(dir
.getName());
109 registry
.register(currPath
, element
);
111 project
.addReference(REF_PROJECT_PATH
, currPath
);
114 /** Get the path of a project (root). */
115 public static TreeSPath
getProjectPath(Project project
) {
116 return (TreeSPath
) project
.getReference(REF_PROJECT_PATH
);
119 private File
findSlcRootFile(File dir
) {
120 for (File file
: dir
.listFiles()) {
121 if (!file
.isDirectory() && file
.getName().equals(slcRootFileName
)) {
126 File parentDir
= dir
.getParentFile();
127 if (parentDir
== null) {
128 return null;// stop condition: not found
130 return findSlcRootFile(parentDir
);
134 private void initSpringContext(Project project
) {
135 System
.getProperties().putAll(project
.getProperties());
136 String acPath
= project
137 .getUserProperty(SlcAntConfig
.APPLICATION_CONTEXT_PROPERTY
);
138 ApplicationContext context
= new FileSystemXmlApplicationContext(acPath
);
139 project
.addReference(REF_ROOT_CONTEXT
, context
);
142 private void addSlcTasks(Project project
) {
143 Properties taskdefs
= new Properties();
145 InputStream in
= project
.getClass().getResourceAsStream(
146 "/org/argeo/slc/ant/taskdefs.properties");
149 } catch (IOException e
) {
150 throw new SlcAntException("Cannot load task definitions", e
);
153 for (Object o
: taskdefs
.keySet()) {
154 String name
= o
.toString();
156 project
.addTaskDefinition(name
, Class
.forName(taskdefs
157 .getProperty(name
)));
158 } catch (ClassNotFoundException e
) {
159 log
.error("Unknown class for task " + name
, e
);