mirror of
https://github.com/google/nomulus.git
synced 2025-07-12 05:58:13 +02:00
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:
parent
c5c74961bb
commit
d3397e991e
17 changed files with 18 additions and 323 deletions
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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() {}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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";
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue