Let Guice inject the ContactStore implementation

Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2009-07-28 08:58:55 -07:00
parent 6791b2859f
commit 08934aee9b
3 changed files with 50 additions and 22 deletions

View File

@@ -55,30 +55,16 @@ import java.util.Iterator;
import java.util.TimeZone;
/** Encrypts {@link ContactInformation} instances and saves them. */
public class EncryptedContactStore implements ContactStore {
class EncryptedContactStore implements ContactStore {
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
public static ContactStore create(final GerritServer gs) {
try {
return new EncryptedContactStore(gs);
} catch (final ContactInformationStoreException initError) {
return new ContactStore() {
@Override
public void store(Account account, ContactInformation info)
throws ContactInformationStoreException {
throw initError;
}
};
}
}
private final GerritServer server;
private PGPPublicKey dest;
private SecureRandom prng;
private URL storeUrl;
private String storeAPPSEC;
private EncryptedContactStore(final GerritServer gs)
EncryptedContactStore(final GerritServer gs)
throws ContactInformationStoreException {
server = gs;
@@ -231,8 +217,8 @@ public class EncryptedContactStore implements ContactStore {
return buf.toByteArray();
}
private String format(final Account account,
final ContactInformation info) throws ContactInformationStoreException {
private String format(final Account account, final ContactInformation info)
throws ContactInformationStoreException {
Timestamp on = account.getContactFiledOn();
if (on == null) {
on = new Timestamp(System.currentTimeMillis());

View File

@@ -0,0 +1,41 @@
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server;
import com.google.gerrit.client.reviewdb.Account;
import com.google.gerrit.client.reviewdb.ContactInformation;
import com.google.gerrit.client.rpc.ContactInformationStoreException;
import com.google.inject.Inject;
import com.google.inject.Provider;
class EncryptedContactStoreProvider implements Provider<ContactStore> {
@Inject
private GerritServer server;
@Override
public ContactStore get() {
try {
return new EncryptedContactStore(server);
} catch (final ContactInformationStoreException initError) {
return new ContactStore() {
@Override
public void store(Account account, ContactInformation info)
throws ContactInformationStoreException {
throw initError;
}
};
}
}
}

View File

@@ -128,15 +128,16 @@ public class GerritServletConfig extends GuiceServletContextListener {
@Override
protected void configure() {
try {
final GerritServer gs = GerritServer.getInstance(true);
bind(GerritServer.class).toInstance(gs);
bind(ContactStore.class).toInstance(EncryptedContactStore.create(gs));
bind(FileTypeRegistry.class).to(MimeUtilFileTypeRegistry.class);
bind(GerritServer.class).toInstance(GerritServer.getInstance(true));
} catch (OrmException e) {
addError(e);
} catch (XsrfException e) {
addError(e);
}
bind(ContactStore.class)
.toProvider(EncryptedContactStoreProvider.class);
bind(FileTypeRegistry.class).to(MimeUtilFileTypeRegistry.class);
}
};
}