From 9f142c6767313e2ac3463a7e4479a18be11d8e23 Mon Sep 17 00:00:00 2001 From: mcilwain Date: Thu, 12 Jan 2017 07:45:51 -0800 Subject: [PATCH] Remove the util package's dependency on the config package This allows us to use util methods from within config, which is a useful thing to be able to do for, e.g., being able to log errors while loading configuration. It makes sense that the util package should be at the very base of the class inheritance hierarchy; config seems logically higher than it. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=144324273 --- .../registry/config/RegistryConfig.java | 13 +++++++--- .../dns/writer/clouddns/CloudDnsWriter.java | 2 +- .../registry/{util => model}/CacheUtils.java | 25 +++---------------- java/google/registry/model/RoidSuffixes.java | 2 +- .../model/billing/RegistrarBillingUtils.java | 2 +- .../registry/model/registry/Registries.java | 2 +- .../model/smd/SignedMarkRevocationList.java | 2 +- .../registry/model/tmch/ClaimsListShard.java | 2 +- .../registrar/RegistrarSettingsAction.java | 1 - .../server/registrar}/SendEmailUtils.java | 5 +++- java/google/registry/util/BUILD | 1 - java/google/registry/util/Retrier.java | 4 +-- .../whois/RegistrarLookupCommand.java | 2 +- .../RegistrarSettingsActionTestCase.java | 1 - .../server/registrar}/SendEmailUtilsTest.java | 3 ++- 15 files changed, 29 insertions(+), 38 deletions(-) rename java/google/registry/{util => model}/CacheUtils.java (59%) rename java/google/registry/{util => ui/server/registrar}/SendEmailUtils.java (94%) rename javatests/google/registry/{util => ui/server/registrar}/SendEmailUtilsTest.java (98%) diff --git a/java/google/registry/config/RegistryConfig.java b/java/google/registry/config/RegistryConfig.java index ce8b9d6de..1b3ed6dc0 100644 --- a/java/google/registry/config/RegistryConfig.java +++ b/java/google/registry/config/RegistryConfig.java @@ -15,6 +15,7 @@ package google.registry.config; import static google.registry.config.ConfigUtils.makeUrl; +import static java.lang.annotation.RetentionPolicy.RUNTIME; import static org.joda.time.Duration.standardDays; import com.google.appengine.api.utils.SystemProperty; @@ -26,9 +27,11 @@ import com.google.common.net.HostAndPort; import dagger.Module; import dagger.Provides; import java.lang.annotation.Documented; +import java.lang.annotation.Retention; import java.net.URI; import java.net.URL; import javax.annotation.Nullable; +import javax.inject.Named; import javax.inject.Qualifier; import javax.inject.Singleton; import org.joda.money.CurrencyUnit; @@ -42,6 +45,7 @@ public final class RegistryConfig { /** Dagger qualifier for configuration settings. */ @Qualifier + @Retention(RUNTIME) @Documented public static @interface Config { String value() default ""; @@ -474,7 +478,7 @@ public final class RegistryConfig { /** * The email address that outgoing emails from the app are sent from. * - * @see google.registry.util.SendEmailUtils + * @see google.registry.ui.server.registrar.SendEmailUtils */ @Provides @Config("googleAppsSendFromEmailAddress") @@ -485,7 +489,7 @@ public final class RegistryConfig { /** * The display name that is used on outgoing emails sent by Nomulus. * - * @see google.registry.util.SendEmailUtils + * @see google.registry.ui.server.registrar.SendEmailUtils */ @Provides @Config("googleAppsAdminEmailDisplayName") @@ -724,10 +728,13 @@ public final class RegistryConfig { * *

The number of milliseconds it'll sleep before giving up is {@code 2^n - 2}. * + *

