Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* 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 com.amazon.awslabs.jdbc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,19 @@
*/
public class ConnectionPluginManager implements CanReleaseResources {

protected static final Map<String, Class<? extends ConnectionPluginFactory>> pluginFactoriesByCode =
new HashMap<String, Class<? extends ConnectionPluginFactory>>() {
{
put("executionTime", ExecutionTimeConnectionPluginFactory.class);
put("auroraHostList", AuroraHostListConnectionPluginFactory.class);
put("logQuery", LogQueryConnectionPluginFactory.class);
put("dataCache", DataCacheConnectionPluginFactory.class);
put("efm", HostMonitoringConnectionPluginFactory.class);
put("failover", FailoverConnectionPluginFactory.class);
put("iam", IamAuthConnectionPluginFactory.class);
}
};
protected static final Map<String, Class<? extends ConnectionPluginFactory>>
pluginFactoriesByCode =
new HashMap<String, Class<? extends ConnectionPluginFactory>>() {
{
put("executionTime", ExecutionTimeConnectionPluginFactory.class);
put("auroraHostList", AuroraHostListConnectionPluginFactory.class);
put("logQuery", LogQueryConnectionPluginFactory.class);
put("dataCache", DataCacheConnectionPluginFactory.class);
put("efm", HostMonitoringConnectionPluginFactory.class);
put("failover", FailoverConnectionPluginFactory.class);
put("iam", IamAuthConnectionPluginFactory.class);
}
};

protected static final String DEFAULT_PLUGINS = "";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@
import java.util.Properties;
import org.checkerframework.checker.nullness.qual.NonNull;

/**
* Implement this interface in order to handle physical connection creation process.
*/
/** Implement this interface in order to handle physical connection creation process. */
public interface ConnectionProvider {

/**
* Called once per connection that needs to be created.
*
* @param protocol The connection protocol (example "jdbc:mysql://")
* @param hostSpec The HostSpec containing the host-port information for the host to connect to
* @param props The Properties to use for the connection
* @param props The Properties to use for the connection
* @return {@link Connection} resulting from the given connection information
* @throws SQLException if an error occurs
*/
Expand All @@ -44,7 +42,7 @@ Connection connect(
/**
* Called once per connection that needs to be created.
*
* @param url The connection URL
* @param url The connection URL
* @param props The Properties to use for the connection
* @return {@link Connection} resulting from the given connection information
* @throws SQLException if an error occurs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ public DataSourceConnectionProvider(final DataSource dataSource) {
this(dataSource, null, null, null, null, null, null);
}

public DataSourceConnectionProvider(final @NonNull DataSource dataSource,
final @Nullable String serverPropertyName,
final @Nullable String portPropertyName,
final @Nullable String urlPropertyName,
final @Nullable String databasePropertyName,
final @Nullable String usernamePropertyName,
final @Nullable String passwordPropertyName) {
public DataSourceConnectionProvider(
final @NonNull DataSource dataSource,
final @Nullable String serverPropertyName,
final @Nullable String portPropertyName,
final @Nullable String urlPropertyName,
final @Nullable String databasePropertyName,
final @Nullable String usernamePropertyName,
final @Nullable String passwordPropertyName) {
this.dataSource = dataSource;
this.serverPropertyName = serverPropertyName;
this.portPropertyName = portPropertyName;
Expand Down Expand Up @@ -92,21 +93,26 @@ public Connection connect(
copy.put(this.portPropertyName, hostSpec.getPort());
}

if (!isNullOrEmpty(this.databasePropertyName) && !isNullOrEmpty(props.getProperty(DATABASE_PROPERTY_NAME))) {
if (!isNullOrEmpty(this.databasePropertyName)
&& !isNullOrEmpty(props.getProperty(DATABASE_PROPERTY_NAME))) {
copy.setProperty(this.databasePropertyName, props.getProperty(DATABASE_PROPERTY_NAME));
}

if (!isNullOrEmpty(this.userPropertyName) && !isNullOrEmpty(props.getProperty(USER_PROPERTY_NAME))) {
if (!isNullOrEmpty(this.userPropertyName)
&& !isNullOrEmpty(props.getProperty(USER_PROPERTY_NAME))) {
copy.setProperty(this.userPropertyName, props.getProperty(USER_PROPERTY_NAME));
}

if (!isNullOrEmpty(this.passwordPropertyName) && !isNullOrEmpty(props.getProperty(PASSWORD_PROPERTY_NAME))) {
if (!isNullOrEmpty(this.passwordPropertyName)
&& !isNullOrEmpty(props.getProperty(PASSWORD_PROPERTY_NAME))) {
copy.setProperty(this.passwordPropertyName, props.getProperty(PASSWORD_PROPERTY_NAME));
}

if (!isNullOrEmpty(this.urlPropertyName) && !isNullOrEmpty(props.getProperty(this.urlPropertyName))) {
if (!isNullOrEmpty(this.urlPropertyName)
&& !isNullOrEmpty(props.getProperty(this.urlPropertyName))) {
Properties urlProperties = PropertyUtils.copyProperties(copy);
// Remove the current url property to replace with a new url built from updated HostSpec and properties
// Remove the current url property to replace with a new url built from updated HostSpec and
// properties
urlProperties.remove(this.urlPropertyName);

copy.setProperty(
Expand Down Expand Up @@ -134,22 +140,26 @@ public Connection connect(
* @return {@link Connection} resulting from the given connection information
* @throws SQLException if an error occurs
*/
public Connection connect(final @NonNull String url, final @NonNull Properties props) throws SQLException {
public Connection connect(final @NonNull String url, final @NonNull Properties props)
throws SQLException {
Properties copy = PropertyUtils.copyProperties(props);

if (!isNullOrEmpty(this.urlPropertyName)) {
copy.setProperty(this.urlPropertyName, url);
}

if (!isNullOrEmpty(this.userPropertyName) && !isNullOrEmpty(props.getProperty(USER_PROPERTY_NAME))) {
if (!isNullOrEmpty(this.userPropertyName)
&& !isNullOrEmpty(props.getProperty(USER_PROPERTY_NAME))) {
copy.put(this.userPropertyName, props.getProperty(USER_PROPERTY_NAME));
}

if (!isNullOrEmpty(this.passwordPropertyName) && !isNullOrEmpty(props.getProperty(PASSWORD_PROPERTY_NAME))) {
if (!isNullOrEmpty(this.passwordPropertyName)
&& !isNullOrEmpty(props.getProperty(PASSWORD_PROPERTY_NAME))) {
copy.put(this.passwordPropertyName, props.getProperty(PASSWORD_PROPERTY_NAME));
}

if (!isNullOrEmpty(this.databasePropertyName) && !isNullOrEmpty(props.getProperty(DATABASE_PROPERTY_NAME))) {
if (!isNullOrEmpty(this.databasePropertyName)
&& !isNullOrEmpty(props.getProperty(DATABASE_PROPERTY_NAME))) {
copy.put(this.databasePropertyName, props.getProperty(DATABASE_PROPERTY_NAME));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import org.checkerframework.checker.nullness.qual.NonNull;

/**
* This class is a basic implementation of {@link ConnectionProvider} interface. It creates and returns a connection
* provided by a target driver or a data source.
* This class is a basic implementation of {@link ConnectionProvider} interface. It creates and
* returns a connection provided by a target driver or a data source.
*/
public class DriverConnectionProvider implements ConnectionProvider {

Expand All @@ -42,7 +42,8 @@ public DriverConnectionProvider(final java.sql.Driver driver) {
this(driver, null, null);
}

public DriverConnectionProvider(final java.sql.Driver driver, String userPropertyName, String passwordPropertyName) {
public DriverConnectionProvider(
final java.sql.Driver driver, String userPropertyName, String passwordPropertyName) {
this.driver = driver;
this.userPropertyName = userPropertyName;
this.passwordPropertyName = passwordPropertyName;
Expand All @@ -53,7 +54,7 @@ public DriverConnectionProvider(final java.sql.Driver driver, String userPropert
*
* @param protocol The connection protocol (example "jdbc:mysql://")
* @param hostSpec The HostSpec containing the host-port information for the host to connect to
* @param props The Properties to use for the connection
* @param props The Properties to use for the connection
* @return {@link Connection} resulting from the given connection information
* @throws SQLException if an error occurs
*/
Expand All @@ -65,15 +66,19 @@ public Connection connect(
throws SQLException {

final String databaseName =
props.getProperty(DATABASE_PROPERTY_NAME) != null ? props.getProperty(DATABASE_PROPERTY_NAME) : "";
props.getProperty(DATABASE_PROPERTY_NAME) != null
? props.getProperty(DATABASE_PROPERTY_NAME)
: "";
final StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append(protocol).append(hostSpec.getUrl()).append(databaseName);

if (!isNullOrEmpty(this.userPropertyName) && !isNullOrEmpty(props.getProperty(USER_PROPERTY_NAME))) {
if (!isNullOrEmpty(this.userPropertyName)
&& !isNullOrEmpty(props.getProperty(USER_PROPERTY_NAME))) {
props.setProperty(this.userPropertyName, props.getProperty(USER_PROPERTY_NAME));
}

if (!isNullOrEmpty(this.passwordPropertyName) && !isNullOrEmpty(props.getProperty(PASSWORD_PROPERTY_NAME))) {
if (!isNullOrEmpty(this.passwordPropertyName)
&& !isNullOrEmpty(props.getProperty(PASSWORD_PROPERTY_NAME))) {
props.setProperty(this.passwordPropertyName, props.getProperty(PASSWORD_PROPERTY_NAME));
}

Expand All @@ -83,7 +88,7 @@ public Connection connect(
/**
* Called once per connection that needs to be created.
*
* @param url The connection URL
* @param url The connection URL
* @param props The Properties to use for the connection
* @return {@link Connection} resulting from the given connection information
* @throws SQLException if an error occurs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package com.amazon.awslabs.jdbc;

import com.amazon.awslabs.jdbc.HostListProvider;

public interface HostListProviderService {

boolean isStaticHostListProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@

package com.amazon.awslabs.jdbc;

import com.amazon.awslabs.jdbc.ConnectionPlugin;
import com.amazon.awslabs.jdbc.HostAvailability;
import com.amazon.awslabs.jdbc.HostListProvider;
import com.amazon.awslabs.jdbc.HostSpec;
import com.amazon.awslabs.jdbc.NodeChangeOptions;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.EnumSet;
Expand All @@ -33,7 +28,8 @@
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Interface for retrieving the current active {@link Connection} and its {@link HostSpec}.
* Interface for retrieving the current active {@link Connection} and its {@link
* com.amazon.awslabs.jdbc.HostSpec}.
*/
public interface PluginService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

public class PluginServiceImpl implements PluginService, CanReleaseResources, HostListProviderService,
PluginManagerService {
public class PluginServiceImpl
implements PluginService, CanReleaseResources, HostListProviderService, PluginManagerService {

private static final Logger LOGGER = Logger.getLogger(PluginServiceImpl.class.getName());

Expand Down Expand Up @@ -108,8 +108,8 @@ public synchronized EnumSet<NodeChangeOptions> setCurrentConnection(
} else {
// update an existing connection

EnumSet<NodeChangeOptions> changes = compare(this.currentConnection, this.currentHostSpec,
connection, hostSpec);
EnumSet<NodeChangeOptions> changes =
compare(this.currentConnection, this.currentHostSpec, connection, hostSpec);

if (!changes.isEmpty()) {

Expand All @@ -119,8 +119,8 @@ public synchronized EnumSet<NodeChangeOptions> setCurrentConnection(
this.currentHostSpec = hostSpec;
this.setInTransaction(false);

EnumSet<OldConnectionSuggestedAction> pluginOpinions = this.pluginManager.notifyConnectionChanged(
changes, skipNotificationForThisPlugin);
EnumSet<OldConnectionSuggestedAction> pluginOpinions =
this.pluginManager.notifyConnectionChanged(changes, skipNotificationForThisPlugin);

boolean shouldCloseConnection =
changes.contains(NodeChangeOptions.CONNECTION_OBJECT_CHANGED)
Expand Down Expand Up @@ -192,20 +192,27 @@ public List<HostSpec> getHosts() {
}

@Override
public void setAvailability(final @NonNull Set<String> hostAliases, final @NonNull HostAvailability availability) {
public void setAvailability(
final @NonNull Set<String> hostAliases, final @NonNull HostAvailability availability) {

if (hostAliases.isEmpty()) {
return;
}

List<HostSpec> hostsToChange = this.getHosts().stream()
.filter((host) -> hostAliases.contains(host.asAlias())
|| host.getAliases().stream().anyMatch((hostAlias) -> hostAliases.contains(hostAlias)))
.distinct()
.collect(Collectors.toList());
List<HostSpec> hostsToChange =
this.getHosts().stream()
.filter(
(host) ->
hostAliases.contains(host.asAlias())
|| host.getAliases().stream()
.anyMatch((hostAlias) -> hostAliases.contains(hostAlias)))
.distinct()
.collect(Collectors.toList());

if (hostsToChange.isEmpty()) {
LOGGER.log(Level.FINEST, String.format("Can't find any host by the following aliases: %s.", hostAliases));
LOGGER.log(
Level.FINEST,
String.format("Can't find any host by the following aliases: %s.", hostAliases));
return;
}

Expand Down Expand Up @@ -236,7 +243,8 @@ public boolean isExplicitReadOnly() {

@Override
public boolean isReadOnly() {
return isExplicitReadOnly() || (this.currentHostSpec != null && this.currentHostSpec.getRole() != HostRole.WRITER);
return isExplicitReadOnly()
|| (this.currentHostSpec != null && this.currentHostSpec.getRole() != HostRole.WRITER);
}

@Override
Expand All @@ -259,7 +267,8 @@ public HostListProvider getHostListProvider() {
if (this.hostListProvider == null) {
synchronized (this) {
if (this.hostListProvider == null) {
this.hostListProvider = new ConnectionStringHostListProvider(this.props, this.originalUrl);
this.hostListProvider =
new ConnectionStringHostListProvider(this.props, this.originalUrl);
this.currentHostSpec = this.getCurrentHostSpec();
}
}
Expand All @@ -277,16 +286,18 @@ public void forceRefreshHostList() throws SQLException {
setNodeList(this.hosts, this.getHostListProvider().forceRefresh());
}

void setNodeList(@Nullable final List<HostSpec> oldHosts,
@Nullable final List<HostSpec> newHosts) {
void setNodeList(
@Nullable final List<HostSpec> oldHosts, @Nullable final List<HostSpec> newHosts) {

Map<String, HostSpec> oldHostMap = oldHosts == null
? new HashMap<>()
: oldHosts.stream().collect(Collectors.toMap(HostSpec::getUrl, (value) -> value));
Map<String, HostSpec> oldHostMap =
oldHosts == null
? new HashMap<>()
: oldHosts.stream().collect(Collectors.toMap(HostSpec::getUrl, (value) -> value));

Map<String, HostSpec> newHostMap = newHosts == null
? new HashMap<>()
: newHosts.stream().collect(Collectors.toMap(HostSpec::getUrl, (value) -> value));
Map<String, HostSpec> newHostMap =
newHosts == null
? new HashMap<>()
: newHosts.stream().collect(Collectors.toMap(HostSpec::getUrl, (value) -> value));

Map<String, EnumSet<NodeChangeOptions>> changes = new HashMap<>();

Expand All @@ -297,8 +308,8 @@ void setNodeList(@Nullable final List<HostSpec> oldHosts,
changes.put(entry.getKey(), EnumSet.of(NodeChangeOptions.NODE_DELETED));
} else {
// host maybe changed
EnumSet<NodeChangeOptions> hostChanges = compare(null, entry.getValue(), null,
correspondingNewHost);
EnumSet<NodeChangeOptions> hostChanges =
compare(null, entry.getValue(), null, correspondingNewHost);
if (!hostChanges.isEmpty()) {
changes.put(entry.getKey(), hostChanges);
}
Expand Down Expand Up @@ -330,7 +341,8 @@ public void setHostListProvider(HostListProvider hostListProvider) {

@Override
public Connection connect(HostSpec hostSpec, Properties props) throws SQLException {
return this.pluginManager.connect(this.driverProtocol, hostSpec, props, this.currentConnection == null);
return this.pluginManager.connect(
this.driverProtocol, hostSpec, props, this.currentConnection == null);
}

@Override
Expand Down
Loading