format: 规范化代码风格
This commit is contained in:
@@ -14,16 +14,16 @@ import org.slf4j.MDC;
|
||||
*/
|
||||
public final class BootStrap {
|
||||
|
||||
public static void main(String[] args){
|
||||
public static void main(String[] args) {
|
||||
beforeInit();
|
||||
init();
|
||||
}
|
||||
|
||||
private static void beforeInit(){
|
||||
private static void beforeInit() {
|
||||
MDC.put("pid", SystemSupport.getPid());
|
||||
}
|
||||
|
||||
private static void init(){
|
||||
private static void init() {
|
||||
SystemInitializer systemInitializer = SystemInitializer
|
||||
.configurer()
|
||||
.configuration(new PropertiesConfiguration(SystemConstants.USER_SETTINGS_PROPERTIES_PATH))
|
||||
|
||||
@@ -81,7 +81,7 @@ public abstract class AbstractConfiguration implements Configuration {
|
||||
// 刷新配置信息
|
||||
refresh0();
|
||||
final Boolean needPrint = getBoolean(ConfigurationKeys.KEY_CFG_LOG_ONSTART);
|
||||
if(needPrint)
|
||||
if (needPrint)
|
||||
printDetails();
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ public abstract class AbstractConfiguration implements Configuration {
|
||||
/**
|
||||
* 载入配置信息请加锁
|
||||
*/
|
||||
protected void load(){
|
||||
protected void load() {
|
||||
try {
|
||||
loadLock.lock();
|
||||
// 清空原有的配置信息
|
||||
|
||||
@@ -16,18 +16,18 @@ public final class InstanceClasses {
|
||||
private InstanceClasses(){throw new UnsupportedOperationException();}
|
||||
|
||||
private static final Map<InstanceType, Class<? extends Instance>> instanceTypeMap =
|
||||
new HashMap<InstanceType, Class<? extends Instance>>(){
|
||||
new HashMap<InstanceType, Class<? extends Instance>>() {
|
||||
{
|
||||
put(InstanceType.ALI_YUN, AliyunInstance.class);
|
||||
put(InstanceType.TENCENT_CLOUD, TencentInstance.class);
|
||||
}
|
||||
};
|
||||
|
||||
public static Class<? extends Instance> match(InstanceType type){
|
||||
public static Class<? extends Instance> match(InstanceType type) {
|
||||
return instanceTypeMap.get(type);
|
||||
}
|
||||
|
||||
public static Class<? extends Instance> match(String type){
|
||||
public static Class<? extends Instance> match(String type) {
|
||||
return instanceTypeMap.get(InstanceType.valueOf(type));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,10 +19,10 @@ public final class InstanceFileFilter implements FileFilter {
|
||||
|
||||
@Override
|
||||
public boolean accept(File pathname) {
|
||||
if(!pathname.isFile())
|
||||
if (!pathname.isFile())
|
||||
return false;
|
||||
for (String suffix : fileSuffix) {
|
||||
if(pathname.getName().endsWith(suffix)){
|
||||
if (pathname.getName().endsWith(suffix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ public abstract class AbstractInstanceContext implements InstanceContext, Multip
|
||||
|
||||
@Override
|
||||
public void refresh() {
|
||||
if(listableInstanceFactories.isEmpty())
|
||||
if (listableInstanceFactories.isEmpty())
|
||||
return;
|
||||
|
||||
// 初始化所有实例工厂
|
||||
@@ -68,7 +68,7 @@ public abstract class AbstractInstanceContext implements InstanceContext, Multip
|
||||
public boolean addInstance(Instance instance, boolean override) {
|
||||
validateInstance(instance);
|
||||
Instance i = instanceMap.get(instance.getName());
|
||||
if(override && i != null){
|
||||
if (override && i != null) {
|
||||
return false;
|
||||
}
|
||||
instanceMap.put(instance.getName(), instance);
|
||||
@@ -120,8 +120,8 @@ public abstract class AbstractInstanceContext implements InstanceContext, Multip
|
||||
/**
|
||||
* 缓存清理
|
||||
*/
|
||||
protected void clearCache(){
|
||||
if(cacheInstanceMap != null
|
||||
protected void clearCache() {
|
||||
if (cacheInstanceMap != null
|
||||
&& !cacheInstanceMap.isEmpty()){
|
||||
int size = cacheInstanceMap.size();
|
||||
cacheInstanceMap.clear();
|
||||
@@ -136,15 +136,15 @@ public abstract class AbstractInstanceContext implements InstanceContext, Multip
|
||||
* @param instances 实例信息
|
||||
* @return 属性设置完整的实例
|
||||
*/
|
||||
private Set<Instance> buildInstances(Collection<Instance> instances){
|
||||
private Set<Instance> buildInstances(Collection<Instance> instances) {
|
||||
//设置实例信息, 如果需要从父类继承
|
||||
return instances.stream()
|
||||
.filter(i -> !InstanceType.INHERITED.equals(i.getType()))
|
||||
.peek(i -> {
|
||||
String fatherName = i.getFatherName();
|
||||
if(fatherName != null && !fatherName.isEmpty()){
|
||||
if (fatherName != null && !fatherName.isEmpty()) {
|
||||
Instance fatherInstance = cacheInstanceMap.get(fatherName);
|
||||
if(fatherInstance != null){
|
||||
if (fatherInstance != null) {
|
||||
try {
|
||||
ReflectionUtils.copyField(fatherInstance, i, true);
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -50,7 +50,7 @@ public abstract class AbstractInstanceFactory implements InstanceFactory, Listab
|
||||
public boolean addInstance(Instance instance, boolean override) {
|
||||
validateInstance(instance);
|
||||
Instance i = instanceMap.get(instance.getName());
|
||||
if(override && i != null){
|
||||
if (override && i != null) {
|
||||
return false;
|
||||
}
|
||||
instanceMap.put(instance.getName(), instance);
|
||||
@@ -65,7 +65,7 @@ public abstract class AbstractInstanceFactory implements InstanceFactory, Listab
|
||||
@Override
|
||||
public void refresh() {
|
||||
Set<Instance> instances = load();
|
||||
if(instances != null && !instances.isEmpty())
|
||||
if (instances != null && !instances.isEmpty())
|
||||
instanceMap = new HashMap<>(instances.stream()
|
||||
.collect(Collectors.toMap(Instance::getName, i -> i)));
|
||||
}
|
||||
@@ -77,7 +77,7 @@ public abstract class AbstractInstanceFactory implements InstanceFactory, Listab
|
||||
|
||||
@Override
|
||||
public void afterRefresh() {
|
||||
if(instanceMap != null
|
||||
if (instanceMap != null
|
||||
&& !instanceMap.isEmpty()){
|
||||
int size = instanceMap.size();
|
||||
instanceMap.clear();
|
||||
|
||||
@@ -10,7 +10,7 @@ import java.util.Set;
|
||||
* @author SerLiunx
|
||||
* @since 1.0
|
||||
*/
|
||||
public abstract class DatabaseInstanceFactory extends AbstractInstanceFactory{
|
||||
public abstract class DatabaseInstanceFactory extends AbstractInstanceFactory {
|
||||
|
||||
@Override
|
||||
protected Set<Instance> load() {
|
||||
|
||||
@@ -25,7 +25,7 @@ public abstract class FileInstanceFactory extends AbstractInstanceFactory {
|
||||
@Override
|
||||
protected Set<Instance> load() {
|
||||
Set<File> files = loadFiles();
|
||||
if(files != null && !files.isEmpty()){
|
||||
if (files != null && !files.isEmpty()) {
|
||||
return files.stream()
|
||||
.map(this::loadInstance)
|
||||
.filter(Objects::nonNull)
|
||||
@@ -56,19 +56,19 @@ public abstract class FileInstanceFactory extends AbstractInstanceFactory {
|
||||
/**
|
||||
* 载入目录下所有符合条件的文件
|
||||
*/
|
||||
private Set<File> loadFiles(){
|
||||
private Set<File> loadFiles() {
|
||||
File pathFile = new File(instanceDir);
|
||||
if(!pathFile.exists()){
|
||||
if (!pathFile.exists()) {
|
||||
boolean result = pathFile.mkdirs();
|
||||
if(!result){
|
||||
if (!result) {
|
||||
throw new IllegalArgumentException("create path failed");
|
||||
}
|
||||
}
|
||||
if(!pathFile.isDirectory()){
|
||||
if (!pathFile.isDirectory()) {
|
||||
throw new IllegalArgumentException("path is not a directory");
|
||||
}
|
||||
File[] files = pathFile.listFiles(new InstanceFileFilter(fileSuffix()));
|
||||
if(files == null || files.length == 0){
|
||||
if (files == null || files.length == 0) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
return Arrays.stream(files).collect(Collectors.toCollection(HashSet::new));
|
||||
|
||||
@@ -18,7 +18,7 @@ import static com.serliunx.ddns.constant.InstanceClasses.match;
|
||||
* @see com.fasterxml.jackson.dataformat.xml.XmlMapper
|
||||
* @see com.fasterxml.jackson.databind.json.JsonMapper
|
||||
*/
|
||||
public abstract class JacksonFileInstanceFactory extends FileInstanceFactory{
|
||||
public abstract class JacksonFileInstanceFactory extends FileInstanceFactory {
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import com.serliunx.ddns.core.instance.Instance;
|
||||
* @author SerLiunx
|
||||
* @since 1.0
|
||||
*/
|
||||
public class JsonFileInstanceFactory extends JacksonFileInstanceFactory{
|
||||
public class JsonFileInstanceFactory extends JacksonFileInstanceFactory {
|
||||
|
||||
public JsonFileInstanceFactory(String instanceDir, JsonMapper jsonMapper) {
|
||||
super(instanceDir, jsonMapper);
|
||||
|
||||
@@ -9,7 +9,7 @@ import com.serliunx.ddns.core.instance.Instance;
|
||||
* @author SerLiunx
|
||||
* @since 1.0
|
||||
*/
|
||||
public class XmlFileInstanceFactory extends JacksonFileInstanceFactory{
|
||||
public class XmlFileInstanceFactory extends JacksonFileInstanceFactory {
|
||||
|
||||
public XmlFileInstanceFactory(String instanceDir, XmlMapper xmlMapper) {
|
||||
super(instanceDir, xmlMapper);
|
||||
|
||||
@@ -78,7 +78,7 @@ public class YamlFileInstanceFactory extends FileInstanceFactory {
|
||||
}
|
||||
|
||||
@SuppressWarnings(value = {"unchecked", "rawtypes"})
|
||||
protected Instance buildInstance(Instance instance, Map<String, Object> valueMap){
|
||||
protected Instance buildInstance(Instance instance, Map<String, Object> valueMap) {
|
||||
Field[] declaredFields = ReflectionUtils.getDeclaredFields(instance.getClass(), true);
|
||||
for (Field f : declaredFields) {
|
||||
if (Modifier.isStatic(f.getModifiers())) {
|
||||
|
||||
@@ -71,9 +71,9 @@ public abstract class AbstractInstance implements Instance {
|
||||
}
|
||||
value = ipAddress;
|
||||
run0();
|
||||
}catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}finally {
|
||||
} finally {
|
||||
this.value = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,9 +115,9 @@ public class AliyunInstance extends AbstractInstance {
|
||||
CompletableFuture<UpdateDomainRecordResponse> requestResponse = client.updateDomainRecord(request);
|
||||
try {
|
||||
requestResponse.whenComplete((v, t) -> {
|
||||
if(t != null){ //出现异常
|
||||
if (t != null) { //出现异常
|
||||
handleThrowable(t);
|
||||
}else{
|
||||
} else {
|
||||
String result = null;
|
||||
try {
|
||||
result = jsonMapper.writeValueAsString(v.getBody());
|
||||
@@ -141,7 +141,7 @@ public class AliyunInstance extends AbstractInstance {
|
||||
try {
|
||||
DescribeDomainRecordInfoResponse response = responseCompletableFuture.get(5, TimeUnit.SECONDS);
|
||||
DescribeDomainRecordInfoResponseBody body = response.getBody();
|
||||
if(body != null){
|
||||
if (body != null) {
|
||||
return body.getValue();
|
||||
}
|
||||
return null;
|
||||
@@ -241,17 +241,17 @@ public class AliyunInstance extends AbstractInstance {
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
private void log(String msg, Object...params){
|
||||
private void log(String msg, Object...params) {
|
||||
log.info("[实例活动][" + name + "]" + msg, params);
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
private void debug(String msg, Object...params){
|
||||
private void debug(String msg, Object...params) {
|
||||
log.debug("[实例活动][" + name + "]" + msg, params);
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
private void error(String msg, Object...params){
|
||||
private void error(String msg, Object...params) {
|
||||
log.error("[实例异常][" + name + "]" + msg, params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,40 +11,40 @@ public final class Assert {
|
||||
|
||||
private Assert(){throw new UnsupportedOperationException();}
|
||||
|
||||
public static void notNull(Object object){
|
||||
public static void notNull(Object object) {
|
||||
notNull(object, null);
|
||||
}
|
||||
|
||||
public static void notNull(Object object, String msg){
|
||||
public static void notNull(Object object, String msg) {
|
||||
if(object == null)
|
||||
throw new NullPointerException(msg);
|
||||
}
|
||||
|
||||
public static void notNull(Object...objects){
|
||||
public static void notNull(Object...objects) {
|
||||
for (Object object : objects) {
|
||||
notNull(object);
|
||||
}
|
||||
}
|
||||
|
||||
public static void isPositive(int i){
|
||||
if(i <= 0){
|
||||
public static void isPositive(int i) {
|
||||
if(i <= 0) {
|
||||
throw new IllegalArgumentException("指定参数必须大于0!");
|
||||
}
|
||||
}
|
||||
|
||||
public static void isLargerThan(int source, int target){
|
||||
if(source <= target){
|
||||
public static void isLargerThan(int source, int target) {
|
||||
if(source <= target) {
|
||||
throw new IllegalArgumentException(String.format("%s太小了, 它必须大于%s", source, target));
|
||||
}
|
||||
}
|
||||
|
||||
public static void notEmpty(Collection<?> collection){
|
||||
public static void notEmpty(Collection<?> collection) {
|
||||
notNull(collection);
|
||||
if(collection.isEmpty())
|
||||
throw new IllegalArgumentException("参数不能为空!");
|
||||
}
|
||||
|
||||
public static void notEmpty(CharSequence charSequence){
|
||||
public static void notEmpty(CharSequence charSequence) {
|
||||
notNull(charSequence);
|
||||
if(charSequence.length() == 0)
|
||||
throw new IllegalArgumentException("参数不能为空!");
|
||||
|
||||
@@ -14,19 +14,19 @@ public final class Configurer {
|
||||
|
||||
Configurer(){}
|
||||
|
||||
public Configurer configuration(Configuration configuration){
|
||||
public Configurer configuration(Configuration configuration) {
|
||||
Assert.notNull(configuration);
|
||||
this.configuration = configuration;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Configurer instanceContext(MultipleSourceInstanceContext instanceContext){
|
||||
public Configurer instanceContext(MultipleSourceInstanceContext instanceContext) {
|
||||
Assert.notNull(instanceContext);
|
||||
this.instanceContext = instanceContext;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SystemInitializer done(){
|
||||
public SystemInitializer done() {
|
||||
Assert.notNull(configuration, instanceContext);
|
||||
return new SystemInitializer(configuration, instanceContext);
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public final class NetworkContextHolder {
|
||||
|
||||
private NetworkContextHolder(){throw new UnsupportedOperationException();}
|
||||
|
||||
public static void setIpAddress(String i){
|
||||
public static void setIpAddress(String i) {
|
||||
try {
|
||||
IP_LOCK.lock();
|
||||
IP_ADDRESS = i;
|
||||
@@ -37,11 +37,11 @@ public final class NetworkContextHolder {
|
||||
}
|
||||
}
|
||||
|
||||
public static String getIpAddress(){
|
||||
public static String getIpAddress() {
|
||||
if(IP_ADDRESS != null)
|
||||
return IP_ADDRESS;
|
||||
try {
|
||||
if(!IP_CONTEXT_WAIT_LATCH.await(IP_CONTEXT_TIME_OUT, TimeUnit.SECONDS)){
|
||||
if(!IP_CONTEXT_WAIT_LATCH.await(IP_CONTEXT_TIME_OUT, TimeUnit.SECONDS)) {
|
||||
log.error("IP地址获取超时.");
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ import static com.serliunx.ddns.config.ConfigurationKeys.KEY_THREAD_POOL_CORE_SI
|
||||
* @author SerLiunx
|
||||
* @since 1.0
|
||||
*/
|
||||
public final class SystemInitializer implements Refreshable{
|
||||
public final class SystemInitializer implements Refreshable {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SystemInitializer.class);
|
||||
|
||||
|
||||
@@ -10,13 +10,13 @@ public final class SystemSupport {
|
||||
|
||||
private static final String PID;
|
||||
|
||||
static{
|
||||
static {
|
||||
PID = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
|
||||
}
|
||||
|
||||
private SystemSupport(){throw new UnsupportedOperationException();}
|
||||
|
||||
public static String getPid(){
|
||||
public static String getPid() {
|
||||
return PID;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ public class JacksonDecoder implements Decoder {
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
public static Decoder getInstance(){
|
||||
public static Decoder getInstance() {
|
||||
return decoder;
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ public class JacksonEncoder implements Encoder {
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
public static Encoder getInstance(){
|
||||
public static Encoder getInstance() {
|
||||
return encoder;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ public interface IPAddressClient {
|
||||
@RequestLine("GET /json")
|
||||
IPAddressResponse getIPAddress();
|
||||
|
||||
static IPAddressClient getInstance(){
|
||||
static IPAddressClient getInstance() {
|
||||
return Feign.builder()
|
||||
.encoder(JacksonEncoder.getInstance())
|
||||
.decoder(JacksonDecoder.getInstance())
|
||||
|
||||
@@ -12,7 +12,7 @@ public final class InstanceUtils {
|
||||
|
||||
private InstanceUtils(){throw new UnsupportedOperationException();}
|
||||
|
||||
public static void validateInstance(Instance instance){
|
||||
public static void validateInstance(Instance instance) {
|
||||
Assert.notNull(instance);
|
||||
String instanceName = instance.getName();
|
||||
if(instanceName == null || instanceName.isEmpty()){
|
||||
|
||||
@@ -20,7 +20,7 @@ public final class ReflectionUtils {
|
||||
* @param setAccessible 是否将字段的可访问性
|
||||
* @return 字段列表
|
||||
*/
|
||||
public static Field[] getDeclaredFields(Class<?> clazz, boolean setAccessible){
|
||||
public static Field[] getDeclaredFields(Class<?> clazz, boolean setAccessible) {
|
||||
if(clazz == null){
|
||||
return null;
|
||||
}
|
||||
@@ -47,7 +47,7 @@ public final class ReflectionUtils {
|
||||
* @param setAccessible 是否将字段的可访问性
|
||||
* @return 字段列表
|
||||
*/
|
||||
public static List<Field> getDeclaredFieldList(Class<?> clazz, boolean setAccessible){
|
||||
public static List<Field> getDeclaredFieldList(Class<?> clazz, boolean setAccessible) {
|
||||
return Arrays.asList(getDeclaredFields(clazz, setAccessible));
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ public final class ReflectionUtils {
|
||||
* @param dest 目标对象
|
||||
* @param onlyNull 是否仅复制源对象不为空的属性
|
||||
*/
|
||||
public static void copyField(Object src, Object dest,boolean onlyNull){
|
||||
public static void copyField(Object src, Object dest,boolean onlyNull) {
|
||||
Class<?> srcClass = src.getClass();
|
||||
Class<?> destClass = dest.getClass();
|
||||
List<Field> srcField = getDeclaredFieldList(srcClass, true);
|
||||
|
||||
Reference in New Issue
Block a user