KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ecm > taskdefs > IDLtoJavaTask


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@objectweb.org
6
//
7
// This library is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 2.1 of the License, or any later version.
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public
18
// License along with this library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
// USA
21
//
22
// Initial developer(s): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.ecm.taskdefs;
31
32 /**
33  ** <p>Build a jar archive from a source directory.</p>
34  **
35  ** <p><bold>Parameters</bold></p>
36  ** <tr>
37  ** <td><bold>Attribute</bold></td>
38  ** <td><bold>Description</bold></td>
39  ** <td><bold>Required</bold></td>
40  ** </tr>
41  ** <tr>
42  ** <td>idlfile</td>
43  ** <td>The OMG IDL file to be compiled.</td>
44  ** <td>Yes</td>
45  ** </tr>
46  ** <tr>
47  ** <td>generateto</td>
48  ** <td>Directory of the generated files.</td>
49  ** <td>Yes</td>
50  ** </tr>
51  ** <tr>
52  ** <td>genfile</td>
53  ** <td>A generated file corresponding to a IDL declaration of the
54  ** compiled OMG IDL file. This file is used to perform an uptodate check.
55  ** <i>NOTE: genfile must be relative to generateto</i></td>
56  ** <td>No, if not set, then the corresponding uptodate check is ignored.</td>
57  ** </tr>
58  ** <tr>
59  ** <td>idldeps</td>
60  ** <td>A list of OMG IDL files which are dependencies of the compiled OMG IDL file.
61  ** This list is used to peform uptodate checks.<br>
62  ** <i>NOTE: not implemented, use nested element instead.</i></td>
63  ** <td>No, if not set, then the corresponding uptodate check is ignored.</td>
64  ** </tr>
65  ** <tr>
66  ** <td>includedirs</td>
67  ** <td>A list of directories used to lookup for included OMG IDL files.<br>
68  ** <i>NOTE: not implemented, use nested element instead.</i></td>
69  ** <td>No.</td>
70  ** </tr>
71  ** <tr>
72  ** <td>includedir</td>
73  ** <td>A directory name used to lookup for included OMG IDL files.<br>
74  ** <i>NOTE: not implemented, use nested element instead.</i></td>
75  ** <td>No.</td>
76  ** </tr>
77  ** <tr>
78  ** <td>prefix</td>
79  ** <td>A prefix which will be appended to the Java package name.</td>
80  ** <td>No.</td>
81  ** </tr>
82  ** <tr>
83  ** <td>archivename</td>
84  ** <td>Name of the built archive.</td>
85  ** <td>No, if not set, then no archive is built.</td>
86  ** </tr>
87  ** <tr>
88  ** <td>compileto</td>
89  ** <td>Directory of the class files.</td>
90  ** <td>No, unless the <tt>archivename</tt> attribute is set.</td>
91  ** </tr>
92  ** <tr>
93  ** <td>buildto</td>
94  ** <td>Directory of the archive (jar) file.</td>
95  ** <td>No, unless the <tt>archivename</tt> attribute is set.</td>
96  ** </tr>
97  ** <tr>
98  ** <td>classpathref (or classpathRef)</td>
99  ** <td>The reference to the classpath used for compilation.</td>
100  ** <td>No</td>
101  ** </tr>
102  ** <tr>
103  ** <td>bootclasspathref (or bootclasspathRef)</td>
104  ** <td>The reference to the bootclasspath used for compilation.</td>
105  ** <td>Yes, unless a nested <tt>bootclasspath</tt> element is present.</td>
106  ** </tr>
107  **
108  ** <p><bold>Parameters specified as nested elements</bold></p>
109  ** <p>This task support the <tt>classpath</tt> and <tt>bootclasspath</tt> attributes also
110  ** as nested elements in the form of path-like structures (unlike the attributes which are
111  ** path references).</p>
112  **
113  ** <p>This task support the <tt>idldeps</tt> and <tt>includedirs</tt> attributes also as nested
114  ** elements in the form of <tt>fileset</tt> element.</p>
115  **
116  ** <p>This task support the <tt>idldep</tt> and <tt>includedir</tt> (without an 's') nested elements
117  ** in the form of a file name.</p>
118  **/

