accumulo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ktur...@apache.org
Subject [accumulo] 02/02: ACCUMULO-4769 simplify crypto module class instantiation
Date Thu, 08 Mar 2018 19:22:50 GMT
This is an automated email from the ASF dual-hosted git repository.

kturner pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/accumulo.git

commit dce8c359056905405abaea9b99efc21dadbc5920
Author: Keith Turner <kturner@apache.org>
AuthorDate: Thu Mar 8 13:47:08 2018 -0500

    ACCUMULO-4769 simplify crypto module class instantiation
---
 .../core/security/crypto/CryptoModuleFactory.java  | 74 ++++++----------------
 1 file changed, 18 insertions(+), 56 deletions(-)

diff --git a/core/src/main/java/org/apache/accumulo/core/security/crypto/CryptoModuleFactory.java
b/core/src/main/java/org/apache/accumulo/core/security/crypto/CryptoModuleFactory.java
index f69e5d1..556c621 100644
--- a/core/src/main/java/org/apache/accumulo/core/security/crypto/CryptoModuleFactory.java
+++ b/core/src/main/java/org/apache/accumulo/core/security/crypto/CryptoModuleFactory.java
@@ -74,38 +74,19 @@ public class CryptoModuleFactory {
   private static CryptoModule instantiateCryptoModule(String cryptoModuleClassname) {
     log.debug("About to instantiate crypto module {}", cryptoModuleClassname);
 
-    CryptoModule cryptoModule = null;
-    Class<?> cryptoModuleClazz = null;
     try {
-      cryptoModuleClazz = AccumuloVFSClassLoader.loadClass(cryptoModuleClassname);
-    } catch (ClassNotFoundException e1) {
-      throw new IllegalArgumentException("Could not find configured crypto module " + cryptoModuleClassname);
-    }
+      CryptoModule cryptoModule = AccumuloVFSClassLoader.loadClass(cryptoModuleClassname).asSubclass(CryptoModule.class).newInstance();
 
-    // Check if the given class implements the CryptoModule interface
-    Class<?>[] interfaces = cryptoModuleClazz.getInterfaces();
-    boolean implementsCryptoModule = false;
-
-    for (Class<?> clazz : interfaces) {
-      if (clazz.equals(CryptoModule.class)) {
-        implementsCryptoModule = true;
-        break;
-      }
-    }
+      log.debug("Successfully instantiated crypto module {}", cryptoModuleClassname);
 
-    if (!implementsCryptoModule) {
+      return cryptoModule;
+    } catch (ClassNotFoundException e1) {
+      throw new IllegalArgumentException("Could not find configured crypto module " + cryptoModuleClassname);
+    } catch (ClassCastException cce) {
       throw new IllegalArgumentException("Configured Accumulo crypto module " + cryptoModuleClassname
+ " does not implement the CryptoModule interface.");
-    } else {
-      try {
-        cryptoModule = (CryptoModule) cryptoModuleClazz.newInstance();
-
-        log.debug("Successfully instantiated crypto module {}", cryptoModuleClassname);
-
-      } catch (InstantiationException | IllegalAccessException e) {
-        throw new IllegalArgumentException("Unable to instantiate the crypto module: " +
cryptoModuleClassname, e);
-      }
+    } catch (InstantiationException | IllegalAccessException e) {
+      throw new IllegalArgumentException("Unable to instantiate the crypto module: " + cryptoModuleClassname,
e);
     }
-    return cryptoModule;
   }
 
   public static SecretKeyEncryptionStrategy getSecretKeyEncryptionStrategy(AccumuloConfiguration
conf) {
@@ -140,39 +121,20 @@ public class CryptoModuleFactory {
 
     log.debug("About to instantiate secret key encryption strategy {}", className);
 
-    SecretKeyEncryptionStrategy strategy = null;
-    Class<?> keyEncryptionStrategyClazz = null;
     try {
-      keyEncryptionStrategyClazz = AccumuloVFSClassLoader.loadClass(className);
-    } catch (ClassNotFoundException e1) {
-      throw new IllegalArgumentException("Could not find configured secret key encryption
strategy: " + className);
-    }
+      SecretKeyEncryptionStrategy strategy = AccumuloVFSClassLoader.loadClass(className).asSubclass(SecretKeyEncryptionStrategy.class).newInstance();
 
-    // Check if the given class implements the CryptoModule interface
-    Class<?>[] interfaces = keyEncryptionStrategyClazz.getInterfaces();
-    boolean implementsSecretKeyStrategy = false;
-
-    for (Class<?> clazz : interfaces) {
-      if (clazz.equals(SecretKeyEncryptionStrategy.class)) {
-        implementsSecretKeyStrategy = true;
-        break;
-      }
-    }
+      log.debug("Successfully instantiated secret key encryption strategy {}", className);
 
-    if (!implementsSecretKeyStrategy) {
-      throw new IllegalArgumentException(
-          "Configured Accumulo secret key encryption strategy \"%s\" does not implement the
SecretKeyEncryptionStrategy interface.");
-    } else {
-      try {
-        strategy = (SecretKeyEncryptionStrategy) keyEncryptionStrategyClazz.newInstance();
-
-        log.debug("Successfully instantiated secret key encryption strategy {}", className);
-
-      } catch (InstantiationException | IllegalAccessException e) {
-        throw new IllegalArgumentException("Unable to instantiate the secret key encryption
strategy: " + className, e);
-      }
+      return strategy;
+    } catch (ClassNotFoundException e1) {
+      throw new IllegalArgumentException("Could not find configured secret key encryption
strategy: " + className);
+    } catch (ClassCastException e) {
+      throw new IllegalArgumentException("Configured Accumulo secret key encryption strategy
\"" + className
+          + "\" does not implement the SecretKeyEncryptionStrategy interface.");
+    } catch (InstantiationException | IllegalAccessException e) {
+      throw new IllegalArgumentException("Unable to instantiate the secret key encryption
strategy: " + className, e);
     }
-    return strategy;
   }
 
   static class NullSecretKeyEncryptionStrategy implements SecretKeyEncryptionStrategy {

-- 
To stop receiving notification emails like this one, please contact
kturner@apache.org.

Mime
View raw message