|
| 1 | +package org.utplsql.cli.datasource; |
| 2 | + |
| 3 | +import com.zaxxer.hikari.HikariDataSource; |
| 4 | +import org.utplsql.cli.ConnectionConfig; |
| 5 | +import org.utplsql.cli.exception.DatabaseConnectionFailed; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.sql.Connection; |
| 9 | +import java.sql.SQLException; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +public class TestedDataSourceProvider { |
| 14 | + |
| 15 | + interface ConnectStringPossibility { |
| 16 | + String getConnectString(ConnectionConfig config); |
| 17 | + String getMaskedConnectString(ConnectionConfig config); |
| 18 | + } |
| 19 | + |
| 20 | + |
| 21 | + private final ConnectionConfig config; |
| 22 | + private List<ConnectStringPossibility> possibilities = new ArrayList<>(); |
| 23 | + |
| 24 | + public TestedDataSourceProvider(ConnectionConfig config) { |
| 25 | + this.config = config; |
| 26 | + |
| 27 | + possibilities.add(new ThickConnectStringPossibility()); |
| 28 | + possibilities.add(new ThinConnectStringPossibility()); |
| 29 | + } |
| 30 | + |
| 31 | + public HikariDataSource getDataSource() throws SQLException { |
| 32 | + |
| 33 | + HikariDataSource ds = new HikariDataSource(); |
| 34 | + |
| 35 | + testAndSetJdbcUrl(ds); |
| 36 | + |
| 37 | + return ds; |
| 38 | + } |
| 39 | + |
| 40 | + public void testAndSetJdbcUrl( HikariDataSource ds ) throws SQLException |
| 41 | + { |
| 42 | + List<String> errors = new ArrayList<>(); |
| 43 | + Throwable lastException = null; |
| 44 | + for (ConnectStringPossibility possibility : possibilities) { |
| 45 | + ds.setJdbcUrl(possibility.getConnectString(config)); |
| 46 | + try (Connection con = ds.getConnection()) { |
| 47 | + return; |
| 48 | + } catch (UnsatisfiedLinkError | Exception e) { |
| 49 | + errors.add(possibility.getMaskedConnectString(config) + ": " + e.getMessage()); |
| 50 | + lastException = e; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + errors.forEach(System.out::println); |
| 55 | + throw new DatabaseConnectionFailed(lastException); |
| 56 | + } |
| 57 | + |
| 58 | + private static class ThickConnectStringPossibility implements ConnectStringPossibility { |
| 59 | + @Override |
| 60 | + public String getConnectString(ConnectionConfig config) { |
| 61 | + return "jdbc:oracle:oci8:" + config.getConnectString(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public String getMaskedConnectString(ConnectionConfig config) { |
| 66 | + return "jdbc:oracle:oci8:****/****@" + config.getConnect(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private static class ThinConnectStringPossibility implements ConnectStringPossibility { |
| 71 | + @Override |
| 72 | + public String getConnectString(ConnectionConfig config) { |
| 73 | + return "jdbc:oracle:thin:" + config.getConnectString(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public String getMaskedConnectString(ConnectionConfig config) { |
| 78 | + return "jdbc:oracle:thin:****/****@" + config.getConnect(); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments