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.core.env;
18
19 import java.util.Collection;
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.beans.factory.ListableBeanFactory;
26 import org.springframework.cloud.aws.core.env.stack.StackResourceRegistry;
27
28 /**
29  * Provides support for resolving logical resource ids to physical resource ids for stack
30  * resources exposed via {@link StackResourceRegistry} instances. This implementation
31  * automatically detects the single, optional stack resource registry bean and uses it for
32  * resolving logical resource ids to physical ones. If no stack resource registry bean can
33  * be found in the bean factory, a pass-through stack resource registry is used instead,
34  * which always returns the provided logical resource id as the physical one.
35  *
36  * @author Christian Stettler
37  */

38 // TODO discuss whether to support more than one stack resource registry and how to deal
39 // with ordering/name conflicts
40 public class StackResourceRegistryDetectingResourceIdResolver
41         implements ResourceIdResolver, BeanFactoryAware, InitializingBean {
42
43     private StackResourceRegistry stackResourceRegistry;
44
45     private ListableBeanFactory beanFactory;
46
47     private static StackResourceRegistry findSingleOptionalStackResourceRegistry(
48             ListableBeanFactory beanFactory) {
49         Collection<StackResourceRegistry> stackResourceRegistries = beanFactory
50                 .getBeansOfType(StackResourceRegistry.class).values();
51
52         if (stackResourceRegistries.size() > 1) {
53             throw new IllegalStateException("Multiple stack resource registries found");
54         }
55         else if (stackResourceRegistries.size() == 1) {
56             return stackResourceRegistries.iterator().next();
57         }
58         else {
59             return null;
60         }
61     }
62
63     /**
64      * Resolves the provided logical resource id to the corresponding physical resource
65      * id. If the logical resource id refers to a resource part of any of the configured
66      * stacks, the corresponding physical resource id from the stack is returned. If none
67      * of the configured stacks contain a resource with the provided logical resource id,
68      * or no stacks are configured at all, the logical resource id is returned as the
69      * physical resource id.
70      * @param logicalResourceId the logical resource id to be resolved
71      * @return the physical resource id
72      */

73     @Override
74     public String resolveToPhysicalResourceId(String logicalResourceId) {
75         if (this.stackResourceRegistry != null) {
76             String physicalResourceId = this.stackResourceRegistry
77                     .lookupPhysicalResourceId(logicalResourceId);
78
79             if (physicalResourceId != null) {
80                 return physicalResourceId;
81             }
82         }
83
84         return logicalResourceId;
85     }
86
87     @Override
88     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
89         if (!(beanFactory instanceof ListableBeanFactory)) {
90             throw new IllegalStateException("Bean factory must be of type '"
91                     + ListableBeanFactory.class.getName() + "'");
92         }
93
94         this.beanFactory = (ListableBeanFactory) beanFactory;
95     }
96
97     @Override
98     public void afterPropertiesSet() throws Exception {
99         this.stackResourceRegistry = findSingleOptionalStackResourceRegistry(
100                 this.beanFactory);
101     }
102
103 }
104