/*
 * Copyright 2013-2014 the original author or authors.
 *
 * 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 org.springframework.cloud.aws.autoconfigure.context;

import com.amazonaws.services.cloudformation.AmazonCloudFormation;
import com.amazonaws.services.ec2.AmazonEC2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.aws.context.annotation.ConditionalOnAwsCloudEnvironment;
import org.springframework.cloud.aws.context.config.annotation.ContextDefaultConfigurationRegistrar;
import org.springframework.cloud.aws.context.config.annotation.ContextStackConfiguration;
import org.springframework.cloud.aws.core.env.stack.config.AutoDetectingStackNameProvider;
import org.springframework.cloud.aws.core.env.stack.config.StackResourceRegistryFactoryBean;
import org.springframework.cloud.aws.core.env.stack.config.StaticStackNameProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;

/**
 * @author Agim Emruli
 */
@Configuration
@Import({ContextCredentialsAutoConfiguration.class, ContextDefaultConfigurationRegistrar.class})
@ConditionalOnClass(name = "com.amazonaws.services.cloudformation.AmazonCloudFormation")
public class ContextStackAutoConfiguration {

    @Configuration
    @ConditionalOnProperty(prefix = "cloud.aws", name = "stack.name")
    public static class StackManualDetectConfiguration extends ContextStackConfiguration {

        @Autowired
        private Environment environment;

        @Override
        @Bean
        @ConditionalOnMissingBean(StackResourceRegistryFactoryBean.class)
        public StackResourceRegistryFactoryBean stackResourceRegistryFactoryBean(AmazonCloudFormation amazonCloudFormation) {
            return new StackResourceRegistryFactoryBean(amazonCloudFormation, new StaticStackNameProvider(this.environment.getProperty("cloud.aws.stack.name")));
        }
    }


    @Configuration
    @ConditionalOnProperty(prefix = "cloud.aws", name = "stack.auto", havingValue = "true", matchIfMissing = true)
    @ConditionalOnAwsCloudEnvironment
    public static class StackAutoDetectConfiguration extends ContextStackConfiguration {

        @Autowired(required = false)
        private AmazonEC2 amazonEC2;

        @Override
        @Bean
        @ConditionalOnMissingBean(StackResourceRegistryFactoryBean.class)
        public StackResourceRegistryFactoryBean stackResourceRegistryFactoryBean(AmazonCloudFormation amazonCloudFormation) {
            return new StackResourceRegistryFactoryBean(amazonCloudFormation, new AutoDetectingStackNameProvider(amazonCloudFormation, this.amazonEC2));
        }

    }
}

@@ -852,16 +852,82 @@
 owired;%0A
+import org.springframework.boot.autoconfigure.AutoConfigureAfter;%0A
 import o
@@ -994,16 +994,16 @@
 nClass;%0A
-
 import o
@@ -2153,16 +2153,82 @@
 ation%22)%0A
+@ConditionalOnMissingBean(StackResourceRegistryFactoryBean.class)%0A
 public c
@@ -2543,82 +2543,8 @@
 ean%0A
-        @ConditionalOnMissingBean(StackResourceRegistryFactoryBean.class)%0A
     
@@ -3218,82 +3218,8 @@
 ean%0A
-        @ConditionalOnMissingBean(StackResourceRegistryFactoryBean.class)%0A
     
@@ -3489,24 +3489,24 @@
 mazonEC2));%0A
+
         %7D%0A%0A 
@@ -3503,17 +3503,16 @@
       %7D%0A
-%0A
     %7D%0A%7D%0A

Move condition from ConfigurationClass to ConfigurationClass itself avoiding problems in EC2 environments