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

优化一波代码。

上级 2ecd0e16
...@@ -33,7 +33,7 @@ public class ByteCodeLoader extends SecureClassLoader { ...@@ -33,7 +33,7 @@ public class ByteCodeLoader extends SecureClassLoader {
/** /**
* Map which represents class name and its compiled java object * Map which represents class name and its compiled java object
*/ */
private static final ConcurrentMap<String, Class<?>> javaFileObjectMap = new ConcurrentHashMap<>(); private static final ConcurrentMap<String, Class<?>> JAVA_FILE_OBJECT_MAP = new ConcurrentHashMap<>();
private final String className; private final String className;
private final byte[] byteCode; private final byte[] byteCode;
...@@ -68,7 +68,7 @@ public class ByteCodeLoader extends SecureClassLoader { ...@@ -68,7 +68,7 @@ public class ByteCodeLoader extends SecureClassLoader {
*/ */
public static Class<?> load(String className, byte[] byteCode) { public static Class<?> load(String className, byte[] byteCode) {
CheckedFunction<String, Class<?>> classLoadFunc = (key) -> new ByteCodeLoader(key, byteCode).loadClass(className); CheckedFunction<String, Class<?>> classLoadFunc = (key) -> new ByteCodeLoader(key, byteCode).loadClass(className);
return CollectionUtil.computeIfAbsent(javaFileObjectMap, className, Unchecked.function(classLoadFunc)); return CollectionUtil.computeIfAbsent(JAVA_FILE_OBJECT_MAP, className, Unchecked.function(classLoadFunc));
} }
/** /**
......
...@@ -506,12 +506,12 @@ public class BeanUtil extends org.springframework.beans.BeanUtils { ...@@ -506,12 +506,12 @@ public class BeanUtil extends org.springframework.beans.BeanUtils {
Assert.notNull(src, "diff Map src is null."); Assert.notNull(src, "diff Map src is null.");
Assert.notNull(src, "diff Map dist is null."); Assert.notNull(src, "diff Map dist is null.");
// 改变 // 改变
Map<String, Object> difference = new HashMap<>(); Map<String, Object> difference = new HashMap<>(8);
difference.putAll(src); difference.putAll(src);
difference.putAll(dist); difference.putAll(dist);
difference.entrySet().removeAll(src.entrySet()); difference.entrySet().removeAll(src.entrySet());
// 老值 // 老值
Map<String, Object> oldValues = new HashMap<>(); Map<String, Object> oldValues = new HashMap<>(8);
difference.keySet().forEach((k) -> oldValues.put(k, src.get(k))); difference.keySet().forEach((k) -> oldValues.put(k, src.get(k)));
BeanDiff diff = new BeanDiff(); BeanDiff diff = new BeanDiff();
diff.getFields().addAll(difference.keySet()); diff.getFields().addAll(difference.keySet());
......
...@@ -32,7 +32,7 @@ public class CountMap { ...@@ -32,7 +32,7 @@ public class CountMap {
private final Map<Object, AtomicLong> data; private final Map<Object, AtomicLong> data;
public CountMap() { public CountMap() {
this(new HashMap<>()); this(new HashMap<>(8));
} }
/** /**
......
...@@ -14,9 +14,6 @@ import java.nio.charset.StandardCharsets; ...@@ -14,9 +14,6 @@ import java.nio.charset.StandardCharsets;
* @author chenxin<chenxin619315 @ gmail.com> * @author chenxin<chenxin619315 @ gmail.com>
*/ */
public class DbSearcher { public class DbSearcher {
public static final int BTREE_ALGORITHM = 1;
public static final int BINARY_ALGORITHM = 2;
public static final int MEMORY_ALGORITYM = 3;
/** /**
* db config * db config
...@@ -274,7 +271,9 @@ public class DbSearcher { ...@@ -274,7 +271,9 @@ public class DbSearcher {
} }
//not matched //not matched
if (dataptr == 0) return null; if (dataptr == 0) {
return null;
}
//3. get the data //3. get the data
int dataLen = (int) ((dataptr >> 24) & 0xFF); int dataLen = (int) ((dataptr >> 24) & 0xFF);
...@@ -342,7 +341,9 @@ public class DbSearcher { ...@@ -342,7 +341,9 @@ public class DbSearcher {
} }
//not matched //not matched
if (dataptr == 0) return null; if (dataptr == 0) {
return null;
}
//get the data //get the data
int dataLen = (int) ((dataptr >> 24) & 0xFF); int dataLen = (int) ((dataptr >> 24) & 0xFF);
......
...@@ -85,7 +85,9 @@ public class Ip2regionUtil { ...@@ -85,7 +85,9 @@ public class Ip2regionUtil {
*/ */
public static long ip2long(String ip) { public static long ip2long(String ip) {
String[] p = ip.split("\\."); String[] p = ip.split("\\.");
if (p.length != 4) return 0; if (p.length != 4) {
return 0;
}
int p1 = ((Integer.parseInt(p[0]) << 24) & 0xFF000000); int p1 = ((Integer.parseInt(p[0]) << 24) & 0xFF000000);
int p2 = ((Integer.parseInt(p[1]) << 16) & 0x00FF0000); int p2 = ((Integer.parseInt(p[1]) << 16) & 0x00FF0000);
...@@ -116,12 +118,18 @@ public class Ip2regionUtil { ...@@ -116,12 +118,18 @@ public class Ip2regionUtil {
*/ */
public static boolean isIpAddress(String ip) { public static boolean isIpAddress(String ip) {
String[] p = ip.split("\\."); String[] p = ip.split("\\.");
if (p.length != 4) return false; if (p.length != 4) {
return false;
}
for (String pp : p) { for (String pp : p) {
if (pp.length() > 3) return false; if (pp.length() > 3) {
return false;
}
int val = Integer.parseInt(pp); int val = Integer.parseInt(pp);
if (val > 255) return false; if (val > 255) {
return false;
}
} }
return true; return true;
......
...@@ -116,7 +116,7 @@ public class MicaTemplate implements ApplicationContextAware, InitializingBean { ...@@ -116,7 +116,7 @@ public class MicaTemplate implements ApplicationContextAware, InitializingBean {
final ScriptEngineManager engineManager = new ScriptEngineManager(); final ScriptEngineManager engineManager = new ScriptEngineManager();
final ScriptEngine engine = engineManager.getEngineByMimeType("text/javascript"); final ScriptEngine engine = engineManager.getEngineByMimeType("text/javascript");
Bindings bindings = engine.createBindings(); Bindings bindings = engine.createBindings();
Map<String, String> config = new HashMap<>(2); Map<String, String> config = new HashMap<>(4);
config.put("open", tplProperties.getOpen()); config.put("open", tplProperties.getOpen());
config.put("close", tplProperties.getClose()); config.put("close", tplProperties.getClose());
bindings.put("console", console); bindings.put("console", console);
......
...@@ -59,7 +59,7 @@ public class LoggingJsonFileAppender implements ILoggingAppender { ...@@ -59,7 +59,7 @@ public class LoggingJsonFileAppender implements ILoggingAppender {
String logDir = environment.getProperty("logging.file.path", LoggingUtil.DEFAULT_LOG_DIR); String logDir = environment.getProperty("logging.file.path", LoggingUtil.DEFAULT_LOG_DIR);
this.logAllFile = logDir + CharPool.SLASH + appName + CharPool.SLASH + LoggingUtil.LOG_FILE_ALL; this.logAllFile = logDir + CharPool.SLASH + appName + CharPool.SLASH + LoggingUtil.LOG_FILE_ALL;
// 4. json 自定义字段 // 4. json 自定义字段
Map<String, Object> customFields = new HashMap<>(); Map<String, Object> customFields = new HashMap<>(4);
customFields.put("appName", appName); customFields.put("appName", appName);
customFields.put("profile", profile); customFields.put("profile", profile);
this.customFieldsJson = JsonUtil.toJson(customFields); this.customFieldsJson = JsonUtil.toJson(customFields);
......
...@@ -49,7 +49,7 @@ public class LoggingLogStashAppender implements ILoggingAppender { ...@@ -49,7 +49,7 @@ public class LoggingLogStashAppender implements ILoggingAppender {
String appName = environment.getRequiredProperty(MicaConstant.SPRING_APP_NAME_KEY); String appName = environment.getRequiredProperty(MicaConstant.SPRING_APP_NAME_KEY);
String profile = environment.getRequiredProperty(MicaConstant.ACTIVE_PROFILES_PROPERTY); String profile = environment.getRequiredProperty(MicaConstant.ACTIVE_PROFILES_PROPERTY);
// 2. json 自定义字段 // 2. json 自定义字段
Map<String, Object> customFields = new HashMap<>(); Map<String, Object> customFields = new HashMap<>(4);
customFields.put("appName", appName); customFields.put("appName", appName);
customFields.put("profile", profile); customFields.put("profile", profile);
this.customFieldsJson = JsonUtil.toJson(customFields); this.customFieldsJson = JsonUtil.toJson(customFields);
......
...@@ -22,6 +22,10 @@ import org.springframework.boot.SpringApplication; ...@@ -22,6 +22,10 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import java.util.HashMap;
import java.util.Map;
/** /**
* logging 日志初始化 * logging 日志初始化
...@@ -30,13 +34,21 @@ import org.springframework.core.env.ConfigurableEnvironment; ...@@ -30,13 +34,21 @@ import org.springframework.core.env.ConfigurableEnvironment;
*/ */
@AutoEnvPostProcessor @AutoEnvPostProcessor
public class LoggingInitializer implements EnvironmentPostProcessor, Ordered { public class LoggingInitializer implements EnvironmentPostProcessor, Ordered {
public static final String LOGGING_FILE_PATH_KEY = "logging.file.path";
public static final String LOGGING_FILE_NAME_KEY = "logging.file.name";
public static final String MICA_LOGGING_PROPERTY_SOURCE_NAME = "micaLoggingPropertySource";
@Override @Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
// 读取系统配置的日志目录,默认为项目下 logs // 读取系统配置的日志目录,默认为项目下 logs
String logBase = environment.getProperty("logging.file.path", LoggingUtil.DEFAULT_LOG_DIR); String logBase = environment.getProperty(LOGGING_FILE_PATH_KEY, LoggingUtil.DEFAULT_LOG_DIR);
// 用于 spring boot admin 中展示日志 // 用于 spring boot admin 中展示日志
System.setProperty("logging.file.name", logBase + "/${spring.application.name}/" + LoggingUtil.LOG_FILE_ALL); if (!environment.containsProperty(LOGGING_FILE_NAME_KEY)) {
Map<String, Object> map = new HashMap<>(2);
map.put(LOGGING_FILE_NAME_KEY, logBase + "/${spring.application.name}/" + LoggingUtil.LOG_FILE_ALL);
MapPropertySource propertySource = new MapPropertySource(MICA_LOGGING_PROPERTY_SOURCE_NAME, map);
environment.getPropertySources().addLast(propertySource);
}
} }
@Override @Override
......
...@@ -126,7 +126,7 @@ public class MicaLoggingConfiguration { ...@@ -126,7 +126,7 @@ public class MicaLoggingConfiguration {
@Order(Ordered.HIGHEST_PRECEDENCE) @Order(Ordered.HIGHEST_PRECEDENCE)
private static class LoggingCondition extends SpringBootCondition { private static class LoggingCondition extends SpringBootCondition {
private static final String LOG_STASH_CLASS_NAME = "net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder"; private static final String LOG_STASH_CLASS_NAME = "net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder";
private static final String Loki_CLASS_NAME = "com.github.loki4j.logback.Loki4jAppender"; private static final String LOKI_CLASS_NAME = "com.github.loki4j.logback.Loki4jAppender";
@Override @Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
...@@ -179,7 +179,7 @@ public class MicaLoggingConfiguration { ...@@ -179,7 +179,7 @@ public class MicaLoggingConfiguration {
} }
private static boolean hasLokiDependencies(ClassLoader classLoader) { private static boolean hasLokiDependencies(ClassLoader classLoader) {
return ClassUtils.isPresent(Loki_CLASS_NAME, classLoader); return ClassUtils.isPresent(LOKI_CLASS_NAME, classLoader);
} }
} }
......
...@@ -61,7 +61,7 @@ public class DruidMetricsConfiguration { ...@@ -61,7 +61,7 @@ public class DruidMetricsConfiguration {
@Bean @Bean
public DruidMetrics druidMetrics(ObjectProvider<Map<String, DataSource>> dataSourcesProvider) { public DruidMetrics druidMetrics(ObjectProvider<Map<String, DataSource>> dataSourcesProvider) {
Map<String, DataSource> dataSourceMap = dataSourcesProvider.getIfAvailable(HashMap::new); Map<String, DataSource> dataSourceMap = dataSourcesProvider.getIfAvailable(HashMap::new);
Map<String, DruidDataSource> druidDataSourceMap = new HashMap<>(1); Map<String, DruidDataSource> druidDataSourceMap = new HashMap<>(2);
dataSourceMap.forEach((name, dataSource) -> { dataSourceMap.forEach((name, dataSource) -> {
// 保证连接池数据和 DataSourcePoolMetadataProvider 的一致 // 保证连接池数据和 DataSourcePoolMetadataProvider 的一致
druidDataSourceMap.put(getDataSourceName(name), DataSourceUnwrapper.unwrap(dataSource, DruidDataSource.class)); druidDataSourceMap.put(getDataSourceName(name), DataSourceUnwrapper.unwrap(dataSource, DruidDataSource.class));
......
...@@ -33,14 +33,14 @@ public enum HostCookieJar implements CookieJar { ...@@ -33,14 +33,14 @@ public enum HostCookieJar implements CookieJar {
*/ */
INSTANCE; INSTANCE;
private static final Map<String, Map<String, Cookie>> COOKIE_MAP = new HashMap<>(); private static final Map<String, Map<String, Cookie>> COOKIE_MAP = new HashMap<>(8);
@Override @Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) { public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
String host = url.host(); String host = url.host();
Map<String, Cookie> cookieMap = COOKIE_MAP.get(host); Map<String, Cookie> cookieMap = COOKIE_MAP.get(host);
if (cookieMap == null) { if (cookieMap == null) {
cookieMap = new HashMap<>(); cookieMap = new HashMap<>(8);
} }
// 便于新 cookie 替换老的 cookie // 便于新 cookie 替换老的 cookie
for (Cookie cookie : cookies) { for (Cookie cookie : cookies) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册