1
16
17 package org.springframework.cloud.aws.core.io.s3;
18
19 import com.amazonaws.services.s3.AmazonS3;
20
21 import org.springframework.beans.BeansException;
22 import org.springframework.beans.factory.BeanFactory;
23 import org.springframework.beans.factory.BeanFactoryAware;
24 import org.springframework.beans.factory.InitializingBean;
25 import org.springframework.cloud.aws.core.support.documentation.RuntimeUse;
26 import org.springframework.core.io.ProtocolResolver;
27 import org.springframework.core.io.Resource;
28 import org.springframework.core.io.ResourceLoader;
29 import org.springframework.core.task.SyncTaskExecutor;
30 import org.springframework.core.task.TaskExecutor;
31
32
37 public class SimpleStorageProtocolResolver
38 implements ProtocolResolver, InitializingBean, BeanFactoryAware {
39
40 private AmazonS3 amazonS3;
41
42
47 private TaskExecutor taskExecutor;
48
49 private BeanFactory beanFactory;
50
51 public SimpleStorageProtocolResolver() {
52 }
53
54 public SimpleStorageProtocolResolver(AmazonS3 amazonS3) {
55 this.amazonS3 = AmazonS3ProxyFactory.createProxy(amazonS3);
56 }
57
58 @RuntimeUse
59 public void setTaskExecutor(TaskExecutor taskExecutor) {
60 this.taskExecutor = taskExecutor;
61 }
62
63 @Override
64 public void afterPropertiesSet() {
65 if (this.taskExecutor == null) {
66 this.taskExecutor = new SyncTaskExecutor();
67 }
68 }
69
70 @Override
71 public Resource resolve(String location, ResourceLoader resourceLoader) {
72 if (SimpleStorageNameUtils.isSimpleStorageResource(location)) {
73 return new SimpleStorageResource(this.getAmazonS3(),
74 SimpleStorageNameUtils.getBucketNameFromLocation(location),
75 SimpleStorageNameUtils.getObjectNameFromLocation(location),
76 this.taskExecutor,
77 SimpleStorageNameUtils.getVersionIdFromLocation(location));
78 }
79 else {
80 return null;
81 }
82 }
83
84 public AmazonS3 getAmazonS3() {
85 if (this.amazonS3 == null) {
86 this.amazonS3 = AmazonS3ProxyFactory
87 .createProxy(this.beanFactory.getBean(AmazonS3.class));
88 }
89 return this.amazonS3;
90 }
91
92 @Override
93 public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
94 this.beanFactory = beanFactory;
95 }
96
97 }
98