Ver Fonte

增加ModuleUtils,用于获取类所在模块的信息

zhouhao há 6 anos atrás
pai
commit
d52f972e5b

+ 14 - 0
hsweb-commons/hsweb-commons-utils/pom.xml

@@ -32,6 +32,15 @@
 
     <description>通用模块-工具类</description>
 
+    <build>
+        <testResources>
+            <testResource>
+                <directory>src/test/resources</directory>
+                <filtering>true</filtering>
+            </testResource>
+        </testResources>
+    </build>
+
     <dependencies>
 
         <dependency>
@@ -71,5 +80,10 @@
             <artifactId>hsweb-easy-orm-core</artifactId>
             <scope>test</scope>
         </dependency>
+
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+        </dependency>
     </dependencies>
 </project>

+ 111 - 0
hsweb-commons/hsweb-commons-utils/src/main/java/org/hswebframework/web/ModuleUtils.java

@@ -0,0 +1,111 @@
+package org.hswebframework.web;
+
+import com.alibaba.fastjson.JSON;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
+import org.springframework.util.StringUtils;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * @author zhouhao
+ * @since 3.0.6
+ */
+@Slf4j
+public abstract class ModuleUtils {
+
+    private ModuleUtils() {
+
+    }
+
+    private final static Map<Class, ModuleInfo> classModuleInfoRepository;
+
+    private final static Map<String, ModuleInfo> nameModuleInfoRepository;
+
+    static {
+        classModuleInfoRepository = new ConcurrentHashMap<>();
+        nameModuleInfoRepository = new ConcurrentHashMap<>();
+        try {
+            log.info("init module info");
+            Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:/hsweb-module.json");
+            for (Resource resource : resources) {
+                String classPath = getClassPath(resource.getURL().getPath(), "hsweb-module.json");
+                ModuleInfo moduleInfo = JSON.parseObject(resource.getInputStream(), ModuleInfo.class);
+                moduleInfo.setClassPath(classPath);
+                ModuleUtils.register(moduleInfo);
+            }
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+        }
+    }
+
+    public static ModuleInfo getModuleByClass(Class type) {
+        return classModuleInfoRepository.computeIfAbsent(type, ModuleUtils::parse);
+    }
+
+    public static String getClassPath(Class type) {
+        String path = type.getResource("").getPath();
+        String packages = type.getPackage().getName();
+        return getClassPath(path, packages);
+    }
+
+    public static String getClassPath(String path, String packages) {
+        int pos = path.contains("!/") ? 3 : path.endsWith("/") ? 2 : 1;
+        return path.substring(0, path.length() - packages.length() - pos);
+    }
+
+    private static ModuleInfo parse(Class type) {
+        String classpath = getClassPath(type);
+        return nameModuleInfoRepository.values()
+                .stream()
+                .filter(moduleInfo -> classpath.equals(moduleInfo.classPath))
+                .findFirst()
+                .orElse(noneInfo);
+    }
+
+    public static ModuleInfo getModule(String id) {
+        return nameModuleInfoRepository.get(id);
+    }
+
+    public static void register(ModuleInfo moduleInfo) {
+        nameModuleInfoRepository.put(moduleInfo.getId(), moduleInfo);
+    }
+
+    private static final ModuleInfo noneInfo = new ModuleInfo();
+
+    @Getter
+    @Setter
+    public static class ModuleInfo {
+
+        private String classPath;
+
+        private String id;
+
+        private String groupId;
+
+        private String artifactId;
+
+        private String gitCommitHash;
+
+        private String gitRepository;
+
+        private String comment;
+
+        private String version;
+
+        public String getId() {
+            if (StringUtils.isEmpty(id)) {
+                id = groupId + "/" + artifactId;
+            }
+            return id;
+        }
+
+        public boolean isNone() {
+            return StringUtils.isEmpty(classPath);
+        }
+    }
+}

+ 26 - 0
hsweb-commons/hsweb-commons-utils/src/test/java/org/hswebframework/web/ModuleUtilsTest.java

@@ -0,0 +1,26 @@
+package org.hswebframework.web;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.slf4j.Logger;
+
+import static org.junit.Assert.*;
+
+/**
+ * @author zhouhao
+ * @since 3.0.6
+ */
+public class ModuleUtilsTest {
+
+    @Test
+    public void test() {
+        ModuleUtils.ModuleInfo moduleInfo = ModuleUtils.getModuleByClass(ModuleUtilsTest.class);
+        Assert.assertNotNull(moduleInfo);
+        Assert.assertFalse(moduleInfo.isNone());
+        Assert.assertEquals(moduleInfo.getArtifactId(),"hsweb-commons-utils");
+
+        ModuleUtils.ModuleInfo noneInfo = ModuleUtils.getModuleByClass(Logger.class);
+        Assert.assertNotNull(noneInfo);
+        Assert.assertTrue(noneInfo.isNone());
+    }
+}

+ 8 - 0
hsweb-commons/hsweb-commons-utils/src/test/resources/hsweb-module.json

@@ -0,0 +1,8 @@
+{
+  "groupId": "${project.groupId}",
+  "artifactId": "${project.artifactId}",
+  "parent": "${parent.groupId}/${parent.artifactId}",
+  "gitCommitHash": "${git.commit.hash}",
+  "gitRepository": "http://github.com/hs-web/hsweb-framework",
+  "version": "${project.version}"
+}