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";
44 private static String SLC_TYPEDEFS_RESOURCE_PATH
= "/org/argeo/slc/ant/typedefs.properties";
47 public void parse(Project project
, Object source
) throws BuildException
{
48 if (!(source
instanceof File
)) {
49 throw new UnsupportedException("Ant file", source
);
51 File sourceFile
= (File
) source
;
54 SlcAntConfig slcAntConfig
= new SlcAntConfig();
56 // Reset basedir property, in order to avoid base dir override when
58 project
.setProperty("basedir", sourceFile
.getParentFile()
60 if (!slcAntConfig
.initProject(project
)) {
61 // not SLC compatible, do normal Ant
62 super.parse(project
, source
);
67 // log4j is initialized only now
68 log
= LogFactory
.getLog(SlcProjectHelper
.class);
70 log
.debug("SLC properties are set, starting initialization..");
72 // init Spring application context
73 initSpringContext(project
);
75 // init structure registry
76 DefaultSRegistry registry
= new DefaultSRegistry();
77 project
.addReference(REF_STRUCTURE_REGISTRY
, registry
);
79 // call the underlying implementation to do the actual work
80 super.parse(project
, source
);
82 // create structure root
83 registerProjectAndParents(project
, slcAntConfig
);
85 addCustomTaskAndTypes(project
);
89 /** Creates the tree-based structure for this project. */
90 private void registerProjectAndParents(Project project
,
91 SlcAntConfig slcAntConfig
) {
92 StructureRegistry registry
= (StructureRegistry
) project
93 .getReference(REF_STRUCTURE_REGISTRY
);
94 File rootDir
= new File(project
95 .getUserProperty(SlcAntConfig
.ROOT_DIR_PROPERTY
))
97 File baseDir
= project
.getBaseDir().getAbsoluteFile();
98 List
<File
> dirs
= new Vector
<File
>();
99 File currentDir
= baseDir
;
101 dirs
.add(currentDir
);
102 currentDir
= currentDir
.getParentFile();
103 if (log
.isTraceEnabled())
104 log
.trace("List " + currentDir
);
105 } while (!currentDir
.equals(rootDir
.getParentFile()));
107 // first path is root dir (because of previous algorithm)
108 TreeSPath currPath
= TreeSPath
.createRootPath(rootDir
.getName());
109 for (int i
= dirs
.size() - 1; i
>= 0; i
--) {
110 File dir
= dirs
.get(i
);
112 // retrieves description for this path
113 final String description
;
114 if (i
== 0) {// project itself
115 description
= project
.getDescription() != null ? project
116 .getDescription() : "[no desc]";
118 description
= slcAntConfig
.getDescriptionForDir(dir
);
120 SimpleSElement element
= new SimpleSElement(description
);
122 // creates and register path
123 if (!dir
.equals(rootDir
)) {// already set
124 currPath
= currPath
.createChild(dir
.getName());
126 registry
.register(currPath
, element
);
128 project
.addReference(REF_PROJECT_PATH
, currPath
);
131 /** Gets the path of a project (root). */
132 public static TreeSPath
getProjectPath(Project project
) {
133 return (TreeSPath
) project
.getReference(REF_PROJECT_PATH
);
136 /** Initializes the Spring application context. */
137 private void initSpringContext(Project project
) {
138 System
.getProperties().putAll((Map
<?
, ?
>) project
.getProperties());
139 String acPath
= project
140 .getUserProperty(SlcAntConfig
.APPLICATION_CONTEXT_PROPERTY
);
141 if (log
.isDebugEnabled())
142 log
.debug("Loading Spring application context from " + acPath
);
143 // FIXME: workaround to the removal of leading '/' by Spring
145 AbstractApplicationContext context
= new FileSystemXmlApplicationContext(
147 context
.registerShutdownHook();
148 project
.addReference(REF_ROOT_CONTEXT
, context
);
151 /** Loads the SLC specific Ant tasks. */
152 private void addCustomTaskAndTypes(Project project
) {
153 Properties taskdefs
= getDefs(project
, SLC_TASKDEFS_RESOURCE_PATH
);
154 for (Object o
: taskdefs
.keySet()) {
155 String name
= o
.toString();
157 project
.addTaskDefinition(name
, Class
.forName(taskdefs
158 .getProperty(name
)));
159 } catch (ClassNotFoundException e
) {
160 log
.error("Unknown class for task " + name
, e
);
163 Properties typedefs
= getDefs(project
, SLC_TYPEDEFS_RESOURCE_PATH
);
164 for (Object o
: typedefs
.keySet()) {
165 String name
= o
.toString();
167 project
.addDataTypeDefinition(name
, Class
.forName(typedefs
168 .getProperty(name
)));
169 } catch (ClassNotFoundException e
) {
170 log
.error("Unknown class for type " + name
, e
);
175 private Properties
getDefs(Project project
, String path
) {
176 Properties defs
= new Properties();
178 InputStream in
= project
.getClass().getResourceAsStream(path
);
181 } catch (IOException e
) {
182 throw new SlcAntException("Cannot load task definitions", e
);