Skip to content

Commit

Permalink
[1] test code
Browse files Browse the repository at this point in the history
  • Loading branch information
eight-nines committed Dec 19, 2022
1 parent 0c783a7 commit 423d725
Show file tree
Hide file tree
Showing 11 changed files with 446 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,86 @@

import lombok.Getter;

@Config(prefix = "eventMesh")
public class CommonConfiguration {
@ConfigFiled(field = "sysid")
public String sysID = "5477";

@ConfigFiled(field = "server.env")
public String eventMeshEnv = "P";

@ConfigFiled(field = "server.idc")
public String eventMeshIDC = "FT";
public String eventMeshCluster = "LS";

@ConfigFiled(field = "server.name")
public String eventMeshName = "";
public List<String> eventMeshProvideServerProtocols;
public String sysID = "5477";
public String eventMeshConnectorPluginType = "rocketmq";
public String eventMeshSecurityPluginType = "security";
public String eventMeshRegistryPluginType = "namesrv";
public List<String> eventMeshMetricsPluginType;
public String eventMeshTracePluginType;

@ConfigFiled(field = "server.cluster")
public String eventMeshCluster = "LS";

@ConfigFiled(field = "server.hostIp")
public String eventMeshServerIp = null;

@ConfigFiled(field = "registry.plugin.server-addr")
public String namesrvAddr = "";


@ConfigFiled(field = "trace.plugin")
public String eventMeshTracePluginType;

@ConfigFiled(field = "metrics.plugin")
public List<String> eventMeshMetricsPluginType;

@ConfigFiled(field = "registry.plugin.type")
public String eventMeshRegistryPluginType = "namesrv";

@ConfigFiled(field = "security.plugin.type")
public String eventMeshSecurityPluginType = "security";

@ConfigFiled(field = "connector.plugin.type")
public String eventMeshConnectorPluginType = "rocketmq";


@ConfigFiled(field = "registry.plugin.username")
public String eventMeshRegistryPluginUsername = "";

@ConfigFiled(field = "registry.plugin.password")
public String eventMeshRegistryPluginPassword = "";

@ConfigFiled(field = "server.registry.registerIntervalInMills")
public Integer eventMeshRegisterIntervalInMills = 10 * 1000;

@ConfigFiled(field = "server.registry.fetchRegistryAddrIntervalInMills")
public Integer eventMeshFetchRegistryAddrInterval = 10 * 1000;
public String eventMeshServerIp = null;


@ConfigFiled(field = "server.trace.enabled")
public boolean eventMeshServerTraceEnable = false;

@ConfigFiled(field = "server.security.enabled")
public boolean eventMeshServerSecurityEnable = false;

@ConfigFiled(field = "server.registry.enabled")
public boolean eventMeshServerRegistryEnable = false;
public boolean eventMeshServerTraceEnable = false;



@ConfigFiled(field = "server.provide.protocols")
public List<String> eventMeshProvideServerProtocols;


@ConfigFiled(reload = true)
public String eventMeshWebhookOrigin = "eventmesh." + eventMeshIDC;

public CommonConfiguration() {
}

public void reload() {
this.eventMeshWebhookOrigin = "eventmesh." + eventMeshIDC;
}

@Getter
protected ConfigurationWrapper configurationWrapper;

public String eventMeshWebhookOrigin = "eventmesh." + eventMeshIDC;

public CommonConfiguration(ConfigurationWrapper configurationWrapper) {
this.configurationWrapper = configurationWrapper;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Config {

String field();
String field() default "";

String path() default "";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 org.apache.eventmesh.common.config;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD})
public @interface ConfigFiled {

/**
* @return The key name of the configuration file
*/
String field() default "";

/**
* Note : When reload is true, the class must have a reload method
*
* @return Whether to reload. This parameter is used when other fields are associated
*/
boolean reload() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,20 @@
@NoArgsConstructor
public class ConfigInfo {

public static final String HUPM_SPOT = "spot";

public static final String HUPM_ROD = "rod";
public static final String HUMP_SPOT = "spot";
public static final String HUMP_ROD = "rod";

private String path;

private String field;

private String prefix;
private String hump;
private boolean monitor;
private boolean removePrefix;

private Class<?> clazz;

private Object object;

private String filePath;

private boolean removePrefix;

private boolean monitor;

private String hump;

Field objectField;

Object instance;

protected void setObjectField(Field objectField) {
this.objectField = objectField;
}

protected Field getObjectField() {
return this.objectField;
}

protected void setInstance(Object instance) {
this.instance = instance;
}

protected Object getInstance() {
return this.instance;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

package org.apache.eventmesh.common.config;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Locale;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;


public class ConfigService {
Expand All @@ -29,12 +33,12 @@ public class ConfigService {

private Properties properties = new Properties();

private ConfigMonitorService configMonitorService = new ConfigMonitorService();
private final ConfigMonitorService configMonitorService = new ConfigMonitorService();

private String configPath;


public static final ConfigService getInstance() {
public static ConfigService getInstance() {
return INSTANCE;
}

Expand All @@ -46,78 +50,99 @@ public ConfigService setConfigPath(String configPath) {
return this;
}

public ConfigService setRootConfig(String path) throws Exception {
public void setRootConfig(String path) throws Exception {
ConfigInfo configInfo = new ConfigInfo();
configInfo.setPath(path);
properties = this.getConfig(configInfo);
return this;
}

public <T> T getConfig(Class<?> clazz) {
Config[] configArray = clazz.getAnnotationsByType(Config.class);
if (configArray.length == 0) {
try {
return this.getConfig(ConfigInfo.builder()
.clazz(clazz)
.hump(ConfigInfo.HUMP_SPOT)
.build());
} catch (Exception e) {
throw new RuntimeException(e);
}
}

Config config = configArray[0];
try {
ConfigInfo configInfo = new ConfigInfo();
configInfo.setClazz(clazz);
configInfo.setHump(config.hump());
configInfo.setPrefix(config.prefix());
configInfo.setMonitor(config.monitor());

return this.getConfig(configInfo);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public void getConfig(Object object) throws Exception {
this.getConfig(object, object.getClass());
}

public void getConfig(Object object, Class<?> clazz) throws Exception {
Config[] configArray = clazz.getAnnotationsByType(Config.class);
if (configArray == null || configArray.length == 0) {
//TODO
if (configArray.length == 0) {
return;
}

for (Config config : configArray) {
ConfigInfo configInfo = new ConfigInfo();
configInfo.setField(config.field());
configInfo.setPath(config.path());
configInfo.setPrefix(config.prefix());
configInfo.setHump(config.hump());
configInfo.setObject(object);
configInfo.setMonitor(config.monitor());

Field field = clazz.getDeclaredField(configInfo.getField());
configInfo.setClazz(field.getType());
Object configObject = this.getConfig(configInfo);
field.setAccessible(true);
field.set(object, configObject);

if (configInfo.isMonitor()) {
configInfo.setObjectField(field);
configInfo.setInstance(object);
configInfo.setObject(configObject);
configMonitorService.monitor(configInfo);
}
}

}

public void getConfig(Object object) throws Exception {
this.getConfig(object, object.getClass());
}

public <T> T getConfig(Class<?> clazz) {
try {
return this.getConfig(ConfigInfo.builder().clazz(clazz).hump(ConfigInfo.HUPM_SPOT).build());
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@SuppressWarnings("unchecked")
public <T> T getConfig(ConfigInfo configInfo) throws Exception {
Object object;
if (Objects.isNull(configInfo.getPath())) {

if (Objects.isNull(configInfo.getPath()) || StringUtils.isEmpty(configInfo.getPath().trim())) {
object = FileLoad.getPropertiesFileLoad().getConfig(properties, configInfo);
} else {
String path = configInfo.getPath();
String filePath;
if (path.startsWith("classPath://")) {
filePath = ConfigService.class.getResource("/" + path.substring(12)).getPath();
filePath = Objects.requireNonNull(ConfigService.class.getResource("/" + path.substring(12))).getPath();
} else if (path.startsWith("file://")) {
filePath = path.substring(7);
} else {
filePath = this.configPath + path;
}

File file = new File(filePath);
if (!file.exists()) {
throw new RuntimeException("fie is not existis");
throw new RuntimeException("fie is not exists");
}

String suffix = path.substring(path.lastIndexOf('.') + 1);
configInfo.setFilePath(filePath);
object = FileLoad.getFileLoad(suffix).getConfig(configInfo);
}
return (T) object;
}

}
}
Loading

0 comments on commit 423d725

Please sign in to comment.