KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > ant > AntSpeedo


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Chassande-Barrioz.
25  *
26  */

27
28 package org.objectweb.speedo.ant;
29
30 import org.apache.tools.ant.BuildException;
31 import org.apache.tools.ant.DirectoryScanner;
32 import org.apache.tools.ant.Project;
33 import org.apache.tools.ant.Task;
34 import org.apache.tools.ant.taskdefs.Javac;
35 import org.apache.tools.ant.taskdefs.MatchingTask;
36 import org.apache.tools.ant.types.DTDLocation;
37 import org.apache.tools.ant.types.Path;
38 import org.apache.tools.ant.types.Reference;
39 import org.objectweb.speedo.api.ExceptionHelper;
40 import org.objectweb.speedo.api.SpeedoException;
41 import org.objectweb.speedo.generation.SpeedoCompiler;
42 import org.objectweb.speedo.generation.api.SpeedoCompilerParameter;
43
44 import java.io.File JavaDoc;
45 import java.util.Arrays JavaDoc;
46 import java.util.Collection JavaDoc;
47
48 /**
49  * Ant task that can be used to execute the generation.
50  * <p>Parameters of the task:</p><ul>
51  * <li><code>localpropertiesfile</code>, indicates whether there is a jdo.properties</li>
52  * <li><code>confFile</code>, location of the corresponding properties files</li>
53  * <li><code>logPropFile</code>, location of the log system properties file</li>
54  * <li><code>projectname</code> is the project name (optional) (Jorm parameter)</li>
55  * <li><code>mappername</code> is the mapper name for which the code will be generated</li>
56  * <li><code>keepsrc</code>, indicates whether generated Java files must be kept (optionnal, the default value is true</li>
57  * <li><code>input</code>, location of the base directory of <code>.class</code> files</li>
58  * <li><code>output</code>, location of the base destination directory</li>
59  * <li><code>classpath</code> nested classpath element containing the speedo and jorm jars</li>
60  * <li><code>jdopath</code> nested element containing a parameter <code>dir</code> and <code>include elements</code> , location of <code>.jdo</code> JDO XML Descriptors</li>
61  * <li><code>jormpath</code> (similar to <code>jdopath</code>), location of <code>.pd</code> JORM XML Descriptors</li>
62  * <li><code>persistenceaware</code> (similar to <code>jdopath</code>), location of Persistence Aware Java Class</li>
63  * <li><code>keepclasses</code>, indicates whether original java classes must be stored in another directory (see below)</li>
64  * <li><code>storedir</code>, location for storing original classes</li>
65  * <li><code>failsonerror</code>, indicates whether the process should fail if an error occurs</li></ul>
66  * @author S.Chassande-Barrioz
67  */

