Let Guice inject the ContactStore implementation
Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
		@@ -55,30 +55,16 @@ 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 implements ContactStore {
 | 
					class EncryptedContactStore implements ContactStore {
 | 
				
			||||||
  private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
 | 
					  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 final GerritServer server;
 | 
				
			||||||
  private PGPPublicKey dest;
 | 
					  private PGPPublicKey dest;
 | 
				
			||||||
  private SecureRandom prng;
 | 
					  private SecureRandom prng;
 | 
				
			||||||
  private URL storeUrl;
 | 
					  private URL storeUrl;
 | 
				
			||||||
  private String storeAPPSEC;
 | 
					  private String storeAPPSEC;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private EncryptedContactStore(final GerritServer gs)
 | 
					  EncryptedContactStore(final GerritServer gs)
 | 
				
			||||||
      throws ContactInformationStoreException {
 | 
					      throws ContactInformationStoreException {
 | 
				
			||||||
    server = gs;
 | 
					    server = gs;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -231,8 +217,8 @@ public class EncryptedContactStore implements ContactStore {
 | 
				
			|||||||
    return buf.toByteArray();
 | 
					    return buf.toByteArray();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private String format(final Account account,
 | 
					  private String format(final Account account, final ContactInformation info)
 | 
				
			||||||
      final ContactInformation info) throws ContactInformationStoreException {
 | 
					      throws ContactInformationStoreException {
 | 
				
			||||||
    Timestamp on = account.getContactFiledOn();
 | 
					    Timestamp on = account.getContactFiledOn();
 | 
				
			||||||
    if (on == null) {
 | 
					    if (on == null) {
 | 
				
			||||||
      on = new Timestamp(System.currentTimeMillis());
 | 
					      on = new Timestamp(System.currentTimeMillis());
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      };
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -128,15 +128,16 @@ public class GerritServletConfig extends GuiceServletContextListener {
 | 
				
			|||||||
      @Override
 | 
					      @Override
 | 
				
			||||||
      protected void configure() {
 | 
					      protected void configure() {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
          final GerritServer gs = GerritServer.getInstance(true);
 | 
					          bind(GerritServer.class).toInstance(GerritServer.getInstance(true));
 | 
				
			||||||
          bind(GerritServer.class).toInstance(gs);
 | 
					 | 
				
			||||||
          bind(ContactStore.class).toInstance(EncryptedContactStore.create(gs));
 | 
					 | 
				
			||||||
          bind(FileTypeRegistry.class).to(MimeUtilFileTypeRegistry.class);
 | 
					 | 
				
			||||||
        } catch (OrmException e) {
 | 
					        } catch (OrmException e) {
 | 
				
			||||||
          addError(e);
 | 
					          addError(e);
 | 
				
			||||||
        } catch (XsrfException e) {
 | 
					        } catch (XsrfException e) {
 | 
				
			||||||
          addError(e);
 | 
					          addError(e);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        bind(ContactStore.class)
 | 
				
			||||||
 | 
					            .toProvider(EncryptedContactStoreProvider.class);
 | 
				
			||||||
 | 
					        bind(FileTypeRegistry.class).to(MimeUtilFileTypeRegistry.class);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user