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
.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 ProjectHelperImpl
{
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 System
.out
.println("Base dir prop2: " + project
.getProperty("basedir"));
56 // In order to avoid base dire override when running in Maven
57 project
.setProperty("basedir", sourceFile
.getParentFile().getAbsolutePath());
58 if (!slcAntConfig
.initProject(project
)) {
59 // not SLC compatible, do normal Ant
60 super.parse(project
, source
);
65 // log4j is initialized only now
66 log
= LogFactory
.getLog(SlcProjectHelper
.class);
68 log
.debug("SLC properties are set, starting initialization..");
69 log
.debug("Base dir1: " + project
.getBaseDir().getAbsoluteFile());
70 log
.debug("Base dir prop1: " + project
.getProperty("basedir"));
72 // init Spring application context
73 initSpringContext(project
);
74 log
.debug("Base dir2: " + project
.getBaseDir().getAbsoluteFile());
76 // init structure registry
77 DefaultSRegistry registry
= new DefaultSRegistry();
78 project
.addReference(REF_STRUCTURE_REGISTRY
, registry
);
80 log
.debug("Base dir prop2: " + project
.getProperty("basedir"));
81 // in order to prevent pb w/ basedir setting:
82 source
= ((File
) source
).getAbsoluteFile();
83 // call the underlying implementation to do the actual work
84 super.parse(project
, source
);
86 log
.debug("Base dir3: " + project
.getBaseDir().getAbsoluteFile());
87 // create structure root
88 registerProjectAndParents(project
, slcAntConfig
);
94 /** Creates the tree-based structure for this project. */
95 private void registerProjectAndParents(Project project
,
96 SlcAntConfig slcAntConfig
) {
97 StructureRegistry registry
= (StructureRegistry
) project
98 .getReference(REF_STRUCTURE_REGISTRY
);
99 File rootDir
= new File(project
100 .getUserProperty(SlcAntConfig
.ROOT_DIR_PROPERTY
))
102 File baseDir
= project
.getBaseDir().getAbsoluteFile();
103 List
<File
> dirs
= new Vector
<File
>();
104 File currentDir
= baseDir
;
106 dirs
.add(currentDir
);
107 currentDir
= currentDir
.getParentFile();
108 if (log
.isTraceEnabled())
109 log
.trace("List " + currentDir
);
110 } while (!currentDir
.equals(rootDir
.getParentFile()));
112 // first path is root dir (because of previous algorithm)
113 TreeSPath currPath
= TreeSPath
.createRootPath(rootDir
.getName());
114 for (int i
= dirs
.size() - 1; i
>= 0; i
--) {
115 File dir
= dirs
.get(i
);
117 // retrieves description for this path
118 final String description
;
119 if (i
== 0) {// project itself
120 description
= project
.getDescription() != null ? project
121 .getDescription() : "[no desc]";
123 description
= slcAntConfig
.getDescriptionForDir(dir
);
125 SimpleSElement element
= new SimpleSElement(description
);
127 // creates and register path
128 if (!dir
.equals(rootDir
)) {// already set
129 currPath
= currPath
.createChild(dir
.getName());
131 registry
.register(currPath
, element
);
133 project
.addReference(REF_PROJECT_PATH
, currPath
);
136 /** Gets the path of a project (root). */
137 public static TreeSPath
getProjectPath(Project project
) {
138 return (TreeSPath
) project
.getReference(REF_PROJECT_PATH
);
141 /** Initializes the Spring application context. */
142 private void initSpringContext(Project project
) {
143 System
.getProperties().putAll((Map
<?
, ?
>) project
.getProperties());
144 String acPath
= project
145 .getUserProperty(SlcAntConfig
.APPLICATION_CONTEXT_PROPERTY
);
146 AbstractApplicationContext context
= new FileSystemXmlApplicationContext(
148 context
.registerShutdownHook();
149 project
.addReference(REF_ROOT_CONTEXT
, context
);
152 /** Loads the SLC specific Ant tasks. */
153 private void addSlcTasks(Project project
) {
154 Properties taskdefs
= new Properties();
156 InputStream in
= project
.getClass().getResourceAsStream(
157 SLC_TASKDEFS_RESOURCE_PATH
);
160 } catch (IOException e
) {
161 throw new SlcAntException("Cannot load task definitions", e
);
164 for (Object o
: taskdefs
.keySet()) {
165 String name
= o
.toString();
167 project
.addTaskDefinition(name
, Class
.forName(taskdefs
168 .getProperty(name
)));
169 } catch (ClassNotFoundException e
) {
170 log
.error("Unknown class for task " + name
, e
);