diff --git a/itests/hive-unit/src/test/java/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java b/itests/hive-unit/src/test/java/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java index e2d9b42..bb62dd5 100644 --- a/itests/hive-unit/src/test/java/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java +++ b/itests/hive-unit/src/test/java/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java @@ -1115,6 +1115,12 @@ public void testDriverProperties() throws SQLException { } + public void testInvalidUrl() throws SQLException { + HiveDriver driver = new HiveDriver(); + + assertNull(driver.connect("jdbc:hive2://localhost:1000", null)); + } + private static void assertDpi(DriverPropertyInfo dpi, String name, String value) { assertEquals("Invalid DriverPropertyInfo name", name, dpi.name); diff --git a/jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDriver.java b/jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDriver.java index 68c0788..e8bff53 100644 --- a/jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDriver.java +++ b/jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveDriver.java @@ -22,6 +22,7 @@ import java.net.URL; import java.sql.Connection; import java.sql.Driver; +import java.sql.DriverManager; import java.sql.DriverPropertyInfo; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; @@ -32,15 +33,14 @@ import java.util.regex.Pattern; /** - * HiveDriver. - * + * HiveDriver to access a HiveServer1. */ public class HiveDriver implements Driver { + static { try { - java.sql.DriverManager.registerDriver(new HiveDriver()); + DriverManager.registerDriver(new HiveDriver()); } catch (SQLException e) { - // TODO Auto-generated catch block e.printStackTrace(); } } @@ -79,7 +79,6 @@ * */ public HiveDriver() { - // TODO Auto-generated constructor stub SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkWrite("foobah"); @@ -88,37 +87,36 @@ public HiveDriver() { /** * Checks whether a given url is in a valid format. - * + * * The current uri format is: jdbc:hive://[host[:port]] - * + * * jdbc:hive:// - run in embedded mode jdbc:hive://localhost - connect to * localhost default port (10000) jdbc:hive://localhost:5050 - connect to * localhost port 5050 - * - * TODO: - write a better regex. - decide on uri format */ - + @Override public boolean acceptsURL(String url) throws SQLException { return Pattern.matches(URL_PREFIX + ".*", url); } + @Override public Connection connect(String url, Properties info) throws SQLException { - return new HiveConnection(url, info); + return acceptsURL(url) ? new HiveConnection(url, info) : null; } /** - * Package scoped access to the Driver's Major Version + * 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 fullVersion = fetchManifestAttribute(Attributes.Name.IMPLEMENTATION_VERSION); String[] tokens = fullVersion.split("\\."); //$NON-NLS-1$ - - if(tokens != null && tokens.length > 0 && tokens[0] != null) { + + if (tokens.length > 0 && tokens[0] != null) { version = Integer.parseInt(tokens[0]); } } catch (Exception e) { @@ -129,20 +127,20 @@ static int getMajorDriverVersion() { } return version; } - + /** - * Package scoped access to the Driver's Minor 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 fullVersion = fetchManifestAttribute(Attributes.Name.IMPLEMENTATION_VERSION); String[] tokens = fullVersion.split("\\."); //$NON-NLS-1$ - - if(tokens != null && tokens.length > 1 && tokens[1] != null) { + + if (tokens.length > 1 && tokens[1] != null) { version = Integer.parseInt(tokens[1]); } } catch (Exception e) { @@ -153,47 +151,51 @@ static int getMinorDriverVersion() { } return version; } - + /** * Returns the major version of this driver. */ + @Override public int getMajorVersion() { - return HiveDriver.getMajorDriverVersion(); + return getMajorDriverVersion(); } /** * Returns the minor version of this driver. */ + @Override public int getMinorVersion() { - return HiveDriver.getMinorDriverVersion(); + return getMinorDriverVersion(); } + @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { // JDK 1.7 throw new SQLFeatureNotSupportedException("Method not supported"); } + @Override public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { if (info == null) { info = new Properties(); } - if ((url != null) && url.startsWith(URL_PREFIX)) { + if (url != null && url.startsWith(URL_PREFIX)) { info = parseURL(url, info); } - DriverPropertyInfo hostProp = new DriverPropertyInfo(HOST_PROPERTY_KEY, - info.getProperty(HOST_PROPERTY_KEY, "")); + DriverPropertyInfo hostProp = + new DriverPropertyInfo(HOST_PROPERTY_KEY, info.getProperty(HOST_PROPERTY_KEY, "")); hostProp.required = false; hostProp.description = "Hostname of Hive Server"; - DriverPropertyInfo portProp = new DriverPropertyInfo(PORT_PROPERTY_KEY, - info.getProperty(PORT_PROPERTY_KEY, "")); + DriverPropertyInfo portProp = + new DriverPropertyInfo(PORT_PROPERTY_KEY, info.getProperty(PORT_PROPERTY_KEY, "")); portProp.required = false; portProp.description = "Port number of Hive Server"; - DriverPropertyInfo dbProp = new DriverPropertyInfo(DBNAME_PROPERTY_KEY, - info.getProperty(DBNAME_PROPERTY_KEY, "default")); + DriverPropertyInfo dbProp = + new DriverPropertyInfo(DBNAME_PROPERTY_KEY, info.getProperty(DBNAME_PROPERTY_KEY, "default")); dbProp.required = false; dbProp.description = "Database name"; @@ -210,6 +212,7 @@ public Logger getParentLogger() throws SQLFeatureNotSupportedException { * Returns whether the driver is JDBC compliant. */ + @Override public boolean jdbcCompliant() { return JDBC_COMPLIANT; } @@ -217,15 +220,9 @@ public boolean jdbcCompliant() { /** * Takes a url in the form of jdbc:hive://[hostname]:[port]/[db_name] and * parses it. Everything after jdbc:hive// is optional. - * - * @param url - * @param defaults - * @return - * @throws java.sql.SQLException */ private Properties parseURL(String url, Properties defaults) throws SQLException { - Properties urlProps = (defaults != null) ? new Properties(defaults) - : new Properties(); + Properties urlProps = defaults != null ? new Properties(defaults) : new Properties(); if (url == null || !url.startsWith(URL_PREFIX)) { throw new SQLException("Invalid connection url: " + url); @@ -242,24 +239,24 @@ private Properties parseURL(String url, Properties defaults) throws SQLException String[] hostPortAndDatabase = connectionInfo.split("/", 2); // [hostname]:[port] - if (hostPortAndDatabase[0].length() > 0) { + if (!hostPortAndDatabase[0].isEmpty()) { String[] hostAndPort = hostPortAndDatabase[0].split(":", 2); - urlProps.put(HOST_PROPERTY_KEY, hostAndPort[0]); + urlProps.setProperty(HOST_PROPERTY_KEY, hostAndPort[0]); if (hostAndPort.length > 1) { - urlProps.put(PORT_PROPERTY_KEY, hostAndPort[1]); + urlProps.setProperty(PORT_PROPERTY_KEY, hostAndPort[1]); } else { - urlProps.put(PORT_PROPERTY_KEY, DEFAULT_PORT); + urlProps.setProperty(PORT_PROPERTY_KEY, DEFAULT_PORT); } } // [db_name] if (hostPortAndDatabase.length > 1) { - urlProps.put(DBNAME_PROPERTY_KEY, hostPortAndDatabase[1]); + urlProps.setProperty(DBNAME_PROPERTY_KEY, hostPortAndDatabase[1]); } return urlProps; } - + /** * Lazy-load manifest attributes as needed. */ @@ -267,19 +264,14 @@ private Properties parseURL(String url, Properties defaults) throws SQLException /** * 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"); + 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(); } @@ -287,13 +279,8 @@ private static synchronized void loadManifestAttributes() throws IOException { /** * 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 { + static String fetchManifestAttribute(Attributes.Name attributeName) throws SQLException { try { loadManifestAttributes(); } catch (IOException e) { diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveDriver.java b/jdbc/src/java/org/apache/hive/jdbc/HiveDriver.java index 6e248d6..b7751f5 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/HiveDriver.java +++ b/jdbc/src/java/org/apache/hive/jdbc/HiveDriver.java @@ -22,6 +22,7 @@ import java.net.URL; import java.sql.Connection; import java.sql.Driver; +import java.sql.DriverManager; import java.sql.DriverPropertyInfo; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; @@ -33,17 +34,15 @@ import org.apache.hive.jdbc.Utils.JdbcConnectionParams; - /** - * HiveDriver. - * + * HiveDriver to access a HiveServer2. */ public class HiveDriver implements Driver { + static { try { - java.sql.DriverManager.registerDriver(new HiveDriver()); + DriverManager.registerDriver(new HiveDriver()); } catch (SQLException e) { - // TODO Auto-generated catch block e.printStackTrace(); } } @@ -68,12 +67,7 @@ */ private static final String PORT_PROPERTY_KEY = "PORT"; - - /** - * - */ public HiveDriver() { - // TODO Auto-generated constructor stub SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkWrite("foobah"); @@ -88,10 +82,8 @@ public HiveDriver() { * jdbc:hive:// - run in embedded mode jdbc:hive://localhost - connect to * localhost default port (10000) jdbc:hive://localhost:5050 - connect to * localhost port 5050 - * - * TODO: - write a better regex. - decide on uri format */ - + @Override public boolean acceptsURL(String url) throws SQLException { return Pattern.matches(Utils.URL_PREFIX + ".*", url); } @@ -101,23 +93,24 @@ public boolean acceptsURL(String url) throws SQLException { * "If the Driver implementation understands the URL, it will return a Connection object; * otherwise it returns null" */ + @Override public Connection connect(String url, Properties info) throws SQLException { return acceptsURL(url) ? new HiveConnection(url, info) : null; } /** * 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 fullVersion = fetchManifestAttribute(Attributes.Name.IMPLEMENTATION_VERSION); String[] tokens = fullVersion.split("\\."); //$NON-NLS-1$ - if(tokens != null && tokens.length > 0 && tokens[0] != null) { + if (tokens.length > 0 && tokens[0] != null) { version = Integer.parseInt(tokens[0]); } } catch (Exception e) { @@ -131,17 +124,17 @@ static int getMajorDriverVersion() { /** * 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 fullVersion = fetchManifestAttribute(Attributes.Name.IMPLEMENTATION_VERSION); String[] tokens = fullVersion.split("\\."); //$NON-NLS-1$ - if(tokens != null && tokens.length > 1 && tokens[1] != null) { + if (tokens.length > 1 && tokens[1] != null) { version = Integer.parseInt(tokens[1]); } } catch (Exception e) { @@ -156,43 +149,47 @@ static int getMinorDriverVersion() { /** * Returns the major version of this driver. */ + @Override public int getMajorVersion() { - return HiveDriver.getMajorDriverVersion(); + return getMajorDriverVersion(); } /** * Returns the minor version of this driver. */ + @Override public int getMinorVersion() { - return HiveDriver.getMinorDriverVersion(); + return getMinorDriverVersion(); } + @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { // JDK 1.7 throw new SQLFeatureNotSupportedException("Method not supported"); } + @Override public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { if (info == null) { info = new Properties(); } - if ((url != null) && url.startsWith(Utils.URL_PREFIX)) { + if (url != null && url.startsWith(Utils.URL_PREFIX)) { info = parseURLforPropertyInfo(url, info); } - DriverPropertyInfo hostProp = new DriverPropertyInfo(HOST_PROPERTY_KEY, - info.getProperty(HOST_PROPERTY_KEY, "")); + DriverPropertyInfo hostProp = + new DriverPropertyInfo(HOST_PROPERTY_KEY, info.getProperty(HOST_PROPERTY_KEY, "")); hostProp.required = false; hostProp.description = "Hostname of Hive Server2"; - DriverPropertyInfo portProp = new DriverPropertyInfo(PORT_PROPERTY_KEY, - info.getProperty(PORT_PROPERTY_KEY, "")); + DriverPropertyInfo portProp = + new DriverPropertyInfo(PORT_PROPERTY_KEY, info.getProperty(PORT_PROPERTY_KEY, "")); portProp.required = false; portProp.description = "Port number of Hive Server2"; - DriverPropertyInfo dbProp = new DriverPropertyInfo(DBNAME_PROPERTY_KEY, - info.getProperty(DBNAME_PROPERTY_KEY, "default")); + DriverPropertyInfo dbProp = + new DriverPropertyInfo(DBNAME_PROPERTY_KEY, info.getProperty(DBNAME_PROPERTY_KEY, "default")); dbProp.required = false; dbProp.description = "Database name"; @@ -208,6 +205,7 @@ public Logger getParentLogger() throws SQLFeatureNotSupportedException { /** * Returns whether the driver is JDBC compliant. */ + @Override public boolean jdbcCompliant() { return JDBC_COMPLIANT; } @@ -217,14 +215,9 @@ public boolean jdbcCompliant() { * parses it. Everything after jdbc:hive// is optional. * * The output from Utils.parseUrl() is massaged for the needs of getPropertyInfo - * @param url - * @param defaults - * @return - * @throws java.sql.SQLException */ private Properties parseURLforPropertyInfo(String url, Properties defaults) throws SQLException { - Properties urlProps = (defaults != null) ? new Properties(defaults) - : new Properties(); + Properties urlProps = defaults != null ? new Properties(defaults) : new Properties(); if (url == null || !url.startsWith(Utils.URL_PREFIX)) { throw new SQLException("Invalid connection url: " + url); @@ -232,20 +225,19 @@ private Properties parseURLforPropertyInfo(String url, Properties defaults) thro JdbcConnectionParams params = Utils.parseURL(url); String host = params.getHost(); - if (host == null){ + if (host == null) { host = ""; } String port = Integer.toString(params.getPort()); - if(host.equals("")){ + if (host.isEmpty()) { port = ""; - } - else if(port.equals("0")){ + } else if (port.equals("0")) { port = Utils.DEFAULT_PORT; } String db = params.getDbName(); - urlProps.put(HOST_PROPERTY_KEY, host); - urlProps.put(PORT_PROPERTY_KEY, port); - urlProps.put(DBNAME_PROPERTY_KEY, db); + urlProps.setProperty(HOST_PROPERTY_KEY, host); + urlProps.setProperty(PORT_PROPERTY_KEY, port); + urlProps.setProperty(DBNAME_PROPERTY_KEY, db); return urlProps; } @@ -257,19 +249,14 @@ else if(port.equals("0")){ /** * 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"); + 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(); } @@ -277,13 +264,8 @@ private static synchronized void loadManifestAttributes() throws IOException { /** * 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 { + static String fetchManifestAttribute(Attributes.Name attributeName) throws SQLException { try { loadManifestAttributes(); } catch (IOException e) {