diff --git a/graphdb/graphcli/pom.xml b/graphdb/graphcli/pom.xml index 2705ebf766ae27df192114cca1a01944e90dc7d9..bf388d2a7048b71397512e6af0ac1c9537a48450 100644 --- a/graphdb/graphcli/pom.xml +++ b/graphdb/graphcli/pom.xml @@ -11,11 +11,20 @@ <maven.compiler.target>1.11</maven.compiler.target> </properties> <dependencies> - <!-- https://mvnrepository.com/artifact/com.ontotext.graphdb/graphdb-free-runtime --> <dependency> <groupId>com.ontotext.graphdb</groupId> <artifactId>graphdb-free-runtime</artifactId> <version>9.2.0</version> </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <version>1.7.30</version> + </dependency> + <dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-core</artifactId> + <version>1.2.3</version> + </dependency> </dependencies> </project> \ No newline at end of file diff --git a/graphdb/graphcli/src/main/java/me/schaertl/graphcli/GraphDB.java b/graphdb/graphcli/src/main/java/me/schaertl/graphcli/GraphDB.java index 2b3cc447e76cca6e4322934effe4dde7dd667806..fbbca2a46aef3e0606abd03640fb0933652c28e3 100644 --- a/graphdb/graphcli/src/main/java/me/schaertl/graphcli/GraphDB.java +++ b/graphdb/graphcli/src/main/java/me/schaertl/graphcli/GraphDB.java @@ -7,6 +7,9 @@ import org.eclipse.rdf4j.repository.RepositoryException; import org.eclipse.rdf4j.repository.manager.RemoteRepositoryManager; import org.eclipse.rdf4j.repository.manager.RepositoryManager; +import java.util.Collection; +import java.util.logging.LogManager; + /** * Wrapper around various RDF4J methods. Represents a connection to a GraphDB * instance on the network. @@ -30,20 +33,26 @@ public class GraphDB implements AutoCloseable { * @throws RepositoryException If connecting to the server or repository failed. */ public GraphDB(String serverURL, String repository) throws RepositoryException { - this.manager = new RemoteRepositoryManager(serverURL); - this.manager.init(); + try { + this.manager = new RemoteRepositoryManager(serverURL); + this.manager.init(); + final Collection<Repository> rs = this.manager.getAllRepositories(); - this.repository = this.manager.getRepository(repository); - if (this.repository == null) { - this.manager.shutDown(); - throw new RepositoryException(String.format("no repository with name %s", repository)); - } + this.repository = this.manager.getRepository(repository); + if (this.repository == null) { + this.manager.shutDown(); + throw new RepositoryException(String.format("no repository with name %s", repository)); + } - try { this.connection = this.repository.getConnection(); - } finally { - this.manager.shutDown(); - this.repository.shutDown(); + } catch (Exception e) { + try { + this.close(); + } catch (Exception ce) { + // ignore the close exception; we have enough already + } + + throw e; } } diff --git a/graphdb/graphcli/src/main/java/me/schaertl/graphcli/Main.java b/graphdb/graphcli/src/main/java/me/schaertl/graphcli/Main.java index 8143cb9bdd1b54c48fa6a019b4b10fd2ab157402..bfa531865bdd455ad6a4ce95aa906eef9e733dcb 100644 --- a/graphdb/graphcli/src/main/java/me/schaertl/graphcli/Main.java +++ b/graphdb/graphcli/src/main/java/me/schaertl/graphcli/Main.java @@ -3,6 +3,8 @@ package me.schaertl.graphcli; import org.eclipse.rdf4j.model.Statement; import org.eclipse.rdf4j.repository.RepositoryConnection; import org.eclipse.rdf4j.repository.RepositoryResult; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; @@ -12,6 +14,8 @@ public class Main { private static final String REPOSITORY = "dump"; private static final int LIMIT = 100; + private static final Logger log = LoggerFactory.getLogger(Main.class); + private static void hello() throws Exception { try (final GraphDB db = new GraphDB(SERVER_URL, REPOSITORY)) { final RepositoryConnection connection = db.connection(); @@ -19,7 +23,7 @@ public class Main { final RepositoryResult<Statement> result = connection.getStatements(null, ULO.INDUCTIVE_ON, null, true); for (Statement s : take(result, LIMIT)) { - System.out.printf("%s INDUCTIVE ON %s", s.getSubject(), s.getObject()); + log.info("{} INDUCTIVE ON {}", s.getSubject(), s.getObject()); } } } @@ -33,7 +37,7 @@ public class Main { private static List<Statement> take(RepositoryResult<Statement> result, int n) { final List<Statement> l = new ArrayList<>(n); - for (int i = 0; i < LIMIT && result.hasNext(); i++) { + for (int i = 0; i < n && result.hasNext(); i++) { final Statement s = result.next(); l.add(s); } diff --git a/graphdb/graphcli/src/main/resources/logback.xml b/graphdb/graphcli/src/main/resources/logback.xml new file mode 100644 index 0000000000000000000000000000000000000000..6156c2188e68d05e5f4ba6ea583ad2b0e38d2663 --- /dev/null +++ b/graphdb/graphcli/src/main/resources/logback.xml @@ -0,0 +1,11 @@ +<configuration> + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> + </encoder> + </appender> + + <root level="INFO"> + <appender-ref ref="STDOUT" /> + </root> +</configuration> \ No newline at end of file