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 2023/10/10 01:47:29 UTC
[servicecomb-java-chassis] 04/25: [SCB-2008]allows not injectable property in model
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/servicecomb-java-chassis.git
commit f2c562ddf92d01905bf828735404e263f0cb40ea
Author: liubao <bi...@qq.com>
AuthorDate: Fri Sep 22 16:46:19 2023 +0800
[SCB-2008]allows not injectable property in model
---
.../config/ConfigurationChangedEvent.java | 97 ++++++++++++++++++++++
.../config/priority/ConfigObjectFactory.java | 9 +-
.../config/priority/PriorityPropertyManager.java | 2 +
.../config/inject/TestConfigObjectFactory.java | 29 +++++++
.../priority/TestPriorityPropertyManager.java | 9 +-
.../registry/sc/MicroserviceHandler.java | 3 +
6 files changed, 141 insertions(+), 8 deletions(-)
diff --git a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigurationChangedEvent.java b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigurationChangedEvent.java
new file mode 100644
index 000000000..ff56ae202
--- /dev/null
+++ b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigurationChangedEvent.java
@@ -0,0 +1,97 @@
+/*
+ * 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.servicecomb.config;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+public class ConfigurationChangedEvent {
+ private final Map<String, Object> added;
+
+ private final Map<String, Object> deleted;
+
+ private final Map<String, Object> updated;
+
+ private final boolean changed;
+
+ private Map<String, Object> complete;
+
+ private ConfigurationChangedEvent(Map<String, Object> added, Map<String, Object> updated,
+ Map<String, Object> deleted, boolean changed) {
+ this.added = added;
+ this.deleted = deleted;
+ this.updated = updated;
+ this.changed = changed;
+ }
+
+ public static ConfigurationChangedEvent createIncremental(Map<String, Object> latest, Map<String, Object> last) {
+ Map<String, Object> itemsCreated = new HashMap<>();
+ Map<String, Object> itemsDeleted = new HashMap<>();
+ Map<String, Object> itemsModified = new HashMap<>();
+ boolean changed = false;
+
+ for (Map.Entry<String, Object> entry : latest.entrySet()) {
+ String itemKey = entry.getKey();
+ if (!last.containsKey(itemKey)) {
+ itemsCreated.put(itemKey, entry.getValue());
+ changed = true;
+ } else if (!Objects.equals(last.get(itemKey), latest.get(itemKey))) {
+ itemsModified.put(itemKey, entry.getValue());
+ changed = true;
+ }
+ }
+ for (String itemKey : last.keySet()) {
+ if (!latest.containsKey(itemKey)) {
+ itemsDeleted.put(itemKey, null);
+ changed = true;
+ }
+ }
+ ConfigurationChangedEvent event = ConfigurationChangedEvent
+ .createIncremental(itemsCreated, itemsModified, itemsDeleted, changed);
+ event.complete = latest;
+ return event;
+ }
+
+ private static ConfigurationChangedEvent createIncremental(Map<String, Object> added, Map<String, Object> updated,
+ Map<String, Object> deleted, boolean changed) {
+ return new ConfigurationChangedEvent(added, updated, deleted, changed);
+ }
+
+ public final Map<String, Object> getAdded() {
+ return added;
+ }
+
+
+ public final Map<String, Object> getUpdated() {
+ return updated;
+ }
+
+
+ public final Map<String, Object> getDeleted() {
+ return deleted;
+ }
+
+ public final Map<String, Object> getComplete() {
+ return complete;
+ }
+
+ public final boolean isChanged() {
+ return changed;
+ }
+}
diff --git a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/priority/ConfigObjectFactory.java b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/priority/ConfigObjectFactory.java
index aa4248f7e..c5b9cf855 100644
--- a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/priority/ConfigObjectFactory.java
+++ b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/priority/ConfigObjectFactory.java
@@ -100,10 +100,13 @@ public class ConfigObjectFactory {
continue;
}
- Setter<Object, Object> setter = beanDescriptionCache.computeIfAbsent(propertyDefinition,
- LambdaMetafactoryUtils::createObjectSetter);
PriorityProperty<?> priorityProperty = createPriorityProperty(propertyDefinition.getField().getAnnotated(),
prefix, parameters);
+ if (priorityProperty == null) {
+ continue;
+ }
+ Setter<Object, Object> setter = beanDescriptionCache.computeIfAbsent(propertyDefinition,
+ LambdaMetafactoryUtils::createObjectSetter);
setter.set(instance, priorityProperty.getValue());
properties.add(new ConfigObjectProperty(setter, priorityProperty));
}
@@ -138,7 +141,7 @@ public class ConfigObjectFactory {
case "java.lang.Boolean":
return createBooleanProperty(field, keys, null);
default:
- throw new IllegalStateException("not support, field=" + field);
+ return null;
}
}
diff --git a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/priority/PriorityPropertyManager.java b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/priority/PriorityPropertyManager.java
index c1e47ac30..95b37a037 100644
--- a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/priority/PriorityPropertyManager.java
+++ b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/priority/PriorityPropertyManager.java
@@ -28,6 +28,7 @@ import org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.configuration.event.ConfigurationEvent;
import org.apache.commons.configuration.event.ConfigurationListener;
+import com.google.common.annotations.VisibleForTesting;
import com.netflix.config.ConfigurationManager;
public class PriorityPropertyManager {
@@ -70,6 +71,7 @@ public class PriorityPropertyManager {
}
}
+ @VisibleForTesting
public Map<Object, List<ConfigObjectProperty>> getConfigObjectMap() {
return configObjectMap;
}
diff --git a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/inject/TestConfigObjectFactory.java b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/inject/TestConfigObjectFactory.java
index b80bb60bf..ec9412898 100644
--- a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/inject/TestConfigObjectFactory.java
+++ b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/inject/TestConfigObjectFactory.java
@@ -423,4 +423,33 @@ public class TestConfigObjectFactory extends TestPriorityPropertyBase {
ArchaiusUtils.setProperty("override.low", null);
Assertions.assertNull(config.strValue);
}
+
+ @InjectProperties(prefix = "root")
+ public static class ConfigWithPojo {
+
+ @InjectProperties(prefix = "root.model")
+ public static class Model {
+ @InjectProperty(defaultValue = "h")
+ public String name;
+ }
+
+ @InjectProperty(defaultValue = "4")
+ public int index;
+
+ public Model model;
+ }
+
+ @Test
+ public void configWithPojoWork() {
+ ConfigWithPojo config = priorityPropertyManager.createConfigObject(ConfigWithPojo.class);
+ ConfigWithPojo.Model model = priorityPropertyManager.createConfigObject(ConfigWithPojo.Model.class);
+ config.model = model;
+
+ Assertions.assertEquals(4, config.index);
+ Assertions.assertEquals("h", config.model.name);
+ ArchaiusUtils.setProperty("root.index", "5");
+ ArchaiusUtils.setProperty("root.model.name", "w");
+ Assertions.assertEquals(5, config.index);
+ Assertions.assertEquals("w", config.model.name);
+ }
}
diff --git a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/priority/TestPriorityPropertyManager.java b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/priority/TestPriorityPropertyManager.java
index 294a506df..15e223b3e 100644
--- a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/priority/TestPriorityPropertyManager.java
+++ b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/priority/TestPriorityPropertyManager.java
@@ -20,7 +20,6 @@ package org.apache.servicecomb.config.priority;
import org.apache.servicecomb.config.ConfigUtil;
import org.apache.servicecomb.config.inject.InjectProperties;
import org.apache.servicecomb.config.inject.InjectProperty;
-import org.apache.servicecomb.config.inject.TestConfigObjectFactory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -37,7 +36,7 @@ public class TestPriorityPropertyManager extends TestPriorityPropertyBase {
@InjectProperties(prefix = "root")
public static class ConfigWithAnnotation {
- @InjectProperty(prefix = "override", keys = {"high", "low"})
+ @InjectProperty(prefix = "override", keys = {"high", "low"}, defaultValue = "abc")
public String strValue;
}
@@ -65,9 +64,9 @@ public class TestPriorityPropertyManager extends TestPriorityPropertyBase {
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
- TestConfigObjectFactory.ConfigWithAnnotation configConfigObject = priorityPropertyManager.createConfigObject(
- TestConfigObjectFactory.ConfigWithAnnotation.class);
- Assertions.assertEquals("abc", configConfigObject.strDef);
+ ConfigWithAnnotation configConfigObject = priorityPropertyManager.createConfigObject(
+ ConfigWithAnnotation.class);
+ Assertions.assertEquals("abc", configConfigObject.strValue);
PriorityProperty<Long> configPriorityProperty = propertyFactory.getOrCreate(Long.class, -1L, -2L, keys);
Assertions.assertEquals(-2L, (long) configPriorityProperty.getValue());
}
diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/registry/sc/MicroserviceHandler.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/registry/sc/MicroserviceHandler.java
index cdd21cdab..ec495c710 100644
--- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/registry/sc/MicroserviceHandler.java
+++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/registry/sc/MicroserviceHandler.java
@@ -107,6 +107,9 @@ public class MicroserviceHandler {
MicroserviceInstance microserviceInstance = new MicroserviceInstance();
String hostName = StringUtils.isEmpty(scConfigurationProperties.getHostname()) ? NetUtils.getHostName()
: scConfigurationProperties.getHostname();
+ if (hostName.length() > 64) {
+ hostName = hostName.substring(0, 64);
+ }
microserviceInstance.setHostName(hostName);
if (StringUtils.isNotEmpty(dataCenterProperties.getName())) {