From 79f16c235ddec441e7aee53ad8533b2770c65f50 Mon Sep 17 00:00:00 2001 From: Mercy Ma Date: Thu, 9 May 2024 23:14:52 +0800 Subject: [PATCH] Add microsphere-i18n-spring-cloud module --- ...eloadableResourceServiceMessageSource.java | 22 ++++ .../src/test/resources/application.properties | 2 + microsphere-i18n-spring-cloud/pom.xml | 103 ++++++++++++++++++ .../I18nCloudAutoConfiguration.java | 44 ++++++++ ...eResourceServiceMessageSourceListener.java | 75 +++++++++++++ .../main/resources/META-INF/spring.factories | 2 + ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../i18n/spring/cloud/I18nBootstrap.java | 40 +++++++ .../i18n/common/i18n_messages_en.properties | 1 + .../common/i18n_messages_zh_CN.properties | 1 + .../src/test/resources/application.properties | 3 + pom.xml | 1 + 12 files changed, 295 insertions(+) create mode 100644 microsphere-i18n-spring-boot/src/test/resources/application.properties create mode 100644 microsphere-i18n-spring-cloud/pom.xml create mode 100644 microsphere-i18n-spring-cloud/src/main/java/io/microsphere/i18n/spring/cloud/autoconfigure/I18nCloudAutoConfiguration.java create mode 100644 microsphere-i18n-spring-cloud/src/main/java/io/microsphere/i18n/spring/cloud/event/ReloadableResourceServiceMessageSourceListener.java create mode 100644 microsphere-i18n-spring-cloud/src/main/resources/META-INF/spring.factories create mode 100644 microsphere-i18n-spring-cloud/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports create mode 100644 microsphere-i18n-spring-cloud/src/test/java/io/microsphere/i18n/spring/cloud/I18nBootstrap.java create mode 100644 microsphere-i18n-spring-cloud/src/test/resources/META-INF/i18n/common/i18n_messages_en.properties create mode 100644 microsphere-i18n-spring-cloud/src/test/resources/META-INF/i18n/common/i18n_messages_zh_CN.properties create mode 100644 microsphere-i18n-spring-cloud/src/test/resources/application.properties diff --git a/microsphere-i18n-core/src/main/java/io/microsphere/i18n/ReloadableResourceServiceMessageSource.java b/microsphere-i18n-core/src/main/java/io/microsphere/i18n/ReloadableResourceServiceMessageSource.java index e37a008..ee6121e 100644 --- a/microsphere-i18n-core/src/main/java/io/microsphere/i18n/ReloadableResourceServiceMessageSource.java +++ b/microsphere-i18n-core/src/main/java/io/microsphere/i18n/ReloadableResourceServiceMessageSource.java @@ -10,14 +10,36 @@ */ public interface ReloadableResourceServiceMessageSource extends ResourceServiceMessageSource { + /** + * Reload if {@link #canReload(String)} returns true, + * The calling {@link #initializeResource(String)} as default + * + * @param changedResource Changes in the resource + */ + default void reload(String changedResource) { + initializeResource(changedResource); + } + /** * Reload if {@link #canReload(Iterable)} returns true, * The calling {@link #initializeResources(Iterable)} as default + * @param changedResources Changes in the resources */ default void reload(Iterable changedResources) { initializeResources(changedResources); } + /** + * Whether the specified resource can be overloaded + * + * @param changedResource Changes in the resource + * @return Supported by default, returning true + */ + default boolean canReload(String changedResource) { + Set resources = getInitializeResources(); + return resources.contains(changedResource); + } + /** * Whether the specified resource list can be overloaded * diff --git a/microsphere-i18n-spring-boot/src/test/resources/application.properties b/microsphere-i18n-spring-boot/src/test/resources/application.properties new file mode 100644 index 0000000..38eb5c4 --- /dev/null +++ b/microsphere-i18n-spring-boot/src/test/resources/application.properties @@ -0,0 +1,2 @@ +management.endpoint.i18n.enabled=true +management.endpoints.web.exposure.include=* \ No newline at end of file diff --git a/microsphere-i18n-spring-cloud/pom.xml b/microsphere-i18n-spring-cloud/pom.xml new file mode 100644 index 0000000..cc5989d --- /dev/null +++ b/microsphere-i18n-spring-cloud/pom.xml @@ -0,0 +1,103 @@ + + + + io.github.microsphere-projects + microsphere-i18n-parent + ${revision} + ../microsphere-i18n-parent/pom.xml + + 4.0.0 + + io.github.microsphere-projects + microsphere-i18n-spring-cloud + ${revision} + jar + + Microsphere :: Internationalisation :: Spring Cloud + Microsphere Internationalisation Spring Cloud + + + + + + io.github.microsphere-projects + microsphere-i18n-spring-boot + ${revision} + + + + + org.springframework.cloud + spring-cloud-commons + true + + + + org.springframework.cloud + spring-cloud-context + true + + + + + org.springframework.boot + spring-boot-autoconfigure + true + + + + org.springframework.boot + spring-boot-starter-web + true + + + + + org.springframework.boot + spring-boot-actuator-autoconfigure + true + + + + + javax.servlet + javax.servlet-api + provided + + + + + org.slf4j + slf4j-api + true + + + + + junit + junit + test + + + + org.springframework + spring-test + test + + + + org.springframework.boot + spring-boot-test + test + + + + ch.qos.logback + logback-classic + test + + + + \ No newline at end of file diff --git a/microsphere-i18n-spring-cloud/src/main/java/io/microsphere/i18n/spring/cloud/autoconfigure/I18nCloudAutoConfiguration.java b/microsphere-i18n-spring-cloud/src/main/java/io/microsphere/i18n/spring/cloud/autoconfigure/I18nCloudAutoConfiguration.java new file mode 100644 index 0000000..479a9a2 --- /dev/null +++ b/microsphere-i18n-spring-cloud/src/main/java/io/microsphere/i18n/spring/cloud/autoconfigure/I18nCloudAutoConfiguration.java @@ -0,0 +1,44 @@ +/* + * 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.microsphere.i18n.spring.cloud.autoconfigure; + +import io.microsphere.i18n.spring.annotation.EnableI18n; +import io.microsphere.i18n.spring.beans.factory.ServiceMessageSourceFactoryBean; +import io.microsphere.i18n.spring.boot.condition.ConditionalOnI18nEnabled; +import io.microsphere.i18n.spring.cloud.event.ReloadableResourceServiceMessageSourceListener; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; + +/** + * I18n Auto-Configuration for Spring Cloud + * + * @author Mercy + * @since 1.0.0 + */ +@ConditionalOnI18nEnabled +@ConditionalOnClass(name = { + "org.springframework.cloud.context.environment.EnvironmentChangeEvent", // spring-cloud-context +}) +@Import({ + ReloadableResourceServiceMessageSourceListener.class +}) +public class I18nCloudAutoConfiguration { + +} diff --git a/microsphere-i18n-spring-cloud/src/main/java/io/microsphere/i18n/spring/cloud/event/ReloadableResourceServiceMessageSourceListener.java b/microsphere-i18n-spring-cloud/src/main/java/io/microsphere/i18n/spring/cloud/event/ReloadableResourceServiceMessageSourceListener.java new file mode 100644 index 0000000..4c88f43 --- /dev/null +++ b/microsphere-i18n-spring-cloud/src/main/java/io/microsphere/i18n/spring/cloud/event/ReloadableResourceServiceMessageSourceListener.java @@ -0,0 +1,75 @@ +/* + * 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.microsphere.i18n.spring.cloud.event; + +import io.microsphere.i18n.ReloadableResourceServiceMessageSource; +import io.microsphere.i18n.ServiceMessageSource; +import io.microsphere.i18n.spring.DelegatingServiceMessageSource; +import io.microsphere.i18n.spring.PropertySourcesServiceMessageSource; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; +import org.springframework.beans.factory.SmartInitializingSingleton; +import org.springframework.cloud.context.environment.EnvironmentChangeEvent; +import org.springframework.context.ApplicationListener; + +import java.util.Set; + +import static io.microsphere.i18n.spring.constants.I18nConstants.SERVICE_MESSAGE_SOURCE_BEAN_NAME; + +/** + * * An {@link ApplicationListener} of {@link EnvironmentChangeEvent} to reload + * {@link ServiceMessageSource} dynamically at the runtime. + * + * @author Mercy + * @see PropertySourcesServiceMessageSource + * @see DelegatingServiceMessageSource + * @see ReloadableResourceServiceMessageSource + * @see ServiceMessageSource + * @see EnvironmentChangeEvent + * @since 1.0.0 + */ +public class ReloadableResourceServiceMessageSourceListener implements SmartInitializingSingleton, + ApplicationListener, BeanFactoryAware { + + private BeanFactory beanFactory; + + private ReloadableResourceServiceMessageSource reloadableResourceServiceMessageSource; + + @Override + public void onApplicationEvent(EnvironmentChangeEvent event) { + Set changedPropertyNames = event.getKeys(); + for (String changedPropertyName : changedPropertyNames) { + String resource = changedPropertyName; + if (reloadableResourceServiceMessageSource.canReload(resource)) { + reloadableResourceServiceMessageSource.reload(resource); + } + } + } + + @Override + public void afterSingletonsInstantiated() { + // Lookup the primary bean of PropertySourcesServiceMessageSource + this.reloadableResourceServiceMessageSource = this.beanFactory.getBean(SERVICE_MESSAGE_SOURCE_BEAN_NAME, + ReloadableResourceServiceMessageSource.class); + } + + @Override + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + this.beanFactory = beanFactory; + } +} diff --git a/microsphere-i18n-spring-cloud/src/main/resources/META-INF/spring.factories b/microsphere-i18n-spring-cloud/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..7b7ac77 --- /dev/null +++ b/microsphere-i18n-spring-cloud/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +io.microsphere.i18n.spring.cloud.autoconfigure.I18nCloudAutoConfiguration \ No newline at end of file diff --git a/microsphere-i18n-spring-cloud/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/microsphere-i18n-spring-cloud/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..ea8db45 --- /dev/null +++ b/microsphere-i18n-spring-cloud/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +io.microsphere.i18n.spring.cloud.autoconfigure.I18nCloudAutoConfiguration \ No newline at end of file diff --git a/microsphere-i18n-spring-cloud/src/test/java/io/microsphere/i18n/spring/cloud/I18nBootstrap.java b/microsphere-i18n-spring-cloud/src/test/java/io/microsphere/i18n/spring/cloud/I18nBootstrap.java new file mode 100644 index 0000000..800f2ab --- /dev/null +++ b/microsphere-i18n-spring-cloud/src/test/java/io/microsphere/i18n/spring/cloud/I18nBootstrap.java @@ -0,0 +1,40 @@ +/* + * 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.microsphere.i18n.spring.cloud; + +import io.microsphere.i18n.spring.beans.factory.ServiceMessageSourceFactoryBean; +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.annotation.Bean; + +/** + * I18n Bootstrap + * + * @author Mercy + * @since TODO + */ +@EnableAutoConfiguration +public class I18nBootstrap { + + public static void main(String[] args) { + new SpringApplicationBuilder(I18nBootstrap.class) + .web(WebApplicationType.SERVLET) + .properties("common.i18n_messages_en.properties=common.a = common-a-2024") + .run(args); + } +} diff --git a/microsphere-i18n-spring-cloud/src/test/resources/META-INF/i18n/common/i18n_messages_en.properties b/microsphere-i18n-spring-cloud/src/test/resources/META-INF/i18n/common/i18n_messages_en.properties new file mode 100644 index 0000000..ecc8d4c --- /dev/null +++ b/microsphere-i18n-spring-cloud/src/test/resources/META-INF/i18n/common/i18n_messages_en.properties @@ -0,0 +1 @@ +common.a = a \ No newline at end of file diff --git a/microsphere-i18n-spring-cloud/src/test/resources/META-INF/i18n/common/i18n_messages_zh_CN.properties b/microsphere-i18n-spring-cloud/src/test/resources/META-INF/i18n/common/i18n_messages_zh_CN.properties new file mode 100644 index 0000000..6aadb97 --- /dev/null +++ b/microsphere-i18n-spring-cloud/src/test/resources/META-INF/i18n/common/i18n_messages_zh_CN.properties @@ -0,0 +1 @@ +common.a = 啊 \ No newline at end of file diff --git a/microsphere-i18n-spring-cloud/src/test/resources/application.properties b/microsphere-i18n-spring-cloud/src/test/resources/application.properties new file mode 100644 index 0000000..be2f681 --- /dev/null +++ b/microsphere-i18n-spring-cloud/src/test/resources/application.properties @@ -0,0 +1,3 @@ +management.endpoint.i18n.enabled=true +management.endpoints.web.exposure.include=* +management.endpoint.env.post.enabled=true \ No newline at end of file diff --git a/pom.xml b/pom.xml index 037f8d0..0f1f1ff 100644 --- a/pom.xml +++ b/pom.xml @@ -62,6 +62,7 @@ microsphere-i18n-openfeign microsphere-i18n-spring microsphere-i18n-spring-boot + microsphere-i18n-spring-cloud