📄 pkgpackagegenerator.java
字号:
} catch (MalformedURLException e) { throw new IOException ("Failed to convert the installation path to URL style: " + installationPath); } catch (IOException e) { throw new IOException ("Failed to write info into file: " + postinstallFile); } finally { // No matter what happens, always close streams already opened. if (mBufferWriter != null) { try { mBufferWriter.close(); } catch (IOException e) { } } } } /* * Creates postremove file. */ private void createFilePostremove(JnlpPackageInfo pkgInfo) throws IOException { String installationPath = pkgInfo.getUniqueTmpDirPath(); String jnlpFileHref = pkgInfo.getJnlpFileHref(); boolean sysCacheEnabled = pkgInfo.getSystemCacheEnabled(); // Create file postremove. If it already exists, delete it first. postremoveFile = new File(pkgInfoFileLocation, FILE_NAME_POSTREMOVE); createFileIgnoreExistance(postremoveFile); // Write the below info into the postremove file: // # Launch javaws using below command: // $JAVAWS_PATH -silent -system -uninstall <jnlp_href> // exit 0 BufferedWriter mBufferWriter = null; try { mBufferWriter = new BufferedWriter(new FileWriter(postremoveFile)); mBufferWriter.write("# Launch javaws using below command:" + "\n"); mBufferWriter.write("\n"); String javawsCommand = "$JAVAWS_PATH "; if (sysCacheEnabled) { javawsCommand += "-system "; } javawsCommand += "-silent -uninstall " + jnlpFileHref + "\n"; mBufferWriter.write(javawsCommand); // Delete the created installation path only if it's empty. mBufferWriter.write("rmdir $BASEDIR > /dev/null 2>&1" + "\n"); mBufferWriter.write("\n"); mBufferWriter.write("exit 0" + "\n"); } catch (IOException e) { throw new IOException ("Failed to write info into file: " + postremoveFile); } finally { // No matter what happens, always close streams already opened. if (mBufferWriter != null) { try { mBufferWriter.close(); } catch (IOException e) { } } } } /* * Generates file prototype according to the extracted local files, * generated information files. */ private void genFilePrototype(JnlpPackageInfo pkgInfo) throws IOException { // Check if the default location for the package information files is writable. File locationFile = new File(pkgInfoFileLocation); if (!FileOperUtility.isDirectoryWritable(locationFile)) { throw new IOException("The default location for the package information files " + "is not writable: " + pkgInfoFileLocation); } // The given JNLP resource path may contain files other than the jnlp resource files, // copy the required resource paths to a temp directory before packaging them. File tmpResourcePathFile = new File(pkgInfoFileLocation, "." + pkgInfo.getPackageName() + "."); tmpResourcePath = tmpResourcePathFile.toString(); FileOperUtility.copyLocalFile(pkgInfo.getResourceDirPath(), tmpResourcePath); String licenseFilePath = pkgInfo.getLicenseDirPath (); if (licenseFilePath != null) { // Copy the copyright files into a temp copyright path, since the license file path // may contains files other than the license files.If no specified license path, return. File tmpCopyrightPathFile = new File(pkgInfoFileLocation, "." + FILE_NAME_COPYRIGHT + "."); tmpCopyrightPath = tmpCopyrightPathFile.toString(); FileOperUtility.copyLocalFile(pkgInfo.getLicenseDirPath(), tmpCopyrightPath); } // Create information files. try { createFilePkginfo(pkgInfo); createFileCheckinstall(); createFilePostinstall(pkgInfo); createFilePostremove(pkgInfo); } catch (IOException e) { throw new IOException(e.getMessage()); } // Create prototype file using the JNLP files, and the generated information files. // Check if file prototype doesn't exist. prototypeFile = new File(pkgInfoFileLocation, FILE_NAME_PROTOTYPE); if (prototypeFile.exists()) { boolean deleteSucceed = prototypeFile.delete(); if (!deleteSucceed) { throw new IOException("Failed to remove the already existed prototype file at: " + prototypeFile); } } // Include the jnlp resource files. String genCommand = "pkgproto " + tmpResourcePath + "= "; // If the user specified license file, store the file in the local directory: // JnlpConstants.LICENSE_INSTALLATION_BASE_PATH/<vendor>/<name>/<version>.<release> if (pkgInfo.getLicenseDirPath() != null) { genCommand += tmpCopyrightPath + "=" + "/usr/share/lib/javaws"; } genCommand += " > " + prototypeFile.toString(); // Generate file prototype using pkgproto. Process proc = null; try { // "/bin/sh -c" is required to interpret shell metacharacters like '>'. proc = Runtime.getRuntime().exec(new String[] {"/bin/sh", "-c", genCommand}); try { proc.waitFor(); } catch (InterruptedException ie) { return; } } catch (IOException e) { throw new IOException("Failed to generate prototype file using command: " + genCommand); } // Up to now, the prototype file is created successfully, but the current user and group // ownerships are used for each entry in the file. But the package might not work when // installed if owned by another user. // Refer to http://www.sunfreeware.com/pkgadd.html for more info. // So, change the ownship of the entries to bin(user) and bin(group). To be exactly, // change the "userid groupid" in the prototype file to "bin bin". try { String[] getUserCommandStrs = { "/bin/sh", "-c", "echo `id` | awk -F\\( '{print $2}' | awk -F\\) '{print $1}'"}; String user = runShellCommand(getUserCommandStrs); if (user != null) { String[] getGroupCommandStrs = { "/bin/sh", "-c", "echo `id` | awk -F\\( '{print $3}' | awk -F\\) '{print $1}'"}; String group = runShellCommand(getGroupCommandStrs); if (group != null) { // Create a temp prototype file. If it already exists, delete it. File tempPrototypeFile = new File(prototypeFile.toString() + ".tmp"); tempPrototypeFile.delete(); String[] changePrototypeCommandStrs = { "/bin/sh", "-c", "sed 's/" + user + " " + group + "/bin bin/' " + prototypeFile.toString() + " > " + tempPrototypeFile.toString()}; Runtime.getRuntime().exec(changePrototypeCommandStrs); try { proc.waitFor(); } catch (InterruptedException ie) { return; } if (tempPrototypeFile.exists()) { // The temp file is created successfully. Delete the original prototype file // and rename the generated temp file to prototype. prototypeFile.delete(); tempPrototypeFile.renameTo(prototypeFile); } } } } catch (IOException e) { // If the above ownships changing operation fails, just ignore it. } // Edit prototype file to add information file entries. // First check if prototype is created successfully. if (!prototypeFile.exists()) { throw new IOException("No generated prototype file for generating the installable package."); } // *Append* information file items into prototype file: BufferedWriter mBufferWriter = null; try { mBufferWriter = new BufferedWriter(new FileWriter(prototypeFile, true)); mBufferWriter.write("i pkginfo=" + pkginfoFile.toString() + "\n"); String inputLicensePath = pkgInfo.getLicenseDirPath(); if (inputLicensePath != null) { copyrightFile = new File(inputLicensePath, "LICENSE.en"); mBufferWriter.write("i copyright=" + copyrightFile.toString() + "\n"); } mBufferWriter.write("i checkinstall=" + checkinstallFile.toString() + "\n"); mBufferWriter.write("i postinstall=" + postinstallFile.toString() + "\n"); mBufferWriter.write("i postremove=" + postremoveFile.toString() + "\n"); } catch (IOException e) { throw new IOException ("Failed to write installation file entries to : " + prototypeFile); } finally { // No matter what happens, always close streams already opened. if (mBufferWriter != null) { try { mBufferWriter.close(); } catch (IOException e) { } } } } /* * Generates the package according to the generated prototype, extracted local * temp files, and generated information files. */ public void generatePackage(JnlpPackageInfo pkgInfo) throws IOException { // If the package destination path is not specified, use the default spool // directory (/var/spool/pkg). String packagePath = pkgInfo.getOutputDirPath(); if (packagePath == null) { packagePath = "/var/spool/pkg"; } File destinationPath = new File(packagePath); if (!FileOperUtility.isDirectoryWritable(destinationPath)) { throw new IOException("The destination directory for the generated package " + "is not writable: " + destinationPath); } // Use the resource path as the location of the generated package information files. String resourcePath = pkgInfo.getResourceDirPath(); pkgInfoFileLocation = resourcePath; // Generate the prototype file. try { genFilePrototype(pkgInfo); } catch (IOException e) { // On failure, try to delete all the created files and directories. deleteTempFiles(); throw new IOException(e.getMessage()); } // Generate the package using command: // pkgmk -o -f <prototype file path> -d packagePath String genCommand = "pkgmk -o -f " + prototypeFile.toString() + " -d " + packagePath; BufferedReader br = null; try { Process proc = Runtime.getRuntime().exec(genCommand); // Output messages. br = new BufferedReader(new InputStreamReader(proc.getInputStream())); String oneLine = null; System.out.println("--- Output messages while generating the PKG package ---"); while ((oneLine = br.readLine()) != null) { System.out.println(oneLine); } // Error messages. br = new BufferedReader(new InputStreamReader(proc.getErrorStream())); while ((oneLine = br.readLine()) != null) { System.out.println(oneLine); } } catch (IOException e) { throw new IOException("Failed to generate an installable package using command: " + genCommand); } finally { // No matter what happens, always close streams already opened. if (br != null) { try { br.close(); } catch (IOException e) { } } // No matter what happens, always try to delete all the created temp files. deleteTempFiles(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -