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

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


The following commit(s) were added to refs/heads/main by this push:
     new 240fcf2  Add method to get properties with a prefix (#1701)
240fcf2 is described below

commit 240fcf208868e9f16cf7b988ede68a2a78313d82
Author: Joseph Koshakow <jkos...@users.noreply.github.com>
AuthorDate: Sun Sep 6 23:07:06 2020 -0400

    Add method to get properties with a prefix (#1701)
    
    This change adds a method to the PluginEnvironment to give system plugins
    the ability to retrieve all configuration properties starting with a given 
prefix.
    
    This closes #1627
---
 .../accumulo/core/client/PluginEnvironment.java    | 10 +++
 .../accumulo/server/ServiceEnvironmentImpl.java    | 14 ++++
 .../server/ServiceEnvironmentImplTest.java         | 79 ++++++++++++++++++++++
 3 files changed, 103 insertions(+)

diff --git 
a/core/src/main/java/org/apache/accumulo/core/client/PluginEnvironment.java 
b/core/src/main/java/org/apache/accumulo/core/client/PluginEnvironment.java
index f58d29a..c73b178 100644
--- a/core/src/main/java/org/apache/accumulo/core/client/PluginEnvironment.java
+++ b/core/src/main/java/org/apache/accumulo/core/client/PluginEnvironment.java
@@ -56,6 +56,16 @@ public interface PluginEnvironment {
     String get(String key);
 
     /**
+     * Returns all properties with a given prefix
+     *
+     * @param prefix
+     *          prefix of properties to be returned. Include the trailing '.' 
in the prefix.
+     * @return all properties with a given prefix
+     * @since 2.1.0
+     */
+    Map<String,String> getWithPrefix(String prefix);
+
+    /**
      * Users can set arbitrary custom properties in Accumulo using the prefix
      * {@code general.custom.}. This method will return all properties with 
that prefix, stripping
      * the prefix. For example, assume the following properties were set :
diff --git 
a/server/base/src/main/java/org/apache/accumulo/server/ServiceEnvironmentImpl.java
 
b/server/base/src/main/java/org/apache/accumulo/server/ServiceEnvironmentImpl.java
index cb69a83..b6f48ba 100644
--- 
a/server/base/src/main/java/org/apache/accumulo/server/ServiceEnvironmentImpl.java
+++ 
b/server/base/src/main/java/org/apache/accumulo/server/ServiceEnvironmentImpl.java
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.stream.Collectors;
 import java.util.stream.StreamSupport;
 
 import org.apache.accumulo.core.client.TableNotFoundException;
@@ -29,6 +30,7 @@ import org.apache.accumulo.core.clientImpl.Tables;
 import org.apache.accumulo.core.conf.AccumuloConfiguration;
 import org.apache.accumulo.core.conf.ConfigurationTypeHelper;
 import org.apache.accumulo.core.conf.Property;
+import org.apache.accumulo.core.conf.PropertyType;
 import org.apache.accumulo.core.data.TableId;
 import org.apache.accumulo.core.spi.common.ServiceEnvironment;
 
@@ -79,6 +81,18 @@ public class ServiceEnvironmentImpl implements 
ServiceEnvironment {
     }
 
     @Override
+    public Map<String,String> getWithPrefix(String prefix) {
+      Property propertyPrefix = Property.getPropertyByKey(prefix);
+      if (propertyPrefix != null && propertyPrefix.getType() == 
PropertyType.PREFIX) {
+        return acfg.getAllPropertiesWithPrefix(propertyPrefix);
+      } else {
+        return StreamSupport.stream(acfg.spliterator(), false)
+            .filter(prop -> prop.getKey().startsWith(prefix))
+            .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
+      }
+    }
+
+    @Override
     public Map<String,String> getCustom() {
       if (customProps == null) {
         customProps = buildCustom(Property.GENERAL_ARBITRARY_PROP_PREFIX);
diff --git 
a/server/base/src/test/java/org/apache/accumulo/server/ServiceEnvironmentImplTest.java
 
b/server/base/src/test/java/org/apache/accumulo/server/ServiceEnvironmentImplTest.java
new file mode 100644
index 0000000..942e7d0
--- /dev/null
+++ 
b/server/base/src/test/java/org/apache/accumulo/server/ServiceEnvironmentImplTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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
+ *
+ *   http://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.server;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+import static org.junit.Assert.assertEquals;
+
+import java.util.Map;
+
+import org.apache.accumulo.core.conf.AccumuloConfiguration;
+import org.apache.accumulo.core.conf.Property;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ServiceEnvironmentImplTest {
+  private ServerContext srvCtx;
+  private AccumuloConfiguration acfg;
+  private ServiceEnvironmentImpl serviceEnvironment;
+
+  @Before
+  public void setUp() {
+    srvCtx = createMock(ServerContext.class);
+    acfg = createMock(AccumuloConfiguration.class);
+    expect(srvCtx.getConfiguration()).andReturn(acfg);
+    replay(srvCtx);
+    serviceEnvironment = new ServiceEnvironmentImpl(srvCtx);
+  }
+
+  @After
+  public void verifyMocks() {
+    verify(srvCtx, acfg);
+  }
+
+  @Test
+  public void getWithRecognizedPrefixTest() {
+    String prefix = Property.RPC_PREFIX.getKey();
+    Map<String,String> expectedPropertyMap = 
Map.of("rpc.javax.net.ssl.keyStoreType", "jks");
+    
expect(acfg.getAllPropertiesWithPrefix(Property.RPC_PREFIX)).andReturn(expectedPropertyMap);
+    replay(acfg);
+
+    Map<String,String> returnedProperties =
+        serviceEnvironment.getConfiguration().getWithPrefix(prefix);
+
+    assertEquals(expectedPropertyMap, returnedProperties);
+  }
+
+  @Test
+  public void getWithUnrecognizedPrefixTest() {
+    String prefix = "a.b";
+    Map<String,String> expectedPropertyMap = Map.of("a.b.favorite.license", 
"apache");
+    
expect(acfg.spliterator()).andReturn(expectedPropertyMap.entrySet().spliterator());
+    replay(acfg);
+
+    Map<String,String> returnedProperties =
+        serviceEnvironment.getConfiguration().getWithPrefix(prefix);
+
+    assertEquals(expectedPropertyMap, returnedProperties);
+  }
+}

Reply via email to