Skip to content

Commit 27970d6

Browse files
committed
fixes JabRef#12844
* Fix for trag-bot comments. * Created separate class PostgresMetadataWriter to follow SRP.
1 parent 5029657 commit 27970d6

File tree

2 files changed

+46
-31
lines changed

2 files changed

+46
-31
lines changed

src/main/java/org/jabref/logic/search/PostgreServer.java

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
package org.jabref.logic.search;
22

33
import java.io.IOException;
4-
import java.nio.file.Path;
54
import java.sql.Connection;
65
import java.sql.SQLException;
7-
import java.time.Instant;
8-
import java.time.format.DateTimeFormatter;
9-
import java.util.HashMap;
10-
import java.util.Map;
116

127
import javax.sql.DataSource;
138

149
import org.jabref.model.search.PostgreConstants;
1510

16-
import com.fasterxml.jackson.databind.ObjectMapper;
1711
import io.zonky.test.db.postgres.embedded.EmbeddedPostgres;
1812
import org.slf4j.Logger;
1913
import org.slf4j.LoggerFactory;
@@ -22,11 +16,6 @@
2216

2317
public class PostgreServer {
2418
private static final Logger LOGGER = LoggerFactory.getLogger(PostgreServer.class);
25-
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
26-
private static final Path POSTGRES_METADATA_FILE = Path.of(
27-
System.getProperty("java.io.tmpdir"),
28-
"jabref-postgres-info-" + ProcessHandle.current().pid() + ".json"
29-
);
3019

3120
private final EmbeddedPostgres embeddedPostgres;
3221
private final DataSource dataSource;
@@ -38,7 +27,7 @@ public PostgreServer() {
3827
.setOutputRedirector(ProcessBuilder.Redirect.DISCARD)
3928
.start();
4029
LOGGER.info("Postgres server started, connection port: {}", embeddedPostgres.getPort());
41-
writeMetadataToFile(embeddedPostgres.getPort());
30+
PostgresMetadataWriter.write(embeddedPostgres.getPort());
4231
} catch (IOException e) {
4332
LOGGER.error("Could not start Postgres server", e);
4433
this.embeddedPostgres = null;
@@ -109,23 +98,4 @@ public void shutdown() {
10998
}
11099
}
111100
}
112-
113-
private void writeMetadataToFile(int port) {
114-
try {
115-
Map<String, Object> meta = createPostgresMetadata(port);
116-
OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValue(POSTGRES_METADATA_FILE.toFile(), meta);
117-
LOGGER.info("Postgres metadata file path: {}", POSTGRES_METADATA_FILE);
118-
} catch (IOException e) {
119-
LOGGER.warn("Failed to write Postgres metadata file", e);
120-
}
121-
}
122-
123-
private Map<String, Object> createPostgresMetadata(int port) {
124-
Map<String, Object> meta = new HashMap<>();
125-
meta.put("javaPid", ProcessHandle.current().pid());
126-
meta.put("postgresPort", port);
127-
meta.put("startedBy", "jabref");
128-
meta.put("startedAt", DateTimeFormatter.ISO_INSTANT.format(Instant.now()));
129-
return meta;
130-
}
131101
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.jabref.logic.search;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Path;
5+
import java.time.Instant;
6+
import java.time.format.DateTimeFormatter;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
14+
public class PostgresMetadataWriter {
15+
private static final Logger LOGGER = LoggerFactory.getLogger(PostgresMetadataWriter.class);
16+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
17+
18+
private PostgresMetadataWriter() {
19+
}
20+
21+
public static Path getMetadataFilePath() {
22+
return Path.of(System.getProperty("java.io.tmpdir"),
23+
"jabref-postgres-info-" + ProcessHandle.current().pid() + ".json");
24+
}
25+
26+
public static void write(int port) {
27+
try {
28+
Map<String, Object> meta = createMetadata(port);
29+
Path path = getMetadataFilePath();
30+
OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValue(path.toFile(), meta);
31+
LOGGER.info("Postgres metadata file path: {}", path);
32+
} catch (IOException e) {
33+
LOGGER.warn("Failed to write Postgres metadata file", e);
34+
}
35+
}
36+
37+
private static Map<String, Object> createMetadata(int port) {
38+
Map<String, Object> meta = new HashMap<>();
39+
meta.put("javaPid", ProcessHandle.current().pid());
40+
meta.put("postgresPort", port);
41+
meta.put("startedBy", "jabref");
42+
meta.put("startedAt", DateTimeFormatter.ISO_INSTANT.format(Instant.now()));
43+
return meta;
44+
}
45+
}

0 commit comments

Comments
 (0)