KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 package org.jdesktop.jdic.packager.impl;
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 /**
30  * This class generates an platform dependent installable package with the given
31  * arguments.
32  * <p>
33  * On Windows, the generated package is in msi format.
34  * <p>
35  * On Linux, the generated package is in rpm format.
36  * <p>
37  * On Solaris, the generated package is in pkg format.
38  *
39  */

40 public final class Jnlp2Package {
41     /**
42      * Jnlp2Packager creator.
43      *
44      */

45     private Jnlp2Package() {
46     }
47
48     /**
49      * Checks boolean properties and reports errors for illegal
50      * values(not true or false).
51      * @param propertyName The given property name.
52      * @return True if the property contains valid value.
53      */

54     private static boolean getBoolProperty(String JavaDoc propertyName) {
55         String JavaDoc propertyValue = System.getProperty(propertyName);
56         boolean retValue = false;
57
58         if (propertyValue == null) {
59             retValue = false;
60         } else {
61             if (propertyValue.equalsIgnoreCase("true")) {
62                 retValue = true;
63             } else if (propertyValue.equalsIgnoreCase("false")) {
64                 retValue = false;
65             } else {
66                 throw new IllegalArgumentException JavaDoc(
67                     "The value of property " + propertyName
68                     + " can only be either true or false.");
69             }
70         }
71
72         return retValue;
73     }
74
75     /**
76      * Checks all the input arguments in the command line, and returns
77      * a JnlpPackageInfo object containning the package related information.
78      * <p>
79      * The JnlpPackageInfo object will be used to generate a installable
80      * package.
81      *
82      * @param args Arguments to be parsed.
83      * @return The parsed JnlpPackageInfo.
84      * @throws IOException If failed to parse the arguments.
85      */

86     private static JnlpPackageInfo parseArguments(String JavaDoc[] args)
87             throws IOException JavaDoc {
88         String JavaDoc version = null;
89         String JavaDoc release = null;
90         boolean showLicense = false;
91         String JavaDoc licenseDirName = null;
92         String JavaDoc bannerJpgFileName = null;
93         String JavaDoc panelJpgFileName = null;
94         boolean shortcutEnabled = false;
95         boolean associationEnabled = false;
96         boolean systemCacheEnabled = false;
97         JnlpPackageInfo pkgInfo = null;
98         
99         /////////////////////////////////////////////////////////////
100
// Check the jnlp file path argument.
101
if (args.length < 1) {
102             throw new IllegalArgumentException JavaDoc(
103                         "Please specify the jnlp file path.");
104         }
105
106         pkgInfo = new JnlpPackageInfo();
107         URL JavaDoc jnlp = null;
108         if (args[0].startsWith("http://")) {
109             try {
110                 jnlp = new URL JavaDoc(args[0]);
111             } catch (MalformedURLException JavaDoc muE) {
112                 throw new IOException JavaDoc("Invalid url argument: " +
113                         muE.getMessage());
114             }
115
116             pkgInfo.parseRemoteJnlpInfo(jnlp);
117         } else {
118             File JavaDoc jnlpFile = new File JavaDoc(args[0]);
119             
120             if (!jnlpFile.exists() || !jnlpFile.isFile()) {
121                 throw new IOException JavaDoc("invalid local jnlp file: " +
122                         jnlpFile.getPath());
123             }
124
125             String JavaDoc resourceDir = System.getProperty(
126                     "JnlpConstants.PROPERTY_NAME_RESOURCEDIR");
127
128             if (resourceDir == null) {
129                 resourceDir = jnlpFile.getParent();
130             }
131             
132             pkgInfo.setResourcePath(getValidFileArgument(resourceDir,
133                     "resource path", false));
134             pkgInfo.parseLocalJnlpInfo(jnlpFile.toURL());
135         }
136         /* Set Localized information as titles, descriptions, etc. */
137         pkgInfo.setLocalizedInformation();
138         
139         /* Check PackageName property. */
140         checkPackageNameArgument(pkgInfo);
141         
142         /*
143          * Check OutputDir property.
144          * If the OutputDir is not set, set default outputdir as current dir.
145          */

146         checkOutputDirNameArgument(pkgInfo);
147         
148         /* Check Version property. */
149         version = System.getProperty(JnlpConstants.PROPERTY_NAME_VERSIONNO);
150         if (version == null) {
151             version = JnlpConstants.DEFAULT_VERSION_NUM;
152         }
153         pkgInfo.setVersion(version);
154         
155         /* Check Release property. */
156         release = System.getProperty(JnlpConstants.PROPERTY_NAME_RELEASENO);
157         if (release == null) {
158             release = JnlpConstants.DEFAULT_RELEASE_NUM;
159         }
160         pkgInfo.setRelease(release);
161         
162         /*
163          * Check License property. If it's set, check if it points to a valid
164          * file.
165          */

166         licenseDirName = System.getProperty(
167                                 JnlpConstants.PROPERTY_NAME_LICENSEDIR);
168         licenseDirName = getValidFileArgument(
169                             licenseDirName, "license directory", true);
170         if (licenseDirName != null) {
171             showLicense = true;
172         }
173         pkgInfo.setShowLicense(showLicense);
174         pkgInfo.setLicenseDirPath(licenseDirName);
175
176         /* Check BannerJpgFile property. */
177         bannerJpgFileName = System.getProperty(
178                             JnlpConstants.PROPERTY_NAME_BANNERJPGFILE);
179         bannerJpgFileName = getValidFileArgument(
180                               bannerJpgFileName, "Banner Jpeg File", true);
181         pkgInfo.setBannerJpgFilePath(bannerJpgFileName);
182         
183         /* Check PanelJpgFile property. */
184         panelJpgFileName = System.getProperty(
185                         JnlpConstants.PROPERTY_NAME_PANELJPGFILE);
186         panelJpgFileName = getValidFileArgument(
187                             panelJpgFileName, "Panel Jpeg File", true);
188         pkgInfo.setPanelJpgFilePath(panelJpgFileName);
189         
190         /* Check MSSDKPath & RawMSIFile property. */
191         checkMSSDKPathArgument(pkgInfo);
192
193         /* Check EnableShortcut property. */
194         shortcutEnabled = getBoolProperty(
195                         JnlpConstants.PROPERTY_NAME_ENABLESHORTCUT);
196         pkgInfo.setShortcutEnabled(shortcutEnabled);
197
198         /* Check EnableAssociation property. */
199         associationEnabled = getBoolProperty(
200                         JnlpConstants.PROPERTY_NAME_ENABLEASSOCIATION);
201         pkgInfo.setAssociationEnabled(associationEnabled);
202
203         /* Check EnableSystemCache property. */
204         systemCacheEnabled = getBoolProperty(
205                         JnlpConstants.PROPERTY_NAME_ENABLESYSTEMCACHE);
206         pkgInfo.setSystemCacheEnabled(systemCacheEnabled);
207         
208         return pkgInfo;
209     }
210     
211     /**
212      * Checks if the file argument is valid.
213      * @param filePath The given file path argument.
214      * @param argName The name of the argument.
215      * @param isNullable true if the argument could be set as null.
216      * @return The valid file argument.
217      * @throws IOException If fails to get the valid file.
218      */

219     private static String JavaDoc getValidFileArgument(
220                     String JavaDoc filePath, String JavaDoc argName, boolean isNullable)
221                     throws IOException JavaDoc {
222         if (filePath == null) {
223             //filePaht argument is null
224
if (isNullable) {
225                 //null argument acceptable
226
return null;
227             } else {
228                 //null argument not acceptable
229
throw new IllegalArgumentException JavaDoc(argName
230                                                     + " could not be null.");
231             }
232         } else {
233             //filePath argument is not null
234
File JavaDoc theFile = new File JavaDoc(filePath);
235             theFile = theFile.getCanonicalFile();
236             if (theFile.canRead()) {
237                 //The file is readable
238
return theFile.getPath();
239             } else {
240                 throw new IllegalArgumentException JavaDoc(
241                              "The given "
242                              + argName
243                              + " is not valid: "
244                              + filePath);
245             }
246         }
247     }
248
249     /**
250      * Checks if the resource files referenced in the jnlp file in valid.
251      * @param pkgInfo The jnlpPackageInfo instance.
252      * @throws IOException If failed during the checking process.
253      */

254     private static void checkResourcePathArgument(JnlpPackageInfo pkgInfo)
255                 throws IOException JavaDoc {
256         String JavaDoc resourceDirName = System.getProperty(
257                                 JnlpConstants.PROPERTY_NAME_RESOURCEDIR);
258         String JavaDoc jnlpFileName = pkgInfo.getJnlpFilePath();
259         File JavaDoc jnlpFile = new File JavaDoc(jnlpFileName);
260         if (resourceDirName == null) {
261             String JavaDoc jnlpFileParentDir = jnlpFile.getParent();
262             resourceDirName = getValidFileArgument(
263                                 jnlpFileParentDir, "resource dir", false);
264         } else {
265             resourceDirName = getValidFileArgument(
266                                 resourceDirName, "resource dir", false);
267         }
268         File JavaDoc resourceDirFile = new File JavaDoc(resourceDirName);
269         // Check if all of the absolute referenced file paths exist.
270
Iterator JavaDoc refIter = pkgInfo.getJnlpRefFilePaths();
271         while (refIter.hasNext()) {
272             String JavaDoc oneRefFilePath = (String JavaDoc) refIter.next();
273             if (oneRefFilePath != null) {
274                 File JavaDoc oneAbsFilePath = new File JavaDoc(resourceDirFile, oneRefFilePath);
275
276                 // Check if the file is a valid file and is readable.
277
if (!FileOperUtility.isFileReadable(oneAbsFilePath)) {
278                     throw new IllegalArgumentException JavaDoc(
279                                 "Cann't read resource file: "
280                                 + oneRefFilePath
281                                 + " from resource path: "
282                                 + resourceDirFile.toString());
283                 }
284             }
285         }
286         pkgInfo.setResourcePath(resourceDirName);
287     }
288
289     /**
290      * Get the package name property from command line option.
291      * @param pkgInfo The JnlpPackageInfo instance.
292      * @throws IOException If failed to get the packagename property.
293      */

294     private static void checkPackageNameArgument(JnlpPackageInfo pkgInfo)
295         throws IOException JavaDoc {
296         String JavaDoc packageName = System.getProperty(
297                             JnlpConstants.PROPERTY_NAME_PACKAGENAME);
298         if (packageName == null) {
299             // No package name is set, then use the jnlp file name by default.
300
File JavaDoc jnlpFile = new File JavaDoc(pkgInfo.getJnlpFilePath());
301             packageName = FileOperUtility.getFileNameWithoutExt(jnlpFile);
302             if (packageName == null) {
303                 throw new IllegalArgumentException JavaDoc(
304                     "The given jnlp file name is not a valid package name.");
305             }
306         } else {
307             // User specified the packagename argument
308
// Check if the user input is valid
309
File JavaDoc packageFile = new File JavaDoc(packageName);
310             if (packageFile.isDirectory()) {
311                 throw new IllegalArgumentException JavaDoc(
312                     "The given jnlp file name is not a valid package name.");
313             }
314         }
315         pkgInfo.setPackageName(packageName);
316     }
317
318     /**
319      * Gets the raw MSI template file path from command line option.
320      * @param pkgInfo The JnlpPakcageInfo instance.
321      * @throws IOException If failed to get the raw MSI template path.
322      */

323     private static void checkMSSDKPathArgument(JnlpPackageInfo pkgInfo)
324             throws IOException JavaDoc {
325         String JavaDoc osName = System.getProperty("os.name").toLowerCase();
326         String JavaDoc msSDKPath = null;
327         String JavaDoc rawMsiFilePath = null;
328         if (osName.startsWith(JnlpConstants.OS_WINDOWS)) {
329             msSDKPath = System.getProperty(
330                             JnlpConstants.PROPERTY_NAME_MS_SDK_PATH);
331             msSDKPath = getValidFileArgument(
332                             msSDKPath, "MS SDK Path", false);
333             msSDKPath = msSDKPath
334                         + (msSDKPath.endsWith(File.separator)
335                            ? "" : File.separator);
336             rawMsiFilePath =
337                 msSDKPath
338                 + JnlpConstants.HIERACHY_TO_RAW_MSI;
339             rawMsiFilePath = getValidFileArgument(
340                             rawMsiFilePath, "raw MSI file path", false);
341         }
342         pkgInfo.setMSSDKDirPath(msSDKPath);
343         pkgInfo.setRawMsiFilePath(rawMsiFilePath);
344     }
345
346     /**
347      * Checks if the output directory is valid.
348      * @param pkgInfo The JnlpPackageInfo instance.
349      * @throws IOException If failed during the checking process.
350      */

351     private static void checkOutputDirNameArgument(JnlpPackageInfo pkgInfo)
352                     throws IOException JavaDoc {
353         String JavaDoc outputDirName = System.getProperty(
354                                 JnlpConstants.PROPERTY_NAME_OUTPUTDIR);
355         if (outputDirName == null) {
356             outputDirName = "." + File.separator;
357         }
358         outputDirName =
359             getValidFileArgument(outputDirName, "output dir", false);
360         //Make sure output dir ends with file separator.
361
if (!outputDirName.endsWith(File.separator)) {
362             outputDirName += File.separator;
363         }
364         File JavaDoc outputDirFile = new File JavaDoc(outputDirName);
365         // Check if the directory already exists.
366
if (outputDirFile.isDirectory()) {
367             if (!outputDirFile.canWrite()) {
368                 throw new IllegalArgumentException JavaDoc(
369                     "The given package path is not writable: "
370                     + outputDirFile);
371             } else {
372                 pkgInfo.setOutputDirPath(outputDirName);
373             }
374         } else {
375             //The given outputDirFile exists and is not a directory!
376
throw new IllegalArgumentException JavaDoc(
377                 "The given output dir is not valid: "
378                 + outputDirFile);
379         }
380     }
381     
382     /**
383      * Invoke the concrete platform relevant implementation to generate the
384      * package.
385      * @param args The given arguments.
386      * @throws IOException If failed to generate the dest package.
387      */

388     public static void generatePackage(String JavaDoc[] args)
389         throws IOException JavaDoc {
390         // Check if the given jnlp file path is an URL. If it it, retrieve the
391
// file locally.
392
boolean isHttpJnlp = false;
393         String JavaDoc tempDirName = null;
394         
395         // Parse and check the command line arguments.
396
JnlpPackageInfo jnlpPkgInfo = null;
397         jnlpPkgInfo = parseArguments(args);
398
399         // Generate the installable package with the given package info.
400
PackageGenerator pkgGenerator = PackageGeneratorFactory.newInstance();
401         pkgGenerator.generatePackage(jnlpPkgInfo);
402     }
403 }
404
Popular Tags