001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.util;
014:
015: import java.util.ResourceBundle;
016: import java.util.jar.Attributes;
017: import java.util.jar.Manifest;
018:
019: public class VersionHelper implements IVersionHelper {
020:
021: public String getVersionInformation() {
022: return getVersionInformation(VersionHelper.class);
023: }
024:
025: public String getVersionInformation(Class clazz) {
026: // The following two lines read from the MANIFEST.MF
027: String implTitle = clazz.getPackage().getImplementationTitle();
028: String implVersion = clazz.getPackage()
029: .getImplementationVersion();
030: if (implVersion != null) {
031: // If we're in a .jar file, then we can return the version information
032: // from the .jar file.
033: return implTitle + " " + implVersion; //$NON-NLS-1$
034: } else {
035: // We're not in a .jar file - try to find the build-res/version file and
036: // read the version information from that.
037: try {
038: ResourceBundle bundle = ResourceBundle
039: .getBundle("build-res.version"); //$NON-NLS-1$
040: StringBuffer buff = new StringBuffer();
041: buff
042: .append(bundle.getString("impl.title")).append(' ').append(bundle.getString("release.major.number")).append('.').append(bundle.getString("release.minor.number")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
043: buff
044: .append('.')
045: .append(
046: bundle
047: .getString("release.milestone.number")).append('.').append(bundle.getString("release.build.number")).append(" (class)"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
048: return buff.toString();
049: } catch (Exception ex) {
050: return "Pentaho BI Platform - No Version Information Available"; //$NON-NLS-1$
051: }
052: }
053: }
054:
055: public static VersionInfo getVersionInfo() {
056: return getVersionInfo(VersionHelper.class);
057: }
058:
059: public static VersionInfo getVersionInfo(Class clazz) {
060: // Try to get the manifest for the class
061: final Manifest manifest = ManifestUtil.getManifest(clazz);
062: if (manifest != null) {
063: // Return the version info from the manifest
064: return createVersionInfo(manifest);
065: }
066: // Return the default version info (from properties file)
067: return createVersionInfo();
068: }
069:
070: /**
071: * Extracts the version information data from the manifest's attributes
072: * and puts them into a VersionInfo instance.
073: * @param manifest the manifest information
074: * @return the version information from the manifest
075: */
076: protected static VersionInfo createVersionInfo(Manifest manifest) {
077: final VersionInfo versionInfo = new VersionInfo();
078: final Attributes mainAttributes = manifest.getMainAttributes();
079: versionInfo.setFromManifest(true);
080: versionInfo.setProductID(mainAttributes
081: .getValue("Implementation-ProductID")); //$NON-NLS-1$
082: versionInfo.setTitle(mainAttributes
083: .getValue("Implementation-Title")); //$NON-NLS-1$
084: versionInfo.setVersion(mainAttributes
085: .getValue("Implementation-Version")); //$NON-NLS-1$
086: return versionInfo;
087: }
088:
089: /**
090: * Extracts the version information data from the <code>build-res/version.properties</code>
091: * file found in the source directory.
092: * @return the version information from the <code>build-res/version.properties</code> file
093: */
094: protected static VersionInfo createVersionInfo() {
095: // We're not in a .jar file - try to find the build-res/version file and
096: // read the version information from that.
097: final VersionInfo versionInfo = new VersionInfo();
098: try {
099: final ResourceBundle bundle = ResourceBundle
100: .getBundle("build-res.version"); //$NON-NLS-1$
101: versionInfo.setFromManifest(false);
102: versionInfo
103: .setProductID(bundle.getString("impl.productID")); //$NON-NLS-1$
104: versionInfo.setTitle(bundle.getString("impl.title")); //$NON-NLS-1$
105: versionInfo.setVersionMajor(bundle
106: .getString("release.major.number")); //$NON-NLS-1$
107: versionInfo.setVersionMinor(bundle
108: .getString("release.minor.number")); //$NON-NLS-1$
109: versionInfo.setVersionBuild(bundle
110: .getString("release.build.number")); //$NON-NLS-1$
111:
112: // The release milestone number has both the release number and the milestone number
113: final String releaseMilestoneNumber = bundle
114: .getString("release.milestone.number"); //$NON-NLS-1$
115: if (releaseMilestoneNumber != null) {
116: String[] parts = releaseMilestoneNumber.replace('-',
117: '.').split("\\."); //$NON-NLS-1$
118: if (parts.length > 0) {
119: versionInfo.setVersionRelease(parts[0]);
120: if (parts.length > 1) {
121: versionInfo.setVersionMilestone(parts[1]);
122: }
123: }
124: }
125: } catch (Exception e) {
126: // TODO log this error
127: }
128: return versionInfo;
129: }
130: }
|