You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/01/10 06:29:21 UTC

[incubator-servicecomb-java-chassis] 07/07: SCB-36 use enum to store configuration actions

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

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit cd6baf12c56f90f0a4879fc897684884817e4944
Author: lijasonvip <li...@huawei.com>
AuthorDate: Wed Jan 10 10:30:44 2018 +0800

    SCB-36 use enum to store configuration actions
    
    Signed-off-by: lijasonvip <li...@huawei.com>
---
 dynamic-config/config-apollo/pom.xml               |  4 +++
 .../sources/ApolloConfigurationSourceImpl.java     | 34 +++++++++++-----------
 .../io/servicecomb/config/client/ApolloClient.java | 16 ++++++----
 .../config/client/ConfigurationAction.java         | 24 +++++++++++++++
 .../sources/ApolloConfigurationSourceImplTest.java | 10 +++++--
 5 files changed, 62 insertions(+), 26 deletions(-)

diff --git a/dynamic-config/config-apollo/pom.xml b/dynamic-config/config-apollo/pom.xml
index 16c278b..1f08254 100644
--- a/dynamic-config/config-apollo/pom.xml
+++ b/dynamic-config/config-apollo/pom.xml
@@ -40,5 +40,9 @@
       <groupId>org.springframework</groupId>
       <artifactId>spring-web</artifactId>
     </dependency>
+    <dependency>
+      <groupId>javax.validation</groupId>
+      <artifactId>validation-api</artifactId>
+    </dependency>
   </dependencies>
 </project>
\ No newline at end of file
diff --git a/dynamic-config/config-apollo/src/main/java/io/servicecomb/config/archaius/sources/ApolloConfigurationSourceImpl.java b/dynamic-config/config-apollo/src/main/java/io/servicecomb/config/archaius/sources/ApolloConfigurationSourceImpl.java
index 37b79f4..b4d6b4e 100644
--- a/dynamic-config/config-apollo/src/main/java/io/servicecomb/config/archaius/sources/ApolloConfigurationSourceImpl.java
+++ b/dynamic-config/config-apollo/src/main/java/io/servicecomb/config/archaius/sources/ApolloConfigurationSourceImpl.java
@@ -18,25 +18,29 @@
 package io.servicecomb.config.archaius.sources;
 
 import static com.netflix.config.WatchedUpdateResult.createIncremental;
+import static io.servicecomb.config.client.ConfigurationAction.CREATE;
+import static io.servicecomb.config.client.ConfigurationAction.DELETE;
+import static io.servicecomb.config.client.ConfigurationAction.SET;
 
 import java.util.List;
 import java.util.Map;
-
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CopyOnWriteArrayList;
 
+import javax.validation.constraints.NotNull;
+
 import org.apache.commons.configuration.Configuration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.collect.ImmutableMap;
-import com.netflix.config.ConcurrentCompositeConfiguration;
 import com.netflix.config.WatchedUpdateListener;
 import com.netflix.config.WatchedUpdateResult;
 
 import io.servicecomb.config.ConfigMapping;
 import io.servicecomb.config.client.ApolloClient;
 import io.servicecomb.config.client.ApolloConfig;
+import io.servicecomb.config.client.ConfigurationAction;
 import io.servicecomb.config.spi.ConfigCenterConfigurationSource;
 
 public class ApolloConfigurationSourceImpl implements ConfigCenterConfigurationSource {
@@ -53,7 +57,7 @@ public class ApolloConfigurationSourceImpl implements ConfigCenterConfigurationS
 
   @Override
   public void init(Configuration localConfiguration) {
-    ApolloConfig.setConcurrentCompositeConfiguration((ConcurrentCompositeConfiguration) localConfiguration);
+    ApolloConfig.setConcurrentCompositeConfiguration(localConfiguration);
     init();
   }
 
@@ -63,17 +67,13 @@ public class ApolloConfigurationSourceImpl implements ConfigCenterConfigurationS
   }
 
   @Override
