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
This commit is contained in:
mcilwain 2017-01-12 07:45:51 -08:00 committed by Ben McIlwain
parent b0bcc1bb3d
commit 9f142c6767
15 changed files with 29 additions and 38 deletions

View file

@ -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 {
*
* <p>The number of milliseconds it'll sleep before giving up is {@code 2^n - 2}.
*
* <p>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.
}

View file

@ -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;

View file

@ -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 <T> Supplier<T> memoizeWithShortExpiration(Supplier<T> original) {
return memoizeForDuration(original, getSingletonCacheRefreshDuration());
}
/**
* Memoize a supplier, with a long expiration specified in the environment config.
*
* <p>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 <T> Supplier<T> memoizeWithLongExpiration(Supplier<T> original) {
return memoizeForDuration(original, getSingletonCachePersistDuration());
}
/** Memoize a supplier, with a given expiration. */
private static <T> Supplier<T> memoizeForDuration(Supplier<T> 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);
}
}

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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",

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;