/**
 * Copyright (C) 2012 Ness Computing, Inc.
 *
 * 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 com.nesscomputing.config;


import com.google.inject.AbstractModule;

/**
 * Binding configuration to Guice. This is a pretty simple module right now but
 * it might grow over time.
 *
 * To get access to the System configuration, import this module.
 *
 */
public class ConfigModule extends AbstractModule
{
    private final Config config;

    public static ConfigModule forTesting(final String ... keyValuePairs)
    {
        return new ConfigModule(Config.getFixedConfig(keyValuePairs));
    }

    public static ConfigModule forTesting()
    {
        return new ConfigModule(Config.getEmptyConfig());
    }

    /**
     * Create a new config module that uses the system properties 'ness.config.location' and 'ness.config' to determine what configurations to load.
     */
    public ConfigModule()
    {
        this.config = Config.getConfig();
    }

    /**
     * Bind the config module as system configuration.
     *
     */
    public ConfigModule(final Config config)
    {
        this.config = config;
    }

    /**
     * @deprecated Use {@link ConfigModule#forTesting(String ... keyValuePairs)}
     */
    @Deprecated
    public ConfigModule(final String ... keyValuePairs)
    {
        this(Config.getFixedConfig(keyValuePairs));
    }

    @Override
    public void configure()
    {
        bind(Config.class).toInstance(config);
    }

    protected Config getConfig()
    {
        return config;
    }
}

@@ -674,16 +674,125 @@
 tModule;
+%0Aimport com.google.inject.Provider;%0Aimport com.google.inject.Scopes;%0Aimport com.google.inject.util.Providers;
 %0A%0A/**%0A *
@@ -1042,29 +1042,47 @@
 e final 
+Provider%3C
 Config
+%3E
  config
+Provider
 ;%0A%0A    p
@@ -1558,40 +1558,535 @@
     
-this.config = Config.getConfig()
+// Use a provider to load during the Injector creating stage, rather than the module instantiaton itself.%0A        // In the case where the Config binding is later overridden by Modules.override, this will ensure we do not%0A        // go out and build the Config object anyway -- that may fail due to e.g. missing System properties%0A        this.configProvider = new Provider%3CConfig%3E()%0A        %7B%0A            @Override%0A            public Config get()%0A            %7B%0A                return Config.getConfig();%0A            %7D%0A        %7D
 ;%0A  
@@ -2242,17 +2242,39 @@
 nfig
- = 
+Provider = Providers.of(
 config
+)
 ;%0A  
@@ -2593,23 +2593,52 @@
 ).to
-Instance(config
+Provider(configProvider).in(Scopes.SINGLETON
 );%0A 
@@ -2674,32 +2674,32 @@
 tConfig()%0A    %7B%0A
-
         return c
@@ -2703,16 +2703,30 @@
 n config
+Provider.get()
 ;%0A    %7D%0A

Change the ConfigModule to not call Config.getConfig() from the module constructor. This moves the IllegalStateException if the system properties are not configured from the module constructor itself into the Guice injector creation stage. Normally this does not matter, but in the case where you've done a Modules.override that replaces the binding, the error no longer happens.