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
.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 an Ant <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
;
32 /** The Ant reference to the Spring application context used. */
33 public static String REF_ROOT_CONTEXT
= "slcApplicationContext";
34 /** The Ant reference to the SLC structure registry used. */
35 public static String REF_STRUCTURE_REGISTRY
= "slcStructureRegistry";
36 /** The Ant reference to the <code>TreePath</code> of the current project */
37 private static String REF_PROJECT_PATH
= "slcProjectPath";
39 * Resource path to the property file listing the SLC specific Ant tasks:
40 * /org/argeo/slc/ant/taskdefs.properties
42 private static String SLC_TASKDEFS_RESOURCE_PATH
= "/org/argeo/slc/ant/taskdefs.properties";
45 public void parse(Project project
, Object source
) throws BuildException
{
48 SlcAntConfig slcAntConfig
= new SlcAntConfig();
49 slcAntConfig
.initProject(project
);
52 // log4j is initialized only now
53 log
= LogFactory
.getLog(SlcProjectHelper
.class);
55 log
.debug("SLC properties are set, starting initialization..");
57 // init Spring application context
58 initSpringContext(project
);
60 // init structure registry
61 DefaultSRegistry registry
= new DefaultSRegistry();
62 project
.addReference(REF_STRUCTURE_REGISTRY
, registry
);
64 // call the underlying implementation to do the actual work
65 super.parse(project
, source
);
67 // create structure root
68 registerProjectAndParents(project
, slcAntConfig
);
74 /** Creates the tree-based structure for this project. */
75 private void registerProjectAndParents(Project project
,
76 SlcAntConfig slcAntConfig
) {
77 StructureRegistry registry
= (StructureRegistry
) project
78 .getReference(REF_STRUCTURE_REGISTRY
);
79 File rootDir
= new File(project
80 .getUserProperty(SlcAntConfig
.ROOT_DIR_PROPERTY
))
82 File baseDir
= project
.getBaseDir().getAbsoluteFile();
84 List
<File
> dirs
= new Vector
<File
>();
85 File currentDir
= baseDir
;
88 currentDir
= currentDir
.getParentFile();
89 if (log
.isTraceEnabled())
90 log
.trace("List " + currentDir
);
91 } while (!currentDir
.equals(rootDir
.getParentFile()));
93 // first path is root dir (because of previous algorithm)
94 TreeSPath currPath
= TreeSPath
.createRootPath(rootDir
.getName());
95 for (int i
= dirs
.size() - 1; i
>= 0; i
--) {
96 File dir
= dirs
.get(i
);
98 // retrieves description for this path
99 final String description
;
100 if (i
== 0) {// project itself
101 description
= project
.getDescription() != null ? project
102 .getDescription() : "";
104 description
= slcAntConfig
.getDescriptionForDir(dir
);
106 SimpleSElement element
= new SimpleSElement(description
);
108 // creates and register path
109 if (!dir
.equals(rootDir
)) {// already set
110 currPath
= currPath
.createChild(dir
.getName());
112 registry
.register(currPath
, element
);
114 project
.addReference(REF_PROJECT_PATH
, currPath
);
117 /** Gets the path of a project (root). */
118 public static TreeSPath
getProjectPath(Project project
) {
119 return (TreeSPath
) project
.getReference(REF_PROJECT_PATH
);
122 /** Initializes the Spring application context. */
123 private void initSpringContext(Project project
) {
124 System
.getProperties().putAll((Map
<?
, ?
>) project
.getProperties());
125 String acPath
= project
126 .getUserProperty(SlcAntConfig
.APPLICATION_CONTEXT_PROPERTY
);
127 AbstractApplicationContext context
= new FileSystemXmlApplicationContext(
129 context
.registerShutdownHook();
130 project
.addReference(REF_ROOT_CONTEXT
, context
);
133 /** Loads the SLC specific Ant tasks. */
134 private void addSlcTasks(Project project
) {
135 Properties taskdefs
= new Properties();
137 InputStream in
= project
.getClass().getResourceAsStream(
138 SLC_TASKDEFS_RESOURCE_PATH
);
141 } catch (IOException e
) {
142 throw new SlcAntException("Cannot load task definitions", e
);
145 for (Object o
: taskdefs
.keySet()) {
146 String name
= o
.toString();
148 project
.addTaskDefinition(name
, Class
.forName(taskdefs
149 .getProperty(name
)));
150 } catch (ClassNotFoundException e
) {
151 log
.error("Unknown class for task " + name
, e
);