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
.context
.support
.AbstractApplicationContext
;
11 import org
.springframework
.context
.support
.FileSystemXmlApplicationContext
;
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
;
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
;
25 * Custom implementation of a <code>ProjectHelper</code> binding a Spring
26 * application context and a structure registry with the Ant project.
28 public class SlcProjectHelper
extends ProjectHelperImpl
{
29 private static Log log
;
31 public static String REF_ROOT_CONTEXT
= "slcApplicationContext";
32 public static String REF_STRUCTURE_REGISTRY
= "slcStructureRegistry";
33 public static String REF_PROJECT_PATH
= "slcProjectPath";
35 private String slcRootFileName
= "slcRoot.properties";
36 private String slcLocalFileName
= "slcLocal.properties";
39 public void parse(Project project
, Object source
) throws BuildException
{
42 File projectBaseDir
= project
.getBaseDir();
43 File slcRootFile
= findSlcRootFile(projectBaseDir
);
44 if (slcRootFile
== null) {
45 throw new SlcAntException("Cannot find SLC root file");
47 SlcAntConfig
.initProject(project
, slcRootFile
);
50 // log4j is initialized only now
51 log
= LogFactory
.getLog(SlcProjectHelper
.class);
53 log
.debug("SLC properties are set, starting initialization..");
55 // init Spring application context
56 initSpringContext(project
);
58 // init structure registry
59 DefaultSRegistry registry
= new DefaultSRegistry();
60 project
.addReference(REF_STRUCTURE_REGISTRY
, registry
);
62 // call the underlying implementation to do the actual work
63 super.parse(project
, source
);
65 // create structure root
66 registerProjectAndParents(project
);
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
))
78 File baseDir
= project
.getBaseDir().getAbsoluteFile();
80 List
<File
> dirs
= new Vector
<File
>();
81 File currentDir
= baseDir
;
84 currentDir
= currentDir
.getParentFile();
85 log
.trace("List " + currentDir
);
86 } while (!currentDir
.equals(rootDir
.getParentFile()));
88 TreeSPath currPath
= null;
89 for (int i
= dirs
.size() - 1; i
>= 0; i
--) {
90 File dir
= dirs
.get(i
);
92 String description
= dir
.getName();
93 File slcLocal
= new File(dir
.getPath() + File
.separator
95 if (slcLocal
.exists()) {
96 Properties properties
= SlcAntConfig
.loadFile(slcLocal
98 description
= properties
99 .getProperty(SlcAntConfig
.DIR_DESCRIPTION_PROPERTY
);
101 if (i
== 0) {// project it self
102 description
= project
.getDescription() != null ? project
103 .getDescription() : "";
106 SimpleSElement element
= new SimpleSElement(description
);
108 if (dir
.equals(rootDir
)) {
109 currPath
= TreeSPath
.createRootPath(rootDir
.getName());
111 currPath
= currPath
.createChild(dir
.getName());
113 registry
.register(currPath
, element
);
115 project
.addReference(REF_PROJECT_PATH
, currPath
);
118 /** Get the path of a project (root). */
119 public static TreeSPath
getProjectPath(Project project
) {
120 return (TreeSPath
) project
.getReference(REF_PROJECT_PATH
);
123 private File
findSlcRootFile(File dir
) {
124 for (File file
: dir
.listFiles()) {
125 if (!file
.isDirectory() && file
.getName().equals(slcRootFileName
)) {
130 File parentDir
= dir
.getParentFile();
131 if (parentDir
== null) {
132 return null;// stop condition: not found
134 return findSlcRootFile(parentDir
);
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
);
147 private void addSlcTasks(Project project
) {
148 Properties taskdefs
= new Properties();
150 InputStream in
= project
.getClass().getResourceAsStream(
151 "/org/argeo/slc/ant/taskdefs.properties");
154 } catch (IOException e
) {
155 throw new SlcAntException("Cannot load task definitions", e
);
158 for (Object o
: taskdefs
.keySet()) {
159 String name
= o
.toString();
161 project
.addTaskDefinition(name
, Class
.forName(taskdefs
162 .getProperty(name
)));
163 } catch (ClassNotFoundException e
) {
164 log
.error("Unknown class for task " + name
, e
);