KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > MakeLNBM


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.nbbuild;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.OutputStreamWriter JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31 import java.io.Reader JavaDoc;
32 import java.net.URI JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Collections JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Properties JavaDoc;
40 import java.util.StringTokenizer JavaDoc;
41 import java.util.Vector JavaDoc;
42 import java.util.jar.Attributes JavaDoc;
43 import java.util.jar.JarFile JavaDoc;
44 import java.util.jar.Manifest JavaDoc;
45 import java.util.zip.ZipEntry JavaDoc;
46 import java.util.zip.ZipFile JavaDoc;
47 import org.apache.tools.ant.BuildException;
48 import org.apache.tools.ant.Project;
49 import org.apache.tools.ant.taskdefs.Jar;
50 import org.apache.tools.ant.taskdefs.MatchingTask;
51 import org.apache.tools.ant.taskdefs.SignJar;
52 import org.apache.tools.ant.types.FileSet;
53
54 /** Makes a localized <code>.nbm</code> (<b>N</b>et<b>B</b>eans <b>M</b>odule) file.
55  * This version is temporary, intended to be used only until
56  * the functionality added to this version since rev 1.29
57  * of MakeNBM.java can be added into MakeNBM.java
58  *
59  * @author Jerry Huth (email: jerry@solidstep.com)
60  */

