KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27 import java.util.jar.Attributes JavaDoc;
28 import java.util.jar.JarFile JavaDoc;
29 import java.util.zip.CRC32 JavaDoc;
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.types.FileSet;
35
36 /**
37  * Create an update tracking file automatically.
38  * @author Michal Zlamal
39  */

40 public class MakeListOfNBM extends Task {
41     File JavaDoc outputFile = null;
42     String JavaDoc moduleName = null;
43     boolean pok = true;
44     FileSet fs = null;
45     
46     /** Sets the directory used to create the NBM list file */
47     public void setOutputfiledir(File JavaDoc s) {
48         outputFile = s;
49         log("Setting outputfile to " + s, Project.MSG_DEBUG);
50     }
51
52     public FileSet createFileSet() {
53         return (fs = new FileSet());
54     }
55     
56     /** Sets the module file */
57     public void setModule(String JavaDoc s) {
58         moduleName = s;
59         log("Setting moduleName to " + s, Project.MSG_DEBUG);
60     }
61
62     public void setTargetName(String JavaDoc t) {
63         pok = false;
64         log("<"+this.getTaskName()+"> attribute targetname has been DEPRECATED");
65     }
66
67     public void execute () throws BuildException {
68         if (!pok) throw new BuildException("Use the fileset to specify the content of the NBM");
69         if ( outputFile == null ) throw new BuildException( "You have to specify output directoty" );
70         if ( moduleName == null ) throw new BuildException( "You have to specify the main module's file" );
71         if ( fs == null ) throw new BuildException( "You have to specify the fileset of files included in this module" );
72
73         log ("Generating information for Auto Update...");
74         
75         UpdateTracking track = new UpdateTracking( outputFile.getAbsolutePath() );
76         Attributes JavaDoc attr;
77         JarFile JavaDoc jar = null;
78         File JavaDoc module = new File JavaDoc( outputFile, moduleName );
79         try {
80             jar = new JarFile JavaDoc(module);
81             attr = jar.getManifest().getMainAttributes();
82         } catch (IOException JavaDoc ex) {
83             throw new BuildException("Can't get manifest attributes for module jar file "+module.getAbsolutePath(), ex, getLocation());
84         } finally {
85             try {
86                 if (jar != null) jar.close();
87             } catch( IOException JavaDoc ex1 ) {
88                 String JavaDoc exmsg = ex1.getMessage();
89                 if (exmsg == null) exmsg = "Unknown error";
90                 log("Caught I/O Exception (msg:\""+exmsg+"\") when trying to close jar file "+module.getAbsolutePath(),Project.MSG_WARN);
91             }
92         }
93         
94         String JavaDoc codename = attr.getValue("OpenIDE-Module"); //NOI18N
95
if (codename == null) {
96             throw new BuildException("Manifest in jar file "+module.getAbsolutePath()+" does not contain OpenIDE-Module", getLocation());
97         }
98         
99         String JavaDoc versionSpecNum = attr.getValue("OpenIDE-Module-Specification-Version"); //NOI18N
100
if (versionSpecNum == null) {
101             log("Manifest in jar file "+module.getAbsolutePath()+" does not contain tag OpenIDE-Module-Specification-Version");
102             return;
103         }
104         
105         UpdateTracking.Version version = track.addNewModuleVersion( codename, versionSpecNum );
106
107         fs.createInclude().setName("config" + File.separator + "Modules" + File.separator + track.getTrackingFileName()); //NOI18N
108

109         // get directory scanner for "default" files
110
DirectoryScanner ds = fs.getDirectoryScanner( this.getProject() );
111         ds.scan();
112         
113         // check if we need also localized and branded files
114
String JavaDoc lmnl = this.getProject().getProperty("locmakenbm.locales"); // NOI18N
115
String JavaDoc lmnb = this.getProject().getProperty("locmakenbm.brands"); // NOI18N
116

117         if ((!(lmnl == null)) && (!(lmnl.trim().equals("")))) { // NOI18N
118
// property locmakenbm.locales is set, let's update the included fileset for locales
119
// defined in that property
120

121             java.util.StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc( lmnl, ", ") ; //NOI18N
122
int cntTok = tokenizer.countTokens();
123             String JavaDoc[] lmnLocales = new String JavaDoc[cntTok];
124             for (int j=0; j < cntTok; j++) {
125                 String JavaDoc s = tokenizer.nextToken();
126                 lmnLocales[j] = s;
127                 log(" lmnLocales[j] == "+lmnLocales[j], Project.MSG_DEBUG); // NOI18N
128
}
129
130             // handle brandings
131
String JavaDoc[] lmnBrands = null;
132             if ((!(lmnb == null)) && (!(lmnb.trim().equals("")))) { // NOI18N
133
tokenizer = new StringTokenizer JavaDoc( lmnb, ", ") ; //NOI18N
134
cntTok = tokenizer.countTokens();
135                 lmnBrands = new String JavaDoc[cntTok];
136                 for (int j=0; j < cntTok; j++) {
137                     String JavaDoc s = tokenizer.nextToken();
138                     lmnBrands[j] = s;
139                     log(" lmnBrands[j] == "+lmnBrands[j], Project.MSG_DEBUG); // NOI18N
140
}
141             }
142
143             // update fileset for localized/branded files
144

145             String JavaDoc[] englishFiles = ds.getIncludedFiles();
146             int sepPos, extPos;
147             String JavaDoc dirName, fname, filename, fext, newinc, ei_codename;
148             String JavaDoc moduleJar = null;
149             boolean skipLocaleDir = false;
150             for (int k=0; k < englishFiles.length; k++) {
151                 // skip records for already localized/branded files
152
if ((englishFiles[k].lastIndexOf("/locale/") >= 0) || // NOI18N
153
(englishFiles[k].lastIndexOf(File.separator+"locale"+File.separator) >= 0)) { // NOI18N
154
skipLocaleDir=true;
155                 } else {
156                     skipLocaleDir=false;
157                 }
158                 log("Examining file " + englishFiles[k], Project.MSG_DEBUG);
159                 sepPos = englishFiles[k].lastIndexOf(File.separator);
160                 if (sepPos < 0) {
161                     dirName = ""; //NOI18N
162
filename = englishFiles[k];
163                 } else {
164                     dirName = englishFiles[k].substring(0,sepPos);
165                     filename = englishFiles[k].substring(sepPos+File.separator.length());
166                 }
167                 extPos = filename.lastIndexOf('.'); //NOI18N
168
if (extPos < 0) {
169                     fname = filename;
170                     fext = ""; //NOI18N
171
} else {
172                     fname = filename.substring(0, extPos);
173                     fext = filename.substring(extPos);
174                 }
175                 for (int j=0; j < lmnLocales.length; j++) {
176                     // localized files
177
if (skipLocaleDir) {
178                         newinc = dirName + File.separator + fname + "_"+lmnLocales[j]+"*" + fext; //NOI18N
179
} else {
180                         newinc = dirName + File.separator + "locale" + File.separator + fname + "_"+lmnLocales[j]+"*" + fext; //NOI18N
181
}
182                     log(" adding include mask \""+newinc+"\"", Project.MSG_DEBUG);
183                     fs.setIncludes( newinc );
184                     // localized & branded files
185
if (!(lmnBrands == null)) {
186                         for (int i=0; i < lmnBrands.length; i++) {
187                             if (skipLocaleDir) {
188                                 newinc = dirName + File.separator + fname + "_"+lmnBrands[i]+"_"+lmnLocales[j]+"*" + fext; //NOI18N
189
} else {
190                                 newinc = dirName + File.separator + "locale" + File.separator + fname + "_"+lmnBrands[i]+"_"+lmnLocales[j]+"*" + fext; //NOI18N
191
}
192                             log(" adding include mask \""+newinc+"\"", Project.MSG_DEBUG);
193                             fs.setIncludes( newinc );
194                         }
195                     }
196                 }
197             }
198             // update directory scanner
199
ds = fs.getDirectoryScanner(this.getProject());
200             ds.scan();
201         }
202         
203         String JavaDoc include[] = ds.getIncludedFiles();
204         log("Including files " + Arrays.toString(include), Project.MSG_VERBOSE);
205         for( int j=0; j < include.length; j++ ){
206             try {
207                 File JavaDoc inFile = new File JavaDoc( ds.getBasedir(), include[j] );
208                 FileInputStream JavaDoc inFileStream = new FileInputStream JavaDoc( inFile );
209                 byte array[] = new byte[ (int) inFile.length() ];
210                 CRC32 JavaDoc crc = new CRC32 JavaDoc();
211                 inFileStream.read( array );
212                 inFileStream.close();
213                 crc.update( array );
214                 String JavaDoc abs = inFile.getAbsolutePath();
215                 String JavaDoc prefix = ds.getBasedir().getAbsolutePath() + File.separatorChar;
216                 if (! abs.startsWith(prefix)) throw new IllegalStateException JavaDoc(abs);
217                 version.addFileWithCrc(abs.substring(prefix.length()).replace(File.separatorChar, '/'), Long.toString( crc.getValue() ) );
218             } catch (IOException JavaDoc ex) {
219                 log( ex.toString() );
220             }
221         }
222         track.write();
223         String JavaDoc moduleName = this.getProject().getProperty("module.name"); //NOI18N
224
if (moduleName == null) {
225             // external module?
226
return;
227         }
228         String JavaDoc[] inc = new String JavaDoc[include.length+2];
229         for (int i=0; i < include.length; i++)
230             inc[i] = include[i];
231         inc[include.length] = "config" + File.separator + "Modules" + File.separator + track.getTrackingFileName(); // NOI18N
232
inc[include.length+1] = UpdateTracking.TRACKING_DIRECTORY + File.separator + track.getTrackingFileName();
233         // Hack. We assume outputFile is a direct subdir of the dest dir.
234
ModuleTracking moduleTracking = new ModuleTracking(outputFile.getParentFile().getAbsolutePath());
235         String JavaDoc nbmfilename = this.getProject().getProperty("nbm"); // NOI18N
236
String JavaDoc nbmhomepage = this.getProject().getProperty("nbm.homepage"); // NOI18N
237
String JavaDoc nbmneedsrestart = this.getProject().getProperty("nbm.needs.restart"); // NOI18N
238
String JavaDoc nbmreleasedate = this.getProject().getProperty("nbm.release.date"); // NOI18N
239
String JavaDoc nbmmoduleauthor = this.getProject().getProperty("nbm.module.author"); // NOI18N
240
String JavaDoc nbmisglobal = this.getProject().getProperty("nbm.is.global"); // NOI18N
241
String JavaDoc nbmtargetcluster = this.getProject().getProperty("nbm.target.cluster"); // NOI18N
242
// ...and again here.
243
moduleTracking.putModule(moduleName, codename, outputFile.getName(), nbmfilename, nbmhomepage, nbmneedsrestart, nbmreleasedate, nbmmoduleauthor, nbmisglobal, nbmtargetcluster, inc);
244         moduleTracking.write();
245     }
246 }
247
Popular Tags