refactor: 数据源插件优化

This commit is contained in:
junjun 2024-07-22 10:33:08 +08:00
parent a1100f22f1
commit 67472fc222
2 changed files with 48 additions and 51 deletions

View File

@ -1,9 +1,6 @@
package io.dataease.datasource.provider; package io.dataease.datasource.provider;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import io.dataease.commons.utils.CommonThreadPool; import io.dataease.commons.utils.CommonThreadPool;
import io.dataease.dataset.utils.FieldUtils; import io.dataease.dataset.utils.FieldUtils;
import io.dataease.datasource.dao.auto.entity.CoreDatasource; import io.dataease.datasource.dao.auto.entity.CoreDatasource;
@ -38,7 +35,6 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.sql.*; import java.sql.*;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -338,50 +334,6 @@ public class CalciteProvider extends Provider {
return connectionObj; return connectionObj;
} }
private void startSshSession(DatasourceConfiguration configuration, ConnectionObj connectionObj, Long datacourseId) throws Exception {
if (configuration.isUseSSH()) {
if (datacourseId == null) {
configuration.setLPort(getLport(null));
connectionObj.setLPort(configuration.getLPort());
connectionObj.setConfiguration(configuration);
Session session = initSession(configuration);
connectionObj.setSession(session);
} else {
Integer lport = Provider.getLPorts().get(datacourseId);
configuration.setLPort(lport);
if (lport != null) {
if (Provider.getSessions().get(datacourseId) == null || !Provider.getSessions().get(datacourseId).isConnected()) {
Session session = initSession(configuration);
Provider.getSessions().put(datacourseId, session);
}
} else {
configuration.setLPort(getLport(datacourseId));
Session session = initSession(configuration);
Provider.getSessions().put(datacourseId, session);
}
configuration.setLPort(lport);
}
}
}
private Session initSession(DatasourceConfiguration configuration) throws Exception {
JSch jsch = new JSch();
Session session = jsch.getSession(configuration.getSshUserName(), configuration.getSshHost(), configuration.getSshPort());
if (!configuration.getSshType().equalsIgnoreCase("password")) {
session.setConfig("PreferredAuthentications", "publickey");
jsch.addIdentity("sshkey", configuration.getSshKey().getBytes(StandardCharsets.UTF_8), null, configuration.getSshKeyPassword() == null ? null : configuration.getSshKeyPassword().getBytes(StandardCharsets.UTF_8));
}
if (configuration.getSshType().equalsIgnoreCase("password")) {
session.setPassword(configuration.getSshPassword());
}
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
session.setPortForwardingL(configuration.getLPort(), configuration.getHost(), configuration.getPort());
return session;
}
private DatasetTableDTO getTableDesc(DatasourceRequest datasourceRequest, ResultSet resultSet) throws SQLException { private DatasetTableDTO getTableDesc(DatasourceRequest datasourceRequest, ResultSet resultSet) throws SQLException {
DatasetTableDTO tableDesc = new DatasetTableDTO(); DatasetTableDTO tableDesc = new DatasetTableDTO();
tableDesc.setDatasourceId(datasourceRequest.getDatasource().getId()); tableDesc.setDatasourceId(datasourceRequest.getDatasource().getId());

View File

@ -1,5 +1,6 @@
package io.dataease.extensions.datasource.provider; package io.dataease.extensions.datasource.provider;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session; import com.jcraft.jsch.Session;
import io.dataease.exception.DEException; import io.dataease.exception.DEException;
import io.dataease.extensions.datasource.constant.SqlPlaceholderConstants; import io.dataease.extensions.datasource.constant.SqlPlaceholderConstants;
@ -15,10 +16,11 @@ import org.apache.calcite.sql.parser.SqlParser;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.Statement;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.Statement;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -191,4 +193,47 @@ public abstract class Provider {
return true; return true;
} }
} }
public void startSshSession(DatasourceConfiguration configuration, ConnectionObj connectionObj, Long datacourseId) throws Exception {
if (configuration.isUseSSH()) {
if (datacourseId == null) {
configuration.setLPort(getLport(null));
connectionObj.setLPort(configuration.getLPort());
connectionObj.setConfiguration(configuration);
Session session = initSession(configuration);
connectionObj.setSession(session);
} else {
Integer lport = Provider.getLPorts().get(datacourseId);
configuration.setLPort(lport);
if (lport != null) {
if (Provider.getSessions().get(datacourseId) == null || !Provider.getSessions().get(datacourseId).isConnected()) {
Session session = initSession(configuration);
Provider.getSessions().put(datacourseId, session);
}
} else {
configuration.setLPort(getLport(datacourseId));
Session session = initSession(configuration);
Provider.getSessions().put(datacourseId, session);
}
configuration.setLPort(lport);
}
}
}
public Session initSession(DatasourceConfiguration configuration) throws Exception {
JSch jsch = new JSch();
Session session = jsch.getSession(configuration.getSshUserName(), configuration.getSshHost(), configuration.getSshPort());
if (!configuration.getSshType().equalsIgnoreCase("password")) {
session.setConfig("PreferredAuthentications", "publickey");
jsch.addIdentity("sshkey", configuration.getSshKey().getBytes(StandardCharsets.UTF_8), null, configuration.getSshKeyPassword() == null ? null : configuration.getSshKeyPassword().getBytes(StandardCharsets.UTF_8));
}
if (configuration.getSshType().equalsIgnoreCase("password")) {
session.setPassword(configuration.getSshPassword());
}
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
session.setPortForwardingL(configuration.getLPort(), configuration.getHost(), configuration.getPort());
return session;
}
} }