Skip to content

Commit

Permalink
Add WritableOperation
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed May 17, 2024
1 parent 900df56 commit 91130f2
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

Expand Down Expand Up @@ -42,7 +43,12 @@ protected final Map<String, String> loadMessages(String resource) {
return messages == null ? emptyMap() : unmodifiableMap(messages);
}

private Properties loadAllProperties(String resource) throws IOException {
public Properties loadAllProperties(Locale locale) throws IOException {
String resource = getResource(locale);
return loadAllProperties(resource);
}

public Properties loadAllProperties(String resource) throws IOException {
List<Reader> propertiesResources = loadAllPropertiesResources(resource);
logger.debug("Source '{}' loads {} Properties Resources['{}']", source, propertiesResources.size(), resource);
if (isEmpty(propertiesResources)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,22 @@
import io.microsphere.i18n.AbstractResourceServiceMessageSource;
import io.microsphere.i18n.ServiceMessageSource;
import io.microsphere.i18n.spring.DelegatingServiceMessageSource;
import io.microsphere.i18n.spring.PropertySourcesServiceMessageSource;
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 org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.cglib.core.Local;

import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySources;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand All @@ -34,6 +43,8 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;

import static io.microsphere.i18n.spring.constants.I18nConstants.SERVICE_MESSAGE_SOURCE_BEAN_NAME;
Expand Down Expand Up @@ -69,8 +80,13 @@
@Endpoint(id = "i18n")
public class I18nEndpoint {

public static final String PROPERTY_SOURCE_NAME = "i18nEndpointPropertySource";

private List<ServiceMessageSource> serviceMessageSources;

@Autowired
private ConfigurableEnvironment environment;

@Autowired
@Qualifier(SERVICE_MESSAGE_SOURCE_BEAN_NAME)
public void initServiceMessageSources(ServiceMessageSource serviceMessageSource) {
Expand Down Expand Up @@ -147,6 +163,59 @@ public List<Map<String, String>> getMessage(@Selector String code, @Selector Loc
return messageMaps;
}

@WriteOperation
public Map<String, Object> addMessage(String source, Locale locale, String code, String message) throws IOException {
PropertySourcesServiceMessageSource serviceMessageSource = getPropertySourcesServiceMessageSource(source);
Properties properties = loadProperties(serviceMessageSource, locale);
// Add a new code with message
properties.setProperty(code, message);

String propertyName = serviceMessageSource.getPropertyName(locale);
StringWriter stringWriter = new StringWriter();
// Properties -> StringWriter
properties.store(stringWriter, "");
// StringWriter -> String
String propertyValue = stringWriter.toString();

MapPropertySource propertySource = getPropertySource();
Map<String, Object> newProperties = propertySource.getSource();
newProperties.put(propertyName, propertyValue);

serviceMessageSource.init();
return newProperties;
}

private Properties loadProperties(PropertySourcesServiceMessageSource serviceMessageSource, Locale locale) throws IOException {
Properties properties = serviceMessageSource.loadAllProperties(locale);
return properties == null ? new Properties() : properties;
}

private MapPropertySource getPropertySource() {
MutablePropertySources propertySources = environment.getPropertySources();
String name = PROPERTY_SOURCE_NAME;
MapPropertySource propertySource = (MapPropertySource) propertySources.get(name);
if (propertySource == null) {
Map<String, Object> properties = new HashMap<>();
propertySource = new MapPropertySource(name, properties);
propertySources.addFirst(propertySource);
}
return propertySource;
}

private PropertySourcesServiceMessageSource getPropertySourcesServiceMessageSource(String source) {
return serviceMessageSources.stream()
.filter(serviceMessageSource ->
Objects.equals(source, serviceMessageSource.getSource()))
.filter(this::isPropertySourcesServiceMessageSource)
.map(PropertySourcesServiceMessageSource.class::cast)
.findFirst()
.get();
}

private boolean isPropertySourcesServiceMessageSource(ServiceMessageSource serviceMessageSource) {
return serviceMessageSource instanceof PropertySourcesServiceMessageSource;
}

private String getResource(ServiceMessageSource serviceMessageSource, Locale locale) {
String resource = null;
if (serviceMessageSource instanceof AbstractResourceServiceMessageSource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,27 @@ protected Locale getInternalLocale() {

@Override
protected List<Reader> loadAllPropertiesResources(String resource) throws IOException {
String propertyName = resource;
String propertiesContent = environment.getProperty(propertyName);
String propertiesContent = getPropertiesContent(resource);
return hasText(propertiesContent) ? asList(new StringReader(propertiesContent)) : emptyList();
}

protected String getPropertiesContent(String resource) {
String propertyName = getPropertyName(resource);
String propertiesContent = environment.getProperty(propertyName);
return propertiesContent;
}

public String getPropertyName(Locale locale) {
String resource = getResource(locale);
String propertyName = getPropertyName(resource);
return propertyName;
}

protected String getPropertyName(String resource) {
String propertyName = resource;
return propertyName;
}

@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
Expand Down

0 comments on commit 91130f2

Please sign in to comment.