001/* 002 * SPDX-FileCopyrightText: none 003 * SPDX-License-Identifier: CC0-1.0 004 */ 005 006package gov.nist.secauto.metaschema.databind.codegen; 007 008import gov.nist.secauto.metaschema.core.metapath.MetapathException; 009import gov.nist.secauto.metaschema.core.model.IModule; 010import gov.nist.secauto.metaschema.core.util.ObjectUtils; 011import gov.nist.secauto.metaschema.databind.model.IBoundModule; 012 013import java.io.IOException; 014import java.nio.file.Path; 015 016import edu.umd.cs.findbugs.annotations.NonNull; 017 018public class DefaultModuleBindingGenerator implements IModuleBindingGenerator { 019 @NonNull 020 private final Path compilePath; 021 022 public DefaultModuleBindingGenerator(@NonNull Path compilePath) { 023 this.compilePath = compilePath; 024 } 025 026 @Override 027 public Class<? extends IBoundModule> generate(IModule module) { 028 ClassLoader classLoader = ModuleCompilerHelper.newClassLoader( 029 compilePath, 030 ObjectUtils.notNull(Thread.currentThread().getContextClassLoader())); 031 032 IProduction production; 033 try { 034 production = ModuleCompilerHelper.compileMetaschema(module, compilePath); 035 } catch (IOException ex) { 036 throw new MetapathException( 037 String.format("Unable to generate and compile classes for module '%s'.", module.getLocation()), 038 ex); 039 } 040 041 try { 042 return ObjectUtils.notNull(production.getModuleProduction(module)).load(classLoader); 043 } catch (ClassNotFoundException ex) { 044 throw new IllegalStateException(ex); 045 } 046 } 047 048}