Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
yanhom1314 committed Nov 17, 2024
1 parent 98f20fb commit 40e4ceb
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ private DynamicTpConst() { }

public static final String UNKNOWN = "---";

public static final String VALUE = "value";

public static final String TRACE_ID = "traceId";

public static final String GLOBAL_CONFIG_PREFIX = MAIN_PROPERTIES_PREFIX + ".globalExecutorProps.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* Interface for managing context in the application.
* Provides methods to access beans, set context, handle events,
* and retrieve environment properties and profiles.
* and retrieve environment properties.
*
* @author vzer200
* @since 1.2.0
Expand Down Expand Up @@ -89,18 +89,4 @@ public interface ContextManager {
* @return the value of the property, or the default value if not found
*/
String getEnvironmentProperty(String key, String defaultValue);

/**
* Retrieves the active profiles.
*
* @return an array of active profile names
*/
String[] getActiveProfiles();

/**
* Retrieves the default profiles.
*
* @return an array of default profile names
*/
String[] getDefaultProfiles();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.Map;

/**
* Helper class for accessing ContextManager and publishing events.
* Helper class for accessing ContextManager.
*
* @author vzer200
* @since 1.2.0
Expand Down Expand Up @@ -68,13 +68,5 @@ public static String getEnvironmentProperty(String key, Object environment) {
public static String getEnvironmentProperty(String key, String defaultValue) {
return contextManager.getEnvironmentProperty(key, defaultValue);
}

public static String[] getActiveProfiles() {
return contextManager.getActiveProfiles();
}

public static String[] getDefaultProfiles() {
return contextManager.getDefaultProfiles();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* NullContextManager related
*
* @author yanhom
* @since 1.1.0
* @since 1.2.0
*/
public class NullContextManager implements ContextManager {

Expand Down Expand Up @@ -61,14 +61,4 @@ public String getEnvironmentProperty(String key, Object environment) {
public String getEnvironmentProperty(String key, String defaultValue) {
throw new UnsupportedOperationException();
}

@Override
public String[] getActiveProfiles() {
throw new UnsupportedOperationException();
}

@Override
public String[] getDefaultProfiles() {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* BeanCopierUtils related
*
* @author vzer200
* @since 1.1.8
* @since 1.2.0
*/
@UtilityClass
public class BeanCopierUtil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,8 @@ private CommonUtil() {

String env = DtpProperties.getInstance().getEnv();
if (StringUtils.isBlank(env)) {
// fix #I8SSGQ
env = ContextManagerHelper.getEnvironmentProperty(APP_ENV_KEY);
}
if (StringUtils.isBlank(env)) {
String[] profiles = ContextManagerHelper.getActiveProfiles();
if (profiles.length < 1) {
profiles = ContextManagerHelper.getDefaultProfiles();
}
if (profiles.length >= 1) {
env = profiles[0];
}
}

String appName = ContextManagerHelper.getEnvironmentProperty(APP_NAME_KEY);
String portStr = ContextManagerHelper.getEnvironmentProperty(APP_PORT_KEY);
int port = StringUtils.isNotBlank(portStr) ? Integer.parseInt(portStr) : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* DtpPropertiesBinderUtil related
*
* @author yanhom
* @since 1.1.0
* @since 1.1.9
*/
@SuppressWarnings("unchecked")
public final class DtpPropertiesBinderUtil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,4 @@ public String getEnvironmentProperty(String key, Object environment) {
public String getEnvironmentProperty(String key, String defaultValue) {
return getInstance().getEnvironment().getProperty(key, defaultValue);
}

@Override
public String[] getActiveProfiles() {
return getInstance().getEnvironment().getActiveProfiles();
}

@Override
public String[] getDefaultProfiles() {
return getInstance().getEnvironment().getDefaultProfiles();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.dromara.dynamictp.spring.initializer;

import org.apache.commons.lang3.StringUtils;
import org.dromara.dynamictp.core.support.init.DtpInitializer;
import org.springframework.context.ConfigurableApplicationContext;

Expand All @@ -34,6 +35,10 @@ public class SpringDtpInitializer implements DtpInitializer {

private static final String SPRING_APP_NAME_KEY = "spring.application.name";

private static final String SERVER_PORT = "server.port";

private static final String ACTIVE_PROFILES = "spring.profiles.active";

@Override
public String getName() {
return "SpringDtpInitializer";
Expand All @@ -43,8 +48,21 @@ public String getName() {
public void init(Object... args) {
ConfigurableApplicationContext c = (ConfigurableApplicationContext) args[0];
String appName = c.getEnvironment().getProperty(SPRING_APP_NAME_KEY, "application");
String appPort = c.getEnvironment().getProperty("server.port", "0");
String appEnv = c.getEnvironment().getProperty("spring.profiles.active", "unknown");
String appPort = c.getEnvironment().getProperty(SERVER_PORT, "0");
String appEnv = c.getEnvironment().getProperty(ACTIVE_PROFILES);
if (StringUtils.isBlank(appEnv)) {
// fix #I8SSGQ
String[] profiles = c.getEnvironment().getActiveProfiles();
if (profiles.length < 1) {
profiles = c.getEnvironment().getDefaultProfiles();
}
if (profiles.length >= 1) {
appEnv = profiles[0];
}
}
if (StringUtils.isBlank(appEnv)) {
appEnv = "unknown";
}
System.setProperty(APP_NAME_KEY, appName);
System.setProperty(APP_PORT_KEY, appPort);
System.setProperty(APP_ENV_KEY, appEnv);
Expand Down

0 comments on commit 40e4ceb

Please sign in to comment.