1 package org
.argeo
.slc
.ant
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
8 import java
.util
.Properties
;
9 import java
.util
.Vector
;
11 import org
.springframework
.context
.support
.AbstractApplicationContext
;
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
.ProjectHelper2
;
20 import org
.argeo
.slc
.core
.UnsupportedException
;
21 import org
.argeo
.slc
.core
.structure
.DefaultSRegistry
;
22 import org
.argeo
.slc
.core
.structure
.SimpleSElement
;
23 import org
.argeo
.slc
.core
.structure
.StructureRegistry
;
24 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
27 * Custom implementation of an Ant <code>ProjectHelper</code> binding a Spring
28 * application context and a structure registry with the Ant project.
30 public class SlcProjectHelper
extends ProjectHelper2
{
31 private static Log log
;
33 /** The Ant reference to the Spring application context used. */
34 public static String REF_ROOT_CONTEXT
= "slcApplicationContext";
35 /** The Ant reference to the SLC structure registry used. */
36 public static String REF_STRUCTURE_REGISTRY
= "slcStructureRegistry";
37 /** The Ant reference to the <code>TreePath</code> of the current project */
38 private static String REF_PROJECT_PATH
= "slcProjectPath";
40 * Resource path to the property file listing the SLC specific Ant tasks:
41 * /org/argeo/slc/ant/taskdefs.properties
43 private static String SLC_TASKDEFS_RESOURCE_PATH
= "/org/argeo/slc/ant/taskdefs.properties";
46 public void parse(Project project
, Object source
) throws BuildException
{
47 if (!(source
instanceof File
)) {
48 throw new UnsupportedException("Ant file", source
);
50 File sourceFile
= (File
) source
;
53 SlcAntConfig slcAntConfig
= new SlcAntConfig();
55 // Reset basedir property, in order to avoid base dir override when
57 project
.setProperty("basedir", sourceFile
.getParentFile()
59 if (!slcAntConfig
.initProject(project
)) {
60 // not SLC compatible, do normal Ant
61 super.parse(project
, source
);
66 // log4j is initialized only now
67 log
= LogFactory
.getLog(SlcProjectHelper
.class);
69 log
.debug("SLC properties are set, starting initialization..");
71 // init Spring application context
72 initSpringContext(project
);
74 // init structure registry
75 DefaultSRegistry registry
= new DefaultSRegistry();
76 project
.addReference(REF_STRUCTURE_REGISTRY
, registry
);
78 // call the underlying implementation to do the actual work
79 super.parse(project
, source
);
81 // create structure root
82 registerProjectAndParents(project
, slcAntConfig
);
88 /** Creates the tree-based structure for this project. */
89 private void registerProjectAndParents(Project project
,
90 SlcAntConfig slcAntConfig
) {
91 StructureRegistry registry
= (StructureRegistry
) project
92 .getReference(REF_STRUCTURE_REGISTRY
);
93 File rootDir
= new File(project
94 .getUserProperty(SlcAntConfig
.ROOT_DIR_PROPERTY
))
96 File baseDir
= project
.getBaseDir().getAbsoluteFile();
97 List
<File
> dirs
= new Vector
<File
>();
98 File currentDir
= baseDir
;
100 dirs
.add(currentDir
);
101 currentDir
= currentDir
.getParentFile();
102 if (log
.isTraceEnabled())
103 log
.trace("List " + currentDir
);
104 } while (!currentDir
.equals(rootDir
.getParentFile()));
106 // first path is root dir (because of previous algorithm)
107 TreeSPath currPath
= TreeSPath
.createRootPath(rootDir
.getName());
108 for (int i
= dirs
.size() - 1; i
>= 0; i
--) {
109 File dir
= dirs
.get(i
);
111 // retrieves description for this path
112 final String description
;
113 if (i
== 0) {// project itself
114 description
= project
.getDescription() != null ? project
115 .getDescription() : "[no desc]";
117 description
= slcAntConfig
.getDescriptionForDir(dir
);
119 SimpleSElement element
= new SimpleSElement(description
);
121 // creates and register path
122 if (!dir
.equals(rootDir
)) {// already set
123 currPath
= currPath
.createChild(dir
.getName());
125 registry
.register(currPath
, element
);
127 project
.addReference(REF_PROJECT_PATH
, currPath
);
130 /** Gets the path of a project (root). */
131 public static TreeSPath
getProjectPath(Project project
) {
132 return (TreeSPath
) project
.getReference(REF_PROJECT_PATH
);
135 /** Initializes the Spring application context. */
136 private void initSpringContext(Project project
) {
137 System
.getProperties().putAll((Map
<?
, ?
>) project
.getProperties());
138 String acPath
= project
139 .getUserProperty(SlcAntConfig
.APPLICATION_CONTEXT_PROPERTY
);
140 AbstractApplicationContext context
= new FileSystemXmlApplicationContext(
142 context
.registerShutdownHook();
143 project
.addReference(REF_ROOT_CONTEXT
, context
);
146 /** Loads the SLC specific Ant tasks. */
147 private void addSlcTasks(Project project
) {
148 Properties taskdefs
= new Properties();
150 InputStream in
= project
.getClass().getResourceAsStream(
151 SLC_TASKDEFS_RESOURCE_PATH
);
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
);