Skip to content

Commit 90459ed

Browse files
authored
Merge pull request #204 from mvegter/refactor/minor-improvements
Some minor improvements
2 parents f15a1f9 + 7cbf736 commit 90459ed

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@ target/
22
*.iml
33
.idea/
44

5+
# Eclipse
6+
.settings/
7+
.classpath
8+
.project
9+
510
# VSCode
611
.factorypath

docs/_includes/footer.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</div>
99
<div class="footer-title">
1010
Sponsored by
11-
<a href="https://container-solutions.com/" target="_blank">
11+
<a href="https://container-solutions.com/" target="_blank" rel="noopener noreferrer">
1212
<img src="{{ site.baseurl }}/assets/images/cs.png">
1313
</a>
1414
</div>

docs/_includes/header.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<div class="header-strip {{ page.collection }} {{ page.name | split: "." | first }}">
22
<div class="header navbar navbar-expand-lg">
33
<div class="logo">
4-
<a href="https://github.com/ContainerSolutions/java-operator-sdk/blob/master/README.md" target="_blank">
4+
<a href="https://github.com/ContainerSolutions/java-operator-sdk/blob/master/README.md" target="_blank" rel="noopener noreferrer">
55
<img src="{{ site.baseurl }}/assets/images/GitHub-Mark-64px.png">
6-
<a href="https://discord.gg/DacEhAy" target="_blank">
6+
<a href="https://discord.gg/DacEhAy" target="_blank" rel="noopener noreferrer">
77
<img src="{{ site.baseurl }}/assets/images/discord.png">
88
</a>
99
</div>

operator-framework/src/main/java/com/github/containersolutions/operator/ControllerUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class ControllerUtils {
1717

1818
private final static double JAVA_VERSION = Double.parseDouble(System.getProperty("java.specification.version"));
1919

20-
private final static Logger log = LoggerFactory.getLogger(Operator.class);
20+
private final static Logger log = LoggerFactory.getLogger(ControllerUtils.class);
2121

2222
// this is just to support testing, this way we don't try to create class multiple times in memory with same name.
2323
// note that other solution is to add a random string to doneable class name

samples/mysql-schema/src/main/java/com/github/containersolutions/operator/sample/SchemaController.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.sql.DriverManager;
1616
import java.sql.ResultSet;
1717
import java.sql.SQLException;
18+
import java.sql.Statement;
1819
import java.util.Base64;
1920

2021
import static java.lang.String.format;
@@ -38,21 +39,27 @@ public SchemaController(KubernetesClient kubernetesClient) {
3839
public UpdateControl<Schema> createOrUpdateResource(Schema schema, Context<Schema> context) {
3940
try (Connection connection = getConnection()) {
4041
if (!schemaExists(connection, schema.getMetadata().getName())) {
41-
connection.createStatement().execute(format("CREATE SCHEMA `%1$s` DEFAULT CHARACTER SET %2$s",
42+
try (Statement statement = connection.createStatement()) {
43+
statement.execute(format("CREATE SCHEMA `%1$s` DEFAULT CHARACTER SET %2$s",
4244
schema.getMetadata().getName(),
4345
schema.getSpec().getEncoding()));
46+
}
4447

4548
String password = RandomStringUtils.randomAlphanumeric(16);
4649
String userName = String.format(USERNAME_FORMAT,
4750
schema.getMetadata().getName());
4851
String secretName = String.format(SECRET_FORMAT,
4952
schema.getMetadata().getName());
50-
connection.createStatement().execute(format(
51-
"CREATE USER '%1$s' IDENTIFIED BY '%2$s'",
52-
userName, password));
53-
connection.createStatement().execute(format(
54-
"GRANT ALL ON `%1$s`.* TO '%2$s'",
55-
schema.getMetadata().getName(), userName));
53+
try (Statement statement = connection.createStatement()) {
54+
statement.execute(format("CREATE USER '%1$s' IDENTIFIED BY '%2$s'",
55+
userName,
56+
password));
57+
}
58+
try (Statement statement = connection.createStatement()) {
59+
statement.execute(format("GRANT ALL ON `%1$s`.* TO '%2$s'",
60+
schema.getMetadata().getName(),
61+
userName));
62+
}
5663
Secret credentialsSecret = new SecretBuilder()
5764
.withNewMetadata().withName(secretName).endMetadata()
5865
.addToData("MYSQL_USERNAME", Base64.getEncoder().encodeToString(userName.getBytes()))
@@ -95,11 +102,17 @@ public boolean deleteResource(Schema schema, Context<Schema> context) {
95102

96103
try (Connection connection = getConnection()) {
97104
if (schemaExists(connection, schema.getMetadata().getName())) {
98-
connection.createStatement().execute("DROP DATABASE `" + schema.getMetadata().getName() + "`");
105+
try (Statement statement = connection.createStatement()) {
106+
statement.execute(format("DROP DATABASE `%1$s`",
107+
schema.getMetadata().getName()));
108+
}
99109
log.info("Deleted Schema '{}'", schema.getMetadata().getName());
100110

101111
if (userExists(connection, schema.getStatus().getUserName())) {
102-
connection.createStatement().execute("DROP USER '" + schema.getStatus().getUserName() + "'");
112+
try (Statement statement = connection.createStatement()) {
113+
statement.execute(format("DROP USER '%1$s'",
114+
schema.getStatus().getUserName()));
115+
}
103116
log.info("Deleted User '{}'", schema.getStatus().getUserName());
104117
}
105118

@@ -134,9 +147,10 @@ private boolean schemaExists(Connection connection, String schemaName) throws SQ
134147
}
135148

136149
private boolean userExists(Connection connection, String userName) throws SQLException {
137-
ResultSet resultSet = connection.createStatement().executeQuery(
138-
format("SELECT User FROM mysql.user WHERE User='%1$s'", userName)
139-
);
140-
return resultSet.first();
150+
try (Statement statement = connection.createStatement()) {
151+
ResultSet resultSet = statement.executeQuery(format("SELECT User FROM mysql.user WHERE User='%1$s'",
152+
userName));
153+
return resultSet.first();
154+
}
141155
}
142156
}

0 commit comments

Comments
 (0)