feat: add get driver class name api
This commit is contained in:
parent
b575a99392
commit
d9eadde676
|
@ -2,4 +2,5 @@
|
||||||
**/build/**
|
**/build/**
|
||||||
.idea/**
|
.idea/**
|
||||||
**/.DS_Store
|
**/.DS_Store
|
||||||
drivers/**
|
drivers/**
|
||||||
|
temp/**
|
|
@ -72,4 +72,12 @@ public class DatabaseTypeController {
|
||||||
Optional<DatabaseTypeDetailResponse> data = databaseTypeService.selectOne(id);
|
Optional<DatabaseTypeDetailResponse> data = databaseTypeService.selectOne(id);
|
||||||
return JsonData.ok(data);
|
return JsonData.ok(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping(Routes.DatabaseType.RESOLVE_DRIVER_CLASS_NAME)
|
||||||
|
@PreAuthorize("hasAnyAuthority('SYS_OWNER')")
|
||||||
|
public JsonData<String> resolveDriverClassName(@RequestBody @Valid DriverClassNameResolveRequest request) {
|
||||||
|
String driverClassName = databaseTypeService.resolveDriverClassName(request);
|
||||||
|
return JsonData.ok(driverClassName);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -163,6 +163,8 @@ public interface Routes {
|
||||||
String UPDATE = BASE + "/database_types";
|
String UPDATE = BASE + "/database_types";
|
||||||
|
|
||||||
String CREATE = BASE + "/database_types";
|
String CREATE = BASE + "/database_types";
|
||||||
|
|
||||||
|
String RESOLVE_DRIVER_CLASS_NAME = BASE + "/database_types/driver_class_name";
|
||||||
}
|
}
|
||||||
|
|
||||||
interface MockData {
|
interface MockData {
|
||||||
|
|
|
@ -37,6 +37,8 @@ subprojects {
|
||||||
jacksonVersion = '2.13.1'
|
jacksonVersion = '2.13.1'
|
||||||
easyExcelVersion = '3.0.5'
|
easyExcelVersion = '3.0.5'
|
||||||
freemarkerVersion = '2.3.31'
|
freemarkerVersion = '2.3.31'
|
||||||
|
retrofitVersion = '2.9.0'
|
||||||
|
commonsIoVersion = '2.11.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
@ -52,6 +54,7 @@ subprojects {
|
||||||
implementation "org.slf4j:slf4j-api:${slf4jVersion}"
|
implementation "org.slf4j:slf4j-api:${slf4jVersion}"
|
||||||
implementation "com.alibaba:easyexcel:${easyExcelVersion}"
|
implementation "com.alibaba:easyexcel:${easyExcelVersion}"
|
||||||
implementation "org.freemarker:freemarker:${freemarkerVersion}"
|
implementation "org.freemarker:freemarker:${freemarkerVersion}"
|
||||||
|
implementation "commons-io:commons-io:${commonsIoVersion}"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,11 +30,12 @@ dependencies {
|
||||||
implementation 'com.auth0:java-jwt:3.18.3'
|
implementation 'com.auth0:java-jwt:3.18.3'
|
||||||
implementation 'org.commonmark:commonmark:0.18.1'
|
implementation 'org.commonmark:commonmark:0.18.1'
|
||||||
implementation 'com.github.javafaker:javafaker:1.0.2'
|
implementation 'com.github.javafaker:javafaker:1.0.2'
|
||||||
|
implementation 'commons-io:commons-io'
|
||||||
implementation 'com.alibaba:easyexcel'
|
implementation 'com.alibaba:easyexcel'
|
||||||
implementation "org.freemarker:freemarker"
|
implementation "org.freemarker:freemarker"
|
||||||
|
|
||||||
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
|
implementation "com.squareup.retrofit2:retrofit:${retrofitVersion}"
|
||||||
implementation 'com.squareup.retrofit2:converter-jackson:2.9.0'
|
implementation "com.squareup.retrofit2:converter-jackson:${retrofitVersion}"
|
||||||
|
|
||||||
// test
|
// test
|
||||||
testImplementation "mysql:mysql-connector-java:${mysqlConnectorVersion}"
|
testImplementation "mysql:mysql-connector-java:${mysqlConnectorVersion}"
|
||||||
|
|
|
@ -43,6 +43,7 @@ public enum DomainErrors implements DatabasirErrors {
|
||||||
DUPLICATE_COLUMN("A_10028", "重复的列"),
|
DUPLICATE_COLUMN("A_10028", "重复的列"),
|
||||||
INVALID_MOCK_DATA_SCRIPT("A_10029", "不合法的表达式"),
|
INVALID_MOCK_DATA_SCRIPT("A_10029", "不合法的表达式"),
|
||||||
CANNOT_DELETE_SELF("A_10030", "无法对自己执行删除账号操作"),
|
CANNOT_DELETE_SELF("A_10030", "无法对自己执行删除账号操作"),
|
||||||
|
DRIVER_CLASS_NAME_OBTAIN_ERROR("A_10031", "获取驱动类名失败"),
|
||||||
;
|
;
|
||||||
|
|
||||||
private final String errCode;
|
private final String errCode;
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.databasir.core.domain.database.data;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class DriverClassNameResolveRequest {
|
||||||
|
|
||||||
|
private String databaseType;
|
||||||
|
|
||||||
|
@NotBlank
|
||||||
|
private String jdbcDriverFileUrl;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.databasir.core.domain.database.data;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class DriverDownloadRequest {
|
||||||
|
|
||||||
|
@NotBlank
|
||||||
|
private String databaseType;
|
||||||
|
|
||||||
|
@NotBlank
|
||||||
|
private String jdbcDriverFileUrl;
|
||||||
|
|
||||||
|
}
|
|
@ -107,4 +107,9 @@ public class DatabaseTypeService {
|
||||||
return databaseTypeDao.selectOptionalById(id)
|
return databaseTypeDao.selectOptionalById(id)
|
||||||
.map(databaseTypePojoConverter::toDetailResponse);
|
.map(databaseTypePojoConverter::toDetailResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String resolveDriverClassName(DriverClassNameResolveRequest request) {
|
||||||
|
return driverResources.resolveSqlDriverNameFromJar(request.getJdbcDriverFileUrl());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,6 @@ import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
@ -38,14 +37,7 @@ public class CustomDatabaseConnectionFactory implements DatabaseConnectionFactor
|
||||||
@Override
|
@Override
|
||||||
public Connection getConnection(Context context) throws SQLException {
|
public Connection getConnection(Context context) throws SQLException {
|
||||||
DatabaseTypePojo type = databaseTypeDao.selectByDatabaseType(context.getDatabaseType());
|
DatabaseTypePojo type = databaseTypeDao.selectByDatabaseType(context.getDatabaseType());
|
||||||
File driverFile;
|
File driverFile = driverResources.loadOrDownload(context.getDatabaseType(), type.getJdbcDriverFileUrl());
|
||||||
try {
|
|
||||||
driverFile = driverResources.download(context.getDatabaseType(), type.getJdbcDriverFileUrl());
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("download driver error " + context, e);
|
|
||||||
throw DomainErrors.DOWNLOAD_DRIVER_ERROR.exception(e.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
URLClassLoader loader = null;
|
URLClassLoader loader = null;
|
||||||
try {
|
try {
|
||||||
loader = new URLClassLoader(
|
loader = new URLClassLoader(
|
||||||
|
@ -58,6 +50,7 @@ public class CustomDatabaseConnectionFactory implements DatabaseConnectionFactor
|
||||||
log.error("load driver error " + context, e);
|
log.error("load driver error " + context, e);
|
||||||
throw DomainErrors.CONNECT_DATABASE_FAILED.exception(e.getMessage());
|
throw DomainErrors.CONNECT_DATABASE_FAILED.exception(e.getMessage());
|
||||||
}
|
}
|
||||||
|
// retrieve the driver class
|
||||||
|
|
||||||
Class<?> clazz = null;
|
Class<?> clazz = null;
|
||||||
Driver driver = null;
|
Driver driver = null;
|
||||||
|
@ -68,9 +61,9 @@ public class CustomDatabaseConnectionFactory implements DatabaseConnectionFactor
|
||||||
log.error("init driver error", e);
|
log.error("init driver error", e);
|
||||||
throw DomainErrors.CONNECT_DATABASE_FAILED.exception("驱动初始化异常, 请检查 Driver name:" + e.getMessage());
|
throw DomainErrors.CONNECT_DATABASE_FAILED.exception("驱动初始化异常, 请检查 Driver name:" + e.getMessage());
|
||||||
} catch (InvocationTargetException
|
} catch (InvocationTargetException
|
||||||
| InstantiationException
|
| InstantiationException
|
||||||
| IllegalAccessException
|
| IllegalAccessException
|
||||||
| NoSuchMethodException e) {
|
| NoSuchMethodException e) {
|
||||||
log.error("init driver error", e);
|
log.error("init driver error", e);
|
||||||
throw DomainErrors.CONNECT_DATABASE_FAILED.exception("驱动初始化异常:" + e.getMessage());
|
throw DomainErrors.CONNECT_DATABASE_FAILED.exception("驱动初始化异常:" + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,18 +3,19 @@ package com.databasir.core.infrastructure.driver;
|
||||||
import com.databasir.core.domain.DomainErrors;
|
import com.databasir.core.domain.DomainErrors;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.StreamUtils;
|
import org.springframework.util.StreamUtils;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.jar.JarFile;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@ -26,44 +27,8 @@ public class DriverResources {
|
||||||
|
|
||||||
private final RestTemplate restTemplate;
|
private final RestTemplate restTemplate;
|
||||||
|
|
||||||
public File download(String databaseType, String driverFileUrl) throws IOException {
|
|
||||||
// create parent directory
|
|
||||||
if (Files.notExists(Path.of(driverBaseDirectory))) {
|
|
||||||
Files.createDirectory(Path.of(driverBaseDirectory));
|
|
||||||
}
|
|
||||||
|
|
||||||
String filePath = driverPath(databaseType);
|
|
||||||
Path path = Path.of(filePath);
|
|
||||||
if (Files.exists(path)) {
|
|
||||||
// ignore
|
|
||||||
log.debug("{} already exists, ignore download from {}", filePath, driverFileUrl);
|
|
||||||
return path.toFile();
|
|
||||||
} else {
|
|
||||||
// download
|
|
||||||
try {
|
|
||||||
return restTemplate.execute(driverFileUrl, HttpMethod.GET, null, response -> {
|
|
||||||
if (response.getStatusCode().is2xxSuccessful()) {
|
|
||||||
File file = path.toFile();
|
|
||||||
StreamUtils.copy(response.getBody(), new FileOutputStream(file));
|
|
||||||
log.info("{} download success ", filePath);
|
|
||||||
return file;
|
|
||||||
} else {
|
|
||||||
log.error("{} download error from {}: {} ", filePath, driverFileUrl, response);
|
|
||||||
throw DomainErrors.DOWNLOAD_DRIVER_ERROR.exception("驱动下载失败:"
|
|
||||||
+ response.getStatusCode()
|
|
||||||
+ ", "
|
|
||||||
+ response.getStatusText());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
log.error(filePath + " download driver error", e);
|
|
||||||
throw DomainErrors.DOWNLOAD_DRIVER_ERROR.exception(e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void delete(String databaseType) {
|
public void delete(String databaseType) {
|
||||||
Path path = Paths.get(driverPath(databaseType));
|
Path path = Paths.get(driverFilePath(driverBaseDirectory, databaseType));
|
||||||
try {
|
try {
|
||||||
Files.deleteIfExists(path);
|
Files.deleteIfExists(path);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -71,13 +36,106 @@ public class DriverResources {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String driverPath(String databaseType) {
|
public String resolveSqlDriverNameFromJar(String driverFileUrl) {
|
||||||
|
String tempFilePath = "temp/" + UUID.randomUUID() + ".jar";
|
||||||
|
File driverFile = doDownload(driverFileUrl, tempFilePath);
|
||||||
|
String className = resolveSqlDriverNameFromJar(driverFile);
|
||||||
|
try {
|
||||||
|
Files.deleteIfExists(driverFile.toPath());
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("delete driver error " + tempFilePath, e);
|
||||||
|
}
|
||||||
|
return className;
|
||||||
|
}
|
||||||
|
|
||||||
|
public File loadOrDownload(String databaseType, String driverFileUrl) {
|
||||||
|
String filePath = driverFilePath(driverBaseDirectory, databaseType);
|
||||||
|
Path path = Path.of(filePath);
|
||||||
|
if (Files.exists(path)) {
|
||||||
|
// ignore
|
||||||
|
log.debug("{} already exists, ignore download from {}", filePath, driverFileUrl);
|
||||||
|
return path.toFile();
|
||||||
|
}
|
||||||
|
return this.doDownload(driverFileUrl, filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private File doDownload(String driverFileUrl, String filePath) {
|
||||||
|
Path path = Path.of(filePath);
|
||||||
|
|
||||||
|
// create parent directory
|
||||||
|
if (Files.notExists(path)) {
|
||||||
|
path.getParent().toFile().mkdirs();
|
||||||
|
try {
|
||||||
|
Files.createFile(path);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("create file error " + filePath, e);
|
||||||
|
throw DomainErrors.DOWNLOAD_DRIVER_ERROR.exception(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// download
|
||||||
|
try {
|
||||||
|
return restTemplate.execute(driverFileUrl, HttpMethod.GET, null, response -> {
|
||||||
|
if (response.getStatusCode().is2xxSuccessful()) {
|
||||||
|
File file = path.toFile();
|
||||||
|
FileOutputStream out = new FileOutputStream(file);
|
||||||
|
StreamUtils.copy(response.getBody(), out);
|
||||||
|
IOUtils.closeQuietly(out, ex -> log.error("close file error", ex));
|
||||||
|
log.info("{} download success ", filePath);
|
||||||
|
return file;
|
||||||
|
} else {
|
||||||
|
log.error("{} download error from {}: {} ", filePath, driverFileUrl, response);
|
||||||
|
throw DomainErrors.DOWNLOAD_DRIVER_ERROR.exception("驱动下载失败:"
|
||||||
|
+ response.getStatusCode()
|
||||||
|
+ ", "
|
||||||
|
+ response.getStatusText());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
log.error(filePath + " download driver error", e);
|
||||||
|
throw DomainErrors.DOWNLOAD_DRIVER_ERROR.exception(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolveSqlDriverNameFromJar(File driverFile) {
|
||||||
|
JarFile jarFile = null;
|
||||||
|
try {
|
||||||
|
jarFile = new JarFile(driverFile);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("resolve driver class name error", e);
|
||||||
|
throw DomainErrors.DRIVER_CLASS_NAME_OBTAIN_ERROR.exception(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
final JarFile driverJar = jarFile;
|
||||||
|
String driverClassName = jarFile.stream()
|
||||||
|
.filter(entry -> entry.getName().contains("META-INF/services/java.sql.Driver"))
|
||||||
|
.findFirst()
|
||||||
|
.map(entry -> {
|
||||||
|
InputStream stream = null;
|
||||||
|
BufferedReader reader = null;
|
||||||
|
try {
|
||||||
|
stream = driverJar.getInputStream(entry);
|
||||||
|
reader = new BufferedReader(new InputStreamReader(stream));
|
||||||
|
return reader.readLine();
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("resolve driver class name error", e);
|
||||||
|
throw DomainErrors.DRIVER_CLASS_NAME_OBTAIN_ERROR.exception(e.getMessage());
|
||||||
|
} finally {
|
||||||
|
IOUtils.closeQuietly(reader, ex -> log.error("close reader error", ex));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.orElseThrow(DomainErrors.DRIVER_CLASS_NAME_OBTAIN_ERROR::exception);
|
||||||
|
IOUtils.closeQuietly(jarFile, ex -> log.error("close jar file error", ex));
|
||||||
|
return driverClassName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String driverFilePath(String baseDir, String databaseType) {
|
||||||
String fileName = databaseType + ".jar";
|
String fileName = databaseType + ".jar";
|
||||||
String filePath;
|
String filePath;
|
||||||
if (driverBaseDirectory.endsWith(File.separator)) {
|
if (baseDir.endsWith(File.separator)) {
|
||||||
filePath = driverBaseDirectory + fileName;
|
filePath = baseDir + fileName;
|
||||||
} else {
|
} else {
|
||||||
filePath = driverBaseDirectory + File.separator + fileName;
|
filePath = baseDir + File.separator + fileName;
|
||||||
}
|
}
|
||||||
return filePath;
|
return filePath;
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 425 KiB |
Binary file not shown.
Before Width: | Height: | Size: 991 KiB |
Loading…
Reference in New Issue