Note: From Mac OS 10.2 onward, this sample code onlys extract the version of WebObjects packages installed in the directory /Library/Receipts
, not for other software packages.
The use of this software is subject to the conditions set out in the license.txt file:
THE SOFTWARE PATCH PROVIDED FROM APPLE IS PROVIDED "AS IS" AND WITHOUT WARRANTY, EXPRESS OR IMPLIED. APPLE SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL APPLE BE LIABLE FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO ANY LOST PROFITS, LOST SAVINGS OR ANY INCIDENTAL OR CONSEQUENTIAL DAMAGES, WHETHER RESULTING FROM IMPAIRED OR LOST DATA, SOFTWARE OR COMPUTER FAILURE OR ANY OTHER CAUSE, EVEN IF APPLE IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY OTHER CLAIM BY CUSTOMER OR FOR ANY THIRD PARTY CLAIM.
Sample Code
In this example, the method
public static NSDictionary productVersions()
is defined in the file Application.java in a WebObjects application project.
import java.io.*;
import com.webobjects.foundation.*;
import com.webobjects.appserver.*;
import com.webobjects.eocontrol.*;
public class Application extends WOApplication {
private static NSDictionary _productVersions = null;
public static void main(String argv[]) {
WOApplication.main(argv, Application.class);
}
public Application() {
super();
System.out.println("Welcome to " + this.name() + "!");
/* ** Put your application initialization code here ** */
System.out.println("Version information:\\n" +
formattedDictionary(productVersions()));
}
public static NSDictionary productVersions() {
if ( _productVersions != null) return _productVersions;
// Go to the /Library/Receipts directory to get the software version
String receiptsDirString = "/Library/Receipts";
File receiptsDir = new java.io.File(receiptsDirString);
if (!receiptsDir.exists() || !receiptsDir.canRead())
return NSDictionary.EmptyDictionary;
// Extract the version information from each package Contents,
// Resources folders and software_version file
try {
String packageFiles[] = receiptsDir.list();
int length = packageFiles.length;
NSMutableDictionary packages = new NSMutableDictionary(length);
for (int i = 0; i < length; i++) {
String packageName = packageFiles[i];
String packagePath = receiptsDirString + "/" + packageName +
"/Contents/Resources/software_version";
File versionFile = new File(packagePath);
if (versionFile.exists()) {
StringBuffer versionString = new StringBuffer();
try {
LineNumberReader reader = new LineNumberReader(
new InputStreamReader(
new FileInputStream(versionFile)));
String line = reader.readLine();
while (line != null) {
versionString.append(" " + line);
line = reader.readLine();
}
}
catch (IOException e) {
// Flag the exception and return an empty string
System.out.println("Raised IOException");
return NSDictionary.EmptyDictionary;
}
if (versionString.length() > 1) {
String packageVersion = versionString.substring(1);
NSArray packagesForVersion =
(NSArray)packages.objectForKey(packageVersion);
if (packagesForVersion == null) {
packages.setObjectForKey(new NSArray(packageName),
packageVersion);
}
else {
NSMutableArray packageVersHolder =
new NSMutableArray(packagesForVersion);
packageVersHolder.addObject(packageName);
packages.setObjectForKey(packageVersHolder,
packageVersion);
}
}
}
}
_productVersions = packages;
return _productVersions;
} catch (SecurityException e) {
return NSDictionary.EmptyDictionary;
}
}
// Make product versions dictionary more readable by alphabetizing by
// package name and writing each package on its own line.
public static String formattedDictionary(NSDictionary aDict) {
String packageVersionString = aDict.toString();
packageVersionString = packageVersionString.substring(1,
packageVersionString.length() - 2);
NSArray packageStrings =
NSArray.componentsSeparatedByString(packageVersionString, "; ");
try {
packageStrings = packageStrings.sortedArrayUsingComparator(
NSComparator.AscendingStringComparator);
} catch (NSComparator.ComparisonException e) {
}
return packageStrings.componentsJoinedByString(";\\n");
}
}
Usage
The _productVersions NSDictionary contains the following:
Version information:
{
"DVDPlayerUpdate 3.1.1 (build 1B203)" = (DVDPlayerUpdate.pkg);
"Epson Print Drivers 1.1 1.1 (build 4L13)" = (EpsonPrintDrivers.pkg);
"Mac OS X 1.0 (build 5S66)" = (NetworkingUpdate.pkg);
"Mac OS X 1.0 (build 5U6)" = (SecurityUpdateJuly2002.pkg);
...