feat: optimize drvier resource code
This commit is contained in:
parent
fbcd45e924
commit
9c9be799f1
|
@ -10,3 +10,4 @@ databasir.db.url=localhost:3306
|
||||||
databasir.db.username=root
|
databasir.db.username=root
|
||||||
databasir.db.password=123456
|
databasir.db.password=123456
|
||||||
databasir.db.driver-directory=drivers
|
databasir.db.driver-directory=drivers
|
||||||
|
databasir.jwt.secret=DatabasirJwtSecret
|
|
@ -62,7 +62,7 @@ public class DatabaseTypeService {
|
||||||
// 名称修改,下载地址修改需要删除原有的 driver
|
// 名称修改,下载地址修改需要删除原有的 driver
|
||||||
if (!Objects.equals(request.getDatabaseType(), data.getDatabaseType())
|
if (!Objects.equals(request.getDatabaseType(), data.getDatabaseType())
|
||||||
|| !Objects.equals(request.getJdbcDriverFileUrl(), data.getJdbcDriverFileUrl())) {
|
|| !Objects.equals(request.getJdbcDriverFileUrl(), data.getJdbcDriverFileUrl())) {
|
||||||
driverResources.delete(data.getDatabaseType());
|
driverResources.deleteByDatabaseType(data.getDatabaseType());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -74,7 +74,7 @@ public class DatabaseTypeService {
|
||||||
throw DomainErrors.MUST_NOT_MODIFY_SYSTEM_DEFAULT_DATABASE_TYPE.exception();
|
throw DomainErrors.MUST_NOT_MODIFY_SYSTEM_DEFAULT_DATABASE_TYPE.exception();
|
||||||
}
|
}
|
||||||
databaseTypeDao.deleteById(id);
|
databaseTypeDao.deleteById(id);
|
||||||
driverResources.delete(data.getDatabaseType());
|
driverResources.deleteByDatabaseType(data.getDatabaseType());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,7 +110,7 @@ public class DatabaseTypeService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String resolveDriverClassName(DriverClassNameResolveRequest request) {
|
public String resolveDriverClassName(DriverClassNameResolveRequest request) {
|
||||||
return driverResources.resolveSqlDriverNameFromJar(request.getJdbcDriverFileUrl());
|
return driverResources.resolveDriverClassName(request.getJdbcDriverFileUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,8 +36,10 @@ 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());
|
String databaseType = context.getDatabaseType();
|
||||||
File driverFile = driverResources.loadOrDownload(context.getDatabaseType(), type.getJdbcDriverFileUrl());
|
DatabaseTypePojo type = databaseTypeDao.selectByDatabaseType(databaseType);
|
||||||
|
File driverFile = driverResources.loadOrDownloadByDatabaseType(databaseType, type.getJdbcDriverFileUrl());
|
||||||
|
|
||||||
URLClassLoader loader = null;
|
URLClassLoader loader = null;
|
||||||
try {
|
try {
|
||||||
loader = new URLClassLoader(
|
loader = new URLClassLoader(
|
||||||
|
|
|
@ -9,6 +9,7 @@ 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.RestClientException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
@ -18,6 +19,7 @@ import java.net.URLClassLoader;
|
||||||
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.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.jar.JarFile;
|
import java.util.jar.JarFile;
|
||||||
|
|
||||||
|
@ -31,7 +33,7 @@ public class DriverResources {
|
||||||
|
|
||||||
private final RestTemplate restTemplate;
|
private final RestTemplate restTemplate;
|
||||||
|
|
||||||
public void delete(String databaseType) {
|
public void deleteByDatabaseType(String databaseType) {
|
||||||
Path path = Paths.get(driverFilePath(driverBaseDirectory, databaseType));
|
Path path = Paths.get(driverFilePath(driverBaseDirectory, databaseType));
|
||||||
try {
|
try {
|
||||||
Files.deleteIfExists(path);
|
Files.deleteIfExists(path);
|
||||||
|
@ -40,9 +42,105 @@ public class DriverResources {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Optional<File> loadByDatabaseType(String databaseType) {
|
||||||
|
Path path = Paths.get(driverFilePath(driverBaseDirectory, databaseType));
|
||||||
|
if (Files.exists(path)) {
|
||||||
|
return Optional.of(path.toFile());
|
||||||
|
} else {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public File loadOrDownloadByDatabaseType(String databaseType, String driverFileUrl) {
|
||||||
|
return loadByDatabaseType(databaseType)
|
||||||
|
.orElseGet(() -> download(driverFileUrl, driverFilePath(driverBaseDirectory, databaseType)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public String resolveDriverClassName(String driverFileUrl) {
|
||||||
|
String tempFilePath = "temp/" + UUID.randomUUID() + ".jar";
|
||||||
|
File driverFile = download(driverFileUrl, tempFilePath);
|
||||||
|
String className = resolveDriverClassName(driverFile);
|
||||||
|
try {
|
||||||
|
Files.deleteIfExists(driverFile.toPath());
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("delete driver error " + tempFilePath, e);
|
||||||
|
}
|
||||||
|
return className;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String resolveDriverClassName(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_NOT_FOUND.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_NOT_FOUND.exception(e.getMessage());
|
||||||
|
} finally {
|
||||||
|
IOUtils.closeQuietly(reader, ex -> log.error("close reader error", ex));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.orElseThrow(DomainErrors.DRIVER_CLASS_NOT_FOUND::exception);
|
||||||
|
IOUtils.closeQuietly(jarFile, ex -> log.error("close jar file error", ex));
|
||||||
|
return driverClassName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private File download(String driverFileUrl, String targetFile) {
|
||||||
|
Path path = Path.of(targetFile);
|
||||||
|
|
||||||
|
// create parent directory
|
||||||
|
if (Files.notExists(path)) {
|
||||||
|
path.getParent().toFile().mkdirs();
|
||||||
|
try {
|
||||||
|
Files.createFile(path);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("create file error " + targetFile, 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 ", targetFile);
|
||||||
|
return file;
|
||||||
|
} else {
|
||||||
|
log.error("{} download error from {}: {} ", targetFile, driverFileUrl, response);
|
||||||
|
throw DomainErrors.DOWNLOAD_DRIVER_ERROR.exception("驱动下载失败:"
|
||||||
|
+ response.getStatusCode()
|
||||||
|
+ ", "
|
||||||
|
+ response.getStatusText());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (RestClientException e) {
|
||||||
|
log.error(targetFile + " download driver error", e);
|
||||||
|
throw DomainErrors.DOWNLOAD_DRIVER_ERROR.exception(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void validateJar(String driverFileUrl, String className) {
|
public void validateJar(String driverFileUrl, String className) {
|
||||||
String tempFilePath = "temp/" + UUID.randomUUID() + ".jar";
|
String tempFilePath = "temp/" + UUID.randomUUID() + ".jar";
|
||||||
File driverFile = doDownload(driverFileUrl, tempFilePath);
|
File driverFile = download(driverFileUrl, tempFilePath);
|
||||||
URLClassLoader loader = null;
|
URLClassLoader loader = null;
|
||||||
try {
|
try {
|
||||||
loader = new URLClassLoader(
|
loader = new URLClassLoader(
|
||||||
|
@ -75,99 +173,6 @@ public class DriverResources {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String resolveSqlDriverNameFromJar(String driverFileUrl) {
|
|
||||||
String tempFilePath = "temp/" + UUID.randomUUID() + ".jar";
|
|
||||||
File driverFile = doDownload(driverFileUrl, tempFilePath);
|
|
||||||
String className = doResolveSqlDriverNameFromJar(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 doResolveSqlDriverNameFromJar(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_NOT_FOUND.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_NOT_FOUND.exception(e.getMessage());
|
|
||||||
} finally {
|
|
||||||
IOUtils.closeQuietly(reader, ex -> log.error("close reader error", ex));
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.orElseThrow(DomainErrors.DRIVER_CLASS_NOT_FOUND::exception);
|
|
||||||
IOUtils.closeQuietly(jarFile, ex -> log.error("close jar file error", ex));
|
|
||||||
return driverClassName;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String driverFilePath(String baseDir, String databaseType) {
|
private String driverFilePath(String baseDir, String databaseType) {
|
||||||
String fileName = databaseType + ".jar";
|
String fileName = databaseType + ".jar";
|
||||||
String filePath;
|
String filePath;
|
||||||
|
|
|
@ -5,14 +5,20 @@ import com.databasir.core.BaseTest;
|
||||||
import com.databasir.core.domain.DomainErrors;
|
import com.databasir.core.domain.DomainErrors;
|
||||||
import com.databasir.core.domain.database.data.DatabaseTypeCreateRequest;
|
import com.databasir.core.domain.database.data.DatabaseTypeCreateRequest;
|
||||||
import com.databasir.core.domain.database.data.DatabaseTypeUpdateRequest;
|
import com.databasir.core.domain.database.data.DatabaseTypeUpdateRequest;
|
||||||
|
import com.databasir.core.infrastructure.driver.DriverResources;
|
||||||
import com.databasir.dao.impl.DatabaseTypeDao;
|
import com.databasir.dao.impl.DatabaseTypeDao;
|
||||||
import com.databasir.dao.tables.pojos.DatabaseTypePojo;
|
import com.databasir.dao.tables.pojos.DatabaseTypePojo;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.test.context.jdbc.Sql;
|
import org.springframework.test.context.jdbc.Sql;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
class DatabaseTypeServiceTest extends BaseTest {
|
class DatabaseTypeServiceTest extends BaseTest {
|
||||||
|
|
||||||
|
@ -22,6 +28,14 @@ class DatabaseTypeServiceTest extends BaseTest {
|
||||||
@Autowired
|
@Autowired
|
||||||
private DatabaseTypeDao databaseTypeDao;
|
private DatabaseTypeDao databaseTypeDao;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private DriverResources driverResources;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
Mockito.doNothing().when(driverResources).validateJar(anyString(), anyString());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void create() {
|
void create() {
|
||||||
DatabaseTypeCreateRequest request = new DatabaseTypeCreateRequest();
|
DatabaseTypeCreateRequest request = new DatabaseTypeCreateRequest();
|
||||||
|
|
Loading…
Reference in New Issue