提交 6600c15d 编写于 作者: 如梦技术's avatar 如梦技术 🐛

代码优化

上级 f847d4bc
......@@ -128,7 +128,7 @@ public class ActiveRecordTxAspect {
if (txConfig != null) {
Config config = DbKit.getConfig(txConfig.value());
if (config == null) {
throw new RuntimeException("Config not found with TxConfig: " + txConfig.value());
throw new IllegalArgumentException("Config not found with @TxConfig: " + txConfig.value());
}
return config;
}
......
......@@ -19,11 +19,11 @@ package net.dreamlu.mica.caffeine.config;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import net.dreamlu.mica.core.utils.ReflectUtil;
import net.dreamlu.mica.core.utils.StringPool;
import org.springframework.boot.convert.DurationStyle;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.lang.Nullable;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.time.Duration;
......@@ -39,7 +39,7 @@ public class CaffeineAutoCacheManager extends CaffeineCacheManager {
private static final Field CACHE_LOADER_FIELD;
static {
CACHE_LOADER_FIELD = Objects.requireNonNull(ReflectUtil.getField(CaffeineCacheManager.class, "cacheLoader"));
CACHE_LOADER_FIELD = Objects.requireNonNull(ReflectionUtils.findField(CaffeineCacheManager.class, "cacheLoader"));
CACHE_LOADER_FIELD.setAccessible(true);
}
......@@ -57,11 +57,7 @@ public class CaffeineAutoCacheManager extends CaffeineCacheManager {
@Nullable
@SuppressWarnings("unchecked")
protected CacheLoader<Object, Object> getCacheLoader() {
try {
return (CacheLoader<Object, Object>) CACHE_LOADER_FIELD.get(this);
} catch (IllegalAccessException e) {
return null;
}
return (CacheLoader<Object, Object>) ReflectionUtils.getField(CACHE_LOADER_FIELD, this);
}
@Override
......
......@@ -38,8 +38,8 @@ public enum DisableValidationTrustManager implements X509TrustManager {
*
* @return TrustManager 数组
*/
public static TrustManager[] getTrustManagers() {
return new TrustManager[]{DisableValidationTrustManager.INSTANCE};
public TrustManager[] getTrustManagers() {
return new TrustManager[]{this};
}
@Override
......
......@@ -17,58 +17,38 @@
package net.dreamlu.mica.http;
import lombok.extern.slf4j.Slf4j;
import okhttp3.internal.annotations.EverythingIsNonNull;
import okhttp3.logging.HttpLoggingInterceptor;
import javax.annotation.Nonnull;
/**
* OkHttp logger, Slf4j and console log.
*
* @author L.cm
*/
public enum HttpLogger {
@Slf4j
public enum HttpLogger implements HttpLoggingInterceptor.Logger {
/**
* http 日志:Slf4j
*/
Slf4j(new Slf4jLogger()),
/**
* http 日志:Console
*/
Console(new ConsoleLogger());
private final HttpLoggingInterceptor.Logger logger;
HttpLogger(HttpLoggingInterceptor.Logger logger) {
this.logger = logger;
}
public HttpLoggingInterceptor.Logger getLogger() {
return logger;
}
/**
* Slf4j日志
*/
@Slf4j
@EverythingIsNonNull
public static class Slf4jLogger implements HttpLoggingInterceptor.Logger {
Slf4j() {
@Override
public void log(String message) {
public void log(@Nonnull String message) {
log.info(message);
}
}
},
/**
* 控制台日志
* http 日志:Console
*/
@EverythingIsNonNull
public static class ConsoleLogger implements HttpLoggingInterceptor.Logger {
Console() {
@Override
public void log(String message) {
public void log(@Nonnull String message) {
// 统一添加前缀,方便在茫茫日志中查看
System.out.print("ConsoleLogger: ");
System.out.println(message);
}
}
};
}
......@@ -66,7 +66,7 @@ public class HttpRequest {
@Nullable
private Boolean followSslRedirects;
@Nullable
private HttpLogger httpLogger;
private HttpLoggingInterceptor.Logger logger;
@Nullable
private HttpLoggingInterceptor.Level logLevel;
@Nullable
......@@ -281,8 +281,8 @@ public class HttpRequest {
if (retryPolicy != null) {
builder.addInterceptor(new RetryInterceptor(retryPolicy));
}
if (httpLogger != null && logLevel != null && HttpLoggingInterceptor.Level.NONE != logLevel) {
builder.addInterceptor(getLoggingInterceptor(httpLogger, logLevel));
if (logger != null && logLevel != null && HttpLoggingInterceptor.Level.NONE != logLevel) {
builder.addInterceptor(getLoggingInterceptor(logger, logLevel));
} else if (globalLoggingInterceptor != null) {
builder.addInterceptor(globalLoggingInterceptor);
}
......@@ -376,34 +376,13 @@ public class HttpRequest {
return this;
}
private static HttpLoggingInterceptor getLoggingInterceptor(HttpLogger httpLogger, HttpLoggingInterceptor.Level level) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(httpLogger.getLogger());
private static HttpLoggingInterceptor getLoggingInterceptor(HttpLoggingInterceptor.Logger httpLogger,
HttpLoggingInterceptor.Level level) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(httpLogger);
loggingInterceptor.setLevel(level);
return loggingInterceptor;
}
/**
* 建议使用 useSlf4jLog 或者 useConsoleLog 方法
*
* @return HttpRequest
*/
@Deprecated
public HttpRequest log() {
this.logLevel = HttpLoggingInterceptor.Level.BODY;
return this;
}
/**
* 建议使用 logLevel 方法
*
* @return HttpRequest
*/
@Deprecated
public HttpRequest log(LogLevel logLevel) {
this.logLevel = logLevel.getLevel();
return this;
}
public HttpRequest useSlf4jLog() {
return useSlf4jLog(LogLevel.BODY);
}
......@@ -420,8 +399,8 @@ public class HttpRequest {
return useLog(HttpLogger.Console, logLevel);
}
public HttpRequest useLog(HttpLogger logger, LogLevel logLevel) {
this.httpLogger = logger;
public HttpRequest useLog(HttpLoggingInterceptor.Logger logger, LogLevel logLevel) {
this.logger = logger;
this.logLevel = logLevel.getLevel();
return this;
}
......@@ -553,7 +532,7 @@ public class HttpRequest {
setGlobalLog(HttpLogger.Slf4j, logLevel);
}
public static void setGlobalLog(HttpLogger logger, LogLevel logLevel) {
public static void setGlobalLog(HttpLoggingInterceptor.Logger logger, LogLevel logLevel) {
HttpRequest.globalLoggingInterceptor = getLoggingInterceptor(logger, logLevel.getLevel());
}
......@@ -569,10 +548,9 @@ public class HttpRequest {
private static void disableSslValidation(OkHttpClient.Builder builder) {
try {
X509TrustManager disabledTrustManager = DisableValidationTrustManager.INSTANCE;
TrustManager[] trustManagers = new TrustManager[]{disabledTrustManager};
DisableValidationTrustManager disabledTrustManager = DisableValidationTrustManager.INSTANCE;
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustManagers, Holder.SECURE_RANDOM);
sslContext.init(null, disabledTrustManager.getTrustManagers(), Holder.SECURE_RANDOM);
SSLSocketFactory disabledSslSocketFactory = sslContext.getSocketFactory();
builder.sslSocketFactory(disabledSslSocketFactory, disabledTrustManager);
builder.hostnameVerifier(TrustAllHostNames.INSTANCE);
......
......@@ -16,8 +16,8 @@
package net.dreamlu.mica.http;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import okhttp3.logging.HttpLoggingInterceptor;
/**
......@@ -26,7 +26,7 @@ import okhttp3.logging.HttpLoggingInterceptor;
* @author L.cm
*/
@Getter
@AllArgsConstructor
@RequiredArgsConstructor
public enum LogLevel {
/**
* No logs.
......@@ -84,5 +84,5 @@ public enum LogLevel {
*/
BODY(HttpLoggingInterceptor.Level.BODY);
private HttpLoggingInterceptor.Level level;
private final HttpLoggingInterceptor.Level level;
}
......@@ -22,7 +22,6 @@ import io.undertow.Undertow;
import io.undertow.server.ConnectorStatistics;
import io.undertow.server.session.SessionManagerStatistics;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.core.utils.Exceptions;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServer;
import org.springframework.boot.web.embedded.undertow.UndertowWebServer;
......@@ -243,11 +242,7 @@ public class UndertowMetrics implements ApplicationListener<ApplicationStartedEv
}
private static Undertow getUndertow(UndertowWebServer undertowWebServer) {
try {
return (Undertow) UNDERTOW_FIELD.get(undertowWebServer);
} catch (IllegalAccessException e) {
throw Exceptions.unchecked(e);
}
return (Undertow) ReflectionUtils.getField(UNDERTOW_FIELD, undertowWebServer);
}
private static UndertowWebServer findUndertowWebServer(ConfigurableApplicationContext applicationContext) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册