You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@accumulo.apache.org by ct...@apache.org on 2023/05/23 21:18:14 UTC

[accumulo] branch 2.1 updated: Pass config to ContextClassLoaderFactory (#3400)

This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/2.1 by this push:
     new 969fc52f1c Pass config to ContextClassLoaderFactory (#3400)
969fc52f1c is described below

commit 969fc52f1c5e0c195a8cef800a87e0fab13cd89c
Author: Ivan Bella <iv...@bella.name>
AuthorDate: Tue May 23 17:18:08 2023 -0400

    Pass config to ContextClassLoaderFactory (#3400)
    
    * Added a context class loader environment that supplies configuration
    * Improve javadoc comments on environment interface
    
    This fixes #3399
    
    Co-authored-by: Christopher Tubbs <ct...@apache.org>
    Co-authored-by: Keith Turner <kt...@apache.org>
---
 .../accumulo/core/classloader/ClassLoaderUtil.java |  4 ++-
 .../spi/common/ContextClassLoaderEnvironment.java  | 34 ++++++++++++++++++++++
 .../core/spi/common/ContextClassLoaderFactory.java | 12 ++++++--
 .../accumulo/core/util/ConfigurationImpl.java      |  3 ++
 4 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/core/src/main/java/org/apache/accumulo/core/classloader/ClassLoaderUtil.java b/core/src/main/java/org/apache/accumulo/core/classloader/ClassLoaderUtil.java
index 47e7ef584a..3a5d0ebda3 100644
--- a/core/src/main/java/org/apache/accumulo/core/classloader/ClassLoaderUtil.java
+++ b/core/src/main/java/org/apache/accumulo/core/classloader/ClassLoaderUtil.java
@@ -21,6 +21,7 @@ package org.apache.accumulo.core.classloader;
 import org.apache.accumulo.core.conf.AccumuloConfiguration;
 import org.apache.accumulo.core.conf.Property;
 import org.apache.accumulo.core.spi.common.ContextClassLoaderFactory;
+import org.apache.accumulo.core.util.ConfigurationImpl;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -46,11 +47,12 @@ public class ClassLoaderUtil {
             ContextClassLoaderFactory.class.getName());
         FACTORY = new DefaultContextClassLoaderFactory(conf);
       } else {
-        // load user's selected implementation
+        // load user's selected implementation and provide it with the service environment
         try {
           var factoryClass = Class.forName(factoryName).asSubclass(ContextClassLoaderFactory.class);
           LOG.info("Creating {}: {}", ContextClassLoaderFactory.class.getName(), factoryName);
           FACTORY = factoryClass.getDeclaredConstructor().newInstance();
+          FACTORY.init(() -> new ConfigurationImpl(conf));
         } catch (ReflectiveOperationException e) {
           throw new IllegalStateException("Unable to load and initialize class: " + factoryName, e);
         }
diff --git a/core/src/main/java/org/apache/accumulo/core/spi/common/ContextClassLoaderEnvironment.java b/core/src/main/java/org/apache/accumulo/core/spi/common/ContextClassLoaderEnvironment.java
new file mode 100644
index 0000000000..328165a952
--- /dev/null
+++ b/core/src/main/java/org/apache/accumulo/core/spi/common/ContextClassLoaderEnvironment.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.accumulo.core.spi.common;
+
+/**
+ * The environment provided to the context class loader factory for its use
+ *
+ * @since 2.1.1
+ */
+public interface ContextClassLoaderEnvironment {
+
+  /**
+   * Get the service environment configuration
+   *
+   * @return The configuration
+   */
+  ServiceEnvironment.Configuration getConfiguration();
+}
diff --git a/core/src/main/java/org/apache/accumulo/core/spi/common/ContextClassLoaderFactory.java b/core/src/main/java/org/apache/accumulo/core/spi/common/ContextClassLoaderFactory.java
index e099049bf5..7f423f3ead 100644
--- a/core/src/main/java/org/apache/accumulo/core/spi/common/ContextClassLoaderFactory.java
+++ b/core/src/main/java/org/apache/accumulo/core/spi/common/ContextClassLoaderFactory.java
@@ -39,12 +39,21 @@ package org.apache.accumulo.core.spi.common;
  * <p>
  * Because this factory is expected to be instantiated early in the application startup process,
  * configuration is expected to be provided within the environment (such as in Java system
- * properties or process environment variables), and is implementation-specific.
+ * properties or process environment variables), and is implementation-specific. However, some
+ * limited environment is also available so implementations can have access to Accumulo's own system
+ * configuration.
  *
  * @since 2.1.0
  */
 public interface ContextClassLoaderFactory {
 
+  /**
+   * Pass the service environment to allow for additional class loader configuration
+   *
+   * @param env the class loader environment
+   */
+  default void init(ContextClassLoaderEnvironment env) {}
+
   /**
    * Get the class loader for the given contextName. Callers should not cache the ClassLoader result
    * as it may change if/when the ClassLoader reloads. Implementations should throw a
@@ -56,5 +65,4 @@ public interface ContextClassLoaderFactory {
    * @return the class loader for the given contextName
    */
   ClassLoader getClassLoader(String contextName);
-
 }
diff --git a/core/src/main/java/org/apache/accumulo/core/util/ConfigurationImpl.java b/core/src/main/java/org/apache/accumulo/core/util/ConfigurationImpl.java
index 72be9ac9b8..6bb2015618 100644
--- a/core/src/main/java/org/apache/accumulo/core/util/ConfigurationImpl.java
+++ b/core/src/main/java/org/apache/accumulo/core/util/ConfigurationImpl.java
@@ -32,6 +32,9 @@ import org.apache.accumulo.core.conf.Property;
 import org.apache.accumulo.core.conf.PropertyType;
 import org.apache.accumulo.core.spi.common.ServiceEnvironment.Configuration;
 
+/**
+ * The implementation class used for providing SPI configuration without exposing internal types.
+ */
 public class ConfigurationImpl implements Configuration {
 
   private final AccumuloConfiguration acfg;