Deprecate more fields in RegistryConfig

This primarily addresses issues with TMCH testing mode and email sending utils.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=143710550
This commit is contained in:
mcilwain 2017-01-05 14:38:38 -08:00 committed by Ben McIlwain
parent c05424b947
commit 25a8bbe890
23 changed files with 203 additions and 265 deletions

View file

@ -16,6 +16,7 @@ package google.registry.config;
import static google.registry.config.ConfigUtils.makeUrl;
import com.google.appengine.api.utils.SystemProperty;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@ -214,8 +215,8 @@ public final class ConfigModule {
/** @see RegistryConfig#getCommitLogDatastoreRetention() */
@Provides
@Config("commitLogDatastoreRetention")
public static Duration provideCommitLogDatastoreRetention(RegistryConfig config) {
return config.getCommitLogDatastoreRetention();
public static Duration provideCommitLogDatastoreRetention() {
return RegistryConfig.getCommitLogDatastoreRetention();
}
/**
@ -423,8 +424,8 @@ public final class ConfigModule {
*/
@Provides
@Config("tmchCaTestingMode")
public static boolean provideTmchCaTestingMode(RegistryConfig config) {
return config.getTmchCaTestingMode();
public static boolean provideTmchCaTestingMode() {
return RegistryConfig.getTmchCaTestingMode();
}
/**
@ -467,6 +468,29 @@ public final class ConfigModule {
}
}
/**
* The email address that outgoing emails from the app are sent from.
*
* @see google.registry.util.SendEmailUtils
*/
@Provides
@Config("googleAppsSendFromEmailAddress")
public static String provideGoogleAppsSendFromEmailAddress() {
return String.format("noreply@%s.appspotmail.com", SystemProperty.applicationId.get());
}
/**
* The display name that is used on outgoing emails sent by Nomulus.
*
* @see google.registry.util.SendEmailUtils
*/
@Provides
@Config("googleAppsAdminEmailDisplayName")
public static String provideGoogleAppsAdminEmailDisplayName() {
// Production example: "Example Registry"
return "Google Domain Registry";
}
/**
* Returns the Google Cloud Storage bucket for staging escrow deposits pending upload.
*
@ -1025,5 +1049,9 @@ public final class ConfigModule {
public static class LocalTestConfig {
public static final String CONTACT_AND_HOST_ROID_SUFFIX = "ROID";
public static final String GOOGLE_APPS_SEND_FROM_EMAIL_ADDRESS = "noreply@testing.example";
public static final String GOOGLE_APPS_ADMIN_EMAIL_DISPLAY_NAME = "Testing Nomulus";
}
}

View file

@ -18,7 +18,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static google.registry.config.ConfigUtils.makeUrl;
import static org.joda.time.Duration.standardDays;
import com.google.appengine.api.utils.SystemProperty;
import com.google.common.base.Optional;
import com.google.common.net.HostAndPort;
import java.net.URL;
@ -48,33 +47,6 @@ public final class ProductionRegistryConfigExample extends RegistryConfig {
this.environment = checkNotNull(environment);
}
/**
* {@inheritDoc}
*
* <p>Thirty days makes a sane default, because it's highly unlikely we'll ever need to generate a
* deposit older than that. And if we do, we could always bring up a separate App Engine instance
* and replay the commit logs off GCS.
*/
@Override
public Duration getCommitLogDatastoreRetention() {
return Duration.standardDays(30);
}
@Override
public String getSnapshotsBucket() {
return RegistryConfig.getProjectId() + "-snapshots";
}
@Override
public boolean getTmchCaTestingMode() {
switch (environment) {
case PRODUCTION:
return false;
default:
return true;
}
}
@Override
public Optional<String> getECatcherAddress() {
throw new UnsupportedOperationException(); // n/a
@ -91,36 +63,11 @@ public final class ProductionRegistryConfigExample extends RegistryConfig {
}
}
@Override
public Duration getSingletonCacheRefreshDuration() {
return Duration.standardMinutes(10);
}
@Override
public Duration getDomainLabelListCacheDuration() {
return Duration.standardHours(1);
}
@Override
public Duration getSingletonCachePersistDuration() {
return Duration.standardDays(365);
}
@Override
public String getReservedTermsExportDisclaimer() {
return RESERVED_TERMS_EXPORT_DISCLAIMER;
}
@Override
public String getGoogleAppsAdminEmailDisplayName() {
return "Example Registry";
}
@Override
public String getGoogleAppsSendFromEmailAddress() {
return String.format("noreply@%s.appspotmail.com", SystemProperty.applicationId.get());
}
@Override
public String getRegistrarDefaultWhoisServer() {
return "whois.nic.registry.example";

View file

@ -49,7 +49,9 @@ public abstract class RegistryConfig {
*
* @see google.registry.export.ExportSnapshotServlet
*/
public abstract String getSnapshotsBucket();
public static String getSnapshotsBucket() {
return getProjectId() + "-snapshots";
}
/**
* Number of sharded commit log buckets.
@ -82,14 +84,23 @@ public abstract class RegistryConfig {
* @see google.registry.backup.DeleteOldCommitLogsAction
* @see google.registry.model.translators.CommitLogRevisionsTranslatorFactory
*/
public abstract Duration getCommitLogDatastoreRetention();
public static Duration getCommitLogDatastoreRetention() {
return Duration.standardDays(30);
}
/**
* Returns {@code true} if TMCH certificate authority should be in testing mode.
*
* @see google.registry.tmch.TmchCertificateAuthority
*/
public abstract boolean getTmchCaTestingMode();
public static boolean getTmchCaTestingMode() {
switch (RegistryEnvironment.get()) {
case PRODUCTION:
return false;
default:
return true;
}
}
public abstract Optional<String> getECatcherAddress();
@ -101,7 +112,16 @@ public abstract class RegistryConfig {
public abstract HostAndPort getServer();
/** Returns the amount of time a singleton should be cached, before expiring. */
public abstract Duration getSingletonCacheRefreshDuration();
public static Duration getSingletonCacheRefreshDuration() {
switch (RegistryEnvironment.get()) {
case UNITTEST:
// All cache durations are set to zero so that unit tests can update and then retrieve data
// immediately without failure.
return Duration.ZERO;
default:
return Duration.standardMinutes(10);
}
}
/**
* Returns the amount of time a domain label list should be cached in memory before expiring.
@ -109,10 +129,24 @@ public abstract class RegistryConfig {
* @see google.registry.model.registry.label.ReservedList
* @see google.registry.model.registry.label.PremiumList
*/
public abstract Duration getDomainLabelListCacheDuration();
public static Duration getDomainLabelListCacheDuration() {
switch (RegistryEnvironment.get()) {
case UNITTEST:
return Duration.ZERO;
default:
return Duration.standardHours(1);
}
}
/** Returns the amount of time a singleton should be cached in persist mode, before expiring. */
public abstract Duration getSingletonCachePersistDuration();
public static Duration getSingletonCachePersistDuration() {
switch (RegistryEnvironment.get()) {
case UNITTEST:
return Duration.ZERO;
default:
return Duration.standardDays(365);
}
}
/**
* Returns the header text at the top of the reserved terms exported list.
@ -121,20 +155,6 @@ public abstract class RegistryConfig {
*/
public abstract String getReservedTermsExportDisclaimer();
/**
* Returns a display name that is used on outgoing emails sent by Nomulus.
*
* @see google.registry.util.SendEmailUtils
*/
public abstract String getGoogleAppsAdminEmailDisplayName();
/**
* Returns the email address that outgoing emails from the app are sent from.
*
* @see google.registry.util.SendEmailUtils
*/
public abstract String getGoogleAppsSendFromEmailAddress();
/**
* Returns default WHOIS server to use when {@code Registrar#getWhoisServer()} is {@code null}.
*

View file

@ -28,6 +28,7 @@ import javax.annotation.concurrent.ThreadSafe;
* passing the {@link RegistryEnvironment} as a single parameter.
*/
@ThreadSafe
@Deprecated // will be replaced by YAML config; see b/33386530 for details
public final class RegistryConfigLoader {
public static final String REGISTRY_CONFIG_PROPERTY = "google.registry.config";

View file

@ -72,14 +72,19 @@ public enum RegistryEnvironment {
/** Globally override registry configuration from within a unit test. */
@VisibleForTesting
@Deprecated
public static void overrideConfigurationForTesting(@Nullable RegistryConfig newConfig) {
configOverride = newConfig;
}
@Nullable
@Deprecated
private static RegistryConfig configOverride;
@Deprecated
private static final RegistryConfig testingConfig = new TestRegistryConfig();
@Deprecated
private final RegistryConfig config = RegistryConfigLoader.load(this);
/** System property for configuring which environment we should use. */

View file

@ -29,21 +29,6 @@ public class TestRegistryConfig extends RegistryConfig {
public TestRegistryConfig() {}
@Override
public Duration getCommitLogDatastoreRetention() {
return Duration.standardDays(30);
}
@Override
public String getSnapshotsBucket() {
return RegistryConfig.getProjectId() + "-snapshots";
}
@Override
public boolean getTmchCaTestingMode() {
return true;
}
@Override
public Optional<String> getECatcherAddress() {
throw new UnsupportedOperationException();
@ -54,38 +39,11 @@ public class TestRegistryConfig extends RegistryConfig {
throw new UnsupportedOperationException();
}
@Override
public Duration getSingletonCacheRefreshDuration() {
// All cache durations are set to zero so that unit tests can update and then retrieve data
// immediately without failure.
return Duration.ZERO;
}
@Override
public Duration getDomainLabelListCacheDuration() {
return Duration.ZERO;
}
@Override
public Duration getSingletonCachePersistDuration() {
return Duration.ZERO;
}
@Override
public String getReservedTermsExportDisclaimer() {
return "This is a disclaimer.\n";
}
@Override
public String getGoogleAppsAdminEmailDisplayName() {
return "Testing Nomulus";
}
@Override
public String getGoogleAppsSendFromEmailAddress() {
return "noreply@testing.example";
}
@Override
public String getRegistrarDefaultWhoisServer() {
return "whois.nic.fakewhois.example";

View file

@ -21,7 +21,7 @@ import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import com.google.common.net.MediaType;
import google.registry.config.RegistryEnvironment;
import google.registry.config.RegistryConfig;
import google.registry.util.Clock;
import google.registry.util.FormattingLogger;
import google.registry.util.NonFinalForTesting;
@ -44,8 +44,6 @@ import javax.servlet.http.HttpServletResponse;
*/
public class ExportSnapshotServlet extends HttpServlet {
private static final RegistryEnvironment ENVIRONMENT = RegistryEnvironment.get();
/** Queue to use for enqueuing the task that will actually launch the backup. */
static final String QUEUE = "export-snapshot"; // See queue.xml.
@ -71,7 +69,7 @@ public class ExportSnapshotServlet extends HttpServlet {
backupService.launchNewBackup(
QUEUE,
snapshotName,
ENVIRONMENT.config().getSnapshotsBucket(),
RegistryConfig.getSnapshotsBucket(),
ExportConstants.getBackupKinds());
// Enqueue a poll task to monitor the backup and load reporting-related kinds into bigquery.
checkSnapshotServlet.enqueuePollTask(snapshotName, ExportConstants.getReportingKinds());

View file

@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.equalTo;
import static com.google.common.base.Predicates.not;
import static google.registry.config.RegistryConfig.getSingletonCacheRefreshDuration;
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.model.ofy.Ofy.RECOMMENDED_MEMCACHE_EXPIRATION;
@ -49,7 +50,6 @@ import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Mapify;
import com.googlecode.objectify.annotation.OnSave;
import com.googlecode.objectify.annotation.Parent;
import google.registry.config.RegistryEnvironment;
import google.registry.model.Buildable;
import google.registry.model.CreateAutoTimestamp;
import google.registry.model.ImmutableObject;
@ -211,24 +211,23 @@ public class Registry extends ImmutableObject implements Buildable {
}
/** A cache that loads the {@link Registry} for a given tld. */
private static final LoadingCache<String, Optional<Registry>> CACHE = CacheBuilder.newBuilder()
.expireAfterWrite(
RegistryEnvironment.get().config().getSingletonCacheRefreshDuration().getMillis(),
MILLISECONDS)
.build(new CacheLoader<String, Optional<Registry>>() {
@Override
public Optional<Registry> load(final String tld) {
// Enter a transactionless context briefly; we don't want to enroll every TLD in a
// transaction that might be wrapping this call, and memcached results are fine here.
return Optional.fromNullable(ofy().doTransactionless(new Work<Registry>() {
@Override
public Registry run() {
return ofy()
.load()
.key(Key.create(getCrossTldKey(), Registry.class, tld))
.now();
}}));
}});
private static final LoadingCache<String, Optional<Registry>> CACHE =
CacheBuilder.newBuilder()
.expireAfterWrite(getSingletonCacheRefreshDuration().getMillis(), MILLISECONDS)
.build(new CacheLoader<String, Optional<Registry>>() {
@Override
public Optional<Registry> load(final String tld) {
// Enter a transactionless context briefly; we don't want to enroll every TLD in a
// transaction that might be wrapping this call, and memcached results are fine here
return Optional.fromNullable(ofy().doTransactionless(new Work<Registry>() {
@Override
public Registry run() {
return ofy()
.load()
.key(Key.create(getCrossTldKey(), Registry.class, tld))
.now();
}}));
}});
/** Returns the registry for a given TLD, throwing if none exists. */
public static Registry get(String tld) {

View file

@ -19,6 +19,7 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Iterables.partition;
import static google.registry.config.RegistryConfig.getDomainLabelListCacheDuration;
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
import static google.registry.model.ofy.ObjectifyService.allocateId;
import static google.registry.model.ofy.ObjectifyService.ofy;
@ -48,7 +49,6 @@ import com.googlecode.objectify.annotation.Ignore;
import com.googlecode.objectify.annotation.OnLoad;
import com.googlecode.objectify.annotation.Parent;
import com.googlecode.objectify.cmd.Query;
import google.registry.config.RegistryEnvironment;
import google.registry.model.Buildable;
import google.registry.model.ImmutableObject;
import google.registry.model.annotations.ReportedOn;
@ -97,24 +97,22 @@ public final class PremiumList extends BaseDomainLabelList<Money, PremiumList.Pr
}
}
private static LoadingCache<String, PremiumList> cache = CacheBuilder
.newBuilder()
.expireAfterWrite(
RegistryEnvironment.get().config().getDomainLabelListCacheDuration().getMillis(),
MILLISECONDS)
.build(new CacheLoader<String, PremiumList>() {
@Override
public PremiumList load(final String listName) {
return ofy().doTransactionless(new Work<PremiumList>() {
private static LoadingCache<String, PremiumList> cache =
CacheBuilder.newBuilder()
.expireAfterWrite(getDomainLabelListCacheDuration().getMillis(), MILLISECONDS)
.build(new CacheLoader<String, PremiumList>() {
@Override
public PremiumList run() {
return ofy().load()
.type(PremiumList.class)
.parent(getCrossTldKey())
.id(listName)
.now();
public PremiumList load(final String listName) {
return ofy().doTransactionless(new Work<PremiumList>() {
@Override
public PremiumList run() {
return ofy().load()
.type(PremiumList.class)
.parent(getCrossTldKey())
.id(listName)
.now();
}});
}});
}});
/**
* Gets the premium price for the specified label on the specified tld, or returns Optional.absent

View file

@ -16,6 +16,7 @@ package google.registry.model.registry.label;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static google.registry.config.RegistryConfig.getDomainLabelListCacheDuration;
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.model.ofy.Ofy.RECOMMENDED_MEMCACHE_EXPIRATION;
@ -40,7 +41,6 @@ import com.googlecode.objectify.annotation.Embed;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Mapify;
import com.googlecode.objectify.mapper.Mapper;
import google.registry.config.RegistryEnvironment;
import google.registry.model.registry.Registry;
import java.util.List;
import java.util.Map;
@ -221,16 +221,20 @@ public final class ReservedList
return builder.build();
}
private static LoadingCache<String, ReservedList> cache = CacheBuilder
.newBuilder()
.expireAfterWrite(
RegistryEnvironment.get().config().getDomainLabelListCacheDuration().getMillis(),
MILLISECONDS)
.build(new CacheLoader<String, ReservedList>() {
@Override
public ReservedList load(String listName) {
return ofy().load().type(ReservedList.class).parent(getCrossTldKey()).id(listName).now();
}});
private static LoadingCache<String, ReservedList> cache =
CacheBuilder.newBuilder()
.expireAfterWrite(getDomainLabelListCacheDuration().getMillis(), MILLISECONDS)
.build(
new CacheLoader<String, ReservedList>() {
@Override
public ReservedList load(String listName) {
return ofy()
.load()
.type(ReservedList.class)
.parent(getCrossTldKey())
.id(listName)
.now();
}});
/**
* Gets the {@link ReservationType} of a label in a single ReservedList, or returns an absent

View file

@ -15,13 +15,13 @@
package google.registry.model.translators;
import static com.google.common.base.MoreObjects.firstNonNull;
import static google.registry.config.RegistryConfig.getCommitLogDatastoreRetention;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Ordering;
import com.googlecode.objectify.Key;
import google.registry.config.RegistryEnvironment;
import google.registry.model.ofy.CommitLogManifest;
import org.joda.time.DateTime;
@ -47,8 +47,6 @@ import org.joda.time.DateTime;
public final class CommitLogRevisionsTranslatorFactory
extends ImmutableSortedMapTranslatorFactory<DateTime, Key<CommitLogManifest>> {
private static final RegistryEnvironment ENVIRONMENT = RegistryEnvironment.get();
/**
* Add a reference to the current commit log to the resource's revisions map.
*
@ -65,7 +63,7 @@ public final class CommitLogRevisionsTranslatorFactory
ImmutableSortedMap<DateTime, Key<CommitLogManifest>> transformBeforeSave(
ImmutableSortedMap<DateTime, Key<CommitLogManifest>> revisions) {
DateTime now = ofy().getTransactionTime();
DateTime threshold = now.minus(ENVIRONMENT.config().getCommitLogDatastoreRetention());
DateTime threshold = now.minus(getCommitLogDatastoreRetention());
DateTime preThresholdTime = firstNonNull(revisions.floorKey(threshold), START_OF_TIME);
return new ImmutableSortedMap.Builder<DateTime, Key<CommitLogManifest>>(Ordering.natural())
.putAll(revisions.subMap(preThresholdTime, true, now.withTimeAtStartOfDay(), false))

View file

@ -14,6 +14,8 @@
package google.registry.tmch;
import static google.registry.config.RegistryConfig.getSingletonCachePersistDuration;
import static google.registry.config.RegistryConfig.getSingletonCacheRefreshDuration;
import static google.registry.util.ResourceUtils.readResourceUtf8;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
@ -21,7 +23,6 @@ import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import google.registry.config.ConfigModule.Config;
import google.registry.config.RegistryEnvironment;
import google.registry.model.tmch.TmchCrl;
import google.registry.util.Clock;
import google.registry.util.NonFinalForTesting;
@ -46,8 +47,6 @@ import javax.inject.Inject;
@ThreadSafe
public final class TmchCertificateAuthority {
private static final RegistryEnvironment ENVIRONMENT = RegistryEnvironment.get();
private static final String ROOT_CRT_FILE = "icann-tmch.crt";
private static final String TEST_ROOT_CRT_FILE = "icann-tmch-test.crt";
private static final String CRL_FILE = "icann-tmch.crl";
@ -71,8 +70,7 @@ public final class TmchCertificateAuthority {
*/
private static final LoadingCache<Boolean, X509CRL> CRL_CACHE =
CacheBuilder.newBuilder()
.expireAfterWrite(
ENVIRONMENT.config().getSingletonCacheRefreshDuration().getMillis(), MILLISECONDS)
.expireAfterWrite(getSingletonCacheRefreshDuration().getMillis(), MILLISECONDS)
.build(
new CacheLoader<Boolean, X509CRL>() {
@Override
@ -102,8 +100,7 @@ public final class TmchCertificateAuthority {
/** A cached function that loads the CRT from a jar resource. */
private static final LoadingCache<Boolean, X509Certificate> ROOT_CACHE =
CacheBuilder.newBuilder()
.expireAfterWrite(
ENVIRONMENT.config().getSingletonCachePersistDuration().getMillis(), MILLISECONDS)
.expireAfterWrite(getSingletonCachePersistDuration().getMillis(), MILLISECONDS)
.build(
new CacheLoader<Boolean, X509Certificate>() {
@Override

View file

@ -71,9 +71,10 @@ public class RegistrarSettingsAction implements Runnable, JsonActionRunner.JsonA
private static final String ARGS_PARAM = "args";
@Inject HttpServletRequest request;
@Inject SessionUtils sessionUtils;
@Inject JsonActionRunner jsonActionRunner;
@Inject Registrar initialRegistrar;
@Inject SendEmailUtils sendEmailUtils;
@Inject SessionUtils sessionUtils;
@Inject @Config("registrarChangesNotificationEmailAddresses") ImmutableList<String>
registrarChangesNotificationEmailAddresses;
@Inject RegistrarSettingsAction() {}
@ -285,7 +286,7 @@ public class RegistrarSettingsAction implements Runnable, JsonActionRunner.JsonA
}
SyncRegistrarsSheetAction.enqueueBackendTask();
if (!registrarChangesNotificationEmailAddresses.isEmpty()) {
SendEmailUtils.sendEmail(
sendEmailUtils.sendEmail(
registrarChangesNotificationEmailAddresses,
String.format("Registrar %s updated", registrarName),
"The following changes were made to the registrar:\n"

View file

@ -15,18 +15,17 @@
package google.registry.util;
import static com.google.common.base.Suppliers.memoizeWithExpiration;
import static google.registry.config.RegistryConfig.getSingletonCachePersistDuration;
import static google.registry.config.RegistryConfig.getSingletonCacheRefreshDuration;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.joda.time.Duration.ZERO;
import com.google.common.base.Supplier;
import google.registry.config.RegistryEnvironment;
import org.joda.time.Duration;
/** Utility methods related to caching. */
public class CacheUtils {
private static final RegistryEnvironment ENVIRONMENT = RegistryEnvironment.get();
/**
* Memoize a supplier, with a short expiration specified in the environment config.
*
@ -34,7 +33,7 @@ public class CacheUtils {
* lists downloaded from the TMCH get updated in datastore and the caches need to be refreshed.)
*/
public static <T> Supplier<T> memoizeWithShortExpiration(Supplier<T> original) {
return memoizeForDuration(original, ENVIRONMENT.config().getSingletonCacheRefreshDuration());
return memoizeForDuration(original, getSingletonCacheRefreshDuration());
}
/**
@ -45,7 +44,7 @@ public class CacheUtils {
* while allowing the production config to set the expiration to forever.
*/
public static <T> Supplier<T> memoizeWithLongExpiration(Supplier<T> original) {
return memoizeForDuration(original, ENVIRONMENT.config().getSingletonCachePersistDuration());
return memoizeForDuration(original, getSingletonCachePersistDuration());
}
/** Memoize a supplier, with a given expiration. */

View file

@ -20,10 +20,9 @@ import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import google.registry.config.RegistryConfig;
import google.registry.config.RegistryEnvironment;
import google.registry.config.ConfigModule.Config;
import java.util.List;
import javax.inject.Inject;
import javax.mail.Message;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
@ -33,30 +32,31 @@ import javax.mail.internet.InternetAddress;
*/
public class SendEmailUtils {
private static final RegistryConfig CONFIG = RegistryEnvironment.get().config();
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
private final String googleAppsSendFromEmailAddress;
private final String googleAppsAdminEmailDisplayName;
@Inject
public SendEmailUtils(
@Config("googleAppsSendFromEmailAddress") String googleAppsSendFromEmailAddress,
@Config("googleAppsAdminEmailDisplayName") String googleAppsAdminEmailDisplayName) {
this.googleAppsSendFromEmailAddress = googleAppsSendFromEmailAddress;
this.googleAppsAdminEmailDisplayName = googleAppsAdminEmailDisplayName;
}
@NonFinalForTesting
private static SendEmailService emailService = new SendEmailService();
/**
* Sends an email from Nomulus to the specified recipient. Returns true iff sending was
* Sends an email from Nomulus to the specified recipient(s). Returns true iff sending was
* successful.
*/
public static boolean sendEmail(String address, String subject, String body) {
return sendEmail(ImmutableList.of(address), subject, body);
}
/**
* Sends an email from Nomulus to the specified recipients. Returns true iff sending was
* successful.
*/
public static boolean sendEmail(Iterable<String> addresses, final String subject, String body) {
public boolean sendEmail(Iterable<String> addresses, final String subject, String body) {
try {
Message msg = emailService.createMessage();
msg.setFrom(new InternetAddress(
CONFIG.getGoogleAppsSendFromEmailAddress(),
CONFIG.getGoogleAppsAdminEmailDisplayName()));
msg.setFrom(
new InternetAddress(googleAppsSendFromEmailAddress, googleAppsAdminEmailDisplayName));
List<InternetAddress> emails = FluentIterable
.from(addresses)
.transform(new Function<String, InternetAddress>() {