Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #2576] Tweak Convert's code framework, for ease of enhancement #2784

Merged
merged 1 commit into from
Jan 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Record information about the configuration class to be converted
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@

package org.apache.eventmesh.common.config;

import org.apache.eventmesh.common.config.convert.ConvertValue.DefaultConverter;

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

/**
* Record information about the field in the configuration class to be converted
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD})
public @interface ConfigFiled {
Expand All @@ -37,4 +42,11 @@
* @return Whether to reload. This parameter is used when other fields are associated
*/
boolean reload() default false;

/**
* In some special cases, used to specify the converter class of the field
*
* @return field converter
*/
Class<?> converter() default DefaultConverter.class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@

package org.apache.eventmesh.common.config;

import org.apache.eventmesh.common.utils.Convert;
import org.apache.eventmesh.common.config.convert.Convert;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Objects;
Expand All @@ -35,9 +34,9 @@
*/
public interface FileLoad {

static final PropertiesFileLoad PROPERTIES_FILE_LOAD = new PropertiesFileLoad();
PropertiesFileLoad PROPERTIES_FILE_LOAD = new PropertiesFileLoad();

static final YamlFileLoad YAML_FILE_LOAD = new YamlFileLoad();
YamlFileLoad YAML_FILE_LOAD = new YamlFileLoad();

public static FileLoad getFileLoad(String fileType) {
if (Objects.equals("properties", fileType)) {
Expand All @@ -48,15 +47,15 @@ public static FileLoad getFileLoad(String fileType) {
return PROPERTIES_FILE_LOAD;
}

public static PropertiesFileLoad getPropertiesFileLoad() {
static PropertiesFileLoad getPropertiesFileLoad() {
return PROPERTIES_FILE_LOAD;
}

public static YamlFileLoad getYamlFileLoad() {
static YamlFileLoad getYamlFileLoad() {
return YAML_FILE_LOAD;
}

public <T> T getConfig(ConfigInfo configInfo) throws IOException;
<T> T getConfig(ConfigInfo configInfo) throws IOException;

class PropertiesFileLoad implements FileLoad {

Expand All @@ -70,12 +69,12 @@ public <T> T getConfig(ConfigInfo configInfo) throws IOException {
return (T) properties;
}

return (T) convert.createObject(configInfo, properties);
return (T) convert.doConvert(configInfo, properties);
}

@SuppressWarnings("unchecked")
public <T> T getConfig(Properties properties, ConfigInfo configInfo) {
return (T) convert.createObject(configInfo, properties);
return (T) convert.doConvert(configInfo, properties);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.convert;

import org.apache.eventmesh.common.config.ConfigInfo;

import java.util.Properties;

/**
* Used to convert Config properties
*/
public class Convert {

public Object doConvert(ConfigInfo configInfo, Properties properties) {
Class<?> clazz = configInfo.getClazz();
ConvertInfo convertInfo = new ConvertInfo();
convertInfo.setConfigInfo(configInfo);
convertInfo.setProperties(properties);
convertInfo.setClazz(clazz);

ConvertValue<?> clazzConverter = ConverterMap.getClazzConverter(clazz);
return clazzConverter.convert(convertInfo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.convert;

import org.apache.eventmesh.common.config.ConfigInfo;

import java.lang.reflect.Field;
import java.util.Properties;

import lombok.Data;

/**
* Records the information about the field to be converted
*/
@Data
public class ConvertInfo {
char hump;
String key;
Field field;
Object value;
Class<?> clazz;
Properties properties;
ConfigInfo configInfo;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.convert;

import java.util.Properties;

/**
* convert convertInfo to obj
*
* @param <T> obj type
*/
public interface ConvertValue<T> {

T convert(ConvertInfo convertInfo);

/**
* @return Whether can to process null values
*/
default boolean canHandleNullValue() {
return false;
}

/**
* @return The value converter needs
*/
default Object processFieldValue(Properties config, String key) {
return config.getProperty(key);
}

class DefaultConverter implements ConvertValue<Object> {

@Override
public Object convert(ConvertInfo convertInfo) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* 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.convert;

import org.apache.eventmesh.common.config.ConfigFiled;
import org.apache.eventmesh.common.config.convert.converter.BaseDataTypeConverter;
import org.apache.eventmesh.common.config.convert.converter.DateConverter;
import org.apache.eventmesh.common.config.convert.converter.EnumConverter;
import org.apache.eventmesh.common.config.convert.converter.IPAddressConverter;
import org.apache.eventmesh.common.config.convert.converter.ListConverter;
import org.apache.eventmesh.common.config.convert.converter.LocalDateConverter;
import org.apache.eventmesh.common.config.convert.converter.LocalDateTimeConverter;
import org.apache.eventmesh.common.config.convert.converter.MapConverter;
import org.apache.eventmesh.common.config.convert.converter.ObjectConverter;
import org.apache.eventmesh.common.config.convert.converter.PropertiesConverter;
import org.apache.eventmesh.common.config.convert.converter.StringConverter;

import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.TreeMap;
import java.util.Vector;

import inet.ipaddr.IPAddress;

/**
* Use to map the field clazz and the converter for the field clazz
*/
public class ConverterMap {

private static final ObjectConverter objectConverter = new ObjectConverter();

private static final Map<Class<?>, ConvertValue<?>> classToConverter = new HashMap<>();

static {
register(new EnumConverter(), Enum.class);
register(new DateConverter(), Date.class);
register(new StringConverter(), String.class);
register(new LocalDateConverter(), LocalDate.class);
register(new IPAddressConverter(), IPAddress.class);
register(new PropertiesConverter(), Properties.class);
register(new LocalDateTimeConverter(), LocalDateTime.class);
register(new ListConverter(), List.class, ArrayList.class, LinkedList.class, Vector.class);
register(new MapConverter(), Map.class, HashMap.class, TreeMap.class, LinkedHashMap.class);
register(new BaseDataTypeConverter.CharacterConverter(), Character.class, char.class);
register(new BaseDataTypeConverter.ByteConverter(), Byte.class, byte.class);
register(new BaseDataTypeConverter.ShortConverter(), Short.class, short.class);
register(new BaseDataTypeConverter.IntegerConverter(), Integer.class, int.class);
register(new BaseDataTypeConverter.LongConverter(), Long.class, long.class);
register(new BaseDataTypeConverter.FloatConverter(), Float.class, float.class);
register(new BaseDataTypeConverter.DoubleConverter(), Double.class, double.class);
register(new BaseDataTypeConverter.BooleanConverter(), Boolean.class, boolean.class);
}

public static void register(ConvertValue<?> convertValue, Class<?>... clazzs) {
for (Class<?> clazz : clazzs) {
classToConverter.put(clazz, convertValue);
}
}

/**
* Get the converter for the field
*
* @param field The field to be parsed
* @return the converter for the field
*/
public static ConvertValue<?> getFieldConverter(Field field) {
Class<?> clazz = field.getType();
ConfigFiled configFiled = field.getAnnotation(ConfigFiled.class);

Class<?> converter1 = configFiled.converter();
if (!converter1.equals(ConvertValue.DefaultConverter.class)) {
if (!classToConverter.containsKey(converter1)) {
try {
ConvertValue<?> convertValue = (ConvertValue<?>) converter1.newInstance();
register(convertValue, converter1);
} catch (Exception e) {
e.printStackTrace();
}
}

return classToConverter.get(converter1);
}

return getClazzConverter(clazz);
}

/**
* Get the converter for the clazz
*
* @param clazz The clazz to be parsed
* @return the converter for the clazz
*/
public static ConvertValue<?> getClazzConverter(Class<?> clazz) {
ConvertValue<?> converter = classToConverter.get(clazz);
if (Objects.isNull(converter)) {
if (clazz.isEnum()) {
converter = classToConverter.get(Enum.class);
} else {
converter = objectConverter;
}
}

return converter;
}

public static Map<Class<?>, ConvertValue<?>> getClassToConverter() {
return classToConverter;
}

public static ObjectConverter getObjectConverter() {
return objectConverter;
}
}
Loading