KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > util > lib > AntJorm


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
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  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.util.lib;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.taskdefs.MatchingTask;
28 import org.apache.tools.ant.taskdefs.UpToDate;
29 import org.apache.tools.ant.taskdefs.Javac;
30 import org.apache.tools.ant.types.DTDLocation;
31 import org.apache.tools.ant.types.Path;
32 import org.apache.tools.ant.types.FileSet;
33 import org.apache.tools.ant.types.Reference;
34 import org.objectweb.jorm.api.PException;
35 import org.objectweb.jorm.compiler.api.JormCompilerParameter;
36 import org.objectweb.jorm.compiler.api.JormCompilerConfigurator;
37 import org.objectweb.jorm.compiler.lib.JormCompiler;
38 import org.objectweb.jorm.compiler.lib.JormCompilerParameterImpl;
39 import org.objectweb.jorm.compiler.lib.JormCompilerConfiguratorImpl;
40 import org.objectweb.jorm.util.io.api.PathExplorer;
41 import org.objectweb.jorm.util.io.lib.DirJavaExplorer;
42
43 import java.io.File JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.StringTokenizer JavaDoc;
46
47 /**
48  * The JORM ant task.
49  */

50 public class AntJorm extends MatchingTask {
51     private String JavaDoc jormConfFile = "jorm.properties";
52     private File JavaDoc pdDir = null;
53
54     private boolean antDtdLocationsSet = false;
55     private ArrayList JavaDoc antDtdLocations = new ArrayList JavaDoc();
56     private boolean antJavacSet = false;
57     private boolean antJavac;
58     private boolean classpathSet = false;
59     private PathExplorer classpath = new DirJavaExplorer();
60     private boolean antOutputSet = false;
61     private String JavaDoc antOutput;
62     private boolean antLogConfFileSet = false;
63     private String JavaDoc antLogConfFile;
64     private boolean antProjectNameSet = false;
65     private String JavaDoc antProjectName;
66     private boolean antBindingInheritanceSet = false;
67     private String JavaDoc antBindingInheritance;
68     private boolean antClassMappingInheritanceSet = false;
69     private String JavaDoc antClassMappingInheritance;
70     private boolean antVerboseSet = false;
71     private boolean antVerbose;
72     private boolean antKeepSrcSet = false;
73     private boolean antKeepSrc;
74     private boolean antBindingAbstractSet = false;
75     private boolean antBindingAbstract;
76     private boolean generatedPDFiles = false;
77     private String JavaDoc mapperNames = null;
78
79     private JormCompilerParameter compilerParameter = null;
80     private JormCompilerConfigurator compilerConfigurator = null;
81     private JormCompiler compiler = null;
82
83     public AntJorm() {
84         compilerParameter = new JormCompilerParameterImpl();
85         compilerConfigurator = new JormCompilerConfiguratorImpl();
86         compiler = new JormCompiler(compilerParameter, compilerConfigurator);
87     }
88
89     /**
90      * It assigns the directory name where the include and exclude clause has
91      * been based. (REQUIRED)
92      */

93     public void setPdDir(File JavaDoc d) {
94         pdDir = d;
95         classpath.addPath(d.getAbsolutePath());
96     }
97
98     /**
99      * Create a DTD location record. This stores the location of a DTD. The
100      * DTD is identified by its public Id. The location may either be a file
101      * location or a resource location.
102      * @return the DTD location object to be configured by Ant
103      */

104     public DTDLocation createDTD() {
105         DTDLocation dtdLocation = new DTDLocation();
106         antDtdLocations.add(dtdLocation);
107         antDtdLocationsSet = true;
108         return dtdLocation;
109     }
110
111     /**
112      * The assigned boolean value indicates if the generated must be compiled.
113      * The compilation is done after the generation of all classes.
114      * true=> no compilation / false=> the classes will be compiled
115      * This parameter can be override for a class if you use a local jorm
116      * configuration file which defines also the property.
117      * (OPTIONAL: default value is false)
118      */

119     public void setJavac(boolean l) {
120         antJavac = l;
121         antJavacSet = true;
122     }
123
124     /**
125      * Defines the list of mappers to be used for compilation. This list is in
126      * the form of "m1.sm1,m1.sm2,m2.sm1" where full mapper names are composed
127      * of a mapper name (e.g., rdb) followed by a "." followed by a sub-mapper
128      * name (e.g., mysql). Full mapper names are separated by ",".
129      * @param mapperNames
130      */

131     public void setMapperNames(String JavaDoc mapperNames) {
132         if (mapperNames != null && mapperNames.length() > 0) {
133             this.mapperNames = mapperNames;
134         }
135     }
136
137     /**
138      * It assigns the classpath where jorm can load .pd files which are
139      * needed (the dependecies).
140      * (OPTIONAL: default value is the PdDir)
141      */

142     public void setOtherPdFiles(Path cp) {
143         String JavaDoc[] dl = cp.list();
144         for (int i = 0; i < dl.length; i++) {
145             classpath.addPath(dl[i]);
146             classpathSet = true;
147         }
148     }
149
150     public void setOtherPdFilesRef(Reference r) {
151         Path p = new Path(project);
152         p.setRefid(r);
153         setOtherPdFiles(p);
154     }
155
156     /**
157      * The ouput name is the directory name where the generated files will
158      * be created. Futhermmore the classes will be compiled in this directory.
159      * (REQUIRED)
160      * @param s the string representation of the output directory
161      */

162     public void setOutput(String JavaDoc s) {
163         antOutput = s;
164         antOutputSet = true;
165     }
166
167     /**
168      * It assigns the jorm configuration file. (REQUIRED)
169      * @param s the string representation of the configuration file.
170      */

171     public void setJormConf(String JavaDoc s) {
172         jormConfFile = s;
173     }
174
175     /**
176      * It assigns the jorm configuration file. (REQUIRED)
177      * @param s the string representation of the configuration file.
178      */

179     public void setLogConf(String JavaDoc s) {
180         antLogConfFile = s;
181         antLogConfFileSet = true;
182     }
183
184     /**
185      * It assigns the project name which must be used.
186      * This parameter can be override for a class if you use a local jorm
187      * configuration file which defines also the property.
188      * (OPTIONAL default value = "" empty string)
189      */

190     public void setProjectName(String JavaDoc s) {
191         antProjectName = s;
192         antProjectNameSet = true;
193     }
194
195     /**
196      * It assigns the class name which the generated PBinding classes must
197      * extend.
198      * This parameter can be override for a class if you use a local jorm
199      * configuration file which defines also the property.
200      * (OPTIONAL default value is "")
201      */

202     public void setBindingInheritance(String JavaDoc s) {
203         antBindingInheritance = s;
204         antBindingInheritanceSet = true;
205     }
206
207     /**
208      * It assigns the class name which the generated PBinding classes must
209      * extend.
210      * This parameter can be override for a class if you use a local jorm
211      * configuration file which defines also the property.
212      * (OPTIONAL default value is "")
213      */

214     public void setMappingInheritance(String JavaDoc s) {
215         antClassMappingInheritance = s;
216         antClassMappingInheritanceSet = true;
217     }
218
219     /**
220      * It assigns the verbose mode to the jorm generator.
221      * This parameter can be override for a class if you use a local jorm
222      * configuration file which defines also the property.
223      * (OPTIONAL the default value is false)
224      */

225     public void setVerbose(boolean verbose) {
226         antVerbose = verbose;
227         antVerboseSet = true;
228     }
229
230     /**
231      * It assigns a boolean to indicate if the generated .java file must be
232      * kept.
233      * This parameter can be override for a class if you use a local jorm
234      * configuration file which defines also the property.
235      * (OPTIONAL the default value is true)
236      */

237     public void setKeepsrc(boolean keepsrc) {
238         antKeepSrc = keepsrc;
239         antKeepSrcSet = true;
240     }
241
242     /**
243      * It assigns the boolean value which indictes if the generated Binding
244      * must be declared abstract.
245      * This parameter can be override for a class if you use a local jorm
246      * configuration file which defines also the property.
247      * This parameter must be defined in the task or in the jorm configuration
248      * file.
249      */

250     public void setBindingAbstract(boolean bindingAbstract) {
251         antBindingAbstract = bindingAbstract;
252         antBindingAbstractSet = true;
253     }
254
255
256     public void setGeneratePDFiles(boolean val) {
257         generatedPDFiles = val;
258     }
259
260     /**
261      * execute method of the Ant task
262      */

263     public void execute() throws BuildException {
264         try {
265             compilerConfigurator.configure(jormConfFile);
266             if (!antLogConfFileSet) {
267                 compilerConfigurator.configureLog(jormConfFile);
268             } else {
269                 compilerConfigurator.configureLog(antLogConfFile);
270             }
271             if (mapperNames != null) {
272                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(mapperNames.trim(), ".,", true);
273                 if (st.hasMoreTokens()) {
274                     compilerConfigurator.removeAllMappers();
275                 }
276                 while (st.hasMoreTokens()) {
277                     String JavaDoc mn = st.nextToken(), smn;
278                     if (mn.equals(".") || mn.equals(",")) {
279                         String JavaDoc errmsg = "Malformed mapperNames string (expect mapper name): " + mapperNames;
280                         log(errmsg, project.MSG_ERR);
281                         throw new BuildException(errmsg);
282                     }
283                     if (!st.hasMoreTokens()) {
284                         String JavaDoc errmsg = "Malformed mapperNames string (expect \".\"): " + mapperNames;
285                         log(errmsg, project.MSG_ERR);
286                         throw new BuildException(errmsg);
287                     }
288                     smn = st.nextToken();
289                     if (!smn.equals(".")) {
290                         String JavaDoc errmsg = "Malformed mapperNames string (expect \".\"): " + mapperNames;
291                         log(errmsg, project.MSG_ERR);
292                         throw new BuildException(errmsg);
293                     }
294                     if (!st.hasMoreTokens()) {
295                         String JavaDoc errmsg = "Malformed mapperNames string (expect submapper name): " + mapperNames;
296                         log(errmsg, project.MSG_ERR);
297                         throw new BuildException(errmsg);
298                     }
299                     smn = st.nextToken();
300                     compilerConfigurator.addSubMapper(mn, smn);
301                     if (st.hasMoreTokens()) {
302                         smn = st.nextToken();
303                         if (!smn.equals(",")) {
304                             String JavaDoc errmsg = "Malformed mapperNames string (expect \",\"): " + mapperNames;
305                             log(errmsg, project.MSG_ERR);
306                             throw new BuildException(errmsg);
307                         }
308                     }
309                 }
310             }
311             compilerParameter.loadConfFile(compilerConfigurator.getGlobalJormcOptsFile(),
312                                            compilerConfigurator.knownMappers());
313             if (antDtdLocationsSet) {
314                 compilerParameter.setDtdLocations(antDtdLocations);
315             }
316             if (antJavacSet) {
317                 compilerParameter.setJavac(antJavac);
318             }
319             if (classpathSet) {
320                 compilerParameter.setClasspath(classpath);
321             }
322             if (antOutputSet) {
323                 compilerParameter.setOutput(antOutput);
324             }
325             if (antLogConfFileSet) {
326                 compilerParameter.setLogConfFile(antLogConfFile);
327             }
328             if (antProjectNameSet) {
329                 compilerParameter.setProjectName(antProjectName);
330             }
331             if (antBindingInheritanceSet) {
332                 compilerParameter.setBindingInheritance(antBindingInheritance);
333             }
334             if (antClassMappingInheritanceSet) {
335                 compilerParameter.setClassMappingInheritance(antClassMappingInheritance);
336             }
337             if (antVerboseSet) {
338                 compilerParameter.setVerbose(antVerbose);
339             }
340             if (antKeepSrcSet) {
341                 compilerParameter.setKeepSrc(antKeepSrc);
342             }
343             if (antBindingAbstractSet) {
344                 compilerParameter.setBindingAbstract(antBindingAbstract);
345             }
346             compilerParameter.setGeneratedPDFiles(generatedPDFiles);
347         } catch (PException e) {
348             Exception JavaDoc current = e;
349             while (current instanceof PException
350                     && ((PException) current).getNestedException() != null) {
351                 current = ((PException) current).getNestedException();
352             }
353             throw new BuildException(current);
354         }
355         log("Assigns immutable properties", project.MSG_DEBUG);
356         // Assigns the properties which CANNOT be overrieded by the jorm
357
// configuration file.
358
log("pdDir=" + pdDir, project.MSG_DEBUG);
359         String JavaDoc[] pdFiles = super.getDirectoryScanner(pdDir).getIncludedFiles();
360         log("Generates(" + pdFiles + ")", project.MSG_DEBUG);
361         generates(pdFiles);
362     }
363
364     public void generates(String JavaDoc[] pdFileNames) throws BuildException {
365         //Add the pd file
366
ArrayList JavaDoc compileList = new ArrayList JavaDoc();
367         for (int i = 0; i < pdFileNames.length; i++) {
368             int idx = pdFileNames[i].lastIndexOf(File.separatorChar);
369             String JavaDoc packageName;
370             String JavaDoc className;
371             if (idx == -1) {
372                 packageName = null;
373                 className = pdFileNames[i];
374             } else {
375                 packageName = pdFileNames[i].substring(0, idx);
376                 className = pdFileNames[i].substring(idx + 1);
377             }
378             className = className.substring(0, className.lastIndexOf('.'));
379             log("-packageName=" + packageName, project.MSG_DEBUG);
380             log("-className=" + className, project.MSG_DEBUG);
381             File JavaDoc pdFile = new File JavaDoc(pdDir, pdFileNames[i]);
382             log("-pdfile=" + pdFile, project.MSG_DEBUG);
383             FileSet fs = new FileSet();
384             fs.setProject(project);
385             fs.setDir(pdDir);
386             fs.setIncludes(pdFileNames[i]);
387             UpToDate u = new UpToDate();
388             u.setProject(project);
389             u.addSrcfiles(fs);
390             // The target is the generated accessor file or pnamegetter file
391
String JavaDoc pngFileName = compilerParameter.getOutput() + File.separator
392                     + (packageName == null ? "" : packageName + File.separator)
393                     + File.separator + className + "PNG.java";
394             log("\tpngFileName=" + pngFileName, project.MSG_DEBUG);
395             String JavaDoc accFileName = compilerParameter.getOutput() + File.separator
396                     + (packageName == null ? "" : packageName + File.separator)
397                     + File.separator + className + "Accessor.java";
398             log("\taccFileName=" + accFileName, project.MSG_DEBUG);
399             boolean mustGenerate;
400             File JavaDoc pngFile = new File JavaDoc(pngFileName);
401             File JavaDoc accFile = new File JavaDoc(accFileName);
402             if (accFile.exists()) {
403                 u.setTargetFile(accFile);
404                 mustGenerate = !u.eval();
405             } else if (pngFile.exists()) {
406                 u.setTargetFile(pngFile);
407                 mustGenerate = !u.eval();
408             } else {
409                 // else no file exists => generate
410
mustGenerate = true;
411             }
412             if (mustGenerate)
413                 compileList.add(pdFileNames[i]);
414         }
415         if (compileList.size() > 0) {
416             project.log("not upToDate", project.MSG_DEBUG);
417             compilerParameter.setInputFiles(compileList);
418             try {
419                 boolean javac = compilerParameter.isJavac();
420                 compilerParameter.setJavac(false);
421                 compiler.process();
422                 if (javac) {
423                     log("javac compilation", project.MSG_DEBUG);
424                     // compiles the generated java files
425
Javac javacinst = new Javac();
426                     Path path = new Path(project, compilerParameter.getOutput());
427                     javacinst.setProject(project);
428                     javacinst.setSrcdir(path);
429                     javacinst.setDestdir(new File JavaDoc(compilerParameter.getOutput()));
430                     javacinst.setDebug(true);
431                     javacinst.setIncludes("**/*.java");
432                     //javacinst.setClasspath(classpath);
433
javacinst.execute();
434                 }
435             } catch (PException e) {
436                 Exception JavaDoc current = e;
437                 while (current instanceof PException
438                         && ((PException) current).getNestedException() != null) {
439                     current = ((PException) current).getNestedException();
440                 }
441                 current.printStackTrace();
442                 throw new BuildException(current);
443             }
444         } else {
445             project.log("upToDate", project.MSG_DEBUG);
446         }
447     }
448 }
449
Popular Tags