Skip to content

Commit

Permalink
Temp Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed Apr 21, 2024
1 parent 063309d commit efc4292
Show file tree
Hide file tree
Showing 14 changed files with 276 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private String getResourceName(Locale locale) {

protected abstract Map<String, String> loadMessages(Locale locale, String resource);

protected final Map<String, String> getMessages(Locale locale) {
public final Map<String, String> getMessages(Locale locale) {
return localizedMessages.getOrDefault(locale, emptyMap());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ public Charset getEncoding() {
return ReloadableResourceServiceMessageSource.super.getEncoding();
}

/**
* Get the read-only list of the composited {@link ServiceMessageSource}
*
* @return non-null
*/
@Nonnull
public List<ServiceMessageSource> getServiceMessageSources() {
return unmodifiableList(serviceMessageSources);
}

@Override
public void destroy() {
List<? extends ServiceMessageSource> serviceMessageSources = this.serviceMessageSources;
Expand Down
27 changes: 26 additions & 1 deletion microsphere-i18n-spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,33 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<optional>true</optional>
</dependency>

<!-- Spring Boot Actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
<optional>true</optional>
</dependency>

<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>

<!-- Bean Validation -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<optional>true</optional>
</dependency>

<!-- slf4j API -->
<dependency>
<groupId>org.slf4j</groupId>
Expand All @@ -61,12 +81,17 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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.boot.actuate;

import io.microsphere.i18n.AbstractResourceServiceMessageSource;
import io.microsphere.i18n.CompositeServiceMessageSource;
import io.microsphere.i18n.ServiceMessageSource;
import io.microsphere.i18n.spring.DelegatingServiceMessageSource;
import io.microsphere.i18n.spring.beans.factory.ServiceMessageSourceFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;

import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import static io.microsphere.i18n.spring.constants.I18nConstants.SERVICE_MESSAGE_SOURCE_BEAN_NAME;
import static java.util.Collections.emptyList;
import static org.springframework.util.CollectionUtils.isEmpty;

/**
* I18n Spring Boot Actuator Endpoint
* <prev>
* {
* "common" : {
* "zh_CN" : {
* "error.a" : "a"
* }
* },
* "test" : {
* "test.i18n_messages_zh_CN.properties" : {
*
* },
* "META-INF/i18n/test/i18n_messages_en.properties":{
* "test.a" : "test-a"
* "test.hello" : "Hello,{}"
* }
* ...
* }
* }
* </prev>
*
* @author <a href="mailto:[email protected]">Mercy</a>
* @since 1.0.0
*/
@Endpoint(id = "i18n")
public class I18nEndpoint {

private List<ServiceMessageSource> serviceMessageSources;

@Autowired
@Qualifier(SERVICE_MESSAGE_SOURCE_BEAN_NAME)
public void initServiceMessageSources(ServiceMessageSource serviceMessageSource) {
List<ServiceMessageSource> serviceMessageSources = emptyList();
if (serviceMessageSource instanceof DelegatingServiceMessageSource) {
DelegatingServiceMessageSource delegatingServiceMessageSource = (DelegatingServiceMessageSource) serviceMessageSource;
serviceMessageSources = delegatingServiceMessageSource.getDelegate().getServiceMessageSources();
}
this.serviceMessageSources = serviceMessageSources;
}

@ReadOperation
public Map<String, Object> invoke() {
List<ServiceMessageSource> serviceMessageSources = this.serviceMessageSources;
int size = serviceMessageSources.size();
Map<String, Object> result = new HashMap<>(size);
for (int i = 0; i < size; i++) {
ServiceMessageSource serviceMessageSource = serviceMessageSources.get(i);
if (serviceMessageSource instanceof CompositeServiceMessageSource) {
CompositeServiceMessageSource compositeServiceMessageSource = (CompositeServiceMessageSource) serviceMessageSource;
for (ServiceMessageSource sms : compositeServiceMessageSource.getServiceMessageSources()) {
if (sms instanceof AbstractResourceServiceMessageSource) {
AbstractResourceServiceMessageSource resourceServiceMessageSource = (AbstractResourceServiceMessageSource) sms;
String source = serviceMessageSource.getSource();
List<Locale> supportedLocales = resourceServiceMessageSource.getSupportedLocales();
Map<String, Map<String, String>> localizedMessages = new HashMap<>(supportedLocales.size());


for (Locale supportedLocale : supportedLocales) {
Map<String, String> messages = resourceServiceMessageSource.getMessages(supportedLocale);
if (!isEmpty(messages)) {
localizedMessages.put(supportedLocale.toString(), messages);
}
}

result.put(source, localizedMessages);
}
}
}

}
return result;
}

private AbstractResourceServiceMessageSource getResourceServiceMessageSource(ServiceMessageSource serviceMessageSource) {
if (serviceMessageSource instanceof ServiceMessageSourceFactoryBean) {
ServiceMessageSourceFactoryBean smffb = (ServiceMessageSourceFactoryBean) serviceMessageSource;
CompositeServiceMessageSource compositeServiceMessageSource = smffb.getDelegate();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.boot.actuate.autoconfigure;

import io.microsphere.i18n.spring.boot.actuate.I18nEndpoint;
import io.microsphere.i18n.spring.boot.autoconfigure.I18nAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;

/**
* I18n Spring Boot Actuator Endpoint Auto-Configuration
*
* @author <a href="mailto:[email protected]">Mercy</a>
* @see I18nEndpoint
* @since 1.0.0
*/
@ConditionalOnClass(name = {
"io.microsphere.i18n.ServiceMessageSource", // microsphere-i18n-core
"io.microsphere.i18n.spring.context.I18nConfiguration", // microsphere-i18n-spring
"org.springframework.boot.actuate.endpoint.annotation.Endpoint", // spring-boot-actuator-autoconfigure
})
@ConditionalOnAvailableEndpoint(endpoint = I18nEndpoint.class)
@AutoConfigureAfter(I18nAutoConfiguration.class)
public class I18nEndpointAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public I18nEndpoint i18nEndpoint() {
return new I18nEndpoint();
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
io.microsphere.i18n.spring.boot.autoconfigure.I18nAutoConfiguration
io.microsphere.i18n.spring.boot.autoconfigure.I18nAutoConfiguration,\
io.microsphere.i18n.spring.boot.actuate.autoconfigure.I18nEndpointAutoConfiguration
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
io.microsphere.i18n.spring.boot.autoconfigure.I18nAutoConfiguration
io.microsphere.i18n.spring.boot.autoconfigure.I18nAutoConfiguration
io.microsphere.i18n.spring.boot.actuate.autoconfigure.I18nEndpointAutoConfiguration
Original file line number Diff line number Diff line change
@@ -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.boot.actuate.autoconfigure;

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;

/**
* TODO Comment
*
* @author <a href="mailto:[email protected]">Mercy</a>
* @since TODO
*/
@EnableAutoConfiguration
public class I18nEndpointAutoConfigurationBootstrap {

public static void main(String[] args) {
new SpringApplicationBuilder(I18nEndpointAutoConfigurationBootstrap.class)
.web(WebApplicationType.SERVLET)
.run(args);
}

@Bean
public static ServiceMessageSourceFactoryBean testServiceMessageSource() {
return new ServiceMessageSourceFactoryBean("test");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
common.a = a
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test.a = test-a
test.hello = Hello,{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test.a = 测试-a
test.hello = 您好,{}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ public String toString() {
return "ServiceMessageSources{" + "delegate=" + delegate + '}';
}

/**
* Get the {@link CompositeServiceMessageSource}
*
* @return the {@link CompositeServiceMessageSource}
*/
public CompositeServiceMessageSource getDelegate() {
return delegate;
}

private List<ServiceMessageSource> getServiceMessageSourceBeans() {
List<ServiceMessageSource> serviceMessageSources = new LinkedList<>();
serviceMessageSourcesProvider.forEach(serviceMessageSources::add);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,8 @@ public void onApplicationEvent(ResourceServiceMessageSourceChangedEvent event) {
}
}
}

public CompositeServiceMessageSource getDelegate() {
return delegate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ io.microsphere.i18n.AbstractServiceMessageSource=\
io.microsphere.i18n.spring.PropertySourcesServiceMessageSource,\
io.microsphere.i18n.DefaultServiceMessageSource

org.springframework.context.ApplicationContextInitializer=\
io.microsphere.i18n.spring.context.I18nInitializer
#org.springframework.context.ApplicationContextInitializer=\
#io.microsphere.i18n.spring.context.I18nInitializer

0 comments on commit efc4292

Please sign in to comment.