Note that this uses {@code @Named} instead of {@code @Config} so that it can be used from + * the low-level util package, which cannot have a dependency on the config package. + * * @see google.registry.util.TaskEnqueuer */ @Provides - @Config("transientFailureRetries") + @Named("transientFailureRetries") public static int provideTransientFailureRetries() { return 12; // Four seconds. } diff --git a/java/google/registry/dns/writer/clouddns/CloudDnsWriter.java b/java/google/registry/dns/writer/clouddns/CloudDnsWriter.java index 73b6644f1..4e81a393b 100644 --- a/java/google/registry/dns/writer/clouddns/CloudDnsWriter.java +++ b/java/google/registry/dns/writer/clouddns/CloudDnsWriter.java @@ -72,7 +72,7 @@ public class CloudDnsWriter implements DnsWriter { private final Clock clock; private final RateLimiter rateLimiter; - // TODO(shikhman): This uses @Config("transientFailureRetries") which may not be tuned for this + // TODO(shikhman): This uses @Named("transientFailureRetries") which may not be tuned for this // application. private final Retrier retrier; private final Duration defaultTtl; diff --git a/java/google/registry/util/CacheUtils.java b/java/google/registry/model/CacheUtils.java similarity index 59% rename from java/google/registry/util/CacheUtils.java rename to java/google/registry/model/CacheUtils.java index 645607ff7..f00b9f64f 100644 --- a/java/google/registry/util/CacheUtils.java +++ b/java/google/registry/model/CacheUtils.java @@ -12,10 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -package google.registry.util; +package google.registry.model; 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; @@ -23,7 +22,7 @@ import static org.joda.time.Duration.ZERO; import com.google.common.base.Supplier; import org.joda.time.Duration; -/** Utility methods related to caching. */ +/** Utility methods related to caching Datastore entities. */ public class CacheUtils { /** @@ -33,25 +32,9 @@ public class CacheUtils { * lists downloaded from the TMCH get updated in datastore and the caches need to be refreshed.) */ public static Supplier memoizeWithShortExpiration(Supplier original) { - return memoizeForDuration(original, getSingletonCacheRefreshDuration()); - } - - /** - * Memoize a supplier, with a long expiration specified in the environment config. - * - *

Use this for things that are loaded lazily but then will never change. This allows the test - * config to set the expiration time to zero so that different test values can be substituted in, - * while allowing the production config to set the expiration to forever. - */ - public static Supplier memoizeWithLongExpiration(Supplier original) { - return memoizeForDuration(original, getSingletonCachePersistDuration()); - } - - /** Memoize a supplier, with a given expiration. */ - private static Supplier memoizeForDuration(Supplier original, Duration expiration) { + Duration expiration = getSingletonCacheRefreshDuration(); return expiration.isEqual(ZERO) - ? original // memoizeWithExpiration won't accept 0 as a refresh duration. + ? original : memoizeWithExpiration(original, expiration.getMillis(), MILLISECONDS); } } - diff --git a/java/google/registry/model/RoidSuffixes.java b/java/google/registry/model/RoidSuffixes.java index c567acfc4..2b19d9872 100644 --- a/java/google/registry/model/RoidSuffixes.java +++ b/java/google/registry/model/RoidSuffixes.java @@ -15,9 +15,9 @@ package google.registry.model; import static com.google.common.base.Preconditions.checkState; +import static google.registry.model.CacheUtils.memoizeWithShortExpiration; import static google.registry.model.common.EntityGroupRoot.getCrossTldKey; import static google.registry.model.ofy.ObjectifyService.ofy; -import static google.registry.util.CacheUtils.memoizeWithShortExpiration; import com.google.common.base.Supplier; import com.google.common.collect.HashBiMap; diff --git a/java/google/registry/model/billing/RegistrarBillingUtils.java b/java/google/registry/model/billing/RegistrarBillingUtils.java index e82fa7797..20cb75e0b 100644 --- a/java/google/registry/model/billing/RegistrarBillingUtils.java +++ b/java/google/registry/model/billing/RegistrarBillingUtils.java @@ -25,10 +25,10 @@ import com.google.common.collect.Maps; import com.google.common.collect.Maps.EntryTransformer; import com.google.common.collect.Ordering; import com.googlecode.objectify.cmd.Query; +import google.registry.model.CacheUtils; import google.registry.model.registrar.Registrar; import google.registry.model.registry.Registries; import google.registry.model.registry.Registry; -import google.registry.util.CacheUtils; import java.util.Map; import org.joda.money.CurrencyUnit; import org.joda.money.Money; diff --git a/java/google/registry/model/registry/Registries.java b/java/google/registry/model/registry/Registries.java index ec2e2e8ea..31619b115 100644 --- a/java/google/registry/model/registry/Registries.java +++ b/java/google/registry/model/registry/Registries.java @@ -19,9 +19,9 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Predicates.equalTo; import static com.google.common.base.Strings.emptyToNull; import static com.google.common.collect.Maps.filterValues; +import static google.registry.model.CacheUtils.memoizeWithShortExpiration; import static google.registry.model.common.EntityGroupRoot.getCrossTldKey; import static google.registry.model.ofy.ObjectifyService.ofy; -import static google.registry.util.CacheUtils.memoizeWithShortExpiration; import static google.registry.util.PreconditionsUtils.checkArgumentNotNull; import com.google.common.base.Optional; diff --git a/java/google/registry/model/smd/SignedMarkRevocationList.java b/java/google/registry/model/smd/SignedMarkRevocationList.java index 19a38abcb..d5a3fa8c3 100644 --- a/java/google/registry/model/smd/SignedMarkRevocationList.java +++ b/java/google/registry/model/smd/SignedMarkRevocationList.java @@ -17,10 +17,10 @@ package google.registry.model.smd; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.Iterables.isEmpty; +import static google.registry.model.CacheUtils.memoizeWithShortExpiration; import static google.registry.model.common.EntityGroupRoot.getCrossTldKey; import static google.registry.model.ofy.ObjectifyService.allocateId; import static google.registry.model.ofy.ObjectifyService.ofy; -import static google.registry.util.CacheUtils.memoizeWithShortExpiration; import static google.registry.util.DateTimeUtils.START_OF_TIME; import static google.registry.util.DateTimeUtils.isBeforeOrAt; diff --git a/java/google/registry/model/tmch/ClaimsListShard.java b/java/google/registry/model/tmch/ClaimsListShard.java index d07c0ca35..4775f5ba5 100644 --- a/java/google/registry/model/tmch/ClaimsListShard.java +++ b/java/google/registry/model/tmch/ClaimsListShard.java @@ -17,10 +17,10 @@ package google.registry.model.tmch; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Verify.verify; +import static google.registry.model.CacheUtils.memoizeWithShortExpiration; import static google.registry.model.ofy.ObjectifyService.allocateId; import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.Ofy.RECOMMENDED_MEMCACHE_EXPIRATION; -import static google.registry.util.CacheUtils.memoizeWithShortExpiration; import static google.registry.util.DateTimeUtils.START_OF_TIME; import com.google.common.annotations.VisibleForTesting; diff --git a/java/google/registry/ui/server/registrar/RegistrarSettingsAction.java b/java/google/registry/ui/server/registrar/RegistrarSettingsAction.java index 2bf07705e..6abb87166 100644 --- a/java/google/registry/ui/server/registrar/RegistrarSettingsAction.java +++ b/java/google/registry/ui/server/registrar/RegistrarSettingsAction.java @@ -45,7 +45,6 @@ import google.registry.ui.server.RegistrarFormFields; import google.registry.util.CidrAddressBlock; import google.registry.util.CollectionUtils; import google.registry.util.DiffUtils; -import google.registry.util.SendEmailUtils; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; diff --git a/java/google/registry/util/SendEmailUtils.java b/java/google/registry/ui/server/registrar/SendEmailUtils.java similarity index 94% rename from java/google/registry/util/SendEmailUtils.java rename to java/google/registry/ui/server/registrar/SendEmailUtils.java index 9cbb57fb3..47977606c 100644 --- a/java/google/registry/util/SendEmailUtils.java +++ b/java/google/registry/ui/server/registrar/SendEmailUtils.java @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package google.registry.util; +package google.registry.ui.server.registrar; import static com.google.common.collect.Iterables.toArray; @@ -21,6 +21,9 @@ import com.google.common.base.Joiner; import com.google.common.base.Predicates; import com.google.common.collect.FluentIterable; import google.registry.config.RegistryConfig.Config; +import google.registry.util.FormattingLogger; +import google.registry.util.NonFinalForTesting; +import google.registry.util.SendEmailService; import java.util.List; import javax.inject.Inject; import javax.mail.Message; diff --git a/java/google/registry/util/BUILD b/java/google/registry/util/BUILD index 70264257a..f625b86b5 100644 --- a/java/google/registry/util/BUILD +++ b/java/google/registry/util/BUILD @@ -8,7 +8,6 @@ java_library( name = "util", srcs = glob(["*.java"]), deps = [ - "//java/google/registry/config", "//third_party/java/objectify:objectify-v4_1", "@com_google_appengine_api_1_0_sdk", "@com_google_code_findbugs_jsr305", diff --git a/java/google/registry/util/Retrier.java b/java/google/registry/util/Retrier.java index 0f29bbc14..98a4d7086 100644 --- a/java/google/registry/util/Retrier.java +++ b/java/google/registry/util/Retrier.java @@ -21,11 +21,11 @@ import static google.registry.util.PredicateUtils.supertypeOf; import com.google.common.base.Predicate; import com.google.common.collect.ImmutableSet; -import google.registry.config.RegistryConfig.Config; import java.io.Serializable; import java.util.Set; import java.util.concurrent.Callable; import javax.inject.Inject; +import javax.inject.Named; import org.joda.time.Duration; /** Wrapper that does retry with exponential backoff. */ @@ -39,7 +39,7 @@ public class Retrier implements Serializable { private final int attempts; @Inject - public Retrier(Sleeper sleeper, @Config("transientFailureRetries") int transientFailureRetries) { + public Retrier(Sleeper sleeper, @Named("transientFailureRetries") int transientFailureRetries) { this.sleeper = sleeper; checkArgument(transientFailureRetries > 0, "Number of attempts must be positive"); this.attempts = transientFailureRetries; diff --git a/java/google/registry/whois/RegistrarLookupCommand.java b/java/google/registry/whois/RegistrarLookupCommand.java index 53c8ed804..0636ca275 100644 --- a/java/google/registry/whois/RegistrarLookupCommand.java +++ b/java/google/registry/whois/RegistrarLookupCommand.java @@ -16,7 +16,7 @@ package google.registry.whois; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Strings.isNullOrEmpty; -import static google.registry.util.CacheUtils.memoizeWithShortExpiration; +import static google.registry.model.CacheUtils.memoizeWithShortExpiration; import static google.registry.util.RegistrarUtils.normalizeRegistrarName; import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; diff --git a/javatests/google/registry/ui/server/registrar/RegistrarSettingsActionTestCase.java b/javatests/google/registry/ui/server/registrar/RegistrarSettingsActionTestCase.java index 14100f458..d219c2cb2 100644 --- a/javatests/google/registry/ui/server/registrar/RegistrarSettingsActionTestCase.java +++ b/javatests/google/registry/ui/server/registrar/RegistrarSettingsActionTestCase.java @@ -37,7 +37,6 @@ import google.registry.testing.AppEngineRule; import google.registry.testing.FakeClock; import google.registry.testing.InjectRule; import google.registry.util.SendEmailService; -import google.registry.util.SendEmailUtils; import java.io.PrintWriter; import java.io.StringWriter; import java.util.Map; diff --git a/javatests/google/registry/util/SendEmailUtilsTest.java b/javatests/google/registry/ui/server/registrar/SendEmailUtilsTest.java similarity index 98% rename from javatests/google/registry/util/SendEmailUtilsTest.java rename to javatests/google/registry/ui/server/registrar/SendEmailUtilsTest.java index 3d500c061..fc3a21719 100644 --- a/javatests/google/registry/util/SendEmailUtilsTest.java +++ b/javatests/google/registry/ui/server/registrar/SendEmailUtilsTest.java @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package google.registry.util; +package google.registry.ui.server.registrar; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Matchers.any; @@ -25,6 +25,7 @@ import com.google.common.collect.ImmutableList; import google.registry.config.RegistryConfig.LocalTestConfig; import google.registry.testing.ExceptionRule; import google.registry.testing.InjectRule; +import google.registry.util.SendEmailService; import java.util.Properties; import javax.mail.Message; import javax.mail.Message.RecipientType;