Skip to content

Commit 98f20fb

Browse files
committed
optimize
1 parent f713eaf commit 98f20fb

File tree

3 files changed

+107
-26
lines changed

3 files changed

+107
-26
lines changed

common/src/main/java/org/dromara/dynamictp/common/manager/ContextManagerHelper.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.dromara.dynamictp.common.manager;
1919

20+
import lombok.extern.slf4j.Slf4j;
2021
import org.dromara.dynamictp.common.util.ExtensionServiceLoader;
2122

2223
import java.util.Map;
@@ -27,51 +28,53 @@
2728
* @author vzer200
2829
* @since 1.2.0
2930
*/
31+
@Slf4j
3032
public class ContextManagerHelper {
3133

32-
private static final ContextManager CONTEXT_MANAGER;
34+
private static ContextManager contextManager;
3335

3436
static {
35-
CONTEXT_MANAGER = ExtensionServiceLoader.getFirst(ContextManager.class);
36-
if (CONTEXT_MANAGER == null) {
37+
contextManager = ExtensionServiceLoader.getFirst(ContextManager.class);
38+
if (contextManager == null) {
39+
contextManager = new NullContextManager();
3740
throw new IllegalStateException("No ContextManager implementation found");
3841
}
3942
}
4043

4144
public static <T> T getBean(Class<T> clazz) {
42-
return CONTEXT_MANAGER.getBean(clazz);
45+
return contextManager.getBean(clazz);
4346
}
4447

4548
public static <T> T getBean(String name, Class<T> clazz) {
46-
return CONTEXT_MANAGER.getBean(name, clazz);
49+
return contextManager.getBean(name, clazz);
4750
}
4851

4952
public static <T> Map<String, T> getBeansOfType(Class<T> clazz) {
50-
return CONTEXT_MANAGER.getBeansOfType(clazz);
53+
return contextManager.getBeansOfType(clazz);
5154
}
5255

5356
public static Object getEnvironment() {
54-
return CONTEXT_MANAGER.getEnvironment();
57+
return contextManager.getEnvironment();
5558
}
5659

5760
public static String getEnvironmentProperty(String key) {
58-
return CONTEXT_MANAGER.getEnvironmentProperty(key);
61+
return contextManager.getEnvironmentProperty(key);
5962
}
6063

6164
public static String getEnvironmentProperty(String key, Object environment) {
62-
return CONTEXT_MANAGER.getEnvironmentProperty(key, environment);
65+
return contextManager.getEnvironmentProperty(key, environment);
6366
}
6467

6568
public static String getEnvironmentProperty(String key, String defaultValue) {
66-
return CONTEXT_MANAGER.getEnvironmentProperty(key, defaultValue);
69+
return contextManager.getEnvironmentProperty(key, defaultValue);
6770
}
6871

6972
public static String[] getActiveProfiles() {
70-
return CONTEXT_MANAGER.getActiveProfiles();
73+
return contextManager.getActiveProfiles();
7174
}
7275

7376
public static String[] getDefaultProfiles() {
74-
return CONTEXT_MANAGER.getDefaultProfiles();
77+
return contextManager.getDefaultProfiles();
7578
}
7679
}
7780

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.dromara.dynamictp.common.manager;
19+
20+
import java.util.Map;
21+
22+
/**
23+
* NullContextManager related
24+
*
25+
* @author yanhom
26+
* @since 1.1.0
27+
*/
28+
public class NullContextManager implements ContextManager {
29+
30+
@Override
31+
public <T> T getBean(Class<T> clazz) {
32+
throw new UnsupportedOperationException();
33+
}
34+
35+
@Override
36+
public <T> T getBean(String name, Class<T> clazz) {
37+
throw new UnsupportedOperationException();
38+
}
39+
40+
@Override
41+
public <T> Map<String, T> getBeansOfType(Class<T> clazz) {
42+
throw new UnsupportedOperationException();
43+
}
44+
45+
@Override
46+
public Object getEnvironment() {
47+
throw new UnsupportedOperationException();
48+
}
49+
50+
@Override
51+
public String getEnvironmentProperty(String key) {
52+
throw new UnsupportedOperationException();
53+
}
54+
55+
@Override
56+
public String getEnvironmentProperty(String key, Object environment) {
57+
throw new UnsupportedOperationException();
58+
}
59+
60+
@Override
61+
public String getEnvironmentProperty(String key, String defaultValue) {
62+
throw new UnsupportedOperationException();
63+
}
64+
65+
@Override
66+
public String[] getActiveProfiles() {
67+
throw new UnsupportedOperationException();
68+
}
69+
70+
@Override
71+
public String[] getDefaultProfiles() {
72+
throw new UnsupportedOperationException();
73+
}
74+
}

common/src/main/java/org/dromara/dynamictp/common/util/DtpPropertiesBinderUtil.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,6 @@ private static Object getProperty(String key, Object environment) {
127127
}
128128
}
129129

