Skip to content

Commit 7a7596f

Browse files
HIVE-29258: Remove Class.forName() for driver loading (#6207)
* HIVE-29258: Remove Class.forName() for driver loading * Addressing SonaType * Addressing review comments * Remove Reflection code in BeeLine.java, DatabaseConnection.java and Commands.java to leverage ServiceLoader * Review comments
1 parent 83bc145 commit 7a7596f

File tree

64 files changed

+55
-224
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+55
-224
lines changed

beeline/src/java/org/apache/hive/beeline/BeeLine.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
import java.util.ResourceBundle;
7272
import java.util.ServiceLoader;
7373
import java.util.Set;
74-
import java.util.SortedSet;
7574
import java.util.StringTokenizer;
7675
import java.util.TreeMap;
7776
import java.util.TreeSet;
@@ -310,12 +309,6 @@ public class BeeLine implements Closeable {
310309

311310
private final Completer beeLineCommandCompleter = new BeeLineCommandCompleter(Arrays.asList(commandHandlers));
312311

313-
static final SortedSet<String> KNOWN_DRIVERS = new TreeSet<String>(Arrays.asList(
314-
new String[] {
315-
"org.apache.hive.jdbc.HiveDriver",
316-
"org.apache.hadoop.hive.jdbc.HiveDriver",
317-
}));
318-
319312
static {
320313
try {
321314
Class.forName("org.jline.reader.LineReader");
@@ -2370,23 +2363,17 @@ private Driver findRegisteredDriver(String url) {
23702363
return null;
23712364
}
23722365

2373-
public Driver findLocalDriver(String url) throws Exception {
2366+
public Driver findLocalDriver(String url) throws SQLException {
23742367
Objects.requireNonNull(url);
23752368

23762369
Collection<Driver> currentDrivers = drivers == null ? Collections.emptyList() : drivers;
2377-
for (Driver d : currentDrivers) {
2378-
try {
2379-
String clazzName = d.getClass().getName();
2380-
Driver driver = (Driver) Class.forName(clazzName, true,
2381-
Thread.currentThread().getContextClassLoader()).newInstance();
2382-
if (driver.acceptsURL(url) && isSupportedLocalDriver(driver)) {
2383-
return driver;
2384-
}
2385-
} catch (SQLException e) {
2386-
throw e;
2370+
for (Driver driver : currentDrivers) {
2371+
// The 'driver' is already an instance from the ServiceLoader.
2372+
// We can use it directly without creating a new one via reflection.
2373+
if (driver.acceptsURL(url) && isSupportedLocalDriver(driver)) {
2374+
return driver;
23872375
}
23882376
}
2389-
23902377
return null;
23912378
}
23922379

beeline/src/java/org/apache/hive/beeline/Commands.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.sql.Statement;
4545
import java.util.ArrayList;
4646
import java.util.Arrays;
47+
import java.util.Comparator;
4748
import java.util.HashMap;
4849
import java.util.Iterator;
4950
import java.util.LinkedList;
@@ -340,48 +341,48 @@ public boolean reconnect(String line) {
340341
return true;
341342
}
342343

343-
344344
public boolean scan(String line) throws IOException {
345-
TreeSet<String> names = new TreeSet<String>();
346-
347345
if (beeLine.getDrivers() == null) {
348346
beeLine.setDrivers(beeLine.scanDrivers());
349347
}
350348

351-
beeLine.info(beeLine.loc("drivers-found-count", beeLine.getDrivers().size()));
349+
// Use a TreeSet to get a unique, sorted list of drivers by class name.
350+
Set<Driver> drivers =
351+
new TreeSet<>(Comparator.comparing(d -> d.getClass().getName()));
352+
drivers.addAll(beeLine.getDrivers());
352353

353-
// unique the list
354-
for (Iterator<Driver> i = beeLine.getDrivers().iterator(); i.hasNext();) {
355-
names.add(i.next().getClass().getName());
356-
}
354+
// Get count of the unique driver in classpath
355+
beeLine.info(beeLine.loc("drivers-found-count", drivers.size()));
357356

358-
beeLine.output(beeLine.getColorBuffer()
359-
.bold(beeLine.getColorBuffer().pad(beeLine.loc("compliant"), 10).getMono())
360-
.bold(beeLine.getColorBuffer().pad(beeLine.loc("jdbc-version"), 8).getMono())
361-
.bold(beeLine.getColorBuffer(beeLine.loc("driver-class")).getMono()));
357+
beeLine.output(
358+
beeLine
359+
.getColorBuffer()
360+
.bold(beeLine.getColorBuffer().pad(beeLine.loc("compliant"), 10).getMono())
361+
.bold(beeLine.getColorBuffer().pad(beeLine.loc("jdbc-version"), 8).getMono())
362+
.bold(beeLine.getColorBuffer(beeLine.loc("driver-class")).getMono()));
362363

363-
for (Iterator<String> i = names.iterator(); i.hasNext();) {
364-
String name = i.next().toString();
364+
for (Driver driver : drivers) {
365+
String name = driver.getClass().getName();
365366
try {
366-
Driver driver = (Driver) Class.forName(name).newInstance();
367-
ColorBuffer msg = beeLine.getColorBuffer()
368-
.pad(driver.jdbcCompliant() ? "yes" : "no", 10)
369-
.pad(driver.getMajorVersion() + "."
370-
+ driver.getMinorVersion(), 8)
371-
.append(name);
367+
// Use the driver instance that ServiceLoader already created for us.
368+
ColorBuffer msg =
369+
beeLine
370+
.getColorBuffer()
371+
.pad(driver.jdbcCompliant() ? "yes" : "no", 10)
372+
.pad(driver.getMajorVersion() + "." + driver.getMinorVersion(), 8)
373+
.append(name);
372374
if (driver.jdbcCompliant()) {
373375
beeLine.output(msg);
374376
} else {
375377
beeLine.output(beeLine.getColorBuffer().red(msg.getMono()));
376378
}
377379
} catch (Throwable t) {
378-
beeLine.output(beeLine.getColorBuffer().red(name)); // error with driver
380+
beeLine.error("Error processing driver " + name);
379381
}
380382
}
381383
return true;
382384
}
383385

384-
385386
public boolean save(String line) throws IOException {
386387
beeLine.info(beeLine.loc("saving-options", beeLine.getOpts().getPropertiesFile()));
387388
beeLine.getOpts().save();
@@ -1670,7 +1671,7 @@ public boolean connect(Properties props) throws IOException {
16701671

16711672
try {
16721673
beeLine.getDatabaseConnections().setConnection(
1673-
new DatabaseConnection(beeLine, driver, url, props));
1674+
new DatabaseConnection(beeLine, url, props));
16741675
beeLine.getDatabaseConnection().getConnection();
16751676

16761677
if (!beeLine.isBeeLine()) {

beeline/src/java/org/apache/hive/beeline/DatabaseConnection.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ class DatabaseConnection {
4949
private final BeeLine beeLine;
5050
private Connection connection;
5151
private DatabaseMetaData meta;
52-
private final String driver;
5352
private final String url;
5453
private final Properties info;
5554
private Schema schema = null;
@@ -59,10 +58,8 @@ public boolean isClosed() {
5958
return (null == connection);
6059
}
6160

62-
public DatabaseConnection(BeeLine beeLine, String driver, String url,
63-
Properties info) throws SQLException {
61+
DatabaseConnection(BeeLine beeLine, String url, Properties info) {
6462
this.beeLine = beeLine;
65-
this.driver = driver;
6663
this.url = url;
6764
this.info = info;
6865
}
@@ -85,14 +82,6 @@ void setCompletions(boolean skipmeta) throws SQLException, IOException {
8582
* Connection to the specified data source.
8683
*/
8784
boolean connect() throws SQLException {
88-
try {
89-
if (driver != null && driver.length() != 0) {
90-
Class.forName(driver);
91-
}
92-
} catch (ClassNotFoundException cnfe) {
93-
return beeLine.error(cnfe);
94-
}
95-
9685
boolean isDriverRegistered = false;
9786
try {
9887
isDriverRegistered = DriverManager.getDriver(getUrl()) != null;
@@ -163,18 +152,19 @@ boolean connect() throws SQLException {
163152

164153
public Connection getConnectionFromLocalDriver(String url, Properties properties) {
165154
Collection<Driver> drivers = beeLine.getDrivers();
166-
for (Driver d : drivers) {
155+
for (Driver driver : drivers) {
167156
try {
168-
if (d.acceptsURL(url) && beeLine.isSupportedLocalDriver(d)) {
169-
String clazzName = d.getClass().getName();
170-
beeLine.debug("Driver name is " + clazzName);
171-
Driver driver =
172-
(Driver) Class.forName(clazzName, true, Thread.currentThread().getContextClassLoader())
173-
.newInstance();
157+
if (driver.acceptsURL(url) && beeLine.isSupportedLocalDriver(driver)) {
158+
beeLine.debug("Driver name is " + driver.getClass().getName());
159+
// The 'driver' is already an instance from the ServiceLoader, so we can use it directly.
174160
return driver.connect(url, properties);
175161
}
176-
} catch (Exception e) {
177-
beeLine.error("Fail to connect with a local driver due to the exception:" + e);
162+
} catch (SQLException e) {
163+
beeLine.error(
164+
"Failed to connect with local driver "
165+
+ driver.getClass().getName()
166+
+ " due to exception: "
167+
+ e);
178168
beeLine.error(e);
179169
}
180170
}

beeline/src/test/org/apache/hive/beeline/ProxyAuthTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
* <HS2host> <HS2Port> <HS2-Server-principal> <client-principal>
4545
*/
4646
public class ProxyAuthTest {
47-
private static final String driverName = "org.apache.hive.jdbc.HiveDriver";
4847
private static final String BEELINE_EXIT = "beeline.system.exit";
4948
private static Connection con = null;
5049
private static boolean noClose = false;
@@ -68,7 +67,6 @@ public static void main(String[] args) throws Exception {
6867
File currentResultFile = null;
6968
String [] beeLineArgs = {};
7069

71-
Class.forName(driverName);
7270
String host = args[0];
7371
String port = args[1];
7472
String serverPrincipal = args[2];

beeline/src/test/org/apache/hive/beeline/TestShutdownHook.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public void testShutdownHook() throws Exception {
3030
PrintStream ops = new PrintStream(os);
3131
BeeLine beeline = new BeeLine();
3232
DatabaseConnections dbConnections = beeline.getDatabaseConnections();
33-
dbConnections.setConnection(new DatabaseConnection(beeline,null,null, null));
34-
dbConnections.setConnection(new DatabaseConnection(beeline,null,null, null));
33+
dbConnections.setConnection(new DatabaseConnection(beeline, null, null));
34+
dbConnections.setConnection(new DatabaseConnection(beeline, null, null));
3535
Assert.assertEquals(2, dbConnections.size());
3636
beeline.setOutputStream(ops);
3737
beeline.getShutdownHook().run();
3838
Assert.assertEquals(0, dbConnections.size());
3939
}
40-
}
40+
}

hcatalog/webhcat/svr/src/main/java/org/apache/hive/hcatalog/templeton/tool/TempletonControllerJob.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,6 @@ private String buildHS2DelegationToken(String user) throws IOException, Interrup
218218
return real.doAs(new PrivilegedExceptionAction<String>() {
219219
@Override
220220
public String run() throws IOException, TException, InterruptedException {
221-
try {
222-
Class.forName("org.apache.hive.jdbc.HiveDriver");
223-
} catch (ClassNotFoundException e) {
224-
throw new IOException(e);
225-
}
226221
String hs2Url = appConf.get(AppConfig.HIVE_SERVER2_URL);
227222
final HiveConnection con;
228223
try {

hplsql/src/main/java/org/apache/hive/hplsql/Conn.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,11 @@ synchronized Connection getConnection(String connName) throws Exception {
146146
* @throws Exception
147147
*/
148148
Connection openConnection(String connStr) throws Exception {
149-
String driver = "org.apache.hadoop.hive.jdbc.HiveDriver";
150149
StringBuilder url = new StringBuilder();
151150
String usr = "";
152151
String pwd = "";
153152
if (connStr != null) {
154153
String[] c = connStr.split(";");
155-
if (c.length >= 1) {
156-
driver = c[0];
157-
}
158154
if (c.length >= 2) {
159155
url.append(c[1]);
160156
}
@@ -174,7 +170,6 @@ else if (pwd.isEmpty()) {
174170
}
175171
}
176172
}
177-
Class.forName(driver);
178173
timer.start();
179174
Connection conn = DriverManager.getConnection(url.toString().trim(), usr, pwd);
180175
timer.stop();

itests/hive-minikdc/src/test/java/org/apache/hive/minikdc/JdbcWithMiniKdcSQLAuthTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public static void beforeTestBase(String transportMode) throws Exception {
5151
hiveConf.setVar(ConfVars.HIVE_SERVER2_TRANSPORT_MODE, transportMode);
5252
System.err.println("Testing using HS2 mode:" + transportMode);
5353

54-
Class.forName(MiniHS2.getJdbcDriverName());
5554
hiveConf.setVar(ConfVars.HIVE_AUTHORIZATION_MANAGER,
5655
SQLStdHiveAuthorizerFactory.class.getName());
5756
hiveConf.setVar(ConfVars.HIVE_AUTHENTICATOR_MANAGER,

itests/hive-minikdc/src/test/java/org/apache/hive/minikdc/TestHS2AuthMechsWithMiniKdc.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public class TestHS2AuthMechsWithMiniKdc extends AbstractLdapTestUnit {
8787
@Before
8888
public void setUpBefore() throws Exception {
8989
if (miniHS2 == null) {
90-
Class.forName(MiniHS2.getJdbcDriverName());
9190
miniHiveKdc = new MiniHiveKdc();
9291
HiveConf hiveConf = new HiveConf();
9392
hiveConf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_ENABLE_DOAS, false);

itests/hive-minikdc/src/test/java/org/apache/hive/minikdc/TestHS2JWTWithMiniKdc.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public class TestHS2JWTWithMiniKdc {
7373

7474
@BeforeClass
7575
public static void setUpBeforeClass() throws Exception {
76-
Class.forName(MiniHS2.getJdbcDriverName());
7776
MOCK_JWKS_SERVER.stubFor(get("/jwks")
7877
.willReturn(ok()
7978
.withBody(Files.readAllBytes(jwtVerificationJWKSFile.toPath()))));

0 commit comments

Comments
 (0)