1 20 21 package org.jdesktop.jdic.packager.impl; 22 23 import java.io.File ; 24 import java.io.FileWriter ; 25 import java.io.BufferedWriter ; 26 import java.io.InputStreamReader ; 27 import java.io.BufferedReader ; 28 import java.io.IOException ; 29 import java.net.URL ; 30 import java.net.MalformedURLException ; 31 32 33 50 public class PkgPackageGenerator implements PackageGenerator { 51 private static final String FILE_NAME_PKGINFO = "pkginfo"; 55 private static final String FILE_NAME_COPYRIGHT = "copyright"; 56 private static final String FILE_NAME_CHECKINSTALL = "checkinstall"; 57 private static final String FILE_NAME_POSTINSTALL = "postinstall"; 58 private static final String FILE_NAME_POSTREMOVE = "postremove"; 59 private static final String FILE_NAME_PROTOTYPE = "prototype"; 60 61 File pkginfoFile = null; 63 File copyrightFile = null; 64 File checkinstallFile = null; 65 File postinstallFile = null; 66 File postremoveFile = null; 67 File prototypeFile = null; 68 69 private static String pkgInfoFileLocation = null; 71 72 private static String tmpResourcePath = null; 74 75 private static String tmpCopyrightPath = null; 77 78 81 private String runShellCommand(String [] commandStrs) { 82 BufferedReader br = null; 83 try { 84 Process proc = Runtime.getRuntime().exec(commandStrs); 85 86 br = new BufferedReader (new InputStreamReader (proc.getInputStream())); 88 String oneLine = null; 89 if ((oneLine = br.readLine()) != null) { 90 return oneLine; 92 } else { 93 return null; 94 } 95 } catch (IOException e) { 96 return null; 98 } finally { 99 if (br != null) { 101 try { 102 br.close(); 103 } catch (IOException e) { 104 } 105 } 106 } 107 } 108 109 113 private void createFileIgnoreExistance(File absFilePath) throws IOException { 114 if (absFilePath.exists()) { 116 boolean deleteSucceed = absFilePath.delete(); 117 if (!deleteSucceed) { 118 throw new IOException ("Failed to remove already existed file: " + absFilePath); 119 } 120 } 121 122 boolean createSucceed = absFilePath.createNewFile(); 123 if (!createSucceed) { 124 throw new IOException ("Failed to create new file: " + absFilePath); 125 } 126 } 127 128 131 private void deleteTempFiles() throws IOException { 132 if (pkginfoFile != null) pkginfoFile.delete(); 133 if (checkinstallFile != null) checkinstallFile.delete(); 134 if (postinstallFile != null) postinstallFile.delete(); 135 if (postremoveFile != null) postremoveFile.delete(); 136 if (prototypeFile != null) prototypeFile.delete(); 137 if (tmpResourcePath != null) { 138 FileOperUtility.deleteDirTree(new File (tmpResourcePath)); 139 } 140 141 if (tmpCopyrightPath != null) { 142 FileOperUtility.deleteDirTree(new File (tmpCopyrightPath)); 143 } 144 } 145 146 149 private void createFilePkginfo(JnlpPackageInfo pkgInfo) throws IOException { 150 String packageName = pkgInfo.getPackageName(); 151 String vendor = pkgInfo.getLocalizedJnlpInfo("en", JnlpConstants.JNLP_FIELD_VENDOR); 152 String productInfo = pkgInfo.getLocalizedJnlpInfo("en", JnlpConstants.JNLP_FIELD_TITLE); 153 String installationPath = pkgInfo.getUniqueTmpDirPath(); 154 String versionNum = pkgInfo.getVersion(); 155 String releaseNum = pkgInfo.getRelease(); 156 String versionStr = versionNum + ",RELEASE " + releaseNum; 158 159 pkginfoFile = new File (pkgInfoFileLocation, FILE_NAME_PKGINFO); 161 createFileIgnoreExistance(pkginfoFile); 162 163 BufferedWriter mBufferWriter = null; 165 try { 166 mBufferWriter = new BufferedWriter (new FileWriter (pkginfoFile)); 167 168 mBufferWriter.write("PKG=" + packageName + "\n"); 169 mBufferWriter.write("NAME=" + productInfo + "\n"); 170 mBufferWriter.write("ARCH=sparc" + "\n"); 171 mBufferWriter.write("CATEGORY=system" + "\n"); 172 mBufferWriter.write("VERSION=" + versionStr + "\n"); 173 mBufferWriter.write("BASEDIR=" + installationPath + "\n"); 174 mBufferWriter.write("PSTAMP=" + vendor + "\n"); 175 mBufferWriter.write("CLASSES=none" + "\n"); 176 } catch (IOException e) { 177 throw new IOException ("Failed to write into file: " + pkginfoFile); 178 } finally { 179 if (mBufferWriter != null) { 181 try { 182 mBufferWriter.close(); 183 } catch (IOException e) { 184 } 185 } 186 } 187 } 188 189 192 private void createFileCheckinstall() throws IOException { 193 checkinstallFile = new File (pkgInfoFileLocation, FILE_NAME_CHECKINSTALL); 195 createFileIgnoreExistance(checkinstallFile); 196 197 BufferedWriter mBufferWriter = null; 199 try { 200 mBufferWriter = new BufferedWriter (new FileWriter (checkinstallFile)); 201 202 String [] javawsCheckScript = JnlpUtility.javawsCheckScript(); 203 for (int lineNum = 0; lineNum < javawsCheckScript.length; lineNum++ ) { 204 mBufferWriter.write(javawsCheckScript[lineNum] + "\n"); 205 } 206 207 213 mBufferWriter.write("cat >$1 <<EOB" + "\n"); 214 mBufferWriter.write("JAVAWS_PATH=${JAVAWS_PATH}" + "\n"); 215 mBufferWriter.write("EOB" + "\n"); 216 mBufferWriter.write("exit 0" + "\n"); 217 } catch (IOException e) { 218 throw new IOException ("Failed to write info into file: " + checkinstallFile); 219 } finally { 220 if (mBufferWriter != null) { 222 try { 223 mBufferWriter.close(); 224 } catch (IOException e) { 225 } 226 } 227 } 228 } 229 230 233 private void createFilePostinstall(JnlpPackageInfo pkgInfo) throws IOException { 234 String installationPath = pkgInfo.getUniqueTmpDirPath(); 235 String jnlpFileName = pkgInfo.getJnlpFileName(); 236 boolean shortcutEnabled = pkgInfo.getShortcutEnabled(); 237 boolean associationEnabled = pkgInfo.getAssociationEnabled(); 238 boolean sysCacheEnabled = pkgInfo.getSystemCacheEnabled() ; 239 240 postinstallFile = new File (pkgInfoFileLocation, FILE_NAME_POSTINSTALL); 242 createFileIgnoreExistance(postinstallFile); 243 244 BufferedWriter mBufferWriter = null; 249 try { 250 mBufferWriter = new BufferedWriter (new FileWriter (postinstallFile)); 251 252 mBufferWriter.write("# Launch javaws using below command:" + "\n"); 253 mBufferWriter.write("\n"); 254 255 File instFile = new File (installationPath); 257 URL codebase = instFile.toURL(); 258 259 String jnlpFileLocation = null; 260 if (installationPath.endsWith(File.separator)) { 261 jnlpFileLocation = installationPath + jnlpFileName; 262 } else { 263 jnlpFileLocation = installationPath + File.separator + jnlpFileName; 264 } 265 266 String javawsCommand = "$JAVAWS_PATH "; 267 if (sysCacheEnabled) { 268 javawsCommand += "-system "; 269 } 270 javawsCommand += "-silent "; 271 if (shortcutEnabled) { 272 javawsCommand += "-shortcut "; 273 } 274 if (associationEnabled) { 275 javawsCommand += "-association "; 276 } 277 javawsCommand += "-import -codebase " + codebase + " " + jnlpFileLocation + "\n"; 278 279 mBufferWriter.write(javawsCommand); 280 281 mBufferWriter.write("\n"); 282 mBufferWriter.write("exit 0" + "\n"); 283 } catch (MalformedURLException e) { 284 throw new IOException ("Failed to convert the installation path to URL style: " + installationPath); 285 } catch (IOException e) { 286 throw new IOException ("Failed to write info into file: " + postinstallFile); 287 } finally { 288 if (mBufferWriter != null) { 290 try { 291 mBufferWriter.close(); 292 } catch (IOException e) { 293 } 294 } 295 } 296 } 297 298 301 private void createFilePostremove(JnlpPackageInfo pkgInfo) throws IOException { 302 String installationPath = pkgInfo.getUniqueTmpDirPath(); 303 String jnlpFileHref = pkgInfo.getJnlpFileHref(); 304 boolean sysCacheEnabled = pkgInfo.getSystemCacheEnabled(); 305 306 postremoveFile = new File (pkgInfoFileLocation, FILE_NAME_POSTREMOVE); 308 createFileIgnoreExistance(postremoveFile); 309 310 BufferedWriter mBufferWriter = null; 315 try { 316 mBufferWriter = new BufferedWriter (new FileWriter (postremoveFile)); 317 318 mBufferWriter.write("# Launch javaws using below command:" + "\n"); 319 mBufferWriter.write("\n"); 320 321 String javawsCommand = "$JAVAWS_PATH "; 322 if (sysCacheEnabled) { 323 javawsCommand += "-system "; 324 } 325 javawsCommand += "-silent -uninstall " + jnlpFileHref + "\n"; 326 mBufferWriter.write(javawsCommand); 327 328 mBufferWriter.write("rmdir $BASEDIR > /dev/null 2>&1" + "\n"); 330 331 mBufferWriter.write("\n"); 332 mBufferWriter.write("exit 0" + "\n"); 333 } catch (IOException e) { 334 throw new IOException ("Failed to write info into file: " + postremoveFile); 335 } finally { 336 if (mBufferWriter != null) { 338 try { 339 mBufferWriter.close(); 340 } catch (IOException e) { 341 } 342 } 343 } 344 } 345 346 350 private void genFilePrototype(JnlpPackageInfo pkgInfo) throws IOException { 351 File locationFile = new File (pkgInfoFileLocation); 353 if (!FileOperUtility.isDirectoryWritable(locationFile)) { 354 throw new IOException ("The default location for the package information files " 355 + "is not writable: " + pkgInfoFileLocation); 356 } 357 358 File tmpResourcePathFile = new File (pkgInfoFileLocation, "." + pkgInfo.getPackageName() + "."); 361 tmpResourcePath = tmpResourcePathFile.toString(); 362 FileOperUtility.copyLocalFile(pkgInfo.getResourceDirPath(), tmpResourcePath); 363 364 String licenseFilePath = pkgInfo.getLicenseDirPath (); 365 if (licenseFilePath != null) { 366 File tmpCopyrightPathFile = new File (pkgInfoFileLocation, "." + FILE_NAME_COPYRIGHT + "."); 369 tmpCopyrightPath = tmpCopyrightPathFile.toString(); 370 FileOperUtility.copyLocalFile(pkgInfo.getLicenseDirPath(), tmpCopyrightPath); 371 } 372 373 try { 375 createFilePkginfo(pkgInfo); 376 createFileCheckinstall(); 377 createFilePostinstall(pkgInfo); 378 createFilePostremove(pkgInfo); 379 } catch (IOException e) { 380 throw new IOException (e.getMessage()); 381 } 382 383 prototypeFile = new File (pkgInfoFileLocation, FILE_NAME_PROTOTYPE); 386 if (prototypeFile.exists()) { 387 boolean deleteSucceed = prototypeFile.delete(); 388 if (!deleteSucceed) { 389 throw new IOException ("Failed to remove the already existed prototype file at: " + prototypeFile); 390 } 391 } 392 393 String genCommand = "pkgproto " + tmpResourcePath + "= "; 395 396 if (pkgInfo.getLicenseDirPath() != null) { 399 genCommand += tmpCopyrightPath + "=" + "/usr/share/lib/javaws"; 400 } 401 genCommand += " > " + prototypeFile.toString(); 402 403 Process proc = null; 405 try { 406 proc = Runtime.getRuntime().exec(new String [] {"/bin/sh", "-c", genCommand}); 408 409 try { 410 proc.waitFor(); 411 } catch (InterruptedException ie) { 412 return; 413 } 414 415 } catch (IOException e) { 416 throw new IOException ("Failed to generate prototype file using command: " + 417 genCommand); 418 } 419 420 try { 427 String [] getUserCommandStrs = { 428 "/bin/sh", "-c", "echo `id` | awk -F\\( '{print $2}' | awk -F\\) '{print $1}'"}; 429 String user = runShellCommand(getUserCommandStrs); 430 if (user != null) { 431 String [] getGroupCommandStrs = { 432 "/bin/sh", "-c", "echo `id` | awk -F\\( '{print $3}' | awk -F\\) '{print $1}'"}; 433 String group = runShellCommand(getGroupCommandStrs); 434 435 if (group != null) { 436 File tempPrototypeFile = new File (prototypeFile.toString() + ".tmp"); 438 tempPrototypeFile.delete(); 439 440 String [] changePrototypeCommandStrs = { 441 "/bin/sh", "-c", "sed 's/" + user + " " + group + "/bin bin/' " + prototypeFile.toString() + " > " 442 + tempPrototypeFile.toString()}; 443 444 Runtime.getRuntime().exec(changePrototypeCommandStrs); 445 try { 446 proc.waitFor(); 447 } catch (InterruptedException ie) { 448 return; 449 } 450 451 if (tempPrototypeFile.exists()) { 452 prototypeFile.delete(); 455 tempPrototypeFile.renameTo(prototypeFile); 456 } 457 } 458 } 459 } catch (IOException e) { 460 } 462 463 if (!prototypeFile.exists()) { 466 throw new IOException ("No generated prototype file for generating the installable package."); 467 } 468 469 BufferedWriter mBufferWriter = null; 471 try { 472 mBufferWriter = new BufferedWriter (new FileWriter (prototypeFile, true)); 473 474 mBufferWriter.write("i pkginfo=" + pkginfoFile.toString() + "\n"); 475 String inputLicensePath = pkgInfo.getLicenseDirPath(); 476 if (inputLicensePath != null) { 477 copyrightFile = new File (inputLicensePath, "LICENSE.en"); 478 mBufferWriter.write("i copyright=" + copyrightFile.toString() + "\n"); 479 } 480 mBufferWriter.write("i checkinstall=" + checkinstallFile.toString() + "\n"); 481 mBufferWriter.write("i postinstall=" + postinstallFile.toString() + "\n"); 482 mBufferWriter.write("i postremove=" + postremoveFile.toString() + "\n"); 483 } catch (IOException e) { 484 throw new IOException ("Failed to write installation file entries to : " + prototypeFile); 485 } finally { 486 if (mBufferWriter != null) { 488 try { 489 mBufferWriter.close(); 490 } catch (IOException e) { 491 } 492 } 493 } 494 } 495 496 500 public void generatePackage(JnlpPackageInfo pkgInfo) throws IOException { 501 String packagePath = pkgInfo.getOutputDirPath(); 504 if (packagePath == null) { 505 packagePath = "/var/spool/pkg"; 506 } 507 File destinationPath = new File (packagePath); 508 if (!FileOperUtility.isDirectoryWritable(destinationPath)) { 509 throw new IOException ("The destination directory for the generated package " 510 + "is not writable: " + destinationPath); 511 } 512 513 String resourcePath = pkgInfo.getResourceDirPath(); 515 pkgInfoFileLocation = resourcePath; 516 517 try { 519 genFilePrototype(pkgInfo); 520 } catch (IOException e) { 521 deleteTempFiles(); 523 524 throw new IOException (e.getMessage()); 525 } 526 527 String genCommand = "pkgmk -o -f " + prototypeFile.toString() + " -d " + packagePath; 530 BufferedReader br = null; 531 try { 532 Process proc = Runtime.getRuntime().exec(genCommand); 533 534 br = new BufferedReader (new InputStreamReader (proc.getInputStream())); 536 String oneLine = null; 537 System.out.println("--- Output messages while generating the PKG package ---"); 538 while ((oneLine = br.readLine()) != null) { 539 System.out.println(oneLine); 540 } 541 542 br = new BufferedReader (new InputStreamReader (proc.getErrorStream())); 544 while ((oneLine = br.readLine()) != null) { 545 System.out.println(oneLine); 546 } 547 } catch (IOException e) { 548 throw new IOException ("Failed to generate an installable package using command: " 549 + genCommand); 550 } finally { 551 if (br != null) { 553 try { 554 br.close(); 555 } catch (IOException e) { 556 } 557 } 558 559 deleteTempFiles(); 561 } 562 } 563 } 564 | Popular Tags |