From 59afbbf114c57d88e93a2ef29cee32e625ad0d95 Mon Sep 17 00:00:00 2001 From: Mike Drob Date: Tue, 6 Feb 2018 14:56:17 -0600 Subject: [PATCH] HBASE-19920 Lazy init for ProtobufUtil classloader --- .../apache/hadoop/hbase/protobuf/ProtobufUtil.java | 22 ++++++++++++---------- .../hadoop/hbase/shaded/protobuf/ProtobufUtil.java | 18 ++++++++++-------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/protobuf/ProtobufUtil.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/protobuf/ProtobufUtil.java index 29ff2a2a61..b9ee06ae00 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/protobuf/ProtobufUtil.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/protobuf/ProtobufUtil.java @@ -169,14 +169,16 @@ public final class ProtobufUtil { } /** - * Dynamic class loader to load filter/comparators - */ - private final static ClassLoader CLASS_LOADER; + * Dynamic class loader to load filter/comparators + */ + private final static class ClassLoaderHolder { + private final static ClassLoader CLASS_LOADER; - static { - ClassLoader parent = ProtobufUtil.class.getClassLoader(); - Configuration conf = HBaseConfiguration.create(); - CLASS_LOADER = new DynamicClassLoader(conf, parent); + static { + ClassLoader parent = ProtobufUtil.class.getClassLoader(); + Configuration conf = HBaseConfiguration.create(); + CLASS_LOADER = new DynamicClassLoader(conf, parent); + } } /** @@ -1432,7 +1434,7 @@ public final class ProtobufUtil { byte [] value = proto.getSerializedComparator().toByteArray(); try { Class c = - (Class)Class.forName(type, true, CLASS_LOADER); + (Class)Class.forName(type, true, ClassLoaderHolder.CLASS_LOADER); Method parseFrom = c.getMethod(funcName, byte[].class); if (parseFrom == null) { throw new IOException("Unable to locate function: " + funcName + " in type: " + type); @@ -1456,7 +1458,7 @@ public final class ProtobufUtil { String funcName = "parseFrom"; try { Class c = - (Class)Class.forName(type, true, CLASS_LOADER); + (Class)Class.forName(type, true, ClassLoaderHolder.CLASS_LOADER); Method parseFrom = c.getMethod(funcName, byte[].class); if (parseFrom == null) { throw new IOException("Unable to locate function: " + funcName + " in type: " + type); @@ -1542,7 +1544,7 @@ public final class ProtobufUtil { String type = parameter.getName(); try { Class c = - (Class)Class.forName(type, true, CLASS_LOADER); + (Class)Class.forName(type, true, ClassLoaderHolder.CLASS_LOADER); Constructor cn = null; try { cn = c.getDeclaredConstructor(String.class); diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/shaded/protobuf/ProtobufUtil.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/shaded/protobuf/ProtobufUtil.java index 5bb3b4ba04..248ec8c2b7 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/shaded/protobuf/ProtobufUtil.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/shaded/protobuf/ProtobufUtil.java @@ -248,12 +248,14 @@ public final class ProtobufUtil { /** * Dynamic class loader to load filter/comparators */ - private final static ClassLoader CLASS_LOADER; + private final static class ClassLoaderHolder { + private final static ClassLoader CLASS_LOADER; - static { - ClassLoader parent = ProtobufUtil.class.getClassLoader(); - Configuration conf = HBaseConfiguration.create(); - CLASS_LOADER = new DynamicClassLoader(conf, parent); + static { + ClassLoader parent = ProtobufUtil.class.getClassLoader(); + Configuration conf = HBaseConfiguration.create(); + CLASS_LOADER = new DynamicClassLoader(conf, parent); + } } /** @@ -1589,7 +1591,7 @@ public final class ProtobufUtil { byte [] value = proto.getSerializedComparator().toByteArray(); try { Class c = - (Class)Class.forName(type, true, CLASS_LOADER); + (Class)Class.forName(type, true, ClassLoaderHolder.CLASS_LOADER); Method parseFrom = c.getMethod(funcName, byte[].class); if (parseFrom == null) { throw new IOException("Unable to locate function: " + funcName + " in type: " + type); @@ -1613,7 +1615,7 @@ public final class ProtobufUtil { String funcName = "parseFrom"; try { Class c = - (Class)Class.forName(type, true, CLASS_LOADER); + (Class)Class.forName(type, true, ClassLoaderHolder.CLASS_LOADER); Method parseFrom = c.getMethod(funcName, byte[].class); if (parseFrom == null) { throw new IOException("Unable to locate function: " + funcName + " in type: " + type); @@ -1699,7 +1701,7 @@ public final class ProtobufUtil { String type = parameter.getName(); try { Class c = - (Class)Class.forName(type, true, CLASS_LOADER); + (Class)Class.forName(type, true, ClassLoaderHolder.CLASS_LOADER); Constructor cn = null; try { cn = c.getDeclaredConstructor(String.class); -- 2.15.1