1
2
3
4
5
6 package gov.nist.secauto.metaschema.databind.codegen;
7
8 import gov.nist.secauto.metaschema.core.metapath.MetapathException;
9 import gov.nist.secauto.metaschema.core.model.IModule;
10 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
11 import gov.nist.secauto.metaschema.databind.model.IBoundModule;
12
13 import java.io.IOException;
14 import java.nio.file.Path;
15
16 import edu.umd.cs.findbugs.annotations.NonNull;
17
18 public class DefaultModuleBindingGenerator implements IModuleBindingGenerator {
19 @NonNull
20 private final Path compilePath;
21
22 public DefaultModuleBindingGenerator(@NonNull Path compilePath) {
23 this.compilePath = compilePath;
24 }
25
26 @Override
27 public Class<? extends IBoundModule> generate(IModule module) {
28 ClassLoader classLoader = ModuleCompilerHelper.newClassLoader(
29 compilePath,
30 ObjectUtils.notNull(Thread.currentThread().getContextClassLoader()));
31
32 IProduction production;
33 try {
34 production = ModuleCompilerHelper.compileMetaschema(module, compilePath);
35 } catch (IOException ex) {
36 throw new MetapathException(
37 String.format("Unable to generate and compile classes for module '%s'.", module.getLocation()),
38 ex);
39 }
40
41 try {
42 return ObjectUtils.notNull(production.getModuleProduction(module)).load(classLoader);
43 } catch (ClassNotFoundException ex) {
44 throw new IllegalStateException(ex);
45 }
46 }
47
48 }