130-
private static boolean contains(String key, Object environment) {
131-
if (environment instanceof Map) {
132-
Map<?, Object> properties = (Map<?, Object>) environment;
133-
return properties.containsKey(key);
134-
} else {
135-
return StringUtils.isNotBlank(ContextManagerHelper.getEnvironmentProperty(key, environment));
136-
}
137-
}
138-
139130
private static void setBasicField(Object source, Field field, String executorFieldName, Object executor, int[] idx) {
140131
String propKey = MAIN_PROPERTIES_PREFIX + "." + executorFieldName + "[" + idx[0] + "]." + field.getName();
141132
setBasicField(source, field, executor, propKey);
@@ -159,29 +150,42 @@ private static void setBasicField(Object source, Field field, Object executor, S
159150
}
160151

161152
private static void setCollectionField(Object source, DtpExecutorProps globalExecutorProps, Object executor, String prefix) {
162-
if (!contains(prefix + ".taskWrapperNames[0]", source) &&
153+
if (isNotContains(prefix + ".taskWrapperNames[0]", source) &&
163154
CollectionUtils.isNotEmpty(globalExecutorProps.getTaskWrapperNames())) {
164155
ReflectUtil.setFieldValue(executor, "taskWrapperNames", globalExecutorProps.getTaskWrapperNames());
165156
}
166-
if (!contains(prefix + ".platformIds[0]", source) &&
157+
if (isNotContains(prefix + ".platformIds[0]", source) &&
167158
CollectionUtils.isNotEmpty(globalExecutorProps.getPlatformIds())) {
168159
ReflectUtil.setFieldValue(executor, PLATFORM_IDS, globalExecutorProps.getPlatformIds());
169160
}
170-
if (!contains(prefix + ".notifyItems[0].type", source) &&
161+
if (isNotContains(prefix + ".notifyItems[0].type", source) &&
171162
CollectionUtils.isNotEmpty(globalExecutorProps.getNotifyItems())) {
172163
ReflectUtil.setFieldValue(executor, NOTIFY_ITEMS, globalExecutorProps.getNotifyItems());
173164
}
174-
if (!contains(prefix + ".awareNames[0]", source) &&
165+
if (isNotContains(prefix + ".awareNames[0]", source) &&
175166
CollectionUtils.isNotEmpty(globalExecutorProps.getAwareNames())) {
176167
ReflectUtil.setFieldValue(executor, AWARE_NAMES, globalExecutorProps.getAwareNames());
177168
}
178169
try {
179-
if (!contains(prefix + ".pluginNames[0]", source) &&
170+
if (isNotContains(prefix + ".pluginNames[0]", source) &&
180171
CollectionUtils.isNotEmpty(globalExecutorProps.getPluginNames())) {
181172
ReflectUtil.setFieldValue(executor, PLUGIN_NAMES, globalExecutorProps.getPluginNames());
182173
}
183174
} catch (Exception e) {
184175
// ignore
185176
}
186177
}
178+
179+
private static boolean isNotContains(String key, Object environment) {
180+
return !contains(key, environment);
181+
}
182+
183+
private static boolean contains(String key, Object environment) {
184+
if (environment instanceof Map) {
185+
Map<?, Object> properties = (Map<?, Object>) environment;
186+
return properties.containsKey(key);
187+
} else {
188+
return StringUtils.isNotBlank(ContextManagerHelper.getEnvironmentProperty(key, environment));
189+
}
190+
}
187191
}

0 commit comments

Comments
 (0)