View Javadoc
1   /*
2    * Copyright 2013 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.core.extend.config;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import groovy.lang.GroovySystem;
25  import groovy.lang.MetaClassRegistry;
26  import groovy.lang.MetaMethod;
27  import org.codehaus.groovy.reflection.CachedClass;
28  import org.codehaus.groovy.runtime.m12n.ExtensionModule;
29  import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl;
30  import org.openehealth.ipf.commons.core.config.OrderedConfigurer;
31  import org.openehealth.ipf.commons.core.config.Registry;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  /**
36   * Configurer used to autowire all classes implementing the
37   * {@link org.openehealth.ipf.commons.core.extend.config.DynamicExtension}
38   * interface using Groovy 2.x Extension Modules
39   *
40   * @author Christian Ohr
41   *
42   * @see DynamicExtension
43   * @see DynamicExtensionModule
44   */
45  public class DynamicExtensionConfigurer<R extends Registry> extends
46          OrderedConfigurer<DynamicExtension, R> {
47  
48      private static final Logger LOG = LoggerFactory.getLogger(DynamicExtensionConfigurer.class);
49  
50      public DynamicExtensionConfigurer() {
51          setOrder(2);
52      }
53  
54      @Override
55      public void configure(DynamicExtension extension) throws Exception {
56          if (extension != null) {
57              LOG.info("Registering new extension module {} defined in class {}",
58                      extension.getModuleName(), extension.getClass());
59              ExtensionModule module = DynamicExtensionModule.newModule(extension);
60              addExtensionMethods(module);
61          }
62      }
63  
64      public static void addExtensionMethods(ExtensionModule module) {
65          MetaClassRegistry metaClassRegistry = GroovySystem.getMetaClassRegistry();
66          ((MetaClassRegistryImpl) metaClassRegistry).getModuleRegistry().addModule(module);
67          Map<CachedClass, List<MetaMethod>> classMap = new HashMap<>();
68          for (MetaMethod metaMethod : module.getMetaMethods()){
69              if (classMap.containsKey(metaMethod.getDeclaringClass())){
70                  classMap.get(metaMethod.getDeclaringClass()).add(metaMethod);
71              } else {
72                  List<MetaMethod> methodList = new ArrayList<>();
73                  methodList.add(metaMethod);
74                  classMap.put(metaMethod.getDeclaringClass(), methodList);
75              }
76              if (metaMethod.isStatic()){
77                  ((MetaClassRegistryImpl)metaClassRegistry).getStaticMethods().add(metaMethod);
78              } else {
79                  ((MetaClassRegistryImpl)metaClassRegistry).getInstanceMethods().add(metaMethod);
80              }
81              LOG.debug("registered method: {}", metaMethod);
82          }
83          for (Map.Entry<CachedClass, List<MetaMethod>> cachedClassEntry : classMap.entrySet()) {
84              cachedClassEntry.getKey().addNewMopMethods(cachedClassEntry.getValue());
85          }
86      }
87  
88      @Override
89      public Collection<DynamicExtension> lookup(Registry registry) {
90          return registry.beans(DynamicExtension.class).values();
91      }
92  
93  }