Create the EncyptedContactStore during servlet startup

Rather than lazily creating it, we always initialize it when the
service needs one.

Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2009-07-27 21:14:00 -07:00
parent 24cca7cc00
commit dc5f2fcbc8
5 changed files with 51 additions and 34 deletions

View File

@@ -60,9 +60,11 @@ import javax.servlet.http.HttpServletRequest;
class AccountSecurityImpl extends BaseServiceImplementation implements class AccountSecurityImpl extends BaseServiceImplementation implements
AccountSecurity { AccountSecurity {
private final Logger log = LoggerFactory.getLogger(getClass()); private final Logger log = LoggerFactory.getLogger(getClass());
private final ContactStore contactStore;
AccountSecurityImpl(final GerritServer gs) { AccountSecurityImpl(final GerritServer gs, final ContactStore cs) {
super(gs); super(gs);
contactStore = cs;
} }
public void mySshKeys(final AsyncCallback<List<AccountSshKey>> callback) { public void mySshKeys(final AsyncCallback<List<AccountSshKey>> callback) {
@@ -226,7 +228,7 @@ class AccountSecurityImpl extends BaseServiceImplementation implements
} }
if (ContactInformation.hasData(info)) { if (ContactInformation.hasData(info)) {
try { try {
EncryptedContactStore.store(me, info); contactStore.store(me, info);
} catch (ContactInformationStoreException e) { } catch (ContactInformationStoreException e) {
throw new Failure(e); throw new Failure(e);
} }

View File

@@ -22,13 +22,16 @@ import com.google.inject.Singleton;
@SuppressWarnings("serial") @SuppressWarnings("serial")
@Singleton @Singleton
class AccountSecuritySrv extends GerritJsonServlet { class AccountSecuritySrv extends GerritJsonServlet {
private final ContactStore contactStore;
@Inject @Inject
AccountSecuritySrv(final GerritServer gs) { AccountSecuritySrv(final GerritServer gs, final ContactStore cs) {
super(gs); super(gs);
contactStore = cs;
} }
@Override @Override
protected Object createServiceHandle() throws Exception { protected Object createServiceHandle() throws Exception {
return new AccountSecurityImpl(server); return new AccountSecurityImpl(server, contactStore);
} }
} }

View File

@@ -0,0 +1,24 @@
// 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;
public interface ContactStore {
void store(Account account, ContactInformation info)
throws ContactInformationStoreException;
}

View File

@@ -20,7 +20,6 @@ import com.google.gerrit.client.reviewdb.ContactInformation;
import com.google.gerrit.client.reviewdb.ReviewDb; import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.client.rpc.Common; import com.google.gerrit.client.rpc.Common;
import com.google.gerrit.client.rpc.ContactInformationStoreException; import com.google.gerrit.client.rpc.ContactInformationStoreException;
import com.google.gwtjsonrpc.server.XsrfException;
import com.google.gwtorm.client.OrmException; import com.google.gwtorm.client.OrmException;
import org.apache.sshd.common.util.SecurityUtils; import org.apache.sshd.common.util.SecurityUtils;
@@ -57,26 +56,21 @@ import java.util.Iterator;
import java.util.TimeZone; import java.util.TimeZone;
/** Encrypts {@link ContactInformation} instances and saves them. */ /** Encrypts {@link ContactInformation} instances and saves them. */
public class EncryptedContactStore { public class EncryptedContactStore implements ContactStore {
private static final TimeZone UTC = TimeZone.getTimeZone("UTC"); private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
private static boolean inited;
private static EncryptedContactStore self;
private static synchronized EncryptedContactStore getInstance() 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 { throws ContactInformationStoreException {
if (!inited) { throw initError;
inited = true;
self = new EncryptedContactStore();
} }
if (self == null) { };
throw new ContactInformationStoreException();
} }
return self;
}
public static void store(final Account account, final ContactInformation info)
throws ContactInformationStoreException {
getInstance().storeImpl(account, info);
} }
private PGPPublicKey dest; private PGPPublicKey dest;
@@ -84,16 +78,8 @@ public class EncryptedContactStore {
private URL storeUrl; private URL storeUrl;
private String storeAPPSEC; private String storeAPPSEC;
private EncryptedContactStore() throws ContactInformationStoreException { private EncryptedContactStore(final GerritServer gs)
final GerritServer gs; throws ContactInformationStoreException {
try {
gs = GerritServer.getInstance();
} catch (OrmException e) {
throw new ContactInformationStoreException(e);
} catch (XsrfException e) {
throw new ContactInformationStoreException(e);
}
if (gs.getContactStoreURL() == null) { if (gs.getContactStoreURL() == null) {
throw new ContactInformationStoreException(new IllegalStateException( throw new ContactInformationStoreException(new IllegalStateException(
"No contactStoreURL configured")); "No contactStoreURL configured"));
@@ -150,7 +136,7 @@ public class EncryptedContactStore {
return null; return null;
} }
private void storeImpl(final Account account, final ContactInformation info) public void store(final Account account, final ContactInformation info)
throws ContactInformationStoreException { throws ContactInformationStoreException {
try { try {
final byte[] plainText = format(account, info).getBytes("UTF-8"); final byte[] plainText = format(account, info).getBytes("UTF-8");

View File

@@ -84,7 +84,9 @@ public class GerritServletConfig extends GuiceServletContextListener {
@Override @Override
protected void configure() { protected void configure() {
try { try {
bind(GerritServer.class).toInstance(GerritServer.getInstance(true)); final GerritServer gs = GerritServer.getInstance(true);
bind(GerritServer.class).toInstance(gs);
bind(ContactStore.class).toInstance(EncryptedContactStore.create(gs));
} catch (OrmException e) { } catch (OrmException e) {
addError(e); addError(e);
} catch (XsrfException e) { } catch (XsrfException e) {