Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Splits the SQL starter into a MySQL and a PostgreSQL one (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoandremartins committed Jan 31, 2018
1 parent 03f327e commit 6042d64
Show file tree
Hide file tree
Showing 17 changed files with 370 additions and 218 deletions.
7 changes: 6 additions & 1 deletion spring-cloud-gcp-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory</artifactId>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand All @@ -83,7 +89,6 @@
<artifactId>postgres-socket-factory</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2018 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.gcp.autoconfigure.sql;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.gcp.autoconfigure.core.AppEngineCondition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

/**
* Provides the {@link CloudSqlJdbcInfoProvider} for apps running on Google App Engine.
*
* @author João André Martins
*/
@Configuration
@ConditionalOnClass({com.google.cloud.sql.mysql.SocketFactory.class})
@Conditional(AppEngineCondition.class)
class AppEngineJdbcInfoProviderAutoConfiguration {

private static final Log LOGGER =
LogFactory.getLog(AppEngineJdbcInfoProviderAutoConfiguration.class);

@Bean
@ConditionalOnMissingBean(CloudSqlJdbcInfoProvider.class)
public CloudSqlJdbcInfoProvider appengineCloudSqlJdbcInfoProvider(
GcpCloudSqlProperties gcpCloudSqlProperties) {
CloudSqlJdbcInfoProvider appEngineProvider =
new AppEngineCloudSqlJdbcInfoProvider(gcpCloudSqlProperties);

if (LOGGER.isInfoEnabled()) {
LOGGER.info("App Engine JdbcUrl provider. Connecting to "
+ appEngineProvider.getJdbcUrl() + " with driver "
+ appEngineProvider.getJdbcDriverClass());
}

return appEngineProvider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ public class DefaultCloudSqlJdbcInfoProvider implements CloudSqlJdbcInfoProvider

private final GcpCloudSqlProperties properties;

public DefaultCloudSqlJdbcInfoProvider(GcpCloudSqlProperties properties) {
private final DatabaseType databaseType;

public DefaultCloudSqlJdbcInfoProvider(GcpCloudSqlProperties properties,
DatabaseType databaseType) {
this.properties = properties;
this.databaseType = databaseType;
Assert.hasText(this.properties.getDatabaseName(), "A database name must be provided.");
Assert.hasText(this.properties.getInstanceConnectionName(),
"An instance connection name must be provided. Refer to "
Expand All @@ -41,12 +45,12 @@ public DefaultCloudSqlJdbcInfoProvider(GcpCloudSqlProperties properties) {

@Override
public String getJdbcDriverClass() {
return this.properties.getDatabaseType().getJdbcDriverName();
return this.databaseType.getJdbcDriverName();
}

@Override
public String getJdbcUrl() {
return String.format(this.properties.getDatabaseType().getJdbcUrlTemplate(),
return String.format(this.databaseType.getJdbcUrlTemplate(),
this.properties.getDatabaseName(),
this.properties.getInstanceConnectionName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,38 @@

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.gcp.autoconfigure.core.AppEngineCondition;
import org.springframework.cloud.gcp.autoconfigure.core.GcpContextAutoConfiguration;
import org.springframework.cloud.gcp.autoconfigure.core.GcpProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.util.StringUtils;

/**
* Google Cloud SQL starter.
*
* <p>Provides Google Cloud SQL instance connectivity through Spring JDBC by providing only a database and instance
* connection name.
* Provides Google Cloud SQL instance connectivity through Spring JDBC by providing only a
* database and instance connection name.
*
* @author João André Martins
*/
@Configuration
@EnableConfigurationProperties({ GcpCloudSqlProperties.class, DataSourceProperties.class })
@ConditionalOnClass(com.google.cloud.sql.mysql.SocketFactory.class)
@ConditionalOnProperty(name = "spring.cloud.gcp.sql.enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnProperty(
name = "spring.cloud.gcp.sql.enabled", havingValue = "true", matchIfMissing = true)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@AutoConfigureAfter(GcpContextAutoConfiguration.class)
@AutoConfigureAfter({GcpContextAutoConfiguration.class,
AppEngineJdbcInfoProviderAutoConfiguration.class,
MySqlJdbcInfoProviderAutoConfiguration.class,
PostgreSqlJdbcInfoProviderAutoConfiguration.class})
@Import({AppEngineJdbcInfoProviderAutoConfiguration.class,
MySqlJdbcInfoProviderAutoConfiguration.class,
PostgreSqlJdbcInfoProviderAutoConfiguration.class})
@ConditionalOnBean(CloudSqlJdbcInfoProvider.class)
public class GcpCloudSqlAutoConfiguration {

public final static String INSTANCE_CONNECTION_NAME_HELP_URL =
Expand All @@ -73,40 +76,6 @@ public GcpCloudSqlAutoConfiguration(GcpCloudSqlProperties gcpCloudSqlProperties,
this.gcpProperties = gcpProperties;
}

@Bean
@ConditionalOnMissingBean(CloudSqlJdbcInfoProvider.class)
@Conditional(AppEngineCondition.class)
public CloudSqlJdbcInfoProvider appengineCloudSqlJdbcInfoProvider() {
CloudSqlJdbcInfoProvider appEngineProvider = new AppEngineCloudSqlJdbcInfoProvider(this.gcpCloudSqlProperties);

if (LOGGER.isInfoEnabled()) {
LOGGER.info("App Engine JdbcUrl provider. Connecting to "
+ appEngineProvider.getJdbcUrl() + " with driver "
+ appEngineProvider.getJdbcDriverClass());
}

setCredentialsProperty();

return appEngineProvider;
}

@Bean
@ConditionalOnMissingBean(CloudSqlJdbcInfoProvider.class)
public CloudSqlJdbcInfoProvider defaultJdbcInfoProvider() {
CloudSqlJdbcInfoProvider defaultProvider = new DefaultCloudSqlJdbcInfoProvider(this.gcpCloudSqlProperties);

if (LOGGER.isInfoEnabled()) {
LOGGER.info("Default " + this.gcpCloudSqlProperties.getDatabaseType().name()
+ " JdbcUrl provider. Connecting to "
+ defaultProvider.getJdbcUrl() + " with driver "
+ defaultProvider.getJdbcDriverClass());
}

setCredentialsProperty();

return defaultProvider;
}

@Bean
@Primary
public DataSourceProperties cloudSqlDataSourceProperties(DataSourceProperties properties,
Expand All @@ -128,6 +97,9 @@ public DataSourceProperties cloudSqlDataSourceProperties(DataSourceProperties pr
else {
LOGGER.warn("spring.datasource.url is specified. Not using generated Cloud SQL configuration");
}

setCredentialsProperty();

return properties;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ public class GcpCloudSqlProperties {
/** Overrides the GCP OAuth2 credentials specified in the Core module. */
private Credentials credentials;

/** SQL database vendor. */
private DatabaseType databaseType = DatabaseType.MYSQL;

public String getDatabaseName() {
return this.databaseName;
}
Expand All @@ -62,14 +59,6 @@ public void setCredentials(Credentials credentials) {
this.credentials = credentials;
}

public DatabaseType getDatabaseType() {
return this.databaseType;
}

public void setDatabaseType(DatabaseType databaseType) {
this.databaseType = databaseType;
}

public static class Credentials {
private Resource location;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2018 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.gcp.autoconfigure.sql;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Provides the {@link CloudSqlJdbcInfoProvider} for MySQL.
*
* @author João André Martins
*/
@Configuration
@ConditionalOnClass({com.google.cloud.sql.mysql.SocketFactory.class, com.mysql.jdbc.Driver.class})
class MySqlJdbcInfoProviderAutoConfiguration {

private static final Log LOGGER =
LogFactory.getLog(MySqlJdbcInfoProviderAutoConfiguration.class);

@Bean
@ConditionalOnMissingBean(CloudSqlJdbcInfoProvider.class)
public CloudSqlJdbcInfoProvider defaultMySqlJdbcInfoProvider(
GcpCloudSqlProperties gcpCloudSqlProperties) {
CloudSqlJdbcInfoProvider defaultProvider =
new DefaultCloudSqlJdbcInfoProvider(gcpCloudSqlProperties, DatabaseType.MYSQL);

if (LOGGER.isInfoEnabled()) {
LOGGER.info("Default " + DatabaseType.MYSQL.name()
+ " JdbcUrl provider. Connecting to "
+ defaultProvider.getJdbcUrl() + " with driver "
+ defaultProvider.getJdbcDriverClass());
}

return defaultProvider;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2018 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.gcp.autoconfigure.sql;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Provides the {@link CloudSqlJdbcInfoProvider} for PostgreSQL.
*
* @author João André Martins
*/
@Configuration
@ConditionalOnClass({com.google.cloud.sql.postgres.SocketFactory.class,
org.postgresql.Driver.class})
class PostgreSqlJdbcInfoProviderAutoConfiguration {

private static final Log LOGGER =
LogFactory.getLog(PostgreSqlJdbcInfoProviderAutoConfiguration.class);

@Bean
@ConditionalOnMissingBean(CloudSqlJdbcInfoProvider.class)
public CloudSqlJdbcInfoProvider defaultPostgreSqlJdbcInfoProvider(
GcpCloudSqlProperties gcpCloudSqlProperties) {
CloudSqlJdbcInfoProvider defaultProvider =
new DefaultCloudSqlJdbcInfoProvider(gcpCloudSqlProperties, DatabaseType.POSTGRESQL);

if (LOGGER.isInfoEnabled()) {
LOGGER.info("Default " + DatabaseType.POSTGRESQL.name()
+ " JdbcUrl provider. Connecting to "
+ defaultProvider.getJdbcUrl() + " with driver "
+ defaultProvider.getJdbcDriverClass());
}

return defaultProvider;
}
}
Loading

0 comments on commit 6042d64

Please sign in to comment.