-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathConnectionConfig.java
142 lines (123 loc) · 5.01 KB
/
ConnectionConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright © 2019 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package io.cdap.plugin.db;
import com.google.common.base.Strings;
import io.cdap.cdap.api.annotation.Description;
import io.cdap.cdap.api.annotation.Macro;
import io.cdap.cdap.api.annotation.Name;
import io.cdap.cdap.api.dataset.lib.KeyValue;
import io.cdap.cdap.api.plugin.PluginConfig;
import io.cdap.plugin.common.KeyValueListParser;
import io.cdap.plugin.db.config.DatabaseConnectionConfig;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
/**
* Defines a base {@link PluginConfig} that Database source, sink, and action can all re-use.
*/
public abstract class ConnectionConfig extends PluginConfig implements DatabaseConnectionConfig {
public static final String CONNECTION_STRING = "connectionString";
public static final String ENABLE_AUTO_COMMIT = "enableAutoCommit";
public static final String USER = "user";
public static final String HOST = "host";
public static final String PORT = "port";
public static final String DATABASE = "database";
public static final String PASSWORD = "password";
public static final String CONNECTION_ARGUMENTS = "connectionArguments";
public static final String JDBC_PLUGIN_NAME = "jdbcPluginName";
public static final String JDBC_PLUGIN_TYPE = "jdbc";
public static final String TRANSACTION_ISOLATION_LEVEL = "transactionIsolationLevel";
@Name(JDBC_PLUGIN_NAME)
@Description("Name of the JDBC driver to use. This is the value of the 'jdbcPluginName' key defined in the JSON " +
"file for the JDBC plugin.")
private String jdbcPluginName;
@Name(USER)
@Description("User to use to connect to the specified database. Required for databases that " +
"need authentication. Optional for databases that do not require authentication.")
@Nullable
@Macro
private String user;
@Name(PASSWORD)
@Description("Password to use to connect to the specified database. Required for databases that " +
"need authentication. Optional for databases that do not require authentication.")
@Nullable
@Macro
private String password;
@Name(CONNECTION_ARGUMENTS)
@Description("A list of arbitrary string key/value pairs as connection arguments.")
@Nullable
@Macro
public String connectionArguments;
public ConnectionConfig() {
}
/**
* Parses connection arguments into a {@link Map}.
* @return a {@link Map} of connection arguments, parsed from the config.
*/
public Map<String, String> getConnectionArguments() {
Map<String, String> arguments = getConnectionArguments(connectionArguments, user, password);
arguments.putAll(getDBSpecificArguments());
return arguments;
}
/**
* Returns list of initialization queries. Initialization queries supposed to be executed preserving order right after
* connection establishing. In the case when there are no initialization queries, an empty list will be returned.
* @return list of initialization queries.
*/
public List<String> getInitQueries() {
return Collections.emptyList();
}
/**
* Parses connection arguments into a {@link Map}.
*
* @param connectionArguments See {@link ConnectionConfig#connectionArguments}.
* @param user See {@link ConnectionConfig#user}.
* @param password See {@link ConnectionConfig#password}.
*/
protected static Map<String, String> getConnectionArguments(@Nullable String connectionArguments,
@Nullable String user, @Nullable String password) {
KeyValueListParser kvParser = new KeyValueListParser("\\s*;\\s*", "=");
Map<String, String> connectionArgumentsMap = new HashMap<>();
if (!Strings.isNullOrEmpty(connectionArguments)) {
for (KeyValue<String, String> keyVal : kvParser.parse(connectionArguments)) {
connectionArgumentsMap.put(keyVal.getKey(), keyVal.getValue());
}
}
if (user != null) {
connectionArgumentsMap.put("user", user);
connectionArgumentsMap.put("password", password);
}
return connectionArgumentsMap;
}
/**
* Provides support for database-specific configuration properties.
* @return {@link Map} of additional connection arguments.
*/
protected Map<String, String> getDBSpecificArguments() {
return Collections.emptyMap();
}
public String getJdbcPluginName() {
return jdbcPluginName;
}
public String getUser() {
return user;
}
public String getPassword() {
return password;
}
}