61 public class MakeLNBM extends MatchingTask {
62
63     /** The same syntax may be used for either <samp>&lt;license&gt;</samp> or
64      * <samp>&lt;description&gt;</samp> subelements.
65      * <p>By setting the property <code>makenbm.nocdata</code> to <code>true</code>,
66      * you can avoid using XML <code>CDATA</code> (for compatibility with older versions
67      * of Auto Update which could not handle it).
68      */

69     public class Blurb {
70         /** You may embed a <samp>&lt;file&gt;</samp> element inside the blurb.
71          * If there is text on either side of it, that will be separated
72          * with a line of dashes automatically.
73          * But use nested <samp>&lt;text&gt;</samp> for this purpose.
74          */

75     public class FileInsert {
76             /** File location. */
77         public void setLocation (File JavaDoc file) throws BuildException {
78                 log("Including contents of " + file, Project.MSG_VERBOSE);
79         long lmod = file.lastModified ();
80         if (lmod > mostRecentInput) mostRecentInput = lmod;
81         addSeparator ();
82         try {
83             InputStream JavaDoc is = new FileInputStream JavaDoc (file);
84             try {
85             Reader JavaDoc r = new InputStreamReader JavaDoc (is, "UTF-8"); //NOI18N
86
char[] buf = new char[4096];
87             int len;
88             while ((len = r.read (buf)) != -1)
89                 text.append (buf, 0, len);
90             } finally {
91             is.close ();
92             }
93         } catch (IOException JavaDoc ioe) {
94                     throw new BuildException ("Exception reading blurb from " + file, ioe, getLocation ());
95         }
96         }
97     }
98     private StringBuffer JavaDoc text = new StringBuffer JavaDoc ();
99     private String JavaDoc name = null;
100         /** There may be freeform text inside the element. Prefer to use nested elements. */
101     public void addText (String JavaDoc t) {
102         addSeparator ();
103         // Strips indentation. Needed because of common style:
104
// <description>
105
// Some text here.
106
// And another line.
107
// </description>
108
t = getProject().replaceProperties(t.trim());
109         int min = Integer.MAX_VALUE;
110         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc (t, "\n"); //NOI18N
111
boolean first = true;
112         while (tok.hasMoreTokens ()) {
113         String JavaDoc line = tok.nextToken ();
114         if (first) {
115             first = false;
116         } else {
117             int i;
118             for (i = 0;
119              i < line.length () &&
120                  Character.isWhitespace (line.charAt (i));
121              i++)
122             ;
123             if (i < min) min = i;
124         }
125         }
126         if (min == 0) {
127         text.append (t);
128         } else {
129         tok = new StringTokenizer JavaDoc (t, "\n"); //NOI18N
130
first = true;
131         while (tok.hasMoreTokens ()) {
132             String JavaDoc line = tok.nextToken ();
133             if (first) {
134             first = false;
135             } else {
136             text.append ('\n'); //NOI18N
137
line = line.substring (min);
138             }
139             text.append (line);
140         }
141         }
142     }
143         /** Contents of a file to include. */
144     public FileInsert createFile () {
145         return new FileInsert ();
146     }
147         /** Text to include literally. */
148         public class Text {
149             public void addText(String JavaDoc t) {
150                 Blurb.this.addText(t);
151             }
152         }
153         // At least on Ant 1.3, mixed content does not work: all the text is added
154
// first, then all the file inserts. Need to use subelements to be sure.
155
/** Include nested literal text. */
156         public Text createText() {
157             return new Text();
158         }
159     private void addSeparator () {
160         if (text.length () > 0) {
161         // some sort of separator
162
if (text.charAt (text.length () - 1) != '\n') //NOI18N
163
text.append ('\n'); //NOI18N
164
text.append ("-----------------------------------------------------\n"); //NOI18N
165
}
166     }
167     public String JavaDoc getText () {
168             String JavaDoc nocdata = getProject().getProperty("makenbm.nocdata"); //NOI18N
169
if (nocdata != null && Project.toBoolean(nocdata)) {
170                 return xmlEscape(text.toString());
171             } else {
172                 return "<![CDATA[" + text.toString () + "]]>"; //NOI18N
173
}
174     }
175         /** You can either set a name for the blurb, or using the <code>file</code> attribute does this.
176          * The name is mandatory for licenses, as this identifies the license in
177          * an update description.
178          */

179     public void setName (String JavaDoc name) {
180         this.name = name;
181     }
182     public String JavaDoc getName () {
183         return name;
184     }
185         /** Include a file (and set the license name according to its basename). */
186     public void setFile (File JavaDoc file) {
187         // This actually adds the text and so on:
188
new FileInsert ().setLocation (file);
189         // Default for the name too, as a convenience.
190
if (name == null) name = file.getName ();
191     }
192     }
193
194     public class ExternalPackage {
195     String JavaDoc name = null;
196     String JavaDoc targetName = null;
197     String JavaDoc startUrl = null;
198     String JavaDoc description = null;
199
200     public void setName(String JavaDoc n) {
201         this.name = n;
202     }
203
204     public void setTargetName(String JavaDoc t) {
205         this.targetName = t;
206     }
207
208     public void setStartURL(String JavaDoc u) {
209         this.startUrl = u;
210     }
211     
212     public void setDescription(String JavaDoc d) {
213         this.description = d;
214     }
215
216     }
217
218     // Similar to org.openide.xml.XMLUtil methods.
219
private static String JavaDoc xmlEscape(String JavaDoc s) {
220         int max = s.length();
221         StringBuffer JavaDoc s2 = new StringBuffer JavaDoc((int)(max * 1.1 + 1));
222         for (int i = 0; i < max; i++) {
223             char c = s.charAt(i);
224             switch (c) {
225                 case '<': //NOI18N
226
s2.append("&lt;"); //NOI18N
227
break;
228                 case '>': //NOI18N
229
s2.append("&gt;"); //NOI18N
230
break;
231                 case '&': //NOI18N
232
s2.append("&amp;"); //NOI18N
233
break;
234                 case '"': //NOI18N
235
s2.append("&quot;"); //NOI18N
236
break;
237                 default:
238                     s2.append(c);
239                     break;
240             }
241         }
242         return s2.toString();
243     }
244
245     /** <samp>&lt;signature&gt;</samp> subelement for signing the NBM. */
246     public /*static*/ class Signature {
247     public File JavaDoc keystore;
248     public String JavaDoc storepass, alias;
249         /** Path to the keystore (private key). */
250     public void setKeystore (File JavaDoc f) {
251         keystore = f;
252     }
253         /** Password for the keystore.
254          * If a question mark (<samp>?</samp>), the NBM will not be signed
255          * and a warning will be printed.
256          */

257     public void setStorepass (String JavaDoc s) {
258         storepass = s;
259     }
260         /** Alias for the private key. */
261     public void setAlias (String JavaDoc s) {
262         alias = s;
263     }
264     }
265
266     private File JavaDoc file = null;
267     private File JavaDoc topdir = null;
268     private File JavaDoc manifest = null;
269     /** see #13850 for explanation */
270     private File JavaDoc module = null;
271     private String JavaDoc homepage = null;
272     private String JavaDoc distribution = null;
273     private String JavaDoc needsrestart = null;
274     private Blurb license = null;
275     private Blurb description = null;
276     private Blurb notification = null;
277     private Signature signature = null;
278     private long mostRecentInput = 0L;
279     private boolean isStandardInclude = true;
280     private Vector JavaDoc<ExternalPackage> externalPackages = null;
281     private boolean manOrModReq = true ;
282     private boolean manOrModReqSet = false ;
283     private String JavaDoc langCode = null ;
284     private String JavaDoc brandingCode = null ;
285     private String JavaDoc modInfo = null ;
286     private File JavaDoc locBundle = null ; // Localizing Bundle
287

288     /** Include netbeans directory - default is true */
289     public void setIsStandardInclude(boolean isStandardInclude) {
290     this.isStandardInclude = isStandardInclude;
291     }
292
293     /** Name of resulting NBM file. */
294     public void setFile (File JavaDoc file) {
295     this.file = file;
296     }
297     /** Top directory.
298      * Expected to contain a subdirectory <samp>netbeans/</samp> with the
299      * desired contents of the NBM.
300      * Will create <samp>Info/info.xml</samp> with metadata.
301      */

302     public void setTopdir (File JavaDoc topdir) {
303     this.topdir = topdir;
304     }
305     /** Module manifest needed for versioning.
306      * @deprecated Use {@link #setModule} instead.
307      */

308     @Deprecated JavaDoc
309     public void setManifest (File JavaDoc manifest) {
310     this.manifest = manifest;
311     long lmod = manifest.lastModified ();
312     if (lmod > mostRecentInput) mostRecentInput = lmod;
313         log(getLocation() + "The 'manifest' attr on <makenbm> is deprecated, please use 'module' instead", Project.MSG_WARN);
314     }
315     /** Module JAR needed for generating the info file.
316      * Information may be gotten either from its manifest,
317      * or if it declares OpenIDE-Module-Localizing-Bundle in its
318      * manifest, from that bundle.
319      * The base locale variant, if any, is also checked if necessary
320      * for the named bundle.
321      * Currently no other locale variants of the module are examined;
322      * the information is available but there is no published specification
323      * of what the resulting variant NBMs (or variant information within
324      * the NBM) should look like.
325      */

326     public void setModule(File JavaDoc module) {
327         this.module = module;
328         // mostRecentInput updated below...
329
}
330     /** URL to a home page describing the module. */
331     public void setHomepage (String JavaDoc homepage) {
332     this.homepage = homepage;
333     }
334     /** Does module need IDE restart to be installed? */
335     public void setNeedsrestart (String JavaDoc needsrestart) {
336         this.needsrestart = needsrestart;
337     }
338     /** URL where this NBM file is expected to be downloadable from. */
339     public void setDistribution (String JavaDoc distribution) throws BuildException {
340         if (distribution.startsWith("http://")) { //NOI18N
341
this.distribution = distribution;
342         } else if (!(distribution.equals(""))) {
343             // workaround for typical bug in build script
344
this.distribution = "http://" + distribution; //NOI18N
345
} else {
346             throw new BuildException("Distribution URL is empty, check build.xml file", getLocation());
347         }
348         // check the URL
349
try {
350             URI JavaDoc uri = java.net.URI.create(this.distribution);
351         } catch (IllegalArgumentException JavaDoc ile) {
352             throw new BuildException("Distribution URL \"" + this.distribution + "\" is not a valid URI", ile, getLocation());
353         }
354     }
355     public Blurb createLicense () {
356     return (license = new Blurb ());
357     }
358     public Blurb createNotification () {
359     return (notification = new Blurb ());
360     }
361     public Blurb createDescription () {
362         log(getLocation() + "The <description> subelement in <makenbm> is deprecated except for emergency patches, please ensure your module has an OpenIDE-Module-Long-Description instead", Project.MSG_WARN);
363     return (description = new Blurb ());
364     }
365     public Signature createSignature () {
366     return (signature = new Signature ());
367     }
368
369     public ExternalPackage createExternalPackage(){
370     ExternalPackage externalPackage = new ExternalPackage ();
371     if (externalPackages == null)
372         externalPackages = new Vector JavaDoc<ExternalPackage>();
373     externalPackages.add( externalPackage );
374     return externalPackage;
375     }
376
377     public void execute () throws BuildException {
378     if (file == null) {
379         throw new BuildException("must set file for makenbm", getLocation());
380         }
381         if (manifest == null && module == null && reqManOrMod()) {
382             throw new BuildException("must set module for makenbm", getLocation());
383         }
384         if (manifest != null && module != null) {
385             throw new BuildException("cannot set both manifest and module for makenbm", getLocation());
386         }
387     // Will create a file Info/info.xml to be stored alongside netbeans/ contents.
388
File JavaDoc infodir = new File JavaDoc (topdir, "Info"); //NOI18N
389
infodir.mkdirs ();
390     File JavaDoc infofile = new File JavaDoc (infodir, "info.xml"); //NOI18N
391
if (infofile.exists ()) {
392             infofile.delete();
393         }
394         Attributes JavaDoc attr = null;
395         if (module != null) {
396             // The normal case; read attributes from its manifest and maybe bundle.
397
long mMod = module.lastModified();
398             if (mostRecentInput < mMod) mostRecentInput = mMod;
399             try {
400                 JarFile JavaDoc modulejar = new JarFile JavaDoc(module);
401                 try {
402                     attr = modulejar.getManifest().getMainAttributes();
403                     String JavaDoc bundlename = attr.getValue("OpenIDE-Module-Localizing-Bundle"); //NOI18N
404
if (bundlename != null) {
405                         Properties JavaDoc p = new Properties JavaDoc();
406                         ZipEntry JavaDoc bundleentry = modulejar.getEntry(bundlename);
407                         if (bundleentry != null) {
408                             InputStream JavaDoc is = modulejar.getInputStream(bundleentry);
409                             try {
410                                 p.load(is);
411                             } finally {
412                                 is.close();
413                             }
414                         } else {
415                             // Not found in main JAR, check locale variant JAR.
416
File JavaDoc variant = new File JavaDoc(new File JavaDoc(module.getParentFile(), "locale"), module.getName()); //NOI18N
417
if (!variant.isFile()) throw new BuildException(bundlename + " not found in " + module, getLocation());
418                             long vmMod = variant.lastModified();
419                             if (mostRecentInput < vmMod) mostRecentInput = vmMod;
420                             ZipFile JavaDoc variantjar = new ZipFile JavaDoc(variant);
421                             try {
422                                 bundleentry = variantjar.getEntry(bundlename);
423                                 if (bundleentry == null) throw new BuildException(bundlename + " not found in " + module + " nor in " + variant, getLocation());
424                                 InputStream JavaDoc is = variantjar.getInputStream(bundleentry);
425                                 try {
426                                     p.load(is);
427                                 } finally {
428                                     is.close();
429                                 }
430                             } finally {
431                                 variantjar.close();
432                             }
433                         }
434                         // Now pick up attributes from the bundle.
435
Iterator JavaDoc it = p.entrySet().iterator();
436                         while (it.hasNext()) {
437                             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
438                             String JavaDoc name = (String JavaDoc)entry.getKey();
439                             if (! name.startsWith("OpenIDE-Module-")) continue; //NOI18N
440
attr.putValue(name, (String JavaDoc)entry.getValue());
441                         }
442                     } // else all loc attrs in main manifest, OK
443
} finally {
444                     modulejar.close();
445                 }
446             } catch (IOException JavaDoc ioe) {
447                 throw new BuildException("exception while reading " + module, ioe, getLocation());
448             }
449         } // else we will read attr later if info file is out of date
450
boolean skipInfo = false;
451     if (infofile.exists ()) {
452         // Check for up-to-date w.r.t. manifest and maybe license file.
453
long iMod = infofile.lastModified ();
454         if (mostRecentInput < iMod)
455         skipInfo = true;
456     }
457     if (! skipInfo) {
458         log ("Creating NBM info file " + infofile);
459             if (manifest != null) {
460                 // Read module manifest for main attributes.
461
try {
462                     InputStream JavaDoc manifestStream = new FileInputStream JavaDoc (manifest);
463                     try {
464                         attr = new Manifest JavaDoc (manifestStream).getMainAttributes ();
465                     } finally {
466                         manifestStream.close ();
467                     }
468                 } catch (IOException JavaDoc e) {
469                     throw new BuildException("exception when reading manifest " + manifest, e, getLocation());
470                 }
471             } // else we read attr before
472
try {
473         OutputStream JavaDoc infoStream = new FileOutputStream JavaDoc (infofile);
474         try {
475                     PrintWriter JavaDoc ps = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(infoStream, "UTF-8"));
476             // Begin writing XML.
477
ps.println ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //NOI18N
478
ps.println("<!DOCTYPE module PUBLIC \"-//NetBeans//DTD Autoupdate Module Info 2.4//EN\" \"http://www.netbeans.org/dtds/autoupdate-info-2_4.dtd\">"); //NOI18N
479
if( attr != null) {
480               String JavaDoc codenamebase = attr.getValue ("OpenIDE-Module"); //NOI18N
481
if (codenamebase == null)
482             throw new BuildException("invalid manifest, does not contain OpenIDE-Module", getLocation());
483               // Strip major release number if any.
484
codenamebase = getCodenameBase( codenamebase) ;
485               ps.println ("<module codenamebase=\"" + xmlEscape(codenamebase) + "\""); //NOI18N
486
}
487             else {
488               ps.print( "<module "); //NOI18N
489
if( modInfo != null && !modInfo.trim().equals( "")) {
490             String JavaDoc codenamebase = getCodenameBase( modInfo) ;
491             ps.println( "codenamebase=\"" + xmlEscape(codenamebase) + "\""); //NOI18N
492
}
493               else {
494             ps.println( "") ; //NOI18N
495
}
496             }
497             if (homepage != null)
498                         ps.println (" homepage=\"" + xmlEscape(homepage) + "\""); //NOI18N
499
if (distribution != null) {
500                         ps.println (" distribution=\"" + xmlEscape(distribution) + "\""); //NOI18N
501
} else {
502                         throw new BuildException("NBM distribution URL is not set", getLocation());
503                     }
504             // Here we only write a name for the license.
505
if (license != null) {
506             String JavaDoc name = license.getName ();
507             if (name == null)
508                 throw new BuildException("Every license must have a name or file attribute", getLocation());
509                         ps.println (" license=\"" + xmlEscape(name) + "\""); //NOI18N
510
}
511             ps.println (" downloadsize=\"0\""); //NOI18N
512
if (needsrestart != null)
513                         ps.println (" needsrestart=\"" + xmlEscape(needsrestart) + "\""); //NOI18N
514
ps.println (">"); //NOI18N
515
if (description != null) {
516             ps.print (" <description>"); //NOI18N
517
ps.print (description.getText ());
518             ps.println ("</description>"); //NOI18N
519
}
520
521             // Write manifest attributes.
522
if( attr != null) {
523                 ps.print (" <manifest "); //NOI18N
524
boolean firstline = true;
525                 List JavaDoc<String JavaDoc> attrNames = new ArrayList JavaDoc<String JavaDoc>(attr.size());
526             Iterator JavaDoc<Object JavaDoc> it = attr.keySet().iterator();
527             while (it.hasNext()) {
528                 attrNames.add(((Attributes.Name JavaDoc)it.next()).toString());
529             }
530             Collections.sort(attrNames);
531                         for (String JavaDoc name: attrNames) {
532                 // Ignore irrelevant attributes (cf. www/www/dtds/autoupdate-catalog-2_0.dtd
533
// and www/www/dtds/autoupdate-info-1_0.dtd):
534
if (! name.startsWith("OpenIDE-Module")) continue; //NOI18N
535
if (name.equals("OpenIDE-Module-Localizing-Bundle")) continue; //NOI18N
536
if (name.equals("OpenIDE-Module-Install")) continue; //NOI18N
537
if (name.equals("OpenIDE-Module-Layer")) continue; //NOI18N
538
if (name.equals("OpenIDE-Module-Description")) continue; //NOI18N
539
if (name.equals("OpenIDE-Module-Package-Dependency-Message")) continue; //NOI18N
540
if (name.equals("OpenIDE-Module-Public-Packages")) continue; //NOI18N
541
if (firstline)
542                     firstline = false;
543                 else
544                   ps.print (" "); //NOI18N
545
ps.println(name + "=\"" + xmlEscape(attr.getValue(name)) + "\""); //NOI18N
546
}
547             ps.println (" />"); //NOI18N
548
}
549             else if( modInfo != null && !modInfo.trim().equals( "")) { //NOI18N
550
String JavaDoc specver, majorver ;
551
552               // Write the l10n tag and lang/branding codes. //
553
ps.println(" <l10n "); //NOI18N
554
if( langCode != null && !langCode.trim().equals( "")) { //NOI18N
555
ps.println( " langcode=\"" + xmlEscape(langCode) + "\"") ; //NOI18N
556
}
557               if( brandingCode != null && !brandingCode.trim().equals( "")) { //NOI18N
558
ps.println( " brandingcode=\"" + xmlEscape(brandingCode) + "\"") ; //NOI18N
559
}
560
561               // Write the major version if possible. //
562
majorver = getMajorVer( modInfo) ;
563               if( majorver != null && !majorver.trim().equals( "")) { //NOI18N
564
ps.println( " module_major_version=\"" + xmlEscape(majorver) + "\"") ; //NOI18N
565
}
566
567               // Write the spec version if possible. //
568
specver = getSpecVer( modInfo) ;
569               if( specver != null && !specver.trim().equals( "")) { //NOI18N
570
ps.println( " module_spec_version=\"" + xmlEscape(specver) + "\"") ; //NOI18N
571
}
572
573               // Read localizing bundle and write relevant attr's. //
574
if( locBundle != null) {
575             writeLocBundleAttrs( ps) ;
576               }
577
578               ps.println( " />") ; //NOI18N
579
}
580
581             // Maybe write out license text.
582
if (license != null) {
583                         ps.print (" <license name=\"" + xmlEscape(license.getName ()) + "\">"); //NOI18N
584
ps.print (license.getText ());
585             ps.println ("</license>"); //NOI18N
586
}
587                     if (notification != null) {
588                         ps.print(" <module_notification>"); //NOI18N
589
ps.print(notification.getText());
590                         ps.println("</module_notification>"); //NOI18N
591
}
592             if (externalPackages != null) {
593             Enumeration JavaDoc<ExternalPackage> exp = externalPackages.elements();
594             while (exp.hasMoreElements()) {
595                 ExternalPackage externalPackage = exp.nextElement();
596                 if (externalPackage.name == null ||
597                 externalPackage.targetName == null ||
598                 externalPackage.startUrl == null)
599                 throw new BuildException("Must define name, targetname, starturl for external package");
600                 ps.print(" <external_package "); //NOI18N
601
ps.print("name=\""+xmlEscape(externalPackage.name)+"\" "); //NOI18N
602
ps.print("target_name=\""+xmlEscape(externalPackage.targetName)+"\" "); //NOI18N
603
ps.print("start_url=\""+xmlEscape(externalPackage.startUrl)+"\""); //NOI18N
604
if (externalPackage.description != null)
605                 ps.print(" description=\""+xmlEscape(externalPackage.description)+"\""); //NOI18N
606
ps.println("/>"); //NOI18N
607
}
608             }
609             ps.println ("</module>"); //NOI18N
610
ps.flush();
611         } finally {
612             infoStream.close ();
613         }
614         } catch (IOException JavaDoc e) {
615         throw new BuildException("exception when creating Info/info.xml", e, getLocation());
616         }
617     }
618
619     // JAR it all up together.
620
long jarModified = file.lastModified (); // may be 0
621
//log ("Ensuring existence of NBM file " + file);
622
Jar jar = (Jar) getProject().createTask("jar"); //NOI18N
623
jar.setDestFile(file);
624     //jar.setBasedir (topdir.getAbsolutePath ());
625
jar.setCompress(true);
626     //jar.createInclude ().setName ("netbeans/"); //NOI18N
627
//jar.createInclude ().setName ("Info/info.xml"); //NOI18N
628
jar.addFileset (getFileSet());
629     jar.setLocation(getLocation());
630     jar.init ();
631     jar.execute ();
632     // Maybe sign it.
633
if (signature != null && file.lastModified () != jarModified) {
634         if (signature.keystore == null)
635         throw new BuildException ("must define keystore attribute on <signature/>");
636         if (signature.storepass == null)
637         throw new BuildException ("must define storepass attribute on <signature/>");
638         if (signature.alias == null)
639         throw new BuildException ("must define alias attribute on <signature/>");
640             if (signature.storepass.equals ("?") || !signature.keystore.exists()) {
641                 log ("Not signing NBM file " + file + "; no stored-key password provided or keystore ("
642              + signature.keystore.toString() + ") doesn't exist", Project.MSG_WARN);
643             } else {
644                 log ("Signing NBM file " + file);
645                 SignJar signjar = (SignJar) getProject().createTask("signjar"); //NOI18N
646
//I have to use Reflection API, because there was changed API in ANT1.5
647
try {
648                     try {
649                         Class JavaDoc[] paramsT = {String JavaDoc.class};
650                         Object JavaDoc[] paramsV1 = {signature.keystore.getAbsolutePath()};
651                         Object JavaDoc[] paramsV2 = {file.getAbsolutePath()};
652                         signjar.getClass().getDeclaredMethod( "setKeystore", paramsT ).invoke( signjar, paramsV1 ); //NOI18N
653
signjar.getClass().getDeclaredMethod( "setJar", paramsT ).invoke( signjar, paramsV2 ); //NOI18N
654
} catch (NoSuchMethodException JavaDoc ex1) {
655                         //Probably ANT 1.5
656
try {
657                             Class JavaDoc[] paramsT = {File JavaDoc.class};
658                             Object JavaDoc[] paramsV1 = {signature.keystore};
659                             Object JavaDoc[] paramsV2 = {file};
660                             signjar.getClass().getDeclaredMethod( "setKeystore", paramsT ).invoke( signjar, paramsV1 ); //NOI18N
661
signjar.getClass().getDeclaredMethod( "setJar", paramsT ).invoke( signjar, paramsV2 ); //NOI18N
662
} catch (NoSuchMethodException JavaDoc ex2) {
663                 //Probably ANT1.5.3
664
try {
665                 Class JavaDoc[] paramsT1 = {File JavaDoc.class};
666                 Class JavaDoc[] paramsT2 = {String JavaDoc.class};
667                 Object JavaDoc[] paramsV1 = {signature.keystore.getAbsolutePath()};
668                 Object JavaDoc[] paramsV2 = {file};
669                 signjar.getClass().getDeclaredMethod( "setKeystore", paramsT2 ).invoke( signjar, paramsV1 ); //NOI18N
670
signjar.getClass().getDeclaredMethod( "setJar", paramsT1 ).invoke( signjar, paramsV2 ); //NOI18N
671
} catch (NoSuchMethodException JavaDoc ex3) {
672                                 throw new BuildException("Unknown Ant version, only Ant 1.6.5 is currently supported.");
673                             }
674                         }
675                     }
676                 } catch (IllegalAccessException JavaDoc ex4) {
677                     throw new BuildException(ex4);
678                 } catch (java.lang.reflect.InvocationTargetException JavaDoc ex5) {
679                     throw new BuildException(ex5);
680                 }
681                 signjar.setStorepass (signature.storepass);
682                 signjar.setAlias (signature.alias);
683                 signjar.setLocation(getLocation());
684                 signjar.init ();
685                 signjar.execute ();
686             }
687     }
688     }
689    
690     // Reflection access from MakeListOfNBM:
691

