1 20 21 package org.jdesktop.jdic.packager.impl; 22 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.net.URL ; 26 import java.net.MalformedURLException ; 27 import java.util.Iterator ; 28 29 40 public final class Jnlp2Package { 41 45 private Jnlp2Package() { 46 } 47 48 54 private static boolean getBoolProperty(String propertyName) { 55 String 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 ( 67 "The value of property " + propertyName 68 + " can only be either true or false."); 69 } 70 } 71 72 return retValue; 73 } 74 75 86 private static JnlpPackageInfo parseArguments(String [] args) 87 throws IOException { 88 String version = null; 89 String release = null; 90 boolean showLicense = false; 91 String licenseDirName = null; 92 String bannerJpgFileName = null; 93 String panelJpgFileName = null; 94 boolean shortcutEnabled = false; 95 boolean associationEnabled = false; 96 boolean systemCacheEnabled = false; 97 JnlpPackageInfo pkgInfo = null; 98 99 if (args.length < 1) { 102 throw new IllegalArgumentException ( 103 "Please specify the jnlp file path."); 104 } 105 106 pkgInfo = new JnlpPackageInfo(); 107 URL jnlp = null; 108 if (args[0].startsWith("http://")) { 109 try { 110 jnlp = new URL (args[0]); 111 } catch (MalformedURLException muE) { 112 throw new IOException ("Invalid url argument: " + 113 muE.getMessage()); 114 } 115 116 pkgInfo.parseRemoteJnlpInfo(jnlp); 117 } else { 118 File jnlpFile = new File (args[0]); 119 120 if (!jnlpFile.exists() || !jnlpFile.isFile()) { 121 throw new IOException ("invalid local jnlp file: " + 122 jnlpFile.getPath()); 123 } 124 125 String 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 137 pkgInfo.setLocalizedInformation(); 138 139 140 checkPackageNameArgument(pkgInfo); 141 142 146 checkOutputDirNameArgument(pkgInfo); 147 148 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 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 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 177 bannerJpgFileName = System.getProperty( 178 JnlpConstants.PROPERTY_NAME_BANNERJPGFILE); 179 bannerJpgFileName = getValidFileArgument( 180 bannerJpgFileName, "Banner Jpeg File", true); 181 pkgInfo.setBannerJpgFilePath(bannerJpgFileName); 182 183 184 panelJpgFileName = System.getProperty( 185 JnlpConstants.PROPERTY_NAME_PANELJPGFILE); 186 panelJpgFileName = getValidFileArgument( 187 panelJpgFileName, "Panel Jpeg File", true); 188 pkgInfo.setPanelJpgFilePath(panelJpgFileName); 189 190 191 checkMSSDKPathArgument(pkgInfo); 192 193 194 shortcutEnabled = getBoolProperty( 195 JnlpConstants.PROPERTY_NAME_ENABLESHORTCUT); 196 pkgInfo.setShortcutEnabled(shortcutEnabled); 197 198 199 associationEnabled = getBoolProperty( 200 JnlpConstants.PROPERTY_NAME_ENABLEASSOCIATION); 201 pkgInfo.setAssociationEnabled(associationEnabled); 202 203 204 systemCacheEnabled = getBoolProperty( 205 JnlpConstants.PROPERTY_NAME_ENABLESYSTEMCACHE); 206 pkgInfo.setSystemCacheEnabled(systemCacheEnabled); 207 208 return pkgInfo; 209 } 210 211 219 private static String getValidFileArgument( 220 String filePath, String argName, boolean isNullable) 221 throws IOException { 222 if (filePath == null) { 223 if (isNullable) { 225 return null; 227 } else { 228 throw new IllegalArgumentException (argName 230 + " could not be null."); 231 } 232 } else { 233 File theFile = new File (filePath); 235 theFile = theFile.getCanonicalFile(); 236 if (theFile.canRead()) { 237 return theFile.getPath(); 239 } else { 240 throw new IllegalArgumentException ( 241 "The given " 242 + argName 243 + " is not valid: " 244 + filePath); 245 } 246 } 247 } 248 249 254 private static void checkResourcePathArgument(JnlpPackageInfo pkgInfo) 255 throws IOException { 256 String resourceDirName = System.getProperty( 257 JnlpConstants.PROPERTY_NAME_RESOURCEDIR); 258 String jnlpFileName = pkgInfo.getJnlpFilePath(); 259 File jnlpFile = new File (jnlpFileName); 260 if (resourceDirName == null) { 261 String jnlpFileParentDir = jnlpFile.getParent(); 262 resourceDirName = getValidFileArgument( 263 jnlpFileParentDir, "resource dir", false); 264 } else { 265 resourceDirName = getValidFileArgument( 266 resourceDirName, "resource dir", false); 267 } 268 File resourceDirFile = new File (resourceDirName); 269 Iterator refIter = pkgInfo.getJnlpRefFilePaths(); 271 while (refIter.hasNext()) { 272 String oneRefFilePath = (String ) refIter.next(); 273 if (oneRefFilePath != null) { 274 File oneAbsFilePath = new File (resourceDirFile, oneRefFilePath); 275 276 if (!FileOperUtility.isFileReadable(oneAbsFilePath)) { 278 throw new IllegalArgumentException ( 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 294 private static void checkPackageNameArgument(JnlpPackageInfo pkgInfo) 295 throws IOException { 296 String packageName = System.getProperty( 297 JnlpConstants.PROPERTY_NAME_PACKAGENAME); 298 if (packageName == null) { 299 File jnlpFile = new File (pkgInfo.getJnlpFilePath()); 301 packageName = FileOperUtility.getFileNameWithoutExt(jnlpFile); 302 if (packageName == null) { 303 throw new IllegalArgumentException ( 304 "The given jnlp file name is not a valid package name."); 305 } 306 } else { 307 File packageFile = new File (packageName); 310 if (packageFile.isDirectory()) { 311 throw new IllegalArgumentException ( 312 "The given jnlp file name is not a valid package name."); 313 } 314 } 315 pkgInfo.setPackageName(packageName); 316 } 317 318 323 private static void checkMSSDKPathArgument(JnlpPackageInfo pkgInfo) 324 throws IOException { 325 String osName = System.getProperty("os.name").toLowerCase(); 326 String msSDKPath = null; 327 String 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 351 private static void checkOutputDirNameArgument(JnlpPackageInfo pkgInfo) 352 throws IOException { 353 String 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 if (!outputDirName.endsWith(File.separator)) { 362 outputDirName += File.separator; 363 } 364 File outputDirFile = new File (outputDirName); 365 if (outputDirFile.isDirectory()) { 367 if (!outputDirFile.canWrite()) { 368 throw new IllegalArgumentException ( 369 "The given package path is not writable: " 370 + outputDirFile); 371 } else { 372 pkgInfo.setOutputDirPath(outputDirName); 373 } 374 } else { 375 throw new IllegalArgumentException ( 377 "The given output dir is not valid: " 378 + outputDirFile); 379 } 380 } 381 382 388 public static void generatePackage(String [] args) 389 throws IOException { 390 boolean isHttpJnlp = false; 393 String tempDirName = null; 394 395 JnlpPackageInfo jnlpPkgInfo = null; 397 jnlpPkgInfo = parseArguments(args); 398 399 PackageGenerator pkgGenerator = PackageGeneratorFactory.newInstance(); 401 pkgGenerator.generatePackage(jnlpPkgInfo); 402 } 403 } 404 | Popular Tags |