Skip to content

Commit caed482

Browse files
committed
Merge pull request #276 from ltsopensource/develop
ConfigurationProperties 支持
2 parents 8df42d7 + f2e4e8b commit caed482

File tree

44 files changed

+949
-703
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+949
-703
lines changed

lts-core/src/main/java/com/github/ltsopensource/autoconfigure/PropertiesConfigurationFactory.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ public static <T> T createPropertiesConfiguration(Class<T> clazz) {
7676
}
7777
ConfigurationProperties annotation = clazz.getAnnotation(ConfigurationProperties.class);
7878
String[] locations = annotation.locations();
79+
return createPropertiesConfiguration(clazz, locations);
80+
}
81+
82+
public static <T> T createPropertiesConfiguration(Class<T> clazz, String[] locations) {
83+
if (clazz == null) {
84+
throw new IllegalArgumentException("clazz should not be null");
85+
}
86+
if (!clazz.isAnnotationPresent(ConfigurationProperties.class)) {
87+
throw new IllegalArgumentException(clazz.getName() + " must annotation with @" + ConfigurationProperties.class.getName());
88+
}
7989
if (locations == null || locations.length == 0) {
8090
throw new IllegalArgumentException(clazz.getName() + " must specified the properties locations");
8191
}

lts-core/src/main/java/com/github/ltsopensource/autoconfigure/resolver/ArrayResolver.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.ltsopensource.autoconfigure.resolver;
22

33
import com.github.ltsopensource.autoconfigure.AutoConfigContext;
4+
import com.github.ltsopensource.autoconfigure.PropertiesConfigurationResolveException;
45
import com.github.ltsopensource.core.commons.utils.PrimitiveTypeUtils;
56
import com.github.ltsopensource.core.json.JSON;
67

@@ -43,7 +44,13 @@ public boolean call(String name, String key, String value) {
4344
for (Map.Entry<String, String> entry : kvMap.entrySet()) {
4445
String value = entry.getValue();
4546

46-
if (PrimitiveTypeUtils.isPrimitiveClass(componentClass)) {
47+
if (componentClass == Class.class) {
48+
try {
49+
Array.set(array, index++, Class.forName(value));
50+
} catch (ClassNotFoundException e) {
51+
throw new PropertiesConfigurationResolveException(e);
52+
}
53+
} else if (PrimitiveTypeUtils.isPrimitiveClass(componentClass)) {
4754
Array.set(array, index++, PrimitiveTypeUtils.convert(value, componentClass));
4855
} else {
4956
Array.set(array, index++, JSON.parse(value, componentClass));
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.github.ltsopensource.autoconfigure.resolver;
2+
3+
import com.github.ltsopensource.autoconfigure.AutoConfigContext;
4+
import com.github.ltsopensource.autoconfigure.PropertiesConfigurationResolveException;
5+
6+
import java.beans.PropertyDescriptor;
7+
8+
/**
9+
* @author Robert HG ([email protected]) on 4/21/16.
10+
*/
11+
public class ClassResolver extends AbstractResolver {
12+
13+
public static final ClassResolver INSTANCE = new ClassResolver();
14+
15+
@Override
16+
public void resolve(final AutoConfigContext context, final PropertyDescriptor descriptor, Class<?> propertyType) {
17+
18+
doFilter(context, descriptor, new Filter() {
19+
@Override
20+
public boolean onCondition(String name, String key, String value) {
21+
return key.equals(name);
22+
}
23+
24+
@Override
25+
public boolean call(String name, String key, String value) {
26+
try {
27+
Class clazz = Class.forName(value);
28+
writeProperty(context, descriptor, clazz);
29+
} catch (ClassNotFoundException e) {
30+
throw new PropertiesConfigurationResolveException(e);
31+
}
32+
return false;
33+
}
34+
});
35+
36+
}
37+
}

lts-core/src/main/java/com/github/ltsopensource/autoconfigure/resolver/CollectionResolver.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ public boolean onCondition(String name, String key, String value) {
3232

3333
@Override
3434
public boolean call(String name, String key, String value) {
35-
if (PrimitiveTypeUtils.isPrimitiveClass(itemType)) {
35+
if (itemType == Class.class) {
36+
try {
37+
collection.add(Class.forName(value));
38+
} catch (ClassNotFoundException e) {
39+
throw new PropertiesConfigurationResolveException(e);
40+
}
41+
} else if (PrimitiveTypeUtils.isPrimitiveClass(itemType)) {
3642
collection.add(PrimitiveTypeUtils.convert(value, itemType));
3743
} else {
3844
collection.add(JSON.parse(value, itemType));

lts-core/src/main/java/com/github/ltsopensource/autoconfigure/resolver/MapResolver.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ public boolean call(String name, String key, String value) {
4545
if (mapKey.startsWith("[") && mapKey.endsWith("]")) {
4646
mapKey = mapKey.substring(1, mapKey.length() - 1);
4747
}
48-
if (PrimitiveTypeUtils.isPrimitiveClass(vClass)) {
48+
if (vClass == Class.class) {
49+
try {
50+
Class clazz = Class.forName(value);
51+
map.put(mapKey, clazz);
52+
} catch (ClassNotFoundException e) {
53+
throw new PropertiesConfigurationResolveException(e);
54+
}
55+
} else if (PrimitiveTypeUtils.isPrimitiveClass(vClass)) {
4956
map.put(mapKey, PrimitiveTypeUtils.convert(value, vClass));
5057
} else {
5158
map.put(mapKey, JSON.parse(value, vClass));

lts-core/src/main/java/com/github/ltsopensource/autoconfigure/resolver/ResolverUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ public class ResolverUtils {
1111

1212
public static Resolver getResolver(Class<?> clazz) {
1313

14-
if (PrimitiveTypeUtils.isPrimitiveClass(clazz)) {
14+
if (clazz == Class.class) {
15+
16+
return ClassResolver.INSTANCE;
17+
18+
} else if (PrimitiveTypeUtils.isPrimitiveClass(clazz)) {
1519

1620
return PrimitiveTypeResolver.INSTANCE;
1721

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
package com.github.ltsopensource.spring.boot.properties;
1+
package com.github.ltsopensource.core.cluster;
2+
3+
import com.github.ltsopensource.core.exception.ConfigPropertiesIllegalException;
24

35
import java.util.HashMap;
46
import java.util.Map;
57

68
/**
7-
* @author Robert HG ([email protected]) on 4/9/16.
9+
* @author Robert HG ([email protected]) on 4/21/16.
810
*/
9-
public class AbstractProperties {
11+
public abstract class AbstractConfigProperties {
1012

1113
/**
1214
* 节点标识(可选)
@@ -29,6 +31,8 @@ public class AbstractProperties {
2931
*/
3032
private Map<String, String> configs = new HashMap<String, String>();
3133

34+
public abstract void checkProperties() throws ConfigPropertiesIllegalException;
35+
3236
public String getClusterName() {
3337
return clusterName;
3438
}
@@ -68,4 +72,5 @@ public String getBindIp() {
6872
public void setBindIp(String bindIp) {
6973
this.bindIp = bindIp;
7074
}
75+
7176
}

lts-core/src/main/java/com/github/ltsopensource/core/cluster/AbstractJobNode.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,6 @@ protected void initConfig() {
148148
AbstractCompiler.setCompiler(compiler);
149149
}
150150

151-
appContext.setEventCenter(ServiceLoader.load(EventCenter.class, config));
152-
153-
appContext.setCommandBodyWrapper(new CommandBodyWrapper(config));
154-
appContext.setMasterElector(new MasterElector(appContext));
155-
appContext.getMasterElector().addMasterChangeListener(masterChangeListeners);
156-
appContext.setRegistryStatMonitor(new RegistryStatMonitor(appContext));
157-
158151
if (StringUtils.isEmpty(config.getIp())) {
159152
config.setIp(NetUtils.getLocalHost());
160153
}
@@ -163,6 +156,13 @@ protected void initConfig() {
163156

164157
LOGGER.info("Current Node config :{}", config);
165158

159+
appContext.setEventCenter(ServiceLoader.load(EventCenter.class, config));
160+
161+
appContext.setCommandBodyWrapper(new CommandBodyWrapper(config));
162+
appContext.setMasterElector(new MasterElector(appContext));
163+
appContext.getMasterElector().addMasterChangeListener(masterChangeListeners);
164+
appContext.setRegistryStatMonitor(new RegistryStatMonitor(appContext));
165+
166166
// 订阅的node管理
167167
SubscribedNodeManager subscribedNodeManager = new SubscribedNodeManager(appContext);
168168
appContext.setSubscribedNodeManager(subscribedNodeManager);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.github.ltsopensource.core.cluster;
2+
3+
import com.github.ltsopensource.core.listener.MasterChangeListener;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.concurrent.atomic.AtomicBoolean;
8+
9+
/**
10+
* @author Robert HG ([email protected]) on 4/21/16.
11+
*/
12+
@SuppressWarnings("unchecked")
13+
public abstract class AbstractNodeBuilder<T extends AbstractJobNode, B extends NodeBuilder> implements NodeBuilder<T> {
14+
15+
protected List<MasterChangeListener> masterChangeListeners;
16+
private AtomicBoolean built = new AtomicBoolean(false);
17+
protected String[] locations;
18+
19+
public final B setPropertiesConfigure(String... locations) {
20+
if (locations == null || locations.length == 0) {
21+
throw new IllegalArgumentException("locations can not null");
22+
}
23+
this.locations = locations;
24+
return (B) this;
25+
}
26+
27+
public B addMasterChangeListener(MasterChangeListener masterChangeListener) {
28+
if (masterChangeListener != null) {
29+
if (masterChangeListeners == null) {
30+
masterChangeListeners = new ArrayList<MasterChangeListener>();
31+
}
32+
masterChangeListeners.add(masterChangeListener);
33+
}
34+
return (B) this;
35+
}
36+
37+
private void checkLocations() {
38+
if (locations == null || locations.length == 0) {
39+
throw new IllegalArgumentException("locations can not null");
40+
}
41+
}
42+
43+
public final T build() {
44+
if (!built.compareAndSet(false, true)) {
45+
throw new IllegalStateException("Already Built");
46+
}
47+
checkLocations();
48+
T node = build0();
49+
if (masterChangeListeners != null) {
50+
for (MasterChangeListener masterChangeListener : masterChangeListeners) {
51+
node.addMasterChangeListener(masterChangeListener);
52+
}
53+
}
54+
return node;
55+
}
56+
57+
protected abstract T build0();
58+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.github.ltsopensource.core.cluster;
2+
3+
/**
4+
* @author Robert HG ([email protected]) on 4/21/16.
5+
*/
6+
public interface NodeBuilder<T> {
7+
8+
T build();
9+
10+
}

0 commit comments

Comments
 (0)