1 /*
2  * Copyright 2013-2019 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.cloud.aws.context.support.io;
18
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import org.springframework.beans.BeansException;
23 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
24 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
25 import org.springframework.cloud.aws.core.io.s3.SimpleStorageProtocolResolver;
26 import org.springframework.context.ResourceLoaderAware;
27 import org.springframework.core.Ordered;
28 import org.springframework.core.io.DefaultResourceLoader;
29 import org.springframework.core.io.ProtocolResolver;
30 import org.springframework.core.io.ResourceLoader;
31
32 /**
33  * Configurer that register the {@link SimpleStorageProtocolResolver} to the resource
34  * resolver to allow resolving s3 based resources in Spring.
35  *
36  * @author Agim Emruli
37  * @author Alain Sahli
38  * @author Maciej Walkowiak
39  * @since 1.0
40  */

41 public class SimpleStorageProtocolResolverConfigurer
42         implements BeanFactoryPostProcessor, Ordered, ResourceLoaderAware {
43
44     private static final Logger LOGGER = LoggerFactory
45             .getLogger(SimpleStorageProtocolResolverConfigurer.class);
46
47     private final ProtocolResolver protocolResolver;
48
49     private ResourceLoader resourceLoader;
50
51     public SimpleStorageProtocolResolverConfigurer(
52             SimpleStorageProtocolResolver simpleStorageProtocolResolver) {
53         this.protocolResolver = simpleStorageProtocolResolver;
54     }
55
56     @Override
57     public int getOrder() {
58         return Ordered.HIGHEST_PRECEDENCE;
59     }
60
61     @Override
62     public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
63             throws BeansException {
64         if (DefaultResourceLoader.class.isAssignableFrom(resourceLoader.getClass())) {
65             ((DefaultResourceLoader) resourceLoader)
66                     .addProtocolResolver(this.protocolResolver);
67         }
68         else {
69             LOGGER.warn("The provided delegate resource loader is not an implementation "
70                     + "of DefaultResourceLoader. Custom Protocol using s3:// prefix will not be enabled.");
71         }
72     }
73
74     @Override
75     public void setResourceLoader(ResourceLoader resourceLoader) {
76         this.resourceLoader = resourceLoader;
77     }
78
79 }
80