/*
 * Copyright 2013-2019 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
 *
 *      https://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.context.support.io;

import org.springframework.beans.BeansException;
import org.springframework.cloud.aws.core.io.s3.SimpleStorageProtocolResolver;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.io.ProtocolResolver;

/**
 * Configurer that register the {@link SimpleStorageProtocolResolver} to the resource
 * resolver to allow resolving s3 based resources in Spring.
 *
 * @author Agim Emruli
 * @author Alain Sahli
 * @since 1.0
 */
public class SimpleStorageProtocolResolverConfigurer
		implements ApplicationContextAware, Ordered {

	private final ProtocolResolver resourceLoader;

	public SimpleStorageProtocolResolverConfigurer(
			SimpleStorageProtocolResolver simpleStorageProtocolResolver) {
		this.resourceLoader = simpleStorageProtocolResolver;
	}

	@Override
	public int getOrder() {
		return Ordered.HIGHEST_PRECEDENCE;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		if (applicationContext instanceof ConfigurableApplicationContext) {
			ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;
			configurableApplicationContext.addProtocolResolver(this.resourceLoader);
		}
	}

}

@@ -675,16 +675,74 @@
 rt.io;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import o
@@ -813,240 +813,310 @@
 ork.
-cloud.aws.core.io.s3.SimpleStorageProtocolResolver;%0Aimport org.springframework.context.ApplicationContext;%0Aimport org.springframework.context.ApplicationContextAware;%0Aimport org.springframework.context.ConfigurableApplicationContext
+beans.factory.config.BeanFactoryPostProcessor;%0Aimport org.springframework.beans.factory.config.ConfigurableListableBeanFactory;%0Aimport org.springframework.cloud.aws.core.io.s3.SimpleStorageProtocolResolver;%0Aimport org.springframework.context.ResourceLoaderAware;%0Aimport org.springframework.core.Ordered
 ;%0Aim
@@ -1137,39 +1137,56 @@
 gframework.core.
-Ordered
+io.DefaultResourceLoader
 ;%0Aimport org.spr
@@ -1223,16 +1223,67 @@
 esolver;
+%0Aimport org.springframework.core.io.ResourceLoader;
 %0A%0A/**%0A *
@@ -1476,16 +1476,44 @@
 n Sahli%0A
+ * @author Maciej Walkowiak%0A
  * @sinc
@@ -1592,73 +1592,253 @@
 nts 
-ApplicationContextAware, Ordered %7B%0A%0A%09private final ProtocolResolv
+BeanFactoryPostProcessor, Ordered, ResourceLoaderAware %7B%0A%0A%09private static final Logger LOGGER = LoggerFactory%0A%09%09%09.getLogger(SimpleStorageProtocolResolverConfigurer.class);%0A%0A%09private final ProtocolResolver protocolResolver;%0A%0A%09private ResourceLoad
 er r
@@ -1975,28 +1975,30 @@
 %0A%09%09this.
-resourceLoad
+protocolResolv
 er = sim
@@ -2134,340 +2134,550 @@
 oid 
-setApplicationContext(ApplicationContext applicationContext)%0A%09%09%09throws BeansException %7B%0A%09%09if (applicationContext instanceof ConfigurableApplicationContext) %7B%0A%09%09%09ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;%0A%09%09%09configurableApplicationContext.addProtocolResolver(
+postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)%0A%09%09%09throws BeansException %7B%0A%09%09if (DefaultResourceLoader.class.isAssignableFrom(resourceLoader.getClass())) %7B%0A%09%09%09((DefaultResourceLoader) resourceLoader)%0A%09%09%09%09%09.addProtocolResolver(this.protocolResolver);%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09LOGGER.warn(%22The provided delegate resource loader is not an implementation %22%0A%09%09%09%09%09+ %22of DefaultResourceLoader. Custom Protocol using s3:// prefix will not be enabled.%22);%0A%09%09%7D%0A%09%7D%0A%0A%09@Override%0A%09public void setResourceLoader(ResourceLoader resourceLoader) %7B%0A%09%09
 this
@@ -2691,21 +2691,33 @@
 ceLoader
-);%0A%09%09%7D
+ = resourceLoader;
 %0A%09%7D%0A%0A%7D%0A

Fix SimpleStorageProtocolResolver bean registration.