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.config;
17  
18  import java.util.Properties;
19  
20  import groovy.lang.MetaMethod;
21  import org.codehaus.groovy.runtime.m12n.ExtensionModule;
22  import org.codehaus.groovy.runtime.m12n.MetaInfExtensionModule;
23  import org.codehaus.groovy.runtime.m12n.PropertiesModuleFactory;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  /**
28   * Extension module that wraps the registration inside logging
29   * statements so that the extension process can be observed if necessary.
30   *
31   * In order to load the extensions with this class,
32   * the extension module descriptor must contain a corresponding line:
33   * <p>
34   * moduleFactory=org.openehealth.ipf.commons.core.config.ExtensionModuleFactory
35   * </p>
36   */
37  public class ExtensionModuleFactory extends PropertiesModuleFactory {
38  
39      private static final Logger LOG = LoggerFactory.getLogger(ExtensionModuleFactory.class);
40  
41      public ExtensionModuleFactory() {
42      }
43  
44      @Override
45      public ExtensionModule newModule(Properties properties, ClassLoader classLoader) {
46          LOG.info("Registering new extension module {} defined in class {}",
47                  properties.getProperty(MODULE_NAME_KEY),
48                  properties.getProperty(MetaInfExtensionModule.MODULE_INSTANCE_CLASSES_KEY));
49          ExtensionModule module = createExtensionModule(properties, classLoader);
50          if (LOG.isDebugEnabled()) {
51              for(MetaMethod method : module.getMetaMethods()) {
52                  LOG.debug("registered method: {}", method);
53              }
54          }
55          return module;
56      }
57  
58      /**
59       * Delegate that actually creates the ExtensionModule. Defaults to calling
60       * {@link MetaInfExtensionModule#newModule(java.util.Properties, ClassLoader)}.
61       *
62       * @param properties extension module properties
63       * @param classLoader classloader
64       * @return new ExtensionModule
65       */
66      protected ExtensionModule createExtensionModule(Properties properties, ClassLoader classLoader) {
67          return MetaInfExtensionModule.newModule(properties, classLoader);
68      }
69  }