From 219517ae0068954f522a5023f11afc550c3c5181 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Wed, 29 Jul 2009 11:04:27 -0700 Subject: [PATCH] Move contact store configuration off GerritServer Instead we get the configuration directly from the GerritServerConfig annotated Config object, which is available through injection. Signed-off-by: Shawn O. Pearce --- .../google/gerrit/server/ContactStore.java | 2 ++ .../gerrit/server/EncryptedContactStore.java | 28 +++++++++++++------ .../server/EncryptedContactStoreProvider.java | 13 +++++++-- .../gerrit/server/GerritConfigProvider.java | 9 +++++- .../google/gerrit/server/GerritServer.java | 8 ------ 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/google/gerrit/server/ContactStore.java b/src/main/java/com/google/gerrit/server/ContactStore.java index 30702ffba6..ee2bb7b08e 100644 --- a/src/main/java/com/google/gerrit/server/ContactStore.java +++ b/src/main/java/com/google/gerrit/server/ContactStore.java @@ -19,6 +19,8 @@ import com.google.gerrit.client.reviewdb.ContactInformation; import com.google.gerrit.client.rpc.ContactInformationStoreException; public interface ContactStore { + boolean isEnabled(); + void store(Account account, ContactInformation info) throws ContactInformationStoreException; } diff --git a/src/main/java/com/google/gerrit/server/EncryptedContactStore.java b/src/main/java/com/google/gerrit/server/EncryptedContactStore.java index 6dd7d47d48..df1df42435 100644 --- a/src/main/java/com/google/gerrit/server/EncryptedContactStore.java +++ b/src/main/java/com/google/gerrit/server/EncryptedContactStore.java @@ -19,9 +19,11 @@ import com.google.gerrit.client.reviewdb.AccountExternalId; import com.google.gerrit.client.reviewdb.ContactInformation; import com.google.gerrit.client.reviewdb.ReviewDb; import com.google.gerrit.client.rpc.ContactInformationStoreException; +import com.google.gerrit.server.config.GerritServerConfig; import com.google.gerrit.server.config.SitePath; import com.google.gwtorm.client.OrmException; import com.google.gwtorm.client.SchemaFactory; +import com.google.inject.Inject; import org.apache.sshd.common.util.SecurityUtils; import org.bouncycastle.bcpg.ArmoredOutputStream; @@ -36,6 +38,7 @@ import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; import org.bouncycastle.openpgp.PGPUtil; +import org.spearce.jgit.lib.Config; import org.spearce.jgit.util.NB; import java.io.ByteArrayOutputStream; @@ -61,26 +64,28 @@ class EncryptedContactStore implements ContactStore { private static final TimeZone UTC = TimeZone.getTimeZone("UTC"); private final SchemaFactory schema; - private PGPPublicKey dest; - private SecureRandom prng; - private URL storeUrl; - private String storeAPPSEC; + private final PGPPublicKey dest; + private final SecureRandom prng; + private final URL storeUrl; + private final String storeAPPSEC; - EncryptedContactStore(final GerritServer server, + @Inject + EncryptedContactStore(@GerritServerConfig final Config cfg, @SitePath final File sitePath, final SchemaFactory sf) throws ContactInformationStoreException { schema = sf; - if (server.getContactStoreURL() == null) { + final String url = cfg.getString("contactstore", null, "url"); + if (url == null) { throw new ContactInformationStoreException(new IllegalStateException( - "No contactStoreURL configured")); + "contactstore.url not set")); } try { - storeUrl = new URL(server.getContactStoreURL()); + storeUrl = new URL(url); } catch (MalformedURLException e) { throw new ContactInformationStoreException(e); } - storeAPPSEC = server.getContactStoreAPPSEC(); + storeAPPSEC = cfg.getString("contactstore", null, "appsec"); if (!SecurityUtils.isBouncyCastleRegistered()) { throw new ContactInformationStoreException(new NoSuchProviderException( @@ -96,6 +101,11 @@ class EncryptedContactStore implements ContactStore { dest = selectKey(readPubRing(sitePath)); } + @Override + public boolean isEnabled() { + return true; + } + private PGPPublicKeyRingCollection readPubRing(final File sitePath) throws ContactInformationStoreException { final File pub = new File(sitePath, "contact_information.pub"); diff --git a/src/main/java/com/google/gerrit/server/EncryptedContactStoreProvider.java b/src/main/java/com/google/gerrit/server/EncryptedContactStoreProvider.java index 2f091f6864..13ef96d0e9 100644 --- a/src/main/java/com/google/gerrit/server/EncryptedContactStoreProvider.java +++ b/src/main/java/com/google/gerrit/server/EncryptedContactStoreProvider.java @@ -18,16 +18,20 @@ import com.google.gerrit.client.reviewdb.Account; import com.google.gerrit.client.reviewdb.ContactInformation; import com.google.gerrit.client.reviewdb.ReviewDb; import com.google.gerrit.client.rpc.ContactInformationStoreException; +import com.google.gerrit.server.config.GerritServerConfig; import com.google.gerrit.server.config.SitePath; import com.google.gwtorm.client.SchemaFactory; import com.google.inject.Inject; import com.google.inject.Provider; +import org.spearce.jgit.lib.Config; + import java.io.File; class EncryptedContactStoreProvider implements Provider { @Inject - private GerritServer server; + @GerritServerConfig + private Config config; @Inject private SchemaFactory schema; @@ -39,9 +43,14 @@ class EncryptedContactStoreProvider implements Provider { @Override public ContactStore get() { try { - return new EncryptedContactStore(server, sitePath, schema); + return new EncryptedContactStore(config, sitePath, schema); } catch (final ContactInformationStoreException initError) { return new ContactStore() { + @Override + public boolean isEnabled() { + return false; + } + @Override public void store(Account account, ContactInformation info) throws ContactInformationStoreException { diff --git a/src/main/java/com/google/gerrit/server/GerritConfigProvider.java b/src/main/java/com/google/gerrit/server/GerritConfigProvider.java index 6531372b4c..7f23b4bd01 100644 --- a/src/main/java/com/google/gerrit/server/GerritConfigProvider.java +++ b/src/main/java/com/google/gerrit/server/GerritConfigProvider.java @@ -42,8 +42,10 @@ class GerritConfigProvider implements Provider { private final GerritServer server; private final SchemaFactory schema; + private GerritSshDaemon sshd; private EmailSender emailSender; + private ContactStore contactStore; @Inject GerritConfigProvider(final GerritServer gs, final SchemaFactory sf) { @@ -61,6 +63,11 @@ class GerritConfigProvider implements Provider { emailSender = d; } + @Inject(optional = true) + void setContactStore(final ContactStore d) { + contactStore = d; + } + private GerritConfig create() throws OrmException { final Config cfg = server.getGerritConfig(); final GerritConfig config = new GerritConfig(); @@ -70,7 +77,7 @@ class GerritConfigProvider implements Provider { config.setGitDaemonUrl(cfg.getString("gerrit", null, "canonicalgiturl")); config.setUseRepoDownload(cfg.getBoolean("repo", null, "showdownloadcommand", false)); - config.setUseContactInfo(server.getContactStoreURL() != null); + config.setUseContactInfo(contactStore != null && contactStore.isEnabled()); config.setAllowRegisterNewEmail(emailSender != null && emailSender.isEnabled()); config.setLoginType(server.getLoginType()); diff --git a/src/main/java/com/google/gerrit/server/GerritServer.java b/src/main/java/com/google/gerrit/server/GerritServer.java index 65eb904fd4..dcdefe1dc0 100644 --- a/src/main/java/com/google/gerrit/server/GerritServer.java +++ b/src/main/java/com/google/gerrit/server/GerritServer.java @@ -307,14 +307,6 @@ public class GerritServer { return getGerritConfig().getString("auth", null, "emailformat"); } - public String getContactStoreURL() { - return getGerritConfig().getString("contactstore", null, "url"); - } - - public String getContactStoreAPPSEC() { - return getGerritConfig().getString("contactstore", null, "appsec"); - } - /** Optional canonical URL for this application. */ public String getCanonicalURL() { String u = getGerritConfig().getString("gerrit", null, "canonicalweburl");