View Javadoc
1   /*
2    * Copyright 2018 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    *         http://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  package org.openehealth.ipf.commons.spring.core.config;
17  
18  import java.util.Map;
19  import java.util.Objects;
20  
21  import org.openehealth.ipf.commons.core.config.ContextFacade;
22  import org.openehealth.ipf.commons.core.config.Registry;
23  import org.springframework.beans.BeansException;
24  import org.springframework.beans.factory.BeanFactory;
25  import org.springframework.beans.factory.BeanFactoryAware;
26  import org.springframework.beans.factory.BeanFactoryUtils;
27  import org.springframework.beans.factory.ListableBeanFactory;
28  
29  /**
30   * Class that bridges the {@link Registry} interface to a Spring
31   * {@link ListableBeanFactory}. You simply need to declare a Spring
32   * bean like this:
33   * <p>
34   * <bean class="org.openehealth.ipf.commons.core.config.SpringRegistry"/>
35   * </p>
36   * or, even easier, using the globalContext tag in the
37   * http://openehealth.org/schema/ipf-commons-core extension namespace:
38   * <pre>
39   *     <ipf-commons-core:globalContext/>
40   * </pre>
41   * Then the "stateful" Groovy Extension Modules will access the Spring
42   * registry.
43   *
44   * @since 2.5
45   */
46  public class SpringRegistry implements Registry, BeanFactoryAware {
47  
48      private ListableBeanFactory beanFactory;
49  
50      public SpringRegistry() {
51      }
52  
53      @Override
54      public Object bean(String name) {
55          return beanFactory.getBean(name);
56      }
57  
58      @Override
59      public <T> T bean(Class<T> requiredType) {
60          return beanFactory.getBean(requiredType);
61      }
62  
63      @Override
64      public <T> Map<String, T> beans(Class<T> requiredType) {
65          return BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory,
66                  ProxyUtils.isJDKDynamicProxy(requiredType) ?
67                          ProxyUtils.getFirstProxiedInterface(requiredType) :
68                          requiredType);
69      }
70  
71      /**
72       * Stores the beanFactory and initialized the {@link ContextFacade}.
73       * 
74       * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
75       */
76      @Override
77      public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
78          this.beanFactory = (ListableBeanFactory) beanFactory;
79          ContextFacade.setRegistry(this);
80      }
81  
82      @Override
83      public boolean equals(Object o) {
84          if (this == o) return true;
85          if (!(o instanceof SpringRegistry)) return false;
86          SpringRegistry that = (SpringRegistry) o;
87          return Objects.equals(beanFactory, that.beanFactory);
88      }
89  
90      @Override
91      public int hashCode() {
92          return Objects.hash(beanFactory);
93      }
94  }