|
1 | 1 | package org.utplsql.cli;
|
2 | 2 |
|
3 |
| -import com.beust.jcommander.ParameterException; |
| 3 | +import com.beust.jcommander.IStringConverter; |
| 4 | +import oracle.ucp.jdbc.PoolDataSource; |
| 5 | +import oracle.ucp.jdbc.PoolDataSourceFactory; |
4 | 6 |
|
| 7 | +import java.io.File; |
5 | 8 | import java.sql.Connection;
|
6 |
| -import java.sql.DriverManager; |
7 | 9 | import java.sql.SQLException;
|
8 | 10 |
|
9 |
| -/** |
10 |
| - * Created by Vinicius on 21/04/2017. |
11 |
| - */ |
12 | 11 | public class ConnectionInfo {
|
13 | 12 |
|
14 |
| - private static final String DEFAULT_HOST = "127.0.0.1"; |
15 |
| - private static final int DEFAULT_PORT = 1521; |
16 |
| - |
17 |
| - private String user; |
18 |
| - private String password; |
19 |
| - private String host; |
20 |
| - private int port; |
21 |
| - private String database; |
22 |
| - |
23 |
| - public ConnectionInfo() {} |
24 |
| - |
25 |
| - public Connection getConnection() throws SQLException { |
26 |
| - return DriverManager.getConnection(getConnectionUrl(), getUser(), getPassword()); |
27 |
| - } |
28 |
| - |
29 |
| - public ConnectionInfo parseConnectionString(String connectionString) |
30 |
| - throws ParameterException, IllegalArgumentException { |
31 |
| - |
32 |
| - if (connectionString == null || connectionString.isEmpty()) |
33 |
| - throw invalidConnectionString(); |
34 |
| - |
35 |
| - int i = connectionString.lastIndexOf("@"); |
36 |
| - if (i == -1) |
37 |
| - throw invalidConnectionString(); |
38 |
| - |
39 |
| - String credentials = connectionString.substring(0, i); |
40 |
| - String host = connectionString.substring(i+1); |
41 |
| - parseCredentials(credentials); |
42 |
| - parseHost(host); |
43 |
| - |
44 |
| - return this; |
45 |
| - } |
46 |
| - |
47 |
| - private void parseCredentials(String str) throws ParameterException, IllegalArgumentException { |
48 |
| - int barIdx = str.indexOf("/"); |
49 |
| - |
50 |
| - if (barIdx == -1 || barIdx == 0 || barIdx == str.length() - 1) |
51 |
| - throw invalidConnectionString(); |
52 |
| - |
53 |
| - this.setUser(str.substring(0, barIdx)); |
54 |
| - this.setPassword(str.substring(barIdx+1)); |
55 |
| - } |
56 |
| - |
57 |
| - private void parseHost(String str) throws ParameterException, IllegalArgumentException { |
58 |
| - if (str == null || str.isEmpty()) |
59 |
| - throw invalidConnectionString(); |
60 |
| - |
61 |
| - int colonIdx = str.indexOf(":"); |
62 |
| - int barIdx = str.indexOf("/"); |
63 |
| - |
64 |
| - if ((colonIdx != -1 && barIdx == -1) || barIdx == 0) // @host:port or // @/db |
65 |
| - throw invalidConnectionString(); |
66 |
| - |
67 |
| - if (colonIdx != -1) { // @host:port/db |
68 |
| - setHost(str.substring(0, colonIdx)); |
69 |
| - setPort(Integer.parseInt(str.substring(colonIdx + 1, barIdx))); |
70 |
| - setDatabase(str.substring(barIdx + 1)); |
71 |
| - } |
72 |
| - else |
73 |
| - if (barIdx != -1) { // @host/db |
74 |
| - setHost(str.substring(0, barIdx)); |
75 |
| - setPort(DEFAULT_PORT); |
76 |
| - setDatabase(str.substring(barIdx + 1)); |
77 |
| - } |
78 |
| - else { // @db |
79 |
| - setHost(DEFAULT_HOST); |
80 |
| - setPort(DEFAULT_PORT); |
81 |
| - setDatabase(str); |
| 13 | + static { |
| 14 | + String oracleHome = System.getenv("ORACLE_HOME"); |
| 15 | + if (oracleHome != null) { |
| 16 | + System.setProperty("oracle.net.tns_admin", |
| 17 | + String.join(File.separator, oracleHome, "NETWORK", "ADMIN")); |
82 | 18 | }
|
83 | 19 | }
|
84 | 20 |
|
85 |
| - private ParameterException invalidConnectionString() { |
86 |
| - return new ParameterException("Invalid connection string."); |
87 |
| - } |
88 |
| - |
89 |
| - public String getUser() { |
90 |
| - return user; |
91 |
| - } |
92 |
| - |
93 |
| - private void setUser(String user) { |
94 |
| - this.user = user; |
95 |
| - } |
96 |
| - |
97 |
| - public String getPassword() { |
98 |
| - return password; |
99 |
| - } |
100 |
| - |
101 |
| - private void setPassword(String password) { |
102 |
| - this.password = password; |
103 |
| - } |
104 |
| - |
105 |
| - public String getHost() { |
106 |
| - return host; |
107 |
| - } |
| 21 | + private PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource(); |
108 | 22 |
|
109 |
| - private void setHost(String host) { |
110 |
| - this.host = host; |
111 |
| - } |
112 |
| - |
113 |
| - public int getPort() { |
114 |
| - return port; |
115 |
| - } |
116 |
| - |
117 |
| - private void setPort(int port) { |
118 |
| - this.port = port; |
119 |
| - } |
120 |
| - |
121 |
| - public String getDatabase() { |
122 |
| - return database; |
| 23 | + public ConnectionInfo(String connectionInfo) { |
| 24 | + try { |
| 25 | + this.pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); |
| 26 | + this.pds.setURL("jdbc:oracle:thin:" + connectionInfo); |
| 27 | + this.pds.setInitialPoolSize(2); |
| 28 | + } catch (SQLException e) { |
| 29 | + e.printStackTrace(); |
| 30 | + } |
123 | 31 | }
|
124 | 32 |
|
125 |
| - private void setDatabase(String database) { |
126 |
| - this.database = database; |
| 33 | + public Connection getConnection() throws SQLException { |
| 34 | + return pds.getConnection(); |
127 | 35 | }
|
128 | 36 |
|
129 |
| - public String getConnectionUrl() { |
130 |
| - return String.format("jdbc:oracle:thin:@//%s:%d/%s", getHost(), getPort(), getDatabase()); |
131 |
| - } |
| 37 | + public static class ConnectionStringConverter implements IStringConverter<ConnectionInfo> { |
132 | 38 |
|
133 |
| - @Override |
134 |
| - public String toString() { |
135 |
| - return String.format("%s@%s:%d/%s", getUser(), getHost(), getPort(), getDatabase()); |
| 39 | + @Override |
| 40 | + public ConnectionInfo convert(String s) { |
| 41 | + return new ConnectionInfo(s); |
| 42 | + } |
136 | 43 | }
|
137 | 44 |
|
138 | 45 | }
|
0 commit comments