-  public void addUpdateListener(WatchedUpdateListener watchedUpdateListener) {
-    if (watchedUpdateListener != null) {
-      listeners.add(watchedUpdateListener);
-    }
+  public void addUpdateListener(@NotNull WatchedUpdateListener watchedUpdateListener) {
+    listeners.add(watchedUpdateListener);
   }
 
   @Override
-  public void removeUpdateListener(WatchedUpdateListener watchedUpdateListener) {
-    if (watchedUpdateListener != null) {
-      listeners.remove(watchedUpdateListener);
-    }
+  public void removeUpdateListener(@NotNull WatchedUpdateListener watchedUpdateListener) {
+    listeners.remove(watchedUpdateListener);
   }
 
   private void updateConfiguration(WatchedUpdateResult result) {
@@ -96,23 +96,23 @@ public class ApolloConfigurationSourceImpl implements ConfigCenterConfigurationS
   }
 
   public class UpdateHandler {
-    public void handle(String action, Map<String, Object> config) {
+    public void handle(ConfigurationAction action, Map<String, Object> config) {
       if (config == null || config.isEmpty()) {
         return;
       }
       Map<String, Object> configuration = ConfigMapping.getConvertedMap(config);
-      if ("create".equals(action)) {
+      if (CREATE.equals(action)) {
         valueCache.putAll(configuration);
 
         updateConfiguration(createIncremental(ImmutableMap.copyOf(configuration),
             null,
             null));
-      } else if ("set".equals(action)) {
+      } else if (SET.equals(action)) {
         valueCache.putAll(configuration);
 
         updateConfiguration(createIncremental(null, ImmutableMap.copyOf(configuration),
             null));
-      } else if ("delete".equals(action)) {
+      } else if (DELETE.equals(action)) {
         for (String itemKey : configuration.keySet()) {
           valueCache.remove(itemKey);
         }
@@ -120,10 +120,10 @@ public class ApolloConfigurationSourceImpl implements ConfigCenterConfigurationS
             null,
             ImmutableMap.copyOf(configuration)));
       } else {
-        LOGGER.error("action: {} is invalid.", action);
+        LOGGER.error("action: {} is invalid.", action.name());
         return;
       }
-      LOGGER.warn("Config value cache changed: action:{}; item:{}", action, configuration.keySet());
+      LOGGER.warn("Config value cache changed: action:{}; item:{}", action.name(), configuration.keySet());
     }
   }
 }
diff --git a/dynamic-config/config-apollo/src/main/java/io/servicecomb/config/client/ApolloClient.java b/dynamic-config/config-apollo/src/main/java/io/servicecomb/config/client/ApolloClient.java
index 065c724..fcf70df 100644
--- a/dynamic-config/config-apollo/src/main/java/io/servicecomb/config/client/ApolloClient.java
+++ b/dynamic-config/config-apollo/src/main/java/io/servicecomb/config/client/ApolloClient.java
@@ -17,6 +17,10 @@
 
 package io.servicecomb.config.client;
 
+import static io.servicecomb.config.client.ConfigurationAction.CREATE;
+import static io.servicecomb.config.client.ConfigurationAction.DELETE;
+import static io.servicecomb.config.client.ConfigurationAction.SET;
+
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
@@ -79,7 +83,7 @@ public class ApolloClient {
   }
 
   class ConfigRefresh implements Runnable {
-    private String serviceUri;
+    private final String serviceUri;
 
     ConfigRefresh(String serviceUris) {
       this.serviceUri = serviceUris;
@@ -136,11 +140,11 @@ public class ApolloClient {
       Map<String, Object> itemsDeleted = new HashMap<>();
       Map<String, Object> itemsModified = new HashMap<>();
       if (before == null || before.isEmpty()) {
-        updateHandler.handle("create", after);
+        updateHandler.handle(CREATE, after);
         return;
       }
       if (after == null || after.isEmpty()) {
-        updateHandler.handle("delete", before);
+        updateHandler.handle(DELETE, before);
         return;
       }
       for (String itemKey : after.keySet()) {
@@ -155,9 +159,9 @@ public class ApolloClient {
           itemsDeleted.put(itemKey, "");
         }
       }
-      updateHandler.handle("create", itemsCreated);
-      updateHandler.handle("set", itemsModified);
-      updateHandler.handle("delete", itemsDeleted);
+      updateHandler.handle(CREATE, itemsCreated);
+      updateHandler.handle(SET, itemsModified);
+      updateHandler.handle(DELETE, itemsDeleted);
     }
   }
 }
diff --git a/dynamic-config/config-apollo/src/main/java/io/servicecomb/config/client/ConfigurationAction.java b/dynamic-config/config-apollo/src/main/java/io/servicecomb/config/client/ConfigurationAction.java
new file mode 100644
index 0000000..62552a1
--- /dev/null
+++ b/dynamic-config/config-apollo/src/main/java/io/servicecomb/config/client/ConfigurationAction.java
@@ -0,0 +1,24 @@
+/*
+ * 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 io.servicecomb.config.client;
+
+public enum ConfigurationAction {
+  CREATE,
+  SET,
+  DELETE
+}
diff --git a/dynamic-config/config-apollo/src/test/java/io/servicecomb/config/archaius/sources/ApolloConfigurationSourceImplTest.java b/dynamic-config/config-apollo/src/test/java/io/servicecomb/config/archaius/sources/ApolloConfigurationSourceImplTest.java
index 83d7614..848cdd1 100644
--- a/dynamic-config/config-apollo/src/test/java/io/servicecomb/config/archaius/sources/ApolloConfigurationSourceImplTest.java
+++ b/dynamic-config/config-apollo/src/test/java/io/servicecomb/config/archaius/sources/ApolloConfigurationSourceImplTest.java
@@ -17,6 +17,10 @@
 
 package io.servicecomb.config.archaius.sources;
 
+import static io.servicecomb.config.client.ConfigurationAction.CREATE;
+import static io.servicecomb.config.client.ConfigurationAction.DELETE;
+import static io.servicecomb.config.client.ConfigurationAction.SET;
+
 import java.util.HashMap;
 import java.util.Map;
 
@@ -38,7 +42,7 @@ public class ApolloConfigurationSourceImplTest {
     UpdateHandler udateHandler = Deencapsulation.getField(apolloConfigurationSource, UpdateHandler.class);
     Map<String, Object> createItems = new HashMap<>();
     createItems.put("testKey", "testValue");
-    udateHandler.handle("create", createItems);
+    udateHandler.handle(CREATE, createItems);
   }
 
   @Test
@@ -49,7 +53,7 @@ public class ApolloConfigurationSourceImplTest {
     UpdateHandler udateHandler = Deencapsulation.getField(apolloConfigurationSource, UpdateHandler.class);
     Map<String, Object> updateItems = new HashMap<>();
     updateItems.put("testKey", "testValue");
-    udateHandler.handle("set", updateItems);
+    udateHandler.handle(SET, updateItems);
   }
 
   @Test
@@ -61,7 +65,7 @@ public class ApolloConfigurationSourceImplTest {
     deleteItems.put("testKey", "testValue");
 
     apolloConfigurationSource.getCurrentData().put("testKey", "testValue");
-    udateHandler.handle("delete", deleteItems);
+    udateHandler.handle(DELETE, deleteItems);
     Assert.assertTrue(apolloConfigurationSource.getCurrentData().isEmpty());
   }
 

-- 
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.