Remove the old RegistryConfig paradigm entirely

We are now ready to begin configuration using YAML, mediated by ConfigModule.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=143818507
This commit is contained in:
mcilwain 2017-01-06 14:50:29 -08:00 committed by Ben McIlwain
parent c5c74961bb
commit d3397e991e
17 changed files with 18 additions and 323 deletions

View file

@ -52,10 +52,6 @@ import org.joda.time.Duration;
* in the user's repository. For this to work, other files need to be copied too, such as the
* {@code @Component} instances under {@code google.registry.module}. This allows modules to be
* substituted at the {@code @Component} level.
*
* <p>There's also a deprecated configuration class that needs to be overridden and supplied via a
* system property. See the instructions in {@link ProductionRegistryConfigExample} and
* {@link RegistryConfigLoader}.
*/
@Module
public final class ConfigModule {

View file

@ -1,43 +0,0 @@
// Copyright 2016 The Nomulus Authors. All Rights Reserved.
//
// 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 google.registry.config;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Optional;
import javax.annotation.concurrent.Immutable;
/**
* Default production configuration for global constants that can't be injected.
*
* <p><b>Warning:</b> Editing this file in a forked repository is not recommended. The recommended
* approach is to copy this file, give it a different name, and then change the system property
* described in the {@link RegistryConfigLoader} documentation.
*/
@Immutable
public final class ProductionRegistryConfigExample extends RegistryConfig {
@SuppressWarnings("unused")
private final RegistryEnvironment environment;
public ProductionRegistryConfigExample(RegistryEnvironment environment) {
this.environment = checkNotNull(environment);
}
@Override
public Optional<String> getECatcherAddress() {
throw new UnsupportedOperationException(); // n/a
}
}

View file

@ -17,18 +17,14 @@ package google.registry.config;
import static google.registry.config.ConfigUtils.makeUrl;
import com.google.common.base.Ascii;
import com.google.common.base.Optional;
import com.google.common.net.HostAndPort;
import java.net.URL;
import org.joda.time.Duration;
/**
* Registry configuration for global constants that can't be injected.
*
* <p>The goal of this custom configuration system is to have our project environments configured
* in type-safe Java code that can be refactored, rather than XML files and system properties.
*/
public abstract class RegistryConfig {
public final class RegistryConfig {
/**
* Returns the App Engine project ID, which is based off the environment name.
@ -104,8 +100,6 @@ public abstract class RegistryConfig {
}
}
public abstract Optional<String> getECatcherAddress();
/**
* Returns the address of the Nomulus app HTTP server.
*
@ -211,5 +205,5 @@ public abstract class RegistryConfig {
}
}
// XXX: Please consider using ConfigModule instead of adding new methods to this file.
private RegistryConfig() {}
}

View file

@ -1,76 +0,0 @@
// Copyright 2016 The Nomulus Authors. All Rights Reserved.
//
// 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 google.registry.config;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import javax.annotation.concurrent.ThreadSafe;
/**
* System property loader of {@link RegistryConfig} instance.
*
* <p>This class reflectively loads the Java class defined by the system property
* {@value #REGISTRY_CONFIG_PROPERTY} whose default value is {@value #REGISTRY_CONFIG_DEFAULT} and
* can be set in {@code appengine-web.xml}. Once the class is loaded, its constructor is called,
* 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";
public static final String REGISTRY_CONFIG_DEFAULT =
"google.registry.config.ProductionRegistryConfigExample";
static RegistryConfig load(RegistryEnvironment environment) {
String className = System.getProperty(REGISTRY_CONFIG_PROPERTY, REGISTRY_CONFIG_DEFAULT);
Class<?> clazz;
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException(String.format(
"Failed to load '%s' as specified by system property '%s'",
className, REGISTRY_CONFIG_PROPERTY), e);
}
if (!RegistryConfig.class.isAssignableFrom(clazz)) {
throw new RuntimeException(String.format(
"%s does not implement %s",
clazz.getSimpleName(), RegistryConfig.class.getSimpleName()));
}
@SuppressWarnings("unchecked")
Class<? extends RegistryConfig> clazzy = (Class<? extends RegistryConfig>) clazz;
if (!Modifier.isPublic(clazzy.getModifiers())) {
throw new RuntimeException(String.format(
"Must be a public class: %s", clazzy.getCanonicalName()));
}
Constructor<? extends RegistryConfig> constructor;
try {
constructor = clazzy.getConstructor(RegistryEnvironment.class);
} catch (NoSuchMethodException | SecurityException e) {
throw new RuntimeException(String.format(
"Must have a public constructor(RegistryEnvironment): %s", clazzy.getCanonicalName()), e);
}
try {
return constructor.newInstance(environment);
} catch (InvocationTargetException e) {
throw new RuntimeException(
String.format("%s constructor threw an exception", clazzy.getSimpleName()), e);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException e) {
throw new RuntimeException(
String.format("Failed to instantiate: %s", clazz.getCanonicalName()), e);
}
}
}

View file

@ -14,9 +14,7 @@
package google.registry.config;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Ascii;
import javax.annotation.Nullable;
/** Registry environments. */
public enum RegistryEnvironment {
@ -54,39 +52,6 @@ public enum RegistryEnvironment {
return valueOf(Ascii.toUpperCase(System.getProperty(PROPERTY, UNITTEST.name())));
}
/**
* Returns configuration for this registry environment.
*
* <p><b>WARNING:</b> Do not store this value to a static field, otherwise you won't be able to
* override it for testing. You should instead store the environment object to a static field.
*/
public RegistryConfig config() {
if (configOverride != null) {
return configOverride;
} else if (this == UNITTEST) {
return testingConfig;
} else {
return config;
}
}
/** 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. */
public static final String PROPERTY = "google.registry.environment";
}

View file

@ -1,30 +0,0 @@
// Copyright 2016 The Nomulus Authors. All Rights Reserved.
//
// 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 google.registry.config;
import com.google.common.base.Optional;
/**
* An implementation of RegistryConfig for unit testing that contains suitable testing data.
*/
public class TestRegistryConfig extends RegistryConfig {
public TestRegistryConfig() {}
@Override
public Optional<String> getECatcherAddress() {
throw new UnsupportedOperationException();
}
}

View file

@ -19,6 +19,7 @@ import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Predicates.notNull;
import static com.google.common.collect.Maps.uniqueIndex;
import static com.googlecode.objectify.ObjectifyService.ofy;
import static google.registry.config.RegistryConfig.getBaseOfyRetryDuration;
import static google.registry.util.CollectionUtils.union;
import static google.registry.util.ObjectifyUtils.OBJECTS_TO_KEYS;
@ -37,7 +38,6 @@ import com.googlecode.objectify.Work;
import com.googlecode.objectify.cmd.Deleter;
import com.googlecode.objectify.cmd.Loader;
import com.googlecode.objectify.cmd.Saver;
import google.registry.config.RegistryEnvironment;
import google.registry.model.annotations.NotBackedUp;
import google.registry.model.annotations.VirtualEntity;
import google.registry.model.ofy.ReadOnlyWork.KillTransactionException;
@ -212,7 +212,7 @@ public class Ofy {
*/
@VisibleForTesting
<R> R transactCommitLoggedWork(CommitLoggedWork<R> work) {
long baseRetryMillis = RegistryEnvironment.get().config().getBaseOfyRetryDuration().getMillis();
long baseRetryMillis = getBaseOfyRetryDuration().getMillis();
for (long attempt = 0, sleepMillis = baseRetryMillis;
true;
attempt++, sleepMillis *= 2) {

View file

@ -24,6 +24,7 @@ import static com.google.common.base.Strings.emptyToNull;
import static com.google.common.base.Strings.nullToEmpty;
import static com.google.common.collect.Sets.immutableEnumSet;
import static com.google.common.io.BaseEncoding.base64;
import static google.registry.config.RegistryConfig.getRegistrarDefaultReferralUrl;
import static google.registry.config.RegistryConfig.getRegistrarDefaultWhoisServer;
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
import static google.registry.model.ofy.ObjectifyService.ofy;
@ -53,7 +54,6 @@ import com.googlecode.objectify.annotation.IgnoreSave;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Parent;
import com.googlecode.objectify.condition.IfNull;
import google.registry.config.RegistryEnvironment;
import google.registry.model.Buildable;
import google.registry.model.CreateAutoTimestamp;
import google.registry.model.ImmutableObject;
@ -162,8 +162,6 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
BRAINTREE;
}
private static final RegistryEnvironment ENVIRONMENT = RegistryEnvironment.get();
/** Regex for E.164 phone number format specified by {@code contact.xsd}. */
private static final Pattern E164_PATTERN = Pattern.compile("\\+[0-9]{1,3}\\.[0-9]{1,14}");
@ -463,10 +461,7 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
}
public String getReferralUrl() {
if (referralUrl == null) {
return ENVIRONMENT.config().getRegistrarDefaultReferralUrl().toString();
}
return referralUrl;
return firstNonNull(referralUrl, getRegistrarDefaultReferralUrl().toString());
}
public String getIcannReferralEmail() {

View file

@ -17,7 +17,7 @@ package google.registry.tools;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.net.HostAndPort;
import google.registry.config.RegistryEnvironment;
import google.registry.config.RegistryConfig;
/**
* Class to contain the configuration flags for AppEngineConnection.
@ -29,7 +29,7 @@ import google.registry.config.RegistryEnvironment;
class AppEngineConnectionFlags {
@Parameter(names = "--server", description = "HOST[:PORT] to which remote commands are sent.")
private static HostAndPort server = RegistryEnvironment.get().config().getServer();
private static HostAndPort server = RegistryConfig.getServer();
HostAndPort getServer() {
return server;