692     public FileSet getFileSet() {
693         FileSet fs = fileset; //makes in apperance to excludes and includes files defined in XML
694
fs.setDir (topdir);
695
696     if (isStandardInclude) {
697       fs.createInclude ().setName ("netbeans/"); //NOI18N
698
fs.createExclude ().setName ("netbeans/update_tracking/*.xml"); //NOI18N
699
}
700
701     fs.createInclude ().setName ("Info/info.xml"); //NOI18N
702
return fs;
703     }
704
705     public Attributes JavaDoc getAttributes() throws IOException JavaDoc {
706         if (manifest != null) {
707             InputStream JavaDoc is = new FileInputStream JavaDoc(manifest);
708             try {
709                 return new Manifest JavaDoc(is).getMainAttributes();
710             } finally {
711                 is.close();
712             }
713         } else if (module != null) {
714             JarFile JavaDoc jar = new JarFile JavaDoc(module);
715             try {
716                 return jar.getManifest().getMainAttributes();
717             } finally {
718                 jar.close();
719             }
720         } else {
721             throw new IOException JavaDoc(getLocation() + "must give either 'manifest' or 'module' on <makenbm>");
722         }
723     }
724
725   protected String JavaDoc getCodenameBase( String JavaDoc openide_module) {
726     String JavaDoc ret = openide_module ;
727     int idx = ret.indexOf ('/'); //NOI18N
728
if (idx != -1) {
729       ret = ret.substring (0, idx);
730     }
731     return( ret) ;
732   }
733
734   protected String JavaDoc getSpecVer( String JavaDoc mod_info) {
735     String JavaDoc ret = null ;
736     int first_idx, second_idx ;
737
738     // If there are 2 slashes. //
739
first_idx = mod_info.indexOf( '/') ; //NOI18N
740
if( first_idx != -1) {
741       second_idx = mod_info.indexOf( '/', first_idx+1) ; //NOI18N
742
if( second_idx != -1) {
743
744     // Return the string after the second slash. //
745
ret = mod_info.substring( second_idx+1, mod_info.length()) ;
746       }
747     }
748
749     // Return null rather than an empty string. //
750
if( ret != null && ret.trim().equals( "")) { //NOI18N
751
ret = null ;
752     }
753     return( ret) ;
754   }
755
756   protected String JavaDoc getMajorVer( String JavaDoc mod_info) {
757     String JavaDoc ret = null ;
758     int first_idx, second_idx ;
759
760     // If there are 2 slashes. //
761
first_idx = mod_info.indexOf( '/') ; //NOI18N
762
if( first_idx != -1) {
763       second_idx = mod_info.indexOf( '/', first_idx+1) ; //NOI18N
764
if( second_idx != -1) {
765
766     // Return the string between the slashes. //
767
ret = mod_info.substring( first_idx+1, second_idx) ;
768       }
769
770       // Else return the string after the first slash. //
771
else {
772     ret = mod_info.substring( first_idx+1, mod_info.length()) ;
773       }
774     }
775
776     // Return null rather than an empty string. //
777
if( ret != null && ret.trim().equals( "")) { //NOI18N
778
ret = null ;
779     }
780     return( ret) ;
781   }
782
783   /** For l10n NBM's, this is the localizing bundle file
784    * that we'll look in to get module name, description, etc.
785    */

