|
5 | 5 | import java.sql.Connection; |
6 | 6 | import java.sql.DriverManager; |
7 | 7 | import java.sql.SQLException; |
8 | | -import java.util.regex.Matcher; |
9 | | -import java.util.regex.Pattern; |
10 | 8 |
|
11 | 9 | /** |
12 | 10 | * Created by Vinicius on 21/04/2017. |
13 | 11 | */ |
14 | 12 | public class ConnectionInfo { |
15 | 13 |
|
16 | | - /** |
17 | | - * Regex pattern to match following connection strings: |
18 | | - * user/pass@127.0.0.1:1521/db |
19 | | - * user/pass@127.0.0.1/db |
20 | | - * user/pass@db |
21 | | - */ |
22 | | - private static final String CONNSTR_PATTERN = |
23 | | - "^(?<user>[0-9a-z]+)/(?<pass>[0-9a-z]+)" + |
24 | | - "(?:(?:@(?<host>[^:/]+)?(?::(?<port>[0-9]+))?(?:/(?<db1>[0-9a-z]+))$)|(?:@(?<db2>[0-9a-z]+)$))"; |
25 | | - |
26 | 14 | private static final String DEFAULT_HOST = "127.0.0.1"; |
27 | 15 | private static final int DEFAULT_PORT = 1521; |
28 | 16 |
|
29 | 17 | private String user; |
30 | 18 | private String password; |
31 | 19 | private String host; |
32 | 20 | private int port; |
33 | | - private String db; |
| 21 | + private String database; |
34 | 22 |
|
35 | | - public ConnectionInfo() { |
36 | | - } |
| 23 | + public ConnectionInfo() {} |
37 | 24 |
|
38 | 25 | public Connection getConnection() throws SQLException { |
39 | 26 | return DriverManager.getConnection(getConnectionUrl(), getUser(), getPassword()); |
40 | 27 | } |
41 | 28 |
|
42 | | - public ConnectionInfo parseConnectionString(String connectionString) throws ParameterException { |
43 | | - Pattern p = Pattern.compile(CONNSTR_PATTERN); |
44 | | - Matcher m = p.matcher(connectionString); |
| 29 | + public ConnectionInfo parseConnectionString(String connectionString) |
| 30 | + throws ParameterException, IllegalArgumentException { |
| 31 | + |
| 32 | + if (connectionString == null || connectionString.isEmpty()) |
| 33 | + throw invalidConnectionString(); |
45 | 34 |
|
46 | | - if (!m.matches()) |
47 | | - throw new ParameterException("Invalid connection string!"); |
| 35 | + int i = connectionString.lastIndexOf("@"); |
| 36 | + if (i == -1) |
| 37 | + throw invalidConnectionString(); |
48 | 38 |
|
49 | | - this.setUser(m.group("user")); |
50 | | - this.setPassword(m.group("pass")); |
51 | | - this.setHost(m.group("host") != null ? m.group("host") : DEFAULT_HOST); |
52 | | - this.setPort(m.group("port") != null ? Integer.parseInt(m.group("port")) : DEFAULT_PORT); |
53 | | - this.setDb(m.group("db1") != null ? m.group("db1") : m.group("db2")); |
| 39 | + String credentials = connectionString.substring(0, i); |
| 40 | + String host = connectionString.substring(i+1); |
| 41 | + parseCredentials(credentials); |
| 42 | + parseHost(host); |
54 | 43 |
|
55 | 44 | return this; |
56 | 45 | } |
57 | 46 |
|
| 47 | + private void parseCredentials(String str) throws ParameterException, IllegalArgumentException { |
| 48 | + int barIdx = str.indexOf("/"); |
| 49 | + |
| 50 | + if (barIdx == -1 || 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) { |
| 65 | + setHost(str.substring(0, colonIdx)); |
| 66 | + setPort(Integer.parseInt(str.substring(colonIdx + 1, barIdx))); |
| 67 | + setDatabase(str.substring(barIdx + 1)); |
| 68 | + } |
| 69 | + else |
| 70 | + if (colonIdx == -1 && barIdx != -1) { |
| 71 | + setHost(str.substring(0, barIdx)); |
| 72 | + setPort(DEFAULT_PORT); |
| 73 | + setDatabase(str.substring(barIdx + 1)); |
| 74 | + } |
| 75 | + else |
| 76 | + if (colonIdx != -1) { |
| 77 | + throw invalidConnectionString(); |
| 78 | + } |
| 79 | + else { |
| 80 | + setHost(DEFAULT_HOST); |
| 81 | + setPort(DEFAULT_PORT); |
| 82 | + setDatabase(str); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private ParameterException invalidConnectionString() { |
| 87 | + return new ParameterException("Invalid connection string."); |
| 88 | + } |
| 89 | + |
58 | 90 | public String getUser() { |
59 | 91 | return user; |
60 | 92 | } |
61 | 93 |
|
62 | | - public void setUser(String user) { |
| 94 | + private void setUser(String user) { |
63 | 95 | this.user = user; |
64 | 96 | } |
65 | 97 |
|
66 | 98 | public String getPassword() { |
67 | 99 | return password; |
68 | 100 | } |
69 | 101 |
|
70 | | - public void setPassword(String password) { |
| 102 | + private void setPassword(String password) { |
71 | 103 | this.password = password; |
72 | 104 | } |
73 | 105 |
|
74 | 106 | public String getHost() { |
75 | 107 | return host; |
76 | 108 | } |
77 | 109 |
|
78 | | - public void setHost(String host) { |
| 110 | + private void setHost(String host) { |
79 | 111 | this.host = host; |
80 | 112 | } |
81 | 113 |
|
82 | 114 | public int getPort() { |
83 | 115 | return port; |
84 | 116 | } |
85 | 117 |
|
86 | | - public void setPort(int port) { |
| 118 | + private void setPort(int port) { |
87 | 119 | this.port = port; |
88 | 120 | } |
89 | 121 |
|
90 | | - public String getDb() { |
91 | | - return db; |
| 122 | + public String getDatabase() { |
| 123 | + return database; |
92 | 124 | } |
93 | 125 |
|
94 | | - public void setDb(String db) { |
95 | | - this.db = db; |
| 126 | + private void setDatabase(String database) { |
| 127 | + this.database = database; |
96 | 128 | } |
97 | 129 |
|
98 | 130 | public String getConnectionUrl() { |
99 | | - return String.format("jdbc:oracle:thin:@//%s:%d/%s", getHost(), getPort(), getDb()); |
| 131 | + return String.format("jdbc:oracle:thin:@//%s:%d/%s", getHost(), getPort(), getDatabase()); |
100 | 132 | } |
101 | 133 |
|
102 | 134 | @Override |
103 | 135 | public String toString() { |
104 | | - return String.format("%s@%s:%d/%s", getUser(), getHost(), getPort(), getDb()); |
| 136 | + return String.format("%s@%s:%d/%s", getUser(), getHost(), getPort(), getDatabase()); |
105 | 137 | } |
106 | 138 |
|
107 | 139 | } |
0 commit comments