You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by cw...@apache.org on 2011/06/15 04:08:42 UTC

svn commit: r1135893 - in /hive/trunk/jdbc/src/java/org/apache/hadoop/hive/jdbc: HiveDatabaseMetaData.java HiveDriver.java

Author: cws
Date: Wed Jun 15 02:08:42 2011
New Revision: 1135893

URL: http://svn.apache.org/viewvc?rev=1135893&view=rev
Log:
HIVE-2140. Return correct Major / Minor version numbers for Hive Driver (Curtis Boyden via cws)

Modified:
    hive/trunk/jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDatabaseMetaData.java
    hive/trunk/jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDriver.java

Modified: hive/trunk/jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDatabaseMetaData.java
URL: http://svn.apache.org/viewvc/hive/trunk/jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDatabaseMetaData.java?rev=1135893&r1=1135892&r2=1135893&view=diff
==============================================================================
--- hive/trunk/jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDatabaseMetaData.java (original)
+++ hive/trunk/jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDatabaseMetaData.java Wed Jun 15 02:08:42 2011
@@ -286,19 +286,19 @@ public class HiveDatabaseMetaData implem
   }
 
   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)
@@ -1050,47 +1050,6 @@ public class HiveDatabaseMetaData implem
     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());

Modified: hive/trunk/jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDriver.java
URL: http://svn.apache.org/viewvc/hive/trunk/jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDriver.java?rev=1135893&r1=1135892&r2=1135893&view=diff
==============================================================================
--- hive/trunk/jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDriver.java (original)
+++ hive/trunk/jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDriver.java Wed Jun 15 02:08:42 2011
@@ -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 @@ public class HiveDriver implements Drive
   }
 
   /**
-   * 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 @@ public class HiveDriver implements Drive
   }
 
   /**
+   * 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 @@ public class HiveDriver implements Drive
 
     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);
+  }
 }