68 public class AntSpeedo extends Task {
69
70     private Path classpath = null;
71     private Path jormclasspath = null;
72     private Description jdo = null;
73     private Description jorm = null;
74     private Description awareFiles = null;
75
76     private SpeedoCompilerParameter scp = null;
77     private SpeedoCompiler sc = null;
78     private File JavaDoc src = null;
79     private File JavaDoc output = null;
80
81     public void init() throws BuildException {
82         super.init();
83         sc = new SpeedoCompiler();
84         scp = sc.getSpeedoCompilerParameter();
85     }
86
87     /**
88      * @deprecated
89      */

90     public void setConfFile(File JavaDoc confFile) {
91     }
92
93     public void setLogPropFile(File JavaDoc logPropFile) {
94         scp.logPropFile = logPropFile.getAbsolutePath();
95     }
96
97     public void setProjectname(String JavaDoc projectName) {
98         scp.projectName = projectName;
99     }
100
101     public void setMappername(String JavaDoc mapperName) {
102         scp.mapperName = mapperName;
103     }
104
105     public void setSrc(File JavaDoc src) {
106         this.src = src;
107     }
108
109     public void setKeepsrc(boolean keepsrc) {
110         scp.keepsrc = keepsrc;
111     }
112
113     /**
114      * Create a DTD location record. This stores the location of a DTD. The
115      * DTD is identified by its public Id. The location may either be a file
116      * location or a resource location.
117      *
118      * @return the DTD location object to be configured by Ant
119      */

120     public DTDLocation createDTD() {
121         DTDLocation dtdLocation = new DTDLocation();
122         scp.dtdLocations.add(dtdLocation);
123         return dtdLocation;
124     }
125
126     public void setOutput(File JavaDoc output) {
127         this.output = output;
128         scp.output = output.getAbsolutePath();
129     }
130
131     public void setInput(File JavaDoc intput) {
132         scp.input = intput.getAbsolutePath();
133     }
134
135     public void setClasspath(Path cp) {
136         if (classpath == null)
137             classpath = cp;
138         else
139             classpath.append(cp);
140     }
141
142     public Path getClasspath() {
143         return classpath;
144     }
145
146     public Path createClasspath() {
147         if (classpath == null)
148             classpath = new Path(getProject());
149         return classpath.createPath();
150     }
151
152     public void setClasspathRef(Reference r) {
153         createClasspath().setRefid(r);
154     }
155
156     public Description createJdopath() {
157         Description d = new Description();
158         this.jdo = d;
159         d.setProject(getProject());
160         return d;
161     }
162     public MatchingTask createJavac() {
163         if(scp.javac == null) {
164             scp.javac = new Javac();
165             scp.javac.setProject(getProject());
166         }
167         return scp.javac;
168     }
169
170     public Description createJormpath() {
171         Description d = new Description();
172         this.jorm = d;
173         d.setProject(getProject());
174         return d;
175     }
176
177     public void setJormClasspath(Path cp) {
178         if (jormclasspath == null)
179             jormclasspath = cp;
180         else
181             jormclasspath.append(cp);
182     }
183
184     public Path getJormClasspath() {
185         return jormclasspath;
186     }
187
188     public Path createJormClasspath() {
189         if (jormclasspath == null)
190             jormclasspath = new Path(getProject());
191         return jormclasspath.createPath();
192     }
193
194     public void setJormClasspathRef(Reference r) {
195         createJormClasspath().setRefid(r);
196     }
197
198     public Description createPersistenceaware() {
199         Description d = new Description();
200         this.awareFiles = d;
201         d.setProject(getProject());
202         return d;
203     }
204
205     public void setGenerateJormFile(boolean val) {
206         scp.generateNeededJormFile = val;
207     }
208
209     public boolean getGenerateJormFile() {
210         return scp.generateNeededJormFile;
211     }
212
213     /**
214      * Main method of the task executed by ant.
215      * Parses xml parameters, loads AntSpeedoExec with its own loader
216      */

217     public void execute() throws BuildException {
218         if (getClass().getClassLoader().getResourceAsStream("jorm.properties") == null) {
219             throw new BuildException("Speedo ant task ERROR: Impossible to " +
220                     "find the 'jorm.properties' file in the classpath, classloader="
221                     + getClass().getClassLoader());
222         }
223         if (scp.mapperName == null) {
224             scp.mapperName = "rdb";
225         }
226         scp.input = src.getAbsolutePath();
227         if (jdo == null) {
228             jdo = createJdopath();
229             jdo.setDir(src);
230             jdo.setIncludes("**/*.jdo");
231         }
232         scp.jdoDir = jdo.dir.getAbsolutePath();
233         scp.jdo = getSelectedFiles(jdo);
234         log("scp.jdo.size=" + scp.jdo.size(), Project.MSG_DEBUG);
235
236
237         if (jorm == null) {
238             jorm = createJormpath();
239             jorm.setDir(src);
240             jorm.setIncludes("**/*.pd");
241         }
242         scp.jormDir = jorm.dir.getAbsolutePath();
243         scp.jorm = getSelectedFiles(jorm);
244         
245         if (awareFiles == null) {
246             awareFiles = createJdopath();
247             awareFiles.setDir(output);
248             awareFiles.setIncludes("**/*.class");
249         }
250         scp.awareFilesDir = awareFiles.dir.getAbsolutePath();
251         scp.awareFiles = getSelectedFiles(awareFiles);
252
253         if (jormclasspath == null) {
254             jormclasspath = classpath;
255         }
256         scp.jormclasspath = Arrays.asList(jormclasspath.list());
257         scp.classpath = classpath;
258
259         log("scp.jormclasspath=" + scp.jormclasspath, Project.MSG_DEBUG);
260
261         if (scp.dtdLocations.size() == 0) {
262             DTDLocation dl = new DTDLocation();
263             dl.setPublicId("-//ObjectWeb Consortium//DTD JORM DEFINITIONS 2.0//EN");
264             dl.setLocation("jorm2.dtd");
265             scp.dtdLocations.add(dl);
266             dl = new DTDLocation();
267             dl.setPublicId("-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 1.0//EN");
268             dl.setLocation("jdo.dtd");
269             scp.dtdLocations.add(dl);
270         }
271         classpath = null;
272         jormclasspath = null;
273         jdo = null;
274         jorm = null;
275         awareFiles = null;
276         scp = null;
277         src = null;
278         output = null;
279         try {
280             log("SpeedoCompiler init", Project.MSG_DEBUG);
281             sc.init();
282             log("SpeedoCompiler process", Project.MSG_DEBUG);
283             sc.process();
284             log("SpeedoCompiler end", Project.MSG_DEBUG);
285         } catch (SpeedoException e) {
286             e.printStackTrace();
287             throw new BuildException(ExceptionHelper.getNested(e));
288         } catch (Throwable JavaDoc e) {
289             e.printStackTrace();
290             throw new BuildException(e);
291         }
292     }
293
294     /**
295      * Extract selected files from description item.
296      * @param desc
297      * @return a collection of String corresponding to selected files
298      */

299     private Collection JavaDoc getSelectedFiles(Description desc) {
300         Collection JavaDoc result = Arrays.asList(
301                 desc.getDirectoryScanner(desc.dir).getIncludedFiles());
302         Collection JavaDoc toDeselct = Arrays.asList(
303                 desc.getDirectoryScanner(desc.dir).getExcludedFiles());
304         log(toDeselct.toString(), Project.MSG_DEBUG);
305         result.removeAll(toDeselct);
306         return result;
307     }
308     
309     /**
310      * Task used to parse nested <code>jdopath</code> or <code>jormpath</code> elements.
311      */

312     public class Description extends MatchingTask {
313
314         public File JavaDoc dir = null;
315
316         public void setDir(File JavaDoc dir) {
317             this.dir = dir;
318         }
319
320         public DirectoryScanner getDirectoryScanner(File JavaDoc p) {
321             return super.getDirectoryScanner(p);
322         }
323     }
324
325 }
Popular Tags