-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #240 from spikymonkey/url_decoding_data_source_1.x
Handle drivers which don't support URL-encoding (1.x)
- Loading branch information
Showing
11 changed files
with
478 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...tor/src/main/java/org/springframework/cloud/service/relational/UrlDecodingDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* 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 org.springframework.cloud.service.relational; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLDecoder; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import java.util.logging.Logger; | ||
import javax.sql.DataSource; | ||
|
||
import org.springframework.beans.BeanWrapper; | ||
import org.springframework.beans.BeanWrapperImpl; | ||
import org.springframework.jdbc.datasource.DelegatingDataSource; | ||
import org.springframework.jdbc.datasource.SimpleDriverDataSource; | ||
|
||
/** | ||
* <p> | ||
* {@link UrlDecodingDataSource} is used to transparently handle combinations of Clouds and database | ||
* drivers which may or may not provide or accept URL-encoded connection strings. | ||
* </p> | ||
* | ||
* <p> | ||
* This {@link DataSource} implementation transparently delegates to an underlying {@link DataSource}, | ||
* except in the case where a connection attempt is made and no previous connection attempt has been | ||
* successful. In this case, if the underlying {@link DataSource} fails to connect, | ||
* {@link UrlDecodingDataSource} makes a test connection using a URL-decoded version of the configured | ||
* JDBC URL. If this is successful, the underlying {@link DataSource} is updated with the decoded JDBC | ||
* URL, and used to establish the final connection which is returned to the client. | ||
* </p> | ||
* | ||
* @author Gareth Clay | ||
*/ | ||
class UrlDecodingDataSource extends DelegatingDataSource { | ||
|
||
interface DataSourceFactory { | ||
DataSource apply(String jdbcUrl); | ||
} | ||
|
||
private static final Logger logger = Logger.getLogger(DelegatingDataSource.class.getName()); | ||
|
||
private final String urlPropertyName; | ||
private final DataSourceFactory connectionTestDataSourceFactory; | ||
|
||
private volatile boolean successfullyConnected = false; | ||
|
||
UrlDecodingDataSource(String jdbcUrl) { | ||
this(newSimpleDriverDataSource(jdbcUrl), "url"); | ||
} | ||
|
||
UrlDecodingDataSource(DataSource targetDataSource, String urlPropertyName) { | ||
this(targetDataSource, urlPropertyName, newSimpleDriverDataSourceFactory()); | ||
} | ||
|
||
UrlDecodingDataSource(DataSource targetDataSource, String urlPropertyName, DataSourceFactory connectionTestDataSourceFactory) { | ||
super(targetDataSource); | ||
this.urlPropertyName = urlPropertyName; | ||
this.connectionTestDataSourceFactory = connectionTestDataSourceFactory; | ||
} | ||
|
||
@Override | ||
public Connection getConnection() throws SQLException { | ||
if (successfullyConnected) return super.getConnection(); | ||
|
||
synchronized (this) { | ||
Connection connection; | ||
|
||
try { | ||
connection = super.getConnection(); | ||
successfullyConnected = true; | ||
} catch (SQLException e) { | ||
logger.info("Database connection failed. Trying again with url-decoded jdbc url"); | ||
DataSource targetDataSource = getTargetDataSource(); | ||
if (targetDataSource == null) throw new IllegalStateException("target DataSource should never be null"); | ||
BeanWrapper dataSourceWrapper = new BeanWrapperImpl(targetDataSource); | ||
String decodedJdbcUrl = decode((String) dataSourceWrapper.getPropertyValue(urlPropertyName)); | ||
DataSource urlDecodedConnectionTestDataSource = connectionTestDataSourceFactory.apply(decodedJdbcUrl); | ||
urlDecodedConnectionTestDataSource.getConnection(); | ||
|
||
logger.info("Connection test successful. Continuing with url-decoded jdbc url"); | ||
dataSourceWrapper.setPropertyValue(urlPropertyName, decodedJdbcUrl); | ||
connection = super.getConnection(); | ||
successfullyConnected = true; | ||
} | ||
|
||
return connection; | ||
} | ||
} | ||
|
||
private static DataSource newSimpleDriverDataSource(String jdbcUrl) { | ||
try { | ||
return new SimpleDriverDataSource(DriverManager.getDriver(jdbcUrl), jdbcUrl); | ||
} catch (SQLException e) { | ||
throw new RuntimeException("Unable to construct DataSource", e); | ||
} | ||
} | ||
|
||
private static DataSourceFactory newSimpleDriverDataSourceFactory() { | ||
return new DataSourceFactory() { | ||
@Override | ||
public DataSource apply(String jdbcUrl) { | ||
return newSimpleDriverDataSource(jdbcUrl); | ||
} | ||
}; | ||
} | ||
|
||
private static String decode(String string) { | ||
try { | ||
return URLDecoder.decode(string, "UTF-8"); | ||
} catch (UnsupportedEncodingException e) { | ||
return string; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.