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.Collections;
20  import java.util.List;
21  
22  import org.codehaus.groovy.runtime.m12n.SimpleExtensionModule;
23  
24  /**
25   * A primitive factory for a {@link org.codehaus.groovy.runtime.m12n.ExtensionModule}
26   * that simply takes an already loaded {@link DynamicExtension} instance.
27   *
28   * @see DynamicExtensionConfigurer
29   */
30  class DynamicExtensionModule extends SimpleExtensionModule {
31  
32      private DynamicExtension extension;
33  
34      private DynamicExtensionModule(DynamicExtension extension) {
35          super(extension.getModuleName(), extension.getModuleVersion());
36          this.extension = extension;
37      }
38  
39      @Override
40      public List<Class> getInstanceMethodsExtensionClasses() {
41          return extension.isStatic() ? Collections.<Class>emptyList() : classList(extension);
42      }
43  
44      @Override
45      public List<Class> getStaticMethodsExtensionClasses() {
46          return extension.isStatic() ? classList(extension) : Collections.<Class>emptyList();
47      }
48  
49      private static List<Class> classList(Object o) {
50          List<Class> l = new ArrayList<>(1);
51          l.add(o.getClass());
52          return Collections.unmodifiableList(l);
53      }
54  
55      public static DynamicExtensionModule newModule(final DynamicExtension extension) {
56          String name = extension.getModuleName();
57          if (name == null)
58              throw new RuntimeException("Module name is null");
59          String version = extension.getModuleVersion();
60          if (version == null)
61              throw new RuntimeException("Module version is null");
62  
63          return new DynamicExtensionModule(extension);
64      }
65  }