From 991fea25033b7f665631e67a84f257f99cce18c5 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 12 Aug 2022 15:08:33 +0200 Subject: [PATCH] Add SubkeyLookupFactory class --- .../sqlite/DatabaseSubkeyLookupFactory.java | 33 +++++++++++++++++++ .../InMemorySubkeyLookupFactory.java | 14 ++++++++ .../subkey_lookup/SubkeyLookupFactory.java | 18 ++++++++++ 3 files changed, 65 insertions(+) create mode 100644 pgp-cert-d-java-jdbc-sqlite-lookup/src/main/java/pgp/cert_d/jdbc/sqlite/DatabaseSubkeyLookupFactory.java create mode 100644 pgp-cert-d-java/src/main/java/pgp/cert_d/subkey_lookup/InMemorySubkeyLookupFactory.java create mode 100644 pgp-cert-d-java/src/main/java/pgp/cert_d/subkey_lookup/SubkeyLookupFactory.java diff --git a/pgp-cert-d-java-jdbc-sqlite-lookup/src/main/java/pgp/cert_d/jdbc/sqlite/DatabaseSubkeyLookupFactory.java b/pgp-cert-d-java-jdbc-sqlite-lookup/src/main/java/pgp/cert_d/jdbc/sqlite/DatabaseSubkeyLookupFactory.java new file mode 100644 index 0000000..d2cecd3 --- /dev/null +++ b/pgp-cert-d-java-jdbc-sqlite-lookup/src/main/java/pgp/cert_d/jdbc/sqlite/DatabaseSubkeyLookupFactory.java @@ -0,0 +1,33 @@ +// SPDX-FileCopyrightText: 2022 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package pgp.cert_d.jdbc.sqlite; + +import pgp.cert_d.subkey_lookup.SubkeyLookup; +import pgp.cert_d.subkey_lookup.SubkeyLookupFactory; + +import java.io.File; +import java.io.IOException; +import java.sql.SQLException; + +/** + * Implementation of {@link SubkeyLookupFactory} which creates a SQLite-based {@link DatabaseSubkeyLookup}. + */ +public class DatabaseSubkeyLookupFactory implements SubkeyLookupFactory { + + @Override + public SubkeyLookup createFileBasedInstance(File baseDirectory) { + File databaseFile = new File(baseDirectory, "_pgpainless_subkey_map.db"); + SubkeyLookupDao dao; + try { + if (!databaseFile.exists()) { + databaseFile.createNewFile(); + } + dao = SqliteSubkeyLookupDaoImpl.forDatabaseFile(databaseFile); + } catch (SQLException | IOException e) { + throw new RuntimeException(e); + } + return new DatabaseSubkeyLookup(dao); + } +} diff --git a/pgp-cert-d-java/src/main/java/pgp/cert_d/subkey_lookup/InMemorySubkeyLookupFactory.java b/pgp-cert-d-java/src/main/java/pgp/cert_d/subkey_lookup/InMemorySubkeyLookupFactory.java new file mode 100644 index 0000000..a224c64 --- /dev/null +++ b/pgp-cert-d-java/src/main/java/pgp/cert_d/subkey_lookup/InMemorySubkeyLookupFactory.java @@ -0,0 +1,14 @@ +// SPDX-FileCopyrightText: 2022 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package pgp.cert_d.subkey_lookup; + +import java.io.File; + +public class InMemorySubkeyLookupFactory implements SubkeyLookupFactory { + @Override + public SubkeyLookup createFileBasedInstance(File baseDirectory) { + return new InMemorySubkeyLookup(); + } +} diff --git a/pgp-cert-d-java/src/main/java/pgp/cert_d/subkey_lookup/SubkeyLookupFactory.java b/pgp-cert-d-java/src/main/java/pgp/cert_d/subkey_lookup/SubkeyLookupFactory.java new file mode 100644 index 0000000..442e9ef --- /dev/null +++ b/pgp-cert-d-java/src/main/java/pgp/cert_d/subkey_lookup/SubkeyLookupFactory.java @@ -0,0 +1,18 @@ +// SPDX-FileCopyrightText: 2022 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package pgp.cert_d.subkey_lookup; + +import java.io.File; + +public interface SubkeyLookupFactory { + + /** + * Create a new {@link SubkeyLookup} instance that lives in the given baseDirectory. + * + * @param baseDirectory base directory + * @return subkey lookup + */ + SubkeyLookup createFileBasedInstance(File baseDirectory); +}