Index: jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDatabaseMetaData.java =================================================================== --- jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDatabaseMetaData.java (revision 1097477) +++ jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDatabaseMetaData.java (working copy) @@ -281,19 +281,19 @@ } public int getDriverMajorVersion() { - return 0; + return HiveDriver.getMajorDriverVersion(); } public int getDriverMinorVersion() { - return 0; + return HiveDriver.getMinorDriverVersion(); } public String getDriverName() throws SQLException { - return fetchManifestAttribute(Attributes.Name.IMPLEMENTATION_TITLE); + return HiveDriver.fetchManifestAttribute(Attributes.Name.IMPLEMENTATION_TITLE); } public String getDriverVersion() throws SQLException { - return fetchManifestAttribute(Attributes.Name.IMPLEMENTATION_VERSION); + return HiveDriver.fetchManifestAttribute(Attributes.Name.IMPLEMENTATION_VERSION); } public ResultSet getExportedKeys(String catalog, String schema, String table) @@ -1045,47 +1045,6 @@ throw new SQLException("Method not supported"); } - /** - * Lazy-load manifest attributes as needed. - */ - private static Attributes manifestAttributes = null; - - /** - * Loads the manifest attributes from the jar. - * - * @throws java.net.MalformedURLException - * @throws IOException - */ - private synchronized void loadManifestAttributes() throws IOException { - if (manifestAttributes != null) { - return; - } - Class clazz = this.getClass(); - String classContainer = clazz.getProtectionDomain().getCodeSource() - .getLocation().toString(); - URL manifestUrl = new URL("jar:" + classContainer - + "!/META-INF/MANIFEST.MF"); - Manifest manifest = new Manifest(manifestUrl.openStream()); - manifestAttributes = manifest.getMainAttributes(); - } - - /** - * Helper to initialize attributes and return one. - * - * @param attributeName - * @return - * @throws SQLException - */ - private String fetchManifestAttribute(Attributes.Name attributeName) - throws SQLException { - try { - loadManifestAttributes(); - } catch (IOException e) { - throw new SQLException("Couldn't load manifest attributes.", e); - } - return manifestAttributes.getValue(attributeName); - } - public static void main(String[] args) throws SQLException { HiveDatabaseMetaData meta = new HiveDatabaseMetaData(null); System.out.println("DriverName: " + meta.getDriverName()); Index: jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDriver.java =================================================================== --- jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDriver.java (revision 1097477) +++ jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDriver.java (working copy) @@ -18,11 +18,15 @@ package org.apache.hadoop.hive.jdbc; +import java.io.IOException; +import java.net.URL; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverPropertyInfo; import java.sql.SQLException; import java.util.Properties; +import java.util.jar.Attributes; +import java.util.jar.Manifest; import java.util.regex.Pattern; /** @@ -40,16 +44,6 @@ } /** - * Major version number of this driver. - */ - private static final int MAJOR_VERSION = 0; - - /** - * Minor version number of this driver. - */ - private static final int MINOR_VERSION = 0; - - /** * Is this driver JDBC compliant? */ private static final boolean JDBC_COMPLIANT = false; @@ -111,19 +105,65 @@ } /** + * Package scoped access to the Driver's Major Version + * @return The Major version number of the driver. -1 if it cannot be determined from + * the manifest.mf file. + */ + static int getMajorDriverVersion() { + int version = -1; + try { + String fullVersion = HiveDriver.fetchManifestAttribute( + Attributes.Name.IMPLEMENTATION_VERSION); + String[] tokens = fullVersion.split("\\."); //$NON-NLS-1$ + + if(tokens != null && tokens.length > 0 && tokens[0] != null) { + version = Integer.parseInt(tokens[0]); + } + } catch (Exception e) { + // Possible reasons to end up here: + // - Unable to read version from manifest.mf + // - Version string is not in the proper X.x.xxx format + version = -1; + } + return version; + } + + /** + * Package scoped access to the Driver's Minor Version + * @return The Minor version number of the driver. -1 if it cannot be determined from the + * manifest.mf file. + */ + static int getMinorDriverVersion() { + int version = -1; + try { + String fullVersion = HiveDriver.fetchManifestAttribute( + Attributes.Name.IMPLEMENTATION_VERSION); + String[] tokens = fullVersion.split("\\."); //$NON-NLS-1$ + + if(tokens != null && tokens.length > 1 && tokens[1] != null) { + version = Integer.parseInt(tokens[1]); + } + } catch (Exception e) { + // Possible reasons to end up here: + // - Unable to read version from manifest.mf + // - Version string is not in the proper X.x.xxx format + version = -1; + } + return version; + } + + /** * Returns the major version of this driver. */ - public int getMajorVersion() { - return MAJOR_VERSION; + return HiveDriver.getMajorDriverVersion(); } /** * Returns the minor version of this driver. */ - public int getMinorVersion() { - return MINOR_VERSION; + return HiveDriver.getMinorDriverVersion(); } public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { @@ -212,4 +252,46 @@ return urlProps; } + + /** + * Lazy-load manifest attributes as needed. + */ + private static Attributes manifestAttributes = null; + + /** + * Loads the manifest attributes from the jar. + * + * @throws java.net.MalformedURLException + * @throws IOException + */ + private static synchronized void loadManifestAttributes() throws IOException { + if (manifestAttributes != null) { + return; + } + Class clazz = HiveDriver.class; + String classContainer = clazz.getProtectionDomain().getCodeSource() + .getLocation().toString(); + URL manifestUrl = new URL("jar:" + classContainer + + "!/META-INF/MANIFEST.MF"); + Manifest manifest = new Manifest(manifestUrl.openStream()); + manifestAttributes = manifest.getMainAttributes(); + } + + /** + * Package scoped to allow manifest fetching from other HiveDriver classes + * Helper to initialize attributes and return one. + * + * @param attributeName + * @return + * @throws SQLException + */ + static String fetchManifestAttribute(Attributes.Name attributeName) + throws SQLException { + try { + loadManifestAttributes(); + } catch (IOException e) { + throw new SQLException("Couldn't load manifest attributes.", e); + } + return manifestAttributes.getValue(attributeName); + } }