786   public void setLocBundle( File JavaDoc f) {
787     locBundle = f ;
788   }
789
790   /** See reqManOrMod() */
791   public void setManOrModReq( boolean b) {
792     manOrModReq = b ;
793     manOrModReqSet = true ;
794   }
795
796   /** If the manifest and module aren't required, use this
797    * to set the module codename, major version and spec version.
798    */

799   public void setModInfo( String JavaDoc s) {
800     modInfo = s ;
801   }
802
803   /** Set the language code for localized NBM's. */
804   public void setLangCode( String JavaDoc s) {
805     langCode = s ;
806   }
807
808   /** Set the branding code for branded NBM's. */
809   public void setBrandingCode( String JavaDoc s) {
810     brandingCode = s ;
811   }
812
813   /** Returns true if either a manifest or a module must be specified.
814    * This is true unless either the global property
815    * makenbm.manOrModReq is false, or the manOrModReq attribute of
816    * this task is false. The attribute, if set, has priority over the
817    * global property.
818    */

819   public boolean reqManOrMod() {
820     String JavaDoc s = null ;
821     boolean req = true ;
822
823     if( manOrModReqSet) {
824       req = manOrModReq ;
825     }
826     else {
827       s = getProject().getProperty("makenbm.manOrModReq"); //NOI18N
828
if( s != null && !s.equals( "")) { //NOI18N
829
req = getProject().toBoolean(s);
830       }
831     }
832
833     return( req) ;
834   }
835
836   protected void writeLocBundleAttrs( PrintWriter JavaDoc ps) {
837     FileInputStream JavaDoc fis ;
838     Properties JavaDoc p = new Properties JavaDoc() ;
839     String JavaDoc s ;
840     boolean hadone = false ;
841
842     try {
843       fis = new FileInputStream JavaDoc( locBundle) ;
844       p.load( fis);
845       fis.close();
846     }
847     catch( Exception JavaDoc e) {
848       System.out.println( "ERROR: " + e.getMessage()) ;
849       e.printStackTrace() ;
850       throw new BuildException() ;
851     }
852
853     s = p.getProperty( "OpenIDE-Module-Name") ; //NOI18N
854
if( writeProp( "OpenIDE-Module-Name", s, ps)) { //NOI18N
855
hadone = true ;
856     }
857
858     s = p.getProperty( "OpenIDE-Module-Long-Description") ; //NOI18N
859
if( writeProp( "OpenIDE-Module-Long-Description", s, ps)) { //NOI18N
860
hadone = true ;
861     }
862
863     if( !hadone) {
864       log( "WARNING: Localizing bundle had neither property: " + locBundle) ;
865     }
866   }
867
868   protected boolean writeProp( String JavaDoc name,
869                    String JavaDoc val,
870                    PrintWriter JavaDoc ps) {
871     boolean ret = false ;
872     if( val != null) {
873       ps.println( name + "=\"" + xmlEscape(val) +"\"") ; //NOI18N
874
ret = true ;
875     }
876     return( ret) ;
877   }
878
879 }
880
Popular Tags