119 abstract public class IDLtoJavaTask
120 extends org.apache.tools.ant.Task
121 {
122     //
123
private java.io.File JavaDoc _idlfile;
124     private java.io.File JavaDoc _generateto;
125     private java.io.File JavaDoc _genfile;
126     private String JavaDoc _genfilename;
127     private java.util.ArrayList JavaDoc _idldeps;
128     private java.util.ArrayList JavaDoc _idldep;
129     private java.util.ArrayList JavaDoc _includedirs;
130     private java.util.ArrayList JavaDoc _includedir;
131     private String JavaDoc _prefix;
132
133     private String JavaDoc _archive;
134     private java.io.File JavaDoc _buildto;
135     private java.io.File JavaDoc _compileto;
136     private BuildJarTask _buildjar;
137
138     // default constructor
139
protected
140     IDLtoJavaTask()
141     {
142         //
143
_idlfile = null;
144         _generateto = null;
145         _genfile = null;
146         _genfilename = null;
147         _idldeps = new java.util.ArrayList JavaDoc();
148         _idldep = new java.util.ArrayList JavaDoc();
149         _includedirs = new java.util.ArrayList JavaDoc();
150         _includedir = new java.util.ArrayList JavaDoc();
151         _prefix = null;
152
153         _archive = null;
154         _buildto = null;
155         _compileto = null;
156         _buildjar = null;
157     }
158
159     //
160
// internal operations
161
//
162

163     private BuildJarTask
164     buildjar()
165     {
166         if (_buildjar==null) {
167             org.apache.tools.ant.Task task = getProject().createTask("buildjar");
168             _buildjar = (BuildJarTask)task;
169         }
170
171         return _buildjar;
172     }
173
174     // NOTE: check required attributes
175
private void
176     validateAttributes()
177     throws org.apache.tools.ant.BuildException
178     {
179         String JavaDoc msg = "";
180         if (_idlfile==null) {
181             msg = "idlfile attribute is missing";
182             throw new org.apache.tools.ant.BuildException(msg);
183         }
184
185         if ((!_idlfile.exists()) || (!_idlfile.isFile())) {
186             msg = "Source OMG IDL file does not exists or is not a file";
187             throw new org.apache.tools.ant.BuildException(msg);
188         }
189
190         if ((!_generateto.exists()) || (!_generateto.isDirectory())) {
191             msg = "Generation directory does not exist or is not a directory";
192             throw new org.apache.tools.ant.BuildException(msg);
193         }
194
195         // compute _genfile
196
_genfile = new java.io.File JavaDoc(_generateto, _genfilename.replace('/', java.io.File.separatorChar));
197         if (_genfile==null) {
198             msg = "[validateAttributes] genfile (null): "+_generateto.getPath()+"/"+_genfilename.replace('/', java.io.File.separatorChar);
199             log(msg);
200         }
201
202         //
203
if (_archive!=null) {
204             if (_compileto==null) {
205                 msg = "compileto attribute must be set when archivename attribute is set";
206                 throw new org.apache.tools.ant.BuildException(msg);
207             }
208
209             if ((!_compileto.exists()) || (!_compileto.isDirectory())) {
210                 msg = "class directory does not exist or is not a directory";
211                 throw new org.apache.tools.ant.BuildException(msg);
212             }
213
214             if (_buildto==null) {
215                 msg = "buildto attribute must be set when archivename attribute is set";
216                 throw new org.apache.tools.ant.BuildException(msg);
217             }
218
219             if ((!_buildto.exists()) || (!_buildto.isDirectory())) {
220                 msg = "archive directory does not exist or is not a directory";
221                 throw new org.apache.tools.ant.BuildException(msg);
222             }
223         }
224     }
225
226     private boolean
227     idlfileUpToDate()
228     {
229         boolean genfile_passed=false;
230         boolean idldeps_passed=false;
231         boolean idldep_passed=false;
232
233         // check against generated file if any
234
if (_genfile!=null) {
235             if ((!_genfile.exists()) || (_genfile.lastModified()<_idlfile.lastModified())) {
236                 return false;
237             }
238             else {
239                 genfile_passed=true;
240             }
241         }
242
243         // check against the IDL dependencies ("idldeps" attribute/nested element)
244
org.apache.tools.ant.types.FileSet[] sets = null;
245         sets = (org.apache.tools.ant.types.FileSet[])_idldeps.toArray(new org.apache.tools.ant.types.FileSet[0]);
246
247         if (sets.length>0) {
248             for (int i=0;i<sets.length;i++) {
249                 org.apache.tools.ant.DirectoryScanner scanner = sets[i].getDirectoryScanner(super.project);
250                 String JavaDoc[] filenames = scanner.getIncludedFiles();
251                 java.io.File JavaDoc file = null;
252
253                 log("[idlfileUpToDate] base dir: "+scanner.getBasedir().getPath());
254                 for (int j=0;j<filenames.length;j++) {
255                     log("[idlfileUpToDate] filename: "+filenames[j]);
256                     file = new java.io.File JavaDoc(scanner.getBasedir(), filenames[j]);
257                     if (_idlfile.lastModified()>file.lastModified()) {
258                         return false;
259                     }
260                 }
261             }
262
263             idldeps_passed=true;
264         }
265
266         // check against the IDL dependencies ("idldep" nested element)
267
NestedElementWithFile[] idlfiles = (NestedElementWithFile[])_idldep.toArray(new NestedElementWithFile[0]);
268         if (idlfiles.length!=0) {
269             for (int i=0;i<idlfiles.length;i++) {
270                 if (_idlfile.lastModified()>idlfiles[i].getFile().lastModified()) {
271                     return false;
272                 }
273             }
274
275             idldep_passed = true;
276         }
277
278         // NOTE: if no check is passed, this means that no uptodate check was performed and therefore
279
// we are forced to say that the file is not uptodate
280
if (genfile_passed || idldeps_passed || idldep_passed) {
281             return true;
282         }
283
284         return false;
285     }
286
287     private boolean
288     jarUpToDate(java.io.File JavaDoc tmpdir)
289     {
290         // check if an archive is requested
291
if (_archive==null) {
292             return true;
293         }
294
295         // check if some source files were compiled
296
if (tmpdir.listFiles().length==0) {
297             return true;
298         }
299
300         return false;
301     }
302
303     //
304
// attribute setters
305
//
306

307     final public void
308     setIdlfile(java.io.File JavaDoc file)
309     {
310         _idlfile = file;
311     }
312
313     final public void
314     setGenerateto(java.io.File JavaDoc dir)
315     {
316         _generateto = dir;
317     }
318
319     final public void
320     setGenfile(String JavaDoc filename)
321     {
322         _genfilename = filename;
323     }
324
325     final public void
326     setIdldeps(String JavaDoc idldeps)
327     {
328         // TODO
329
// NOT IMPLEMENTED
330
}
331
332     final public void
333     addIdldeps(org.apache.tools.ant.types.FileSet set)
334     {
335         _idldeps.add(set);
336     }
337
338     final public NestedElementWithFile
339     createIdldep()
340     {
341         NestedElementWithFile idldep = new NestedElementWithFile();
342         _idldep.add(idldep);
343         return idldep;
344     }
345
346     final public void
347     addIncludedirs(org.apache.tools.ant.types.DirSet set)
348     {
349         _includedirs.add(set);
350     }
351
352     final public NestedElementWithFile
353     createIncludedir()
354     {
355         NestedElementWithFile dir = new NestedElementWithFile();
356         _includedir.add(dir);
357         return dir;
358     }
359
360     final public void
361     setPrefix(String JavaDoc p)
362     {
363         _prefix = p;
364     }
365
366     final public void
367     setCompileto(java.io.File JavaDoc dir)
368     {
369         _compileto = dir;
370     }
371
372     final public void
373     setArchivename(String JavaDoc name)
374     {
375         _archive = name;
376     }
377
378     final public void
379     setBuildto(java.io.File JavaDoc dir)
380     {
381         _buildto = dir;
382     }
383
384     final public void
385     setClasspathRef(org.apache.tools.ant.types.Reference cp)
386     {
387         buildjar().setClasspathRef(cp);
388     }
389
390     final public void
391     setClasspathref(org.apache.tools.ant.types.Reference cp)
392     {
393         buildjar().setClasspathref(cp);
394     }
395
396     final public void
397     setBootclasspathRef(org.apache.tools.ant.types.Reference cp)
398     {
399         buildjar().setBootclasspathRef(cp);
400     }
401
402     final public void
403     setBootclasspathref(org.apache.tools.ant.types.Reference cp)
404     {
405         buildjar().setBootclasspathref(cp);
406     }
407
408     final public org.apache.tools.ant.types.Path
409     createClasspath()
410     {
411         return buildjar().createClasspath();
412     }
413
414     final public org.apache.tools.ant.types.Path
415     createBootclasspath()
416     {
417         return buildjar().createBootclasspath();
418     }
419
420     //
421
// org.apache.tools.ant.Task
422
//
423

424     final public void
425     execute()
426     throws org.apache.tools.ant.BuildException
427     {
428         //
429
validateAttributes();
430
431         // create temp directory
432
java.io.File JavaDoc tmpdir = FileHelper.createTempDir();
433
434         // compile IDL file
435
if (!idlfileUpToDate()) {
436             log("[execute] OMG IDL file is not uptodate: "+_idlfile.getName());
437
438             java.util.ArrayList JavaDoc vdirs = new java.util.ArrayList JavaDoc();
439
440             // add dir sets
441
org.apache.tools.ant.types.DirSet[] sets = null;
442             sets = (org.apache.tools.ant.types.DirSet[])_includedirs.toArray(new org.apache.tools.ant.types.DirSet[0]);
443
444             for (int i=0;i<sets.length;i++) {
445                 org.apache.tools.ant.DirectoryScanner scanner = sets[i].getDirectoryScanner(super.project);
446                 String JavaDoc[] dirnames = scanner.getIncludedDirectories();
447
448                 for (int j=0;j<dirnames.length;j++) {
449                     java.io.File JavaDoc dir = new java.io.File JavaDoc(scanner.getBasedir(), dirnames[j]);
450                     if (!dir.exists()) {
451                         String JavaDoc msg = "Included directory does not exists: "+dir.getPath();
452                         log(msg, org.apache.tools.ant.Project.MSG_WARN);
453                     }
454                     else if (!dir.isDirectory()) {
455                         String JavaDoc msg = "Included directory is not a directory: "+dir.getPath();
456                         log(msg, org.apache.tools.ant.Project.MSG_WARN);
457                     }
458                     else {
459                         vdirs.add(dir);
460                     }
461                 }
462             }
463
464             // add dir names
465
// check validity of these dirs
466
NestedElementWithFile[] dirs = (NestedElementWithFile[])_includedir.toArray(new NestedElementWithFile[0]);
467             for (int i=0;i<dirs.length;i++) {
468                 java.io.File JavaDoc dir = dirs[i].getFile();
469                 if (!dir.exists()) {
470                     String JavaDoc msg = "Included directory does not exists: "+dir.getPath();
471                     log(msg, org.apache.tools.ant.Project.MSG_WARN);
472                 }
473                 else if (!dir.isDirectory()) {
474                     String JavaDoc msg = "Included directory is not a directory: "+dir.getPath();
475                     log(msg, org.apache.tools.ant.Project.MSG_WARN);
476                 }
477                 else {
478                     vdirs.add(dir);
479                 }
480             }
481
482             java.io.File JavaDoc[] idirs = (java.io.File JavaDoc[])vdirs.toArray(new java.io.File JavaDoc[0]);
483             compileIDL(_idlfile, tmpdir, idirs, _prefix);
484         }
485
486         // build jar
487
if (!jarUpToDate(tmpdir)) {
488             // use BuildJarTask for that
489
log("[execute] jar is not uptodate");
490             buildjar().setSrcdir(tmpdir);
491             buildjar().setCompileto(_compileto);
492             buildjar().setArchivename(_archive);
493             buildjar().setBuildto(_buildto);
494             buildjar().execute();
495         }
496
497         // move files
498
FileHelper.moveFiles(tmpdir.listFiles(), _generateto);
499
500         // delete dir
501
FileHelper.deleteDir(tmpdir);
502     }
503
504     //
505
// abstract operations
506
//
507

508     abstract protected void
509     compileIDL(java.io.File JavaDoc idlfile, java.io.File JavaDoc compileto,
510                java.io.File JavaDoc[] includedirs, String JavaDoc prefix);
511 }
512
Popular Tags