Skip to content

Commit cd95b56

Browse files
authored
Merge pull request #1 from blockwarecom/DEXI-505
Dexi 505
2 parents fe7903b + 445c86f commit cd95b56

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.blockware.spring-boot</groupId>
88
<artifactId>sqldb-postgresql</artifactId>
9-
<version>0.0.3</version>
9+
<version>0.0.4</version>
1010

1111
<licenses>
1212
<license>
@@ -70,7 +70,7 @@
7070
<dependency>
7171
<groupId>com.blockware.spring-boot</groupId>
7272
<artifactId>spring-boot</artifactId>
73-
<version>0.0.5</version>
73+
<version>0.0.6</version>
7474
</dependency>
7575
</dependencies>
7676

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.blockware.spring.annotation;
22

3-
import com.blockware.spring.postgres.PostgresConfiguration;
3+
import com.blockware.spring.postgres.AbstractPostgresConfig;
44
import org.springframework.context.annotation.Import;
55

66
import java.lang.annotation.*;
@@ -9,8 +9,5 @@
99
@Retention(RetentionPolicy.RUNTIME)
1010
@Documented
1111
@Inherited
12-
@Import({
13-
PostgresConfiguration.class
14-
})
1512
public @interface BlockwareEnablePostgres {
1613
}

src/main/java/com/blockware/spring/postgres/PostgresConfiguration.java renamed to src/main/java/com/blockware/spring/postgres/AbstractPostgresConfig.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
import org.springframework.beans.factory.annotation.Value;
77
import org.springframework.boot.jdbc.DataSourceBuilder;
88
import org.springframework.context.annotation.Bean;
9-
import org.springframework.context.annotation.Primary;
109

1110
import javax.sql.DataSource;
1211
import java.sql.*;
1312
import java.util.Optional;
1413

1514
@Slf4j
16-
public class PostgresConfiguration {
15+
abstract public class AbstractPostgresConfig {
1716
private static final String RESOURCE_TYPE = "sqldb.blockware.com/v1/postgresql";
1817

1918
private static final String PORT_TYPE = "postgres";
@@ -24,37 +23,37 @@ public class PostgresConfiguration {
2423
@Autowired
2524
private BlockwareClusterService blockwareClusterService;
2625

27-
private String getDatabaseName() {
28-
return applicationName;
26+
private final String resourceName;
27+
28+
public AbstractPostgresConfig(String resourceName) {
29+
this.resourceName = resourceName;
2930
}
3031

3132
@Bean
32-
@Primary
3333
public DataSource dataSource() {
3434

35-
final BlockwareClusterService.ResourceInfo info = blockwareClusterService.getResourceInfo(RESOURCE_TYPE, PORT_TYPE);
35+
final BlockwareClusterService.ResourceInfo info = blockwareClusterService.getResourceInfo(RESOURCE_TYPE, PORT_TYPE, resourceName);
3636
Optional<String> dbUsername = Optional.ofNullable(info.getCredentials().get("username"));
3737
Optional<String> dbPassword = Optional.ofNullable(info.getCredentials().get("password"));
3838

39+
String databaseName = String.valueOf(info.getOptions().getOrDefault("dbName", resourceName));
40+
3941
String jdbcBaseUrl = String.format("jdbc:postgresql://%s:%s", info.getHost(), info.getPort());
4042

41-
log.info("Connecting to postgres server: {}", jdbcBaseUrl);
43+
log.info("Connecting to postgres server: {} for database: {}", jdbcBaseUrl, databaseName);
4244

43-
String jdbcUrl = String.format("%s/%s", jdbcBaseUrl, getDatabaseName());
45+
String jdbcUrl = String.format("%s/%s", jdbcBaseUrl, databaseName);
4446

4547
final DataSourceBuilder<?> builder = DataSourceBuilder.create();
4648

4749
builder.url(jdbcUrl);
4850

49-
if (dbUsername.isPresent()) {
50-
builder.username(dbUsername.get())
51-
.password(dbPassword.orElse(""));
52-
}
53-
51+
dbUsername.ifPresent(s -> builder.username(s)
52+
.password(dbPassword.orElse("")));
5453

5554
try {
5655
ensureDatabase(jdbcBaseUrl,
57-
getDatabaseName(),
56+
databaseName,
5857
dbUsername.orElse(null),
5958
dbPassword.orElse(null)
6059
);

0 commit comments

Comments
 (0)