kumaab commented on code in PR #442: URL: https://github.com/apache/ranger/pull/442#discussion_r1880831919
########## agents-common/src/main/java/org/apache/ranger/plugin/contextenricher/RangerAbstractContextEnricher.java: ########## @@ -40,298 +31,292 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Map; +import java.util.Properties; public abstract class RangerAbstractContextEnricher implements RangerContextEnricher { - private static final Logger LOG = LoggerFactory.getLogger(RangerAbstractContextEnricher.class); - - protected RangerContextEnricherDef enricherDef; - protected String serviceName; - protected String appId; - protected RangerServiceDef serviceDef; - private RangerPluginContext pluginContext; - protected RangerPolicyEngineOptions options = new RangerPolicyEngineOptions(); - - @Override - public void setEnricherDef(RangerContextEnricherDef enricherDef) { - this.enricherDef = enricherDef; - } - - @Override - public void setServiceName(String serviceName) { - this.serviceName = serviceName; - } - - @Override - public void setServiceDef(RangerServiceDef serviceDef) { - this.serviceDef = serviceDef; - } - - @Override - public void setAppId(String appId) { - this.appId = appId; - } - - @Override - public void init() { - if(LOG.isDebugEnabled()) { - LOG.debug("==> RangerAbstractContextEnricher.init(" + enricherDef + ")"); - } - - RangerAuthContext authContext = getAuthContext(); - - if (authContext != null) { - authContext.addOrReplaceRequestContextEnricher(this, null); - } else { - if (LOG.isDebugEnabled()) { - LOG.debug("authContext is null. This context-enricher is not added to authContext"); - } - } - - if(LOG.isDebugEnabled()) { - LOG.debug("<== RangerAbstractContextEnricher.init(" + enricherDef + ")"); - } - } - - @Override - public void enrich(RangerAccessRequest request, Object dataStore) { - enrich(request); - } - - @Override - public boolean preCleanup() { - if(LOG.isDebugEnabled()) { - LOG.debug("==> RangerAbstractContextEnricher.preCleanup(" + enricherDef + ")"); - } - - RangerAuthContext authContext = getAuthContext(); - - if (authContext != null) { - authContext.cleanupRequestContextEnricher(this); - } else { - if (LOG.isDebugEnabled()) { - LOG.debug("authContext is null. AuthContext need not be cleaned."); - } - } - - if(LOG.isDebugEnabled()) { - LOG.debug("<== RangerAbstractContextEnricher.preCleanup(" + enricherDef + ")"); - } - - return true; - } - - @Override - public void cleanup() { - preCleanup(); - } - - @Override - protected void finalize() throws Throwable { - try { - cleanup(); - } - finally { - super.finalize(); - } - } - - @Override - public String getName() { - return enricherDef == null ? null : enricherDef.getName(); - } - - public RangerContextEnricherDef getEnricherDef() { - return enricherDef; - } - - public String getServiceName() { - return serviceName; - } - - public RangerServiceDef getServiceDef() { - return serviceDef; - } - - public String getAppId() { - return appId; - } - - public String getOption(String name) { - String ret = null; - - Map<String, String> options = enricherDef != null ? enricherDef.getEnricherOptions() : null; - - if(options != null && name != null) { - ret = options.get(name); - } - - return ret; - } - - public RangerAuthContext getAuthContext() { - RangerPluginContext pluginContext = this.pluginContext; - - return pluginContext != null ? pluginContext.getAuthContext() : null; - } - - final public void setPluginContext(RangerPluginContext pluginContext) { - this.pluginContext = pluginContext; - } - - public RangerPluginContext getPluginContext() { - return this.pluginContext; - } - - final public void setPolicyEngineOptions(RangerPolicyEngineOptions options) { - this.options = options; - } + private static final Logger LOG = LoggerFactory.getLogger(RangerAbstractContextEnricher.class); + + protected RangerContextEnricherDef enricherDef; + protected String serviceName; + protected String appId; + protected RangerServiceDef serviceDef; + protected RangerPolicyEngineOptions options = new RangerPolicyEngineOptions(); + private RangerPluginContext pluginContext; + + public RangerContextEnricherDef getEnricherDef() { + return enricherDef; + } + + @Override + public void setEnricherDef(RangerContextEnricherDef enricherDef) { + this.enricherDef = enricherDef; + } + + public String getServiceName() { + return serviceName; + } + + @Override + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + } + + public RangerServiceDef getServiceDef() { + return serviceDef; + } + + @Override + public void setServiceDef(RangerServiceDef serviceDef) { + this.serviceDef = serviceDef; + } + + public String getAppId() { + return appId; + } + + @Override + public void setAppId(String appId) { + this.appId = appId; + } + + @Override + public void init() { + LOG.debug("==> RangerAbstractContextEnricher.init({})", enricherDef); + + RangerAuthContext authContext = getAuthContext(); + + if (authContext != null) { + authContext.addOrReplaceRequestContextEnricher(this, null); + } else { + LOG.debug("authContext is null. This context-enricher is not added to authContext"); + } + + LOG.debug("<== RangerAbstractContextEnricher.init({})", enricherDef); + } + + @Override + public void enrich(RangerAccessRequest request, Object dataStore) { + enrich(request); + } + + @Override + public boolean preCleanup() { + LOG.debug("==> RangerAbstractContextEnricher.preCleanup({})", enricherDef); + + RangerAuthContext authContext = getAuthContext(); + + if (authContext != null) { + authContext.cleanupRequestContextEnricher(this); + } else { + LOG.debug("authContext is null. AuthContext need not be cleaned."); + } - public RangerPluginConfig getPluginConfig() { - RangerPluginContext pluginContext = this.pluginContext; + LOG.debug("<== RangerAbstractContextEnricher.preCleanup({})", enricherDef); - return pluginContext != null ? pluginContext.getConfig() : null; - } + return true; + } - public RangerPolicyEngineOptions getPolicyEngineOptions() { - return options; - } + @Override + public void cleanup() { + preCleanup(); + } - public void notifyAuthContextChanged() { - RangerPluginContext pluginContext = this.pluginContext; + @Override + public String getName() { + return enricherDef == null ? null : enricherDef.getName(); + } - if (pluginContext != null) { - pluginContext.notifyAuthContextChanged(); - } - } - - public String getPropertyPrefix() { - RangerPluginConfig pluginConfig = getPluginConfig(); - - return pluginConfig != null ? pluginConfig.getPropertyPrefix() : "ranger.plugin." + serviceDef.getName(); - } + public String getOption(String name) { + String ret = null; - public String getConfig(String configName, String defaultValue) { - RangerPluginContext pluginContext = this.pluginContext; - String ret = defaultValue; - Configuration config = pluginContext != null ? pluginContext.getConfig() : null; + Map<String, String> options = enricherDef != null ? enricherDef.getEnricherOptions() : null; - if (config != null) { - ret = config.get(configName, defaultValue); - } + if (options != null && name != null) { + ret = options.get(name); + } - return ret; - } + return ret; + } - public int getIntConfig(String configName, int defaultValue) { - RangerPluginContext pluginContext = this.pluginContext; - int ret = defaultValue; - Configuration config = pluginContext != null ? pluginContext.getConfig() : null; + public RangerAuthContext getAuthContext() { + RangerPluginContext pluginContext = this.pluginContext; - if (config != null) { - ret = config.getInt(configName, defaultValue); - } + return pluginContext != null ? pluginContext.getAuthContext() : null; + } - return ret; - } + public RangerPluginContext getPluginContext() { + return this.pluginContext; + } - public boolean getBooleanConfig(String configName, boolean defaultValue) { - RangerPluginContext pluginContext = this.pluginContext; - boolean ret = defaultValue; - Configuration config = pluginContext != null ? pluginContext.getConfig() : null; + public final void setPluginContext(RangerPluginContext pluginContext) { + this.pluginContext = pluginContext; + } - if (config != null) { - ret = config.getBoolean(configName, defaultValue); - } + public RangerPluginConfig getPluginConfig() { + RangerPluginContext pluginContext = this.pluginContext; - return ret; - } + return pluginContext != null ? pluginContext.getConfig() : null; + } - public String getOption(String name, String defaultValue) { - String ret = defaultValue; - String val = getOption(name); + public RangerPolicyEngineOptions getPolicyEngineOptions() { + return options; + } - if(val != null) { - ret = val; - } + public final void setPolicyEngineOptions(RangerPolicyEngineOptions options) { + this.options = options; + } - return ret; - } + public void notifyAuthContextChanged() { + RangerPluginContext pluginContext = this.pluginContext; - public boolean getBooleanOption(String name, boolean defaultValue) { - boolean ret = defaultValue; - String val = getOption(name); + if (pluginContext != null) { + pluginContext.notifyAuthContextChanged(); + } + } - if(val != null) { - ret = Boolean.parseBoolean(val); - } + public String getPropertyPrefix() { + RangerPluginConfig pluginConfig = getPluginConfig(); - return ret; - } + return pluginConfig != null ? pluginConfig.getPropertyPrefix() : "ranger.plugin." + serviceDef.getName(); + } - public char getCharOption(String name, char defaultValue) { - char ret = defaultValue; - String val = getOption(name); + public String getConfig(String configName, String defaultValue) { + RangerPluginContext pluginContext = this.pluginContext; + String ret = defaultValue; + Configuration config = pluginContext != null ? pluginContext.getConfig() : null; - if(! StringUtils.isEmpty(val)) { - ret = val.charAt(0); - } + if (config != null) { + ret = config.get(configName, defaultValue); + } - return ret; - } + return ret; + } - public long getLongOption(String name, long defaultValue) { - long ret = defaultValue; - String val = getOption(name); + public int getIntConfig(String configName, int defaultValue) { + RangerPluginContext pluginContext = this.pluginContext; + int ret = defaultValue; + Configuration config = pluginContext != null ? pluginContext.getConfig() : null; - if(val != null) { - ret = Long.parseLong(val); - } + if (config != null) { + ret = config.getInt(configName, defaultValue); + } - return ret; - } + return ret; + } - public Properties readProperties(String fileName) { - Properties ret = null; - URL fileURL = null; - File f = new File(fileName); + public boolean getBooleanConfig(String configName, boolean defaultValue) { + RangerPluginContext pluginContext = this.pluginContext; + boolean ret = defaultValue; + Configuration config = pluginContext != null ? pluginContext.getConfig() : null; - if (f.exists() && f.isFile() && f.canRead()) { - try (InputStream inStr = new FileInputStream(f);) { - fileURL = f.toURI().toURL(); - } catch (FileNotFoundException exception) { - LOG.error("Error processing input file:" + fileName + " or no privilege for reading file " + fileName, exception); - } catch (IOException malformedException) { - LOG.error("Error processing input file:" + fileName + " cannot be converted to URL " + fileName, malformedException); - } - } else { - fileURL = getClass().getResource(fileName); + if (config != null) { + ret = config.getBoolean(configName, defaultValue); + } - if (fileURL == null && !fileName.startsWith("/")) { - fileURL = getClass().getResource("/" + fileName); - } + return ret; + } - if (fileURL == null) { - fileURL = ClassLoader.getSystemClassLoader().getResource(fileName); + public String getOption(String name, String defaultValue) { + String ret = defaultValue; + String val = getOption(name); - if (fileURL == null && !fileName.startsWith("/")) { - fileURL = ClassLoader.getSystemClassLoader().getResource("/" + fileName); - } - } - } + if (val != null) { + ret = val; + } - if (fileURL != null) { - try(InputStream inStr = fileURL.openStream()) { + return ret; + } - Properties prop = new Properties(); + public boolean getBooleanOption(String name, boolean defaultValue) { + boolean ret = defaultValue; + String val = getOption(name); - prop.load(inStr); + if (val != null) { + ret = Boolean.parseBoolean(val); + } - ret = prop; - } catch (Exception excp) { - LOG.error("failed to load properties from file '" + fileName + "'", excp); - } - } + return ret; + } - return ret; - } + public char getCharOption(String name, char defaultValue) { + char ret = defaultValue; + String val = getOption(name); + + if (!StringUtils.isEmpty(val)) { + ret = val.charAt(0); + } + + return ret; + } + + public long getLongOption(String name, long defaultValue) { Review Comment: for readability, please see above. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@ranger.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org