KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > packager > impl > RpmPackageGenerator


1 /*
2  * Copyright 2003 Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * - The names of its contributors may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * Contributor(s):
29  *
30  */

31
32 package org.jdesktop.jdic.packager.impl;
33
34 import java.io.IOException JavaDoc;
35 import java.io.File JavaDoc;
36 import java.io.PrintWriter JavaDoc;
37 import java.io.BufferedReader JavaDoc;
38 import java.io.BufferedWriter JavaDoc;
39 import java.io.FileWriter JavaDoc;
40 import java.io.InputStreamReader JavaDoc;
41
42
43 /**
44  * Concrete implementation of interface PackageGenerator for rpm packages
45  * generating on Linux.
46  * <pre>
47  * The steps to create installable packages are briefly as below:
48  * 1. Set macros _topdir to the dir whitch general user has write permissions.
49  * 2. Create spec file in _topdir/SPECS to define the process of building rpm package.
50  * 3. launch rpmbuild -bb <spec> to actually build the rpm package, the target package
51  * would be put at _topdir/RPMS/i386
52  * </pre>
53  */

54 public class RpmPackageGenerator implements PackageGenerator {
55     private String JavaDoc homePath;
56     private String JavaDoc topPath;
57     private String JavaDoc buildPath;
58     private String JavaDoc rpmsPath;
59     private String JavaDoc specsPath;
60  
61     public RpmPackageGenerator() {
62         homePath = System.getProperty("user.home");
63         topPath = homePath + "/.rpm/";
64         buildPath = homePath + "/.rpm/BUILD/";
65         rpmsPath = homePath + "/.rpm/RPMS/";
66         specsPath = homePath + "/.rpm/SPECS/";
67     }
68     
69     /*
70      * Generates the package according to the generated .spec file.
71      */

72     public void generatePackage(JnlpPackageInfo pkgInfo) throws IOException JavaDoc {
73         File JavaDoc spec = null;
74         BufferedReader JavaDoc in = null;
75         String JavaDoc[] cmdArray = new String JavaDoc[3];
76
77         try {
78             setTopdir(pkgInfo);
79             spec = createSpec(pkgInfo);
80             String JavaDoc rpmVersion = getRpmVersion();
81             if (rpmVersion != null && rpmVersion.compareToIgnoreCase("4.0") >= 0)
82                 cmdArray[0] = "rpmbuild";
83             else
84                 cmdArray[0] = "rpm";
85             cmdArray[1] = "-bb";
86             cmdArray[2] = spec.getAbsolutePath();
87             Process JavaDoc p = Runtime.getRuntime().exec(cmdArray);
88             in = new BufferedReader JavaDoc(
89                                     new InputStreamReader JavaDoc(p.getErrorStream()));
90             String JavaDoc line;
91             while((line = in.readLine()) != null) {
92                 System.out.println(line);
93             }
94
95         } finally {
96             if(in != null)
97                 in.close();
98
99             /* delete the .rpmmacros file we created in the home directory */
100             File JavaDoc macros = new File JavaDoc(homePath + "/.rpmmacros");
101             if (macros.exists()) {
102                 if (!macros.delete())
103                     System.out.println("Cannot delete ~/.rpmmacros");
104             }
105         }
106                
107     }
108
109     private String JavaDoc getRpmVersion() throws IOException JavaDoc {
110         String JavaDoc[] cmdArray = {"rpm", "--version"};
111         BufferedReader JavaDoc in = null;
112         try {
113             Process JavaDoc p = Runtime.getRuntime().exec(cmdArray);
114             in = new BufferedReader JavaDoc(
115                                     new InputStreamReader JavaDoc(p.getInputStream()));
116             String JavaDoc line = in.readLine();
117             if (line != null) {
118                 line = line.trim();
119                 return line.substring(line.lastIndexOf(" ")).trim();
120             }
121             else
122                 return null;
123         } finally {
124             if(in != null)
125                 in.close();
126         }
127     }
128     
129     private File JavaDoc createSpec(JnlpPackageInfo pkgInfo) throws IOException JavaDoc {
130         int i = 0;
131     
132     PrintWriter JavaDoc pw = null;
133         String JavaDoc appName = pkgInfo.getPackageName();
134         String JavaDoc dest = pkgInfo.getOutputDirPath();
135         
136     if (dest == null) {
137             dest = System.getProperty("user.dir");
138             File JavaDoc destDir = new File JavaDoc(dest);
139             if (!destDir.canWrite())
140                 throw new IOException JavaDoc("Cannot write to the current dir, please specify the PackagePath property");
141         }
142 /* String installationPath = pkgInfo.getUniqueTmpDirPath() + "/"; */
143         String JavaDoc installationPath = "/tmp/.jnlp/" + appName + "/";
144         String JavaDoc resourcePath = pkgInfo.getResourceDirPath() + "/";
145         String JavaDoc description = pkgInfo.getLocalizedJnlpInfo(JnlpConstants.LOCALES[0], JnlpConstants.JNLP_FIELD_DESCRIPTION);
146         String JavaDoc release = pkgInfo.getRelease();
147         String JavaDoc version = pkgInfo.getVersion();
148         String JavaDoc license = "NONE";
149         String JavaDoc licensePath = pkgInfo.getLicenseDirPath();
150         boolean hasLicense = false;
151         if (licensePath != null) {
152             hasLicense = true;
153             license = "License files are located at /usr/share/doc/packages/" + appName + "/License";
154         }
155         String JavaDoc group = "System Environment/Base";
156         boolean shortcut = pkgInfo.getShortcutEnabled();
157         boolean association = pkgInfo.getAssociationEnabled();
158         boolean result = false;
159     boolean systemCacheEnabled = pkgInfo.getSystemCacheEnabled();
160     
161         try {
162             /* create spec file in the SPECS directory of topdir */
163             File JavaDoc spec = new File JavaDoc(specsPath + appName + ".spec");
164             pw = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new FileWriter JavaDoc(spec)));
165
166             /* create the header of the spec file */
167             String JavaDoc input = new String JavaDoc();
168             input = "Summary: " + description + "\n";
169         for(i=1; i<10; i++) {
170             if((description = pkgInfo.getLocalizedJnlpInfo(JnlpConstants.LOCALES[i], JnlpConstants.JNLP_FIELD_DESCRIPTION)) != null) {
171             input += "Summary(" + JnlpConstants.LOCALES[i] + "): " + description + "\n";
172         }
173         }
174             input += "name: " + appName + "\n";
175             input += "Release: " + release + "\n";
176             input += "Version: " + version + "\n";
177             input += "License: " + license + "\n";
178             input += "Group: " + group + "\n";
179             input += "BuildRoot: " + buildPath + "\n";
180             input += "\n";
181             pw.println(input);
182
183             /* create description */
184         description = pkgInfo.getLocalizedJnlpInfo(JnlpConstants.LOCALES[0], JnlpConstants.JNLP_FIELD_DESCRIPTION);
185         input = "%description\n";
186             input += description + "\n";
187             input += "\n";
188         for(i=1; i<10; i++) {
189             if((description = pkgInfo.getLocalizedJnlpInfo(JnlpConstants.LOCALES[i], JnlpConstants.JNLP_FIELD_DESCRIPTION)) != null) {
190             input += "%description -l " + JnlpConstants.LOCALES[i] + "\n";
191             input += description + "\n";
192         }
193         }
194         pw.println(input);
195
196         /* copy files to install path in BuildRoot */
197         try {
198         FileOperUtility.copyLocalFile(pkgInfo.getResourceDirPath(), buildPath + installationPath);
199         if (hasLicense)
200             FileOperUtility.copyLocalFile(licensePath, buildPath + File.separator + "License");
201     } catch (IOException JavaDoc ioE) {
202             throw new IOException JavaDoc("Cannot copy resource files to BuildRoot: " + ioE.getMessage());
203     }
204     
205
206     /* create pre install script */
207     input = "%pre\n";
208     String JavaDoc[] checkScript;
209     checkScript = JnlpUtility.javawsCheckScript();
210     for(i=0; i<checkScript.length; i++) {
211         input += checkScript[i] + "\n";
212     }
213     input += "echo preinstall finished\n";
214     pw.println(input);
215
216             /* create post install script */
217             input = "%post\n";
218             input += "JNLP_ASSOCIATION=`grep '^[^#]*application/x-java-jnlp-file' /etc/mailcap`\n";
219             input += "JAVAWS_PATH=`echo $JNLP_ASSOCIATION | awk -F\\; '{print $2}' | awk '{print $1}'`\n";
220             input += "echo JAVAWS_PATH: $JAVAWS_PATH\n";
221             input += "$JAVAWS_PATH ";
222             if (systemCacheEnabled)
223                 input += "-system ";
224             input += "-silent -import ";
225             if (shortcut)
226                 input += "-shortcut ";
227             if (association)
228                 input += "-association ";
229             input += "-codebase "
230                 + "file://"
231                 + installationPath
232                 + " "
233                 + installationPath
234                 + "/"
235                 + pkgInfo.getJnlpFileName()
236                 + "\n";
237             input += "\n";
238             pw.println(input);
239
240             /* create pre uninstall script */
241             input = "%preun\n";
242
243             for(i=0; i<checkScript.length; i++) {
244                 input += checkScript[i] + "\n";
245             }
246
247             pw.println(input);
248
249             /* create postuninstall script */
250             input = "%postun\n";
251             input += "JNLP_ASSOCIATION=`grep '^[^#]*application/x-java-jnlp-file' /etc/mailcap`\n";
252             input += "JAVAWS_PATH=`echo $JNLP_ASSOCIATION | awk -F\\; '{print $2}' | awk '{print $1}'`\n";
253             input += "\n";
254             input += "$JAVAWS_PATH ";
255             if (systemCacheEnabled)
256                 input += "-system ";
257             input += "-silent -uninstall "
258                   + pkgInfo.getJnlpFileHref()
259                   + "\n";
260             input += "\n";
261             pw.println(input);
262
263             /* create clean script */
264             input = "%clean\n";
265             input += "mv " + "`find " + rpmsPath + " -name \"*.rpm\"` " + dest + "\n";
266             input += "cd " + topPath + "\n";
267             input += "cd ..\n";
268             input += "rm -rf " + topPath + "\n";
269             input += "\n";
270             pw.println(input);
271
272             /* create the files list */
273             input = "%files\n";
274             input += installationPath + "\n";
275             if (hasLicense)
276                 input += "%doc License\n";
277             pw.println(input);
278
279             pw.close();
280             return spec;
281         } finally {
282             if (pw != null)
283                 pw.close();
284         }
285     }
286     
287     private void setTopdir(JnlpPackageInfo pkgInfo) throws IOException JavaDoc {
288         PrintWriter JavaDoc pw = null;
289
290         try {
291             boolean result = false;
292
293             /* create topdir in users home directory */
294             File JavaDoc buildDir = new File JavaDoc(buildPath);
295             result = buildDir.mkdirs();
296             if(result == false) {
297                 result = buildDir.exists();
298                 if(result == false)
299                     throw new IOException JavaDoc("Cannot create BUILD dir in $HOME directory");
300             }
301             File JavaDoc installDir = new File JavaDoc(buildPath + pkgInfo.getUniqueTmpDirPath() + "/");
302             result = installDir.mkdirs();
303             if(result == false) {
304                 result = installDir.exists();
305                 if(result == false)
306                     throw new IOException JavaDoc("Cannot create install dir in BuildRoot");
307             }
308             File JavaDoc rpmsDir = new File JavaDoc(rpmsPath);
309             result = rpmsDir.mkdirs();
310             if(result == false) {
311                 result = rpmsDir.exists();
312                 if(result == false)
313                     throw new IOException JavaDoc("Cannot create RPMS dir in $HOME directory");
314             }
315             File JavaDoc specsDir = new File JavaDoc(specsPath);
316             result = specsDir.mkdirs();
317             if(result == false) {
318                 result = specsDir.exists();
319                 if(result == false)
320                     throw new IOException JavaDoc("Cannot create SPECS dir in $HOME directory");
321             }
322
323             /* modify the .rpmmacros to specify _topdir macros */
324             pw = new PrintWriter JavaDoc(
325                                  new BufferedWriter JavaDoc(new FileWriter JavaDoc(homePath + "/.rpmmacros")));
326             pw.println("%_topdir " + topPath);
327             pw.close();
328         } finally {
329             if(pw != null)
330                 pw.close();
331         }
332     }
333 }
334
Popular Tags