CredentialProviderFactory refactor Change-Id: I460f715ec06656d869fdd48d0a30333cbc09c225
Project: http://git-wip-us.apache.org/repos/asf/oozie/repo Commit: http://git-wip-us.apache.org/repos/asf/oozie/commit/4d8549da Tree: http://git-wip-us.apache.org/repos/asf/oozie/tree/4d8549da Diff: http://git-wip-us.apache.org/repos/asf/oozie/diff/4d8549da Branch: refs/heads/oya Commit: 4d8549da7479e0de6a658e5f5710121275f13194 Parents: 8d5f9a7 Author: Gezapeti Cseh <[email protected]> Authored: Tue May 9 17:31:35 2017 -0700 Committer: Gezapeti Cseh <[email protected]> Committed: Tue May 9 18:44:39 2017 -0700 ---------------------------------------------------------------------- .../hadoop/CredentialsProviderFactory.java | 62 ++++++++++---------- .../oozie/action/hadoop/JavaActionExecutor.java | 4 +- 2 files changed, 34 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/oozie/blob/4d8549da/core/src/main/java/org/apache/oozie/action/hadoop/CredentialsProviderFactory.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/oozie/action/hadoop/CredentialsProviderFactory.java b/core/src/main/java/org/apache/oozie/action/hadoop/CredentialsProviderFactory.java index 43905b1..fc8c065 100644 --- a/core/src/main/java/org/apache/oozie/action/hadoop/CredentialsProviderFactory.java +++ b/core/src/main/java/org/apache/oozie/action/hadoop/CredentialsProviderFactory.java @@ -19,58 +19,60 @@ package org.apache.oozie.action.hadoop; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import org.apache.hadoop.security.UserGroupInformation; -import org.apache.hadoop.util.ReflectionUtils; import org.apache.oozie.service.ConfigurationService; import org.apache.oozie.util.XLog; public class CredentialsProviderFactory { - CredentialsProvider cred; - String type; public static final String CRED_KEY = "oozie.credentials.credentialclasses"; private static final XLog LOG = XLog.getLog(CredentialsProviderFactory.class); + private static final Map<String, Class<CredentialsProvider>> providerCache = new HashMap<>(); + private static CredentialsProviderFactory instance; - public CredentialsProviderFactory(String type) { - this.type = type; - this.cred = null; - LOG.debug("Credentials Provider is created for Type: {0}", type); + public static CredentialsProviderFactory getInstance() throws ClassNotFoundException { + if(instance == null) { + instance = new CredentialsProviderFactory(); + } + return instance; } - /** - * Create Credential object - * - * @return Credential object - * @throws Exception - */ - public CredentialsProvider createCredentialsProvider() throws Exception { - String type; - String classname; + private CredentialsProviderFactory() throws ClassNotFoundException { for (String function : ConfigurationService.getStrings(CRED_KEY)) { function = trim(function); LOG.debug("Creating Credential class for : " + function); String[] str = function.split("="); if (str.length > 0) { - type = str[0]; - classname = str[1]; + String type = str[0]; + String classname = str[1]; if (classname != null) { LOG.debug("Creating Credential type : '{0}', class Name : '{1}'", type, classname); - if (this.type.equalsIgnoreCase(str[0])) { - Class<?> klass = null; - try { - klass = Thread.currentThread().getContextClassLoader().loadClass(classname); - } - catch (ClassNotFoundException ex) { - LOG.warn("Exception while loading the class", ex); - throw ex; - } - - cred = (CredentialsProvider) ReflectionUtils.newInstance(klass, null); + Class<?> klass = null; + try { + klass = Thread.currentThread().getContextClassLoader().loadClass(classname); + } + catch (ClassNotFoundException ex) { + LOG.warn("Exception while loading the class '{0}'", classname, ex); + throw ex; } + providerCache.put(type, (Class<CredentialsProvider>) klass); + } else { + LOG.warn("Credential provider class is null for '{0}', skipping", type); } } } - return cred; + } + + /** + * Create Credential object + * + * @return Credential object + * @throws Exception + */ + public CredentialsProvider createCredentialsProvider(String type) throws Exception { + return providerCache.get(type).newInstance(); } /** http://git-wip-us.apache.org/repos/asf/oozie/blob/4d8549da/core/src/main/java/org/apache/oozie/action/hadoop/JavaActionExecutor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/oozie/action/hadoop/JavaActionExecutor.java b/core/src/main/java/org/apache/oozie/action/hadoop/JavaActionExecutor.java index 048b75c..980bd95 100644 --- a/core/src/main/java/org/apache/oozie/action/hadoop/JavaActionExecutor.java +++ b/core/src/main/java/org/apache/oozie/action/hadoop/JavaActionExecutor.java @@ -1188,8 +1188,8 @@ public class JavaActionExecutor extends ActionExecutor { String credName = entry.getKey(); CredentialsProperties credProps = entry.getValue(); if (credProps != null) { - CredentialsProviderFactory tokenProviderFactory = new CredentialsProviderFactory(credProps.getType()); - CredentialsProvider tokenProvider = tokenProviderFactory.createCredentialsProvider(); + CredentialsProviderFactory tokenProviderFactory = CredentialsProviderFactory.getInstance(); + CredentialsProvider tokenProvider = tokenProviderFactory.createCredentialsProvider(credProps.getType()); if (tokenProvider != null) { tokenProvider.updateCredentials(credentials, jobconf, credProps, context); LOG.debug("Retrieved Credential '" + credName + "' for action " + action.getId());
