Skip to content

Commit

Permalink
Load resource stream over file (#733)
Browse files Browse the repository at this point in the history
* Don't load resource file, instead use a stream for the bytes
Add extra debug info if needed

* Don't rely on library to read the DDL

* Remove unused import
  • Loading branch information
prd-fox authored and melowe committed May 7, 2019
1 parent 0e39dde commit 7e39a03
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 24 deletions.
2 changes: 1 addition & 1 deletion config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>


</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,4 @@ public void setCrossDomainConfig(CrossDomainConfig crossDomainConfig) {
this.crossDomainConfig = crossDomainConfig;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ private Options createOptions() {
.required()
.build());


options.addOption(
Option.builder()
.longOpt("exporttype")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.quorum.tessera.data.migration;

import com.quorum.tessera.io.IOCallback;
import com.quorum.tessera.io.UriCallback;

import java.io.BufferedReader;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class H2DataExporter implements DataExporter {

Expand All @@ -25,9 +23,11 @@ public void export(final StoreLoader loader,

final String connectionString = "jdbc:h2:" + output.toString();

final URL sqlFile = getClass().getResource(CREATE_TABLE_RESOURCE);
final Path uri = UriCallback.execute(() -> Paths.get(sqlFile.toURI()));
final List<String> createTableStatements = IOCallback.execute(() -> Files.readAllLines(uri));
final List<String> createTableStatements = Stream.of(getClass().getResourceAsStream(CREATE_TABLE_RESOURCE))
.map(InputStreamReader::new)
.map(BufferedReader::new)
.flatMap(BufferedReader::lines)
.collect(Collectors.toList());

final JdbcDataExporter jdbcDataExporter
= new JdbcDataExporter(connectionString, INSERT_ROW, createTableStatements);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.quorum.tessera.data.migration;

import java.util.Arrays;

public class Main {

private Main() {
Expand All @@ -12,7 +14,14 @@ public static void main(final String... args) {
final int result = new CmdLineExecutor().execute(args);
System.exit(result);
} catch (final Exception ex) {
System.err.println(ex.getMessage());
System.err.println("An error has occurred: " + ex.getMessage());

if (Arrays.asList(args).contains("debug")) {
System.err.println();
System.err.println("Exception message: " + ex.getMessage());
System.err.println("Exception class: " + ex.getClass());
}

System.exit(1);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package com.quorum.tessera.data.migration;

import com.quorum.tessera.io.IOCallback;
import com.quorum.tessera.io.UriCallback;
import org.apache.commons.io.IOUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.*;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SqliteDataExporter implements DataExporter {

private static final String INSERT_ROW = "INSERT INTO ENCRYPTED_TRANSACTION (HASH, ENCODED_PAYLOAD) VALUES (?, ?)";

private static final String CREATE_TABLE_RESOURCE = "/ddls/sqlite-ddl.sql";

@Override
public void export(final StoreLoader loader,
final Path output,
Expand All @@ -25,14 +26,16 @@ public void export(final StoreLoader loader,

final String connectionString = "jdbc:sqlite:" + output.toString();

final URI sqlFile = UriCallback.execute(() -> getClass().getResource("/ddls/sqlite-ddl.sql").toURI());

final List<String> createTables = IOCallback.execute(() -> Files.readAllLines(Paths.get(sqlFile)));
final List<String> createTableStatements = Stream.of(getClass().getResourceAsStream(CREATE_TABLE_RESOURCE))
.map(InputStreamReader::new)
.map(BufferedReader::new)
.flatMap(BufferedReader::lines)
.collect(Collectors.toList());

try (Connection conn = DriverManager.getConnection(connectionString, username, password)) {

try (Statement stmt = conn.createStatement()) {
for (final String createTable : createTables) {
for (final String createTable : createTableStatements) {
stmt.executeUpdate(createTable);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ public void doStuffAndBreak() {
Main.main();
}

@Test
public void outputDebugInformationWithNullCause() {
expectedSystemExit.expectSystemExitWithStatus(1);
Main.main("debug");
}

}
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,6 @@
<scope>runtime</scope>
</dependency>


<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down

0 comments on commit 7e39a03

Please sign in to comment.