Skip to content

Commit

Permalink
[Optimize] [Web] add jar group (#3092)
Browse files Browse the repository at this point in the history
Co-authored-by: zackyoungh <[email protected]>
  • Loading branch information
zackyoungh and zackyoungh authored Jan 30, 2024
1 parent 3243e9d commit 55d20ea
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.dinky.service.SystemService;

import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -86,7 +87,7 @@ public Result<String> readFile(@RequestParam String path) {

@GetMapping("/queryAllClassLoaderJarFiles")
@ApiOperation("Query All ClassLoader Jar Files")
public Result<List<String>> queryAllClassLoaderJarFiles() {
public Result<Map<String, List<String>>> queryAllClassLoaderJarFiles() {
return Result.succeed(systemService.queryAllClassLoaderJarFiles());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.dinky.data.model.ext.FileNode;

import java.util.List;
import java.util.Map;

/**
* SystemService
Expand All @@ -46,5 +47,5 @@ public interface SystemService {
*/
String readFile(String path);

List<String> queryAllClassLoaderJarFiles();
Map<String, List<String>> queryAllClassLoaderJarFiles();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,39 @@
import org.dinky.utils.TreeUtil;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import cn.hutool.core.util.ClassLoaderUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Singleton;
import cn.hutool.core.map.MapUtil;
import cn.hutool.system.JavaRuntimeInfo;
import lombok.extern.slf4j.Slf4j;

/**
* SystemServiceImpl
*
* @since 2022/10/15 19:17
*/
@Service
@Slf4j
public class SystemServiceImpl implements SystemService {
private static final Map<String, List<String>> JAR_GROUP_MAP = MapUtil.builder(new HashMap<String, List<String>>())
.put("Dinky", Arrays.asList("org.dinky"))
.put("Flink", Arrays.asList("org.apache.flink"))
.put("CDC", Arrays.asList("com.ververica"))
.build();

@Override
public List<TreeNodeDTO> listLogDir() {
Expand All @@ -58,16 +75,41 @@ public String readFile(String path) {
* @return
*/
@Override
public List<String> queryAllClassLoaderJarFiles() {
List<String> jarFiles = new ArrayList<>();
ClassLoader systemClassLoader = ClassLoaderUtil.getSystemClassLoader();
if (systemClassLoader instanceof URLClassLoader) {
URLClassLoader urlClassLoader = (URLClassLoader) systemClassLoader;
URL[] urlClassLoaderURLs = urlClassLoader.getURLs();
for (URL url : urlClassLoaderURLs) {
jarFiles.add(url.getFile());
public Map<String, List<String>> queryAllClassLoaderJarFiles() {
Map<String, List<String>> result = new LinkedHashMap<>();
filterJarGroup(result);
return result;
}

private static void filterJarGroup(Map<String, List<String>> result) {
List<String> pathList =
Arrays.asList(Singleton.get(JavaRuntimeInfo.class).getClassPathArray());
result.put("System", pathList);
List<File> jarList = pathList.stream()
.map(FileUtil::file)
.filter(x -> FileUtil.pathEndsWith(x, ".jar"))
.collect(Collectors.toList());
if (CollUtil.isNotEmpty(jarList)) {
for (File file : jarList) {
try (JarFile jarFile = new JarFile(file)) {
Manifest manifest = jarFile.getManifest();
if (manifest == null) {
continue;
}
Attributes attributes = manifest.getMainAttributes();
if (attributes != null) {
String groupId = attributes.getValue("Implementation-Vendor-Id");
JAR_GROUP_MAP.forEach((k, v) -> {
if (v.contains(groupId)) {
result.computeIfAbsent(k, key -> new ArrayList<>());
result.get(k).add(file.getAbsolutePath());
}
});
}
} catch (Exception e) {
log.error("get All ClassLoaderJarFiles error", e);
}
}
}
return jarFiles;
}
}
27 changes: 23 additions & 4 deletions dinky-web/src/pages/SettingCenter/ClassLoaderJars/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,41 @@
import CodeShow from '@/components/CustomEditor/CodeShow';
import { queryClassLoaderJars } from '@/pages/SettingCenter/ClassLoaderJars/service';
import { l } from '@/utils/intl';
import { Alert, Space } from 'antd';
import { Alert, Space, Tabs } from 'antd';
import { useEffect, useState } from 'react';

export default () => {
const [data, setData] = useState<string>('');
const [data, setData] = useState<Record<string, string[]>>();

useEffect(() => {
queryClassLoaderJars().then((res) => {
if (res && res.length > 0) setData(res.join('\n'));
if (res) setData(res);
});
}, []);

return (
<Space size={'large'} direction={'vertical'}>
<Alert message={l('sys.classLoaderJars.tips')} type='info' showIcon />
<CodeShow showFloatButton enableMiniMap height={'88vh'} code={data} language={'java'} />
{data && (
<Tabs
defaultActiveKey='1'
items={Object.keys(data).map((key) => {
return {
key: key,
label: key,
children: (
<CodeShow
showFloatButton
enableMiniMap
height={'75vh'}
code={data[key].join('\n')}
language={'java'}
/>
)
};
})}
/>
)}
</Space>
);
};
6 changes: 5 additions & 1 deletion dinky-web/src/pages/SettingCenter/ClassLoaderJars/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@
import { queryDataByParams } from '@/services/BusinessCrud';

export const queryClassLoaderJars = async () => {
return (await queryDataByParams<string[]>('/api/system/queryAllClassLoaderJarFiles')) ?? [];
return (
(await queryDataByParams<Record<string, string[]>>(
'/api/system/queryAllClassLoaderJarFiles'
)) ?? {}
);
};

0 comments on commit 55d20ea

Please sign in to comment.