From a0f020729c9fa8fe9c53dd40f62168cab4c51480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=A2=A6=E6=8A=80=E6=9C=AF?= <596392912@qq.com> Date: Tue, 12 Mar 2024 20:36:57 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=AE=8C=E5=96=84=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/tool/jackson/JsonUtil.java | 454 +++++++++++++++++- .../core/tool/utils/ResourceUtil.java | 29 +- .../core/tool/utils/RuntimeUtil.java | 91 ++++ 3 files changed, 559 insertions(+), 15 deletions(-) create mode 100644 blade-core-tool/src/main/java/org/springblade/core/tool/utils/RuntimeUtil.java diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/jackson/JsonUtil.java b/blade-core-tool/src/main/java/org/springblade/core/tool/jackson/JsonUtil.java index 27da19c..d739494 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/jackson/JsonUtil.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/jackson/JsonUtil.java @@ -17,18 +17,21 @@ package org.springblade.core.tool.jackson; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.TreeNode; import com.fasterxml.jackson.core.json.JsonReadFeature; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.databind.type.CollectionLikeType; +import com.fasterxml.jackson.databind.type.MapType; import lombok.extern.slf4j.Slf4j; import org.springblade.core.tool.utils.*; import org.springframework.lang.Nullable; import java.io.IOException; import java.io.InputStream; +import java.io.Reader; import java.text.SimpleDateFormat; import java.time.ZoneId; import java.util.*; @@ -266,7 +269,6 @@ public class JsonUtil { } } - /** * 将json byte 数组反序列化成对象 * @@ -401,12 +403,452 @@ public class JsonUtil { } } + /** + * clazz 获取 JavaType + * + * @param clazz Class + * @return MapType + */ + public static JavaType getType(Class clazz) { + return getInstance().getTypeFactory().constructType(clazz); + } + + /** + * 封装 map type,keyClass String + * + * @param valueClass value 类型 + * @return MapType + */ + public static MapType getMapType(Class valueClass) { + return getMapType(String.class, valueClass); + } + + /** + * 封装 map type + * + * @param keyClass key 类型 + * @param valueClass value 类型 + * @return MapType + */ + public static MapType getMapType(Class keyClass, Class valueClass) { + return getInstance().getTypeFactory().constructMapType(Map.class, keyClass, valueClass); + } + + /** + * 封装 map type + * + * @param elementClass 集合值类型 + * @return CollectionLikeType + */ + public static CollectionLikeType getListType(Class elementClass) { + return getInstance().getTypeFactory().constructCollectionLikeType(List.class, elementClass); + } + + /** + * 封装参数化类型 + * + *

+ * 例如: Map.class, String.class, String.class 对应 Map[String, String] + *

+ * + * @param parametrized 泛型参数化 + * @param parameterClasses 泛型参数类型 + * @return JavaType + */ + public static JavaType getParametricType(Class parametrized, Class... parameterClasses) { + return getInstance().getTypeFactory().constructParametricType(parametrized, parameterClasses); + } + + /** + * 封装参数化类型,用来构造复杂的泛型 + * + *

+ * 例如: Map.class, String.class, String.class 对应 Map[String, String] + *

+ * + * @param parametrized 泛型参数化 + * @param parameterTypes 泛型参数类型 + * @return JavaType + */ + public static JavaType getParametricType(Class parametrized, JavaType... parameterTypes) { + return getInstance().getTypeFactory().constructParametricType(parametrized, parameterTypes); + } + + + /** + * 读取集合 + * + * @param content bytes + * @param elementClass elementClass + * @param 泛型 + * @return 集合 + */ + public static List readList(@Nullable byte[] content, Class elementClass) { + if (content == null || content.length == 0) { + return Collections.emptyList(); + } + try { + return getInstance().readValue(content, getListType(elementClass)); + } catch (IOException e) { + throw Exceptions.unchecked(e); + } + } + + /** + * 读取集合 + * + * @param content InputStream + * @param elementClass elementClass + * @param 泛型 + * @return 集合 + */ + public static List readList(@Nullable InputStream content, Class elementClass) { + if (content == null) { + return Collections.emptyList(); + } + try { + return getInstance().readValue(content, getListType(elementClass)); + } catch (IOException e) { + throw Exceptions.unchecked(e); + } + } + + /** + * 读取集合 + * + * @param reader java.io.Reader + * @param elementClass elementClass + * @param 泛型 + * @return 集合 + */ + public static List readList(@Nullable Reader reader, Class elementClass) { + if (reader == null) { + return Collections.emptyList(); + } + try { + return getInstance().readValue(reader, getListType(elementClass)); + } catch (IOException e) { + throw Exceptions.unchecked(e); + } + } + + /** + * 读取集合 + * + * @param content bytes + * @param elementClass elementClass + * @param 泛型 + * @return 集合 + */ + public static List readList(@Nullable String content, Class elementClass) { + if (StringUtil.isBlank(content)) { + return Collections.emptyList(); + } + try { + return getInstance().readValue(content, getListType(elementClass)); + } catch (IOException e) { + throw Exceptions.unchecked(e); + } + } + + /** + * 读取集合 + * + * @param content bytes + * @return 集合 + */ + public static Map readMap(@Nullable byte[] content) { + return readMap(content, Object.class); + } + + /** + * 读取集合 + * + * @param content InputStream + * @return 集合 + */ + public static Map readMap(@Nullable InputStream content) { + return readMap(content, Object.class); + } + + /** + * 读取集合 + * + * @param reader java.io.Reader + * @return 集合 + */ + public static Map readMap(@Nullable Reader reader) { + return readMap(reader, Object.class); + } + + /** + * 读取集合 + * + * @param content bytes + * @return 集合 + */ + public static Map readMap(@Nullable String content) { + return readMap(content, Object.class); + } + + /** + * 读取集合 + * + * @param content bytes + * @param valueClass 值类型 + * @param 泛型 + * @return 集合 + */ + public static Map readMap(@Nullable byte[] content, Class valueClass) { + return readMap(content, String.class, valueClass); + } + + /** + * 读取集合 + * + * @param content InputStream + * @param valueClass 值类型 + * @param 泛型 + * @return 集合 + */ + public static Map readMap(@Nullable InputStream content, Class valueClass) { + return readMap(content, String.class, valueClass); + } + + /** + * 读取集合 + * + * @param reader java.io.Reader + * @param valueClass 值类型 + * @param 泛型 + * @return 集合 + */ + public static Map readMap(@Nullable Reader reader, Class valueClass) { + return readMap(reader, String.class, valueClass); + } + + /** + * 读取集合 + * + * @param content bytes + * @param valueClass 值类型 + * @param 泛型 + * @return 集合 + */ + public static Map readMap(@Nullable String content, Class valueClass) { + return readMap(content, String.class, valueClass); + } + + /** + * 读取集合 + * + * @param content bytes + * @param keyClass key类型 + * @param valueClass 值类型 + * @param 泛型 + * @param 泛型 + * @return 集合 + */ + public static Map readMap(@Nullable byte[] content, Class keyClass, Class valueClass) { + if (content == null || content.length == 0) { + return Collections.emptyMap(); + } + try { + return getInstance().readValue(content, getMapType(keyClass, valueClass)); + } catch (IOException e) { + throw Exceptions.unchecked(e); + } + } + + /** + * 读取集合 + * + * @param content InputStream + * @param keyClass key类型 + * @param valueClass 值类型 + * @param 泛型 + * @param 泛型 + * @return 集合 + */ + public static Map readMap(@Nullable InputStream content, Class keyClass, Class valueClass) { + if (content == null) { + return Collections.emptyMap(); + } + try { + return getInstance().readValue(content, getMapType(keyClass, valueClass)); + } catch (IOException e) { + throw Exceptions.unchecked(e); + } + } + + /** + * 读取集合 + * + * @param reader java.io.Reader + * @param keyClass key类型 + * @param valueClass 值类型 + * @param 泛型 + * @param 泛型 + * @return 集合 + */ + public static Map readMap(@Nullable Reader reader, Class keyClass, Class valueClass) { + if (reader == null) { + return Collections.emptyMap(); + } + try { + return getInstance().readValue(reader, getMapType(keyClass, valueClass)); + } catch (IOException e) { + throw Exceptions.unchecked(e); + } + } + + /** + * 读取集合 + * + * @param content bytes + * @param keyClass key类型 + * @param valueClass 值类型 + * @param 泛型 + * @param 泛型 + * @return 集合 + */ + public static Map readMap(@Nullable String content, Class keyClass, Class valueClass) { + if (StringUtil.isBlank(content)) { + return Collections.emptyMap(); + } + try { + return getInstance().readValue(content, getMapType(keyClass, valueClass)); + } catch (IOException e) { + throw Exceptions.unchecked(e); + } + } + + /** + * jackson 的类型转换 + * + * @param fromValue 来源对象 + * @param toValueType 转换的类型 + * @param 泛型标记 + * @return 转换结果 + */ + public static T convertValue(Object fromValue, Class toValueType) { + return getInstance().convertValue(fromValue, toValueType); + } + + /** + * jackson 的类型转换 + * + * @param fromValue 来源对象 + * @param toValueType 转换的类型 + * @param 泛型标记 + * @return 转换结果 + */ + public static T convertValue(Object fromValue, JavaType toValueType) { + return getInstance().convertValue(fromValue, toValueType); + } + + /** + * jackson 的类型转换 + * + * @param fromValue 来源对象 + * @param toValueTypeRef 泛型类型 + * @param 泛型标记 + * @return 转换结果 + */ + public static T convertValue(Object fromValue, TypeReference toValueTypeRef) { + return getInstance().convertValue(fromValue, toValueTypeRef); + } + + /** + * tree 转对象 + * + * @param treeNode TreeNode + * @param valueType valueType + * @param 泛型标记 + * @return 转换结果 + */ + public static T treeToValue(TreeNode treeNode, Class valueType) { + try { + return getInstance().treeToValue(treeNode, valueType); + } catch (JsonProcessingException e) { + throw Exceptions.unchecked(e); + } + } + + /** + * tree 转对象 + * + * @param treeNode TreeNode + * @param valueType valueType + * @param 泛型标记 + * @return 转换结果 + */ + public static T treeToValue(TreeNode treeNode, JavaType valueType) { + try { + return getInstance().treeToValue(treeNode, valueType); + } catch (JsonProcessingException e) { + throw Exceptions.unchecked(e); + } + } + + /** + * 对象转 tree + * + * @param fromValue fromValue + * @param 泛型标记 + * @return 转换结果 + */ + public static T valueToTree(@Nullable Object fromValue) { + return getInstance().valueToTree(fromValue); + } + + /** + * 判断是否可以序列化 + * + * @param value 对象 + * @return 是否可以序列化 + */ + public static boolean canSerialize(@Nullable Object value) { + if (value == null) { + return true; + } + return getInstance().canSerialize(value.getClass()); + } + + /** + * 判断是否可以反序列化 + * + * @param type JavaType + * @return 是否可以反序列化 + */ + public static boolean canDeserialize(JavaType type) { + return getInstance().canDeserialize(type); + } + + /** + * 创建 ObjectNode + * + * @return ObjectNode + */ + public static ObjectNode createObjectNode() { + return getInstance().createObjectNode(); + } + + /** + * 创建 ArrayNode + * + * @return ArrayNode + */ + public static ArrayNode createArrayNode() { + return getInstance().createArrayNode(); + } + public static ObjectMapper getInstance() { return JacksonHolder.INSTANCE; } private static class JacksonHolder { - private static ObjectMapper INSTANCE = new JacksonObjectMapper(); + private static final ObjectMapper INSTANCE = new JacksonObjectMapper(); } public static class JacksonObjectMapper extends ObjectMapper { diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ResourceUtil.java b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ResourceUtil.java index 0b89acd..787d559 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ResourceUtil.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ResourceUtil.java @@ -15,11 +15,7 @@ */ package org.springblade.core.tool.utils; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.FileSystemResource; -import org.springframework.core.io.Resource; -import org.springframework.core.io.UrlResource; -import org.springframework.core.io.support.ResourcePatternResolver; +import org.springframework.core.io.*; import org.springframework.util.Assert; import java.io.IOException; @@ -42,7 +38,6 @@ public class ResourceUtil extends org.springframework.util.ResourceUtils { * 2. file: * 3. ftp: * 4. http: and https: - * 5. classpath*: * 6. C:/dir1/ and /Users/lcm *

* @@ -53,7 +48,8 @@ public class ResourceUtil extends org.springframework.util.ResourceUtils { public static Resource getResource(String resourceLocation) throws IOException { Assert.notNull(resourceLocation, "Resource location must not be null"); if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) { - return new ClassPathResource(resourceLocation); + String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length()); + return new ClassPathResource(path); } if (resourceLocation.startsWith(FTP_URL_PREFIX)) { return new UrlResource(resourceLocation); @@ -61,10 +57,25 @@ public class ResourceUtil extends org.springframework.util.ResourceUtils { if (resourceLocation.matches(HTTP_REGEX)) { return new UrlResource(resourceLocation); } - if (resourceLocation.startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX)) { - return SpringUtil.getContext().getResource(resourceLocation); + if (resourceLocation.startsWith(FILE_URL_PREFIX)) { + return new FileUrlResource(resourceLocation); } return new FileSystemResource(resourceLocation); } + /** + * 读取资源文件为字符串 + * + * @param resourceLocation 资源文件地址 + * @return 字符串 + */ + public static String getAsString(String resourceLocation) { + try { + Resource resource = getResource(resourceLocation); + return IoUtil.toString(resource.getInputStream()); + } catch (IOException e) { + throw Exceptions.unchecked(e); + } + } + } diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/RuntimeUtil.java b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/RuntimeUtil.java new file mode 100644 index 0000000..f430c52 --- /dev/null +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/RuntimeUtil.java @@ -0,0 +1,91 @@ +/** + * Copyright (c) 2018-2028, DreamLu 卢春梦 (qq596392912@gmail.com). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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.springblade.core.tool.utils; + + +import java.lang.management.ManagementFactory; +import java.time.Duration; +import java.time.Instant; +import java.util.List; + +/** + * 运行时工具类 + * + * @author L.cm + */ +public class RuntimeUtil { + private static volatile int pId = -1; + private static final int CPU_NUM = Runtime.getRuntime().availableProcessors(); + + /** + * 获得当前进程的PID + *

+ * 当失败时返回-1 + * + * @return pid + */ + public static int getPId() { + if (pId > 0) { + return pId; + } + // something like '@', at least in SUN / Oracle JVMs + final String jvmName = ManagementFactory.getRuntimeMXBean().getName(); + final int index = jvmName.indexOf(CharPool.AT); + if (index > 0) { + pId = NumberUtil.toInt(jvmName.substring(0, index), -1); + return pId; + } + return pId; + } + + /** + * 返回应用启动的时间 + * + * @return {Instant} + */ + public static Instant getStartTime() { + return Instant.ofEpochMilli(ManagementFactory.getRuntimeMXBean().getStartTime()); + } + + /** + * 返回应用启动到现在的时间 + * + * @return {Duration} + */ + public static Duration getUpTime() { + return Duration.ofMillis(ManagementFactory.getRuntimeMXBean().getUptime()); + } + + /** + * 返回输入的JVM参数列表 + * + * @return jvm参数 + */ + public static String getJvmArguments() { + List vmArguments = ManagementFactory.getRuntimeMXBean().getInputArguments(); + return StringUtil.join(vmArguments, StringPool.SPACE); + } + + /** + * 获取CPU核数 + * + * @return cpu count + */ + public static int getCpuNum() { + return CPU_NUM; + } + +}