Search in sources :

Example 1 with ClassLoaderPureGraphCache

use of org.finos.legend.pure.m3.serialization.runtime.cache.ClassLoaderPureGraphCache in project legend-pure by finos.

the class JavaClassLoaderSourceCodeGenerator method gen.

public static void gen(long start, Path filePath, String name, MutableList<CompiledExtension> extensions) {
    RichIterable<CodeRepository> repositoriesForCompilation = Lists.fixedSize.of(CodeRepository.newPlatformCodeRepository());
    PureCodeStorage codeStorage = new PureCodeStorage(null, new ClassLoaderCodeStorage(repositoriesForCompilation));
    ClassLoaderPureGraphCache graphCache = new ClassLoaderPureGraphCache();
    PureRuntime runtime = new PureRuntimeBuilder(codeStorage).withCache(graphCache).setTransactionalByDefault(false).buildAndTryToInitializeFromCache();
    if (!runtime.isInitialized()) {
        CacheState cacheState = graphCache.getCacheState();
        if (cacheState != null) {
            String lastStackTrace = cacheState.getLastStackTrace();
            if (lastStackTrace != null) {
                System.out.println("Cache initialization failure: " + lastStackTrace);
            }
        }
        System.out.println("Initialization from caches failed - compiling from scratch");
        runtime.reset();
        runtime.loadAndCompileCore();
        runtime.loadAndCompileSystem();
    }
    System.out.format("Finished Pure initialization (%.6fs)%n", (System.nanoTime() - start) / 1_000_000_000.0);
    JavaSourceCodeGenerator javaSourceCodeGenerator = new JavaSourceCodeGenerator(runtime.getProcessorSupport(), runtime.getCodeStorage(), true, filePath, false, extensions, name, JavaPackageAndImportBuilder.externalizablePackage());
    javaSourceCodeGenerator.generateCode();
    javaSourceCodeGenerator.generatePureCoreHelperClasses(new ProcessorContext(runtime.getProcessorSupport(), false));
}
Also used : PureRuntime(org.finos.legend.pure.m3.serialization.runtime.PureRuntime) CodeRepository(org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository) ClassLoaderPureGraphCache(org.finos.legend.pure.m3.serialization.runtime.cache.ClassLoaderPureGraphCache) ClassLoaderCodeStorage(org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.classpath.ClassLoaderCodeStorage) CacheState(org.finos.legend.pure.m3.serialization.runtime.cache.CacheState) PureRuntimeBuilder(org.finos.legend.pure.m3.serialization.runtime.PureRuntimeBuilder) PureCodeStorage(org.finos.legend.pure.m3.serialization.filesystem.PureCodeStorage)

Example 2 with ClassLoaderPureGraphCache

use of org.finos.legend.pure.m3.serialization.runtime.cache.ClassLoaderPureGraphCache in project legend-pure by finos.

the class PureCompiledJarMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        getLog().info("Generating Java Compiled JAR");
        getLog().info("  Requested repositories: " + Arrays.toString(this.repositories));
        getLog().info("  Excluded repositories: " + Arrays.toString(this.excludedRepositories));
        getLog().info("  Extra repositories: " + Arrays.toString(this.extraRepositories));
        RichIterable<CodeRepository> resolvedRepositories = resolveRepositories(repositories, excludedRepositories, extraRepositories);
        getLog().info("  Repositories with resolved dependencies: " + resolvedRepositories);
        Path distributedMetadataDirectory = null;
        Path codegenDirectory = null;
        if (!this.generateMetadata) {
            getLog().info("  Classes output directory: " + this.classesDirectory);
            getLog().info("  No metadata output");
        } else if (this.useSingleDir) {
            distributedMetadataDirectory = this.classesDirectory.toPath();
            getLog().info("  All in output directory: " + this.classesDirectory);
        } else {
            distributedMetadataDirectory = this.targetDirectory.toPath().resolve("metadata-distributed");
            getLog().info("  Classes output directory: " + this.classesDirectory);
            getLog().info("  Distributed metadata output directory: " + distributedMetadataDirectory);
        }
        if (this.generateSources) {
            codegenDirectory = this.targetDirectory.toPath().resolve("generated");
            getLog().info("  Codegen output directory: " + codegenDirectory);
        }
        long start = System.nanoTime();
        // Initialize runtime
        PureRuntime runtime;
        try {
            getLog().info("  Beginning Pure initialization");
            SetIterable<CodeRepository> repositoriesForCompilation = PureCodeStorage.getRepositoryDependencies(PureRepositoriesExternal.repositories(), resolvedRepositories);
            // Add the project output to the plugin classloader
            URL[] urlsForClassLoader = ListAdapter.adapt(project.getCompileClasspathElements()).collect(mavenCompilePath -> {
                try {
                    return new File(mavenCompilePath).toURI().toURL();
                } catch (MalformedURLException e) {
                    throw new RuntimeException(e);
                }
            }).toArray(new URL[0]);
            getLog().info("    Project classLoader URLs " + Arrays.toString(urlsForClassLoader));
            ClassLoader classLoader = new URLClassLoader(urlsForClassLoader, PureCompiledJarMojo.class.getClassLoader());
            // Initialize from PAR files cache
            PureCodeStorage codeStorage = new PureCodeStorage(null, new ClassLoaderCodeStorage(classLoader, repositoriesForCompilation));
            ClassLoaderPureGraphCache graphCache = new ClassLoaderPureGraphCache(classLoader);
            runtime = new PureRuntimeBuilder(codeStorage).withCache(graphCache).setTransactionalByDefault(false).buildAndTryToInitializeFromCache();
            if (!runtime.isInitialized()) {
                CacheState cacheState = graphCache.getCacheState();
                if (cacheState != null) {
                    String lastStackTrace = cacheState.getLastStackTrace();
                    if (lastStackTrace != null) {
                        getLog().warn("    Cache initialization failure: " + lastStackTrace);
                    }
                }
                getLog().info("    Initialization from caches failed - compiling from scratch");
                runtime.reset();
                runtime.loadAndCompileCore();
                runtime.loadAndCompileSystem();
            }
            getLog().info(String.format("    Finished Pure initialization (%.6fs)", (System.nanoTime() - start) / 1_000_000_000.0));
        } catch (PureException e) {
            getLog().error(String.format("    Error initializing Pure (%.6fs)", (System.nanoTime() - start) / 1_000_000_000.0), e);
            throw new MojoFailureException(e.getInfo(), e);
        } catch (Exception e) {
            getLog().error(String.format("    Error initializing Pure (%.6fs)", (System.nanoTime() - start) / 1_000_000_000.0), e);
            throw new MojoExecutionException("    Error initializing Pure", e);
        }
        if (generateMetadata) {
            // Write distributed metadata
            String writeMetadataStep = "writing distributed Pure metadata";
            long writeMetadataStart = startStep(writeMetadataStep);
            try {
                DistributedBinaryGraphSerializer.serialize(runtime, distributedMetadataDirectory);
                completeStep(writeMetadataStep, writeMetadataStart);
            } catch (Exception e) {
                throw mojoException(e, writeMetadataStep, writeMetadataStart, start);
            }
        }
        JavaStandaloneLibraryGenerator generator = JavaStandaloneLibraryGenerator.newGenerator(runtime, CompiledExtensionLoader.extensions(), false, JavaPackageAndImportBuilder.externalizablePackage());
        // Generate Java sources
        Generate generate;
        String generateStep = "Pure compiled mode Java code generation";
        long generateStart = startStep(generateStep);
        try {
            generate = generator.generateOnly(this.generateSources, codegenDirectory);
            completeStep(generateStep, generateStart);
        } catch (Exception e) {
            throw mojoException(e, generateStep, generateStart, start);
        }
        // Set generator and runtime to null so the memory can be cleaned up
        generator = null;
        runtime = null;
        // Compile Java sources
        PureJavaCompiler compiler;
        String compilationStep = "Pure compiled mode Java code compilation";
        long compilationStart = startStep(compilationStep);
        try {
            compiler = JavaStandaloneLibraryGenerator.compileOnly(generate.getJavaSources(), generate.getExternalizableSources(), false);
            completeStep(compilationStep, compilationStart);
        } catch (Exception e) {
            throw mojoException(e, compilationStep, compilationStart, start);
        }
        // Write class files
        String writeClassFilesStep = "writing Pure compiled mode Java classes";
        long writeClassFilesStart = startStep(writeClassFilesStep);
        try {
            compiler.writeClassJavaSources(this.classesDirectory.toPath());
            completeStep(writeClassFilesStep, writeClassFilesStart);
        } catch (Exception e) {
            throw mojoException(e, writeClassFilesStep, writeClassFilesStart, start);
        }
        // Set compiler to null so the memory can be cleaned up
        compiler = null;
        getLog().info(String.format("  Finished building Pure compiled mode jar (%.6fs)", (System.nanoTime() - start) / 1_000_000_000.0));
    } catch (Exception e) {
        throw new MojoExecutionException("error", e);
    }
}
Also used : Arrays(java.util.Arrays) URL(java.net.URL) CodeRepository(org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository) PureRepositoriesExternal(org.finos.legend.pure.configuration.PureRepositoriesExternal) SetIterable(org.eclipse.collections.api.set.SetIterable) Parameter(org.apache.maven.plugins.annotations.Parameter) PureCodeStorage(org.finos.legend.pure.m3.serialization.filesystem.PureCodeStorage) Generate(org.finos.legend.pure.runtime.java.compiled.generation.Generate) Mojo(org.apache.maven.plugins.annotations.Mojo) URLClassLoader(java.net.URLClassLoader) RichIterable(org.eclipse.collections.api.RichIterable) MavenProject(org.apache.maven.project.MavenProject) DistributedBinaryGraphSerializer(org.finos.legend.pure.runtime.java.compiled.serialization.binary.DistributedBinaryGraphSerializer) PureJavaCompiler(org.finos.legend.pure.runtime.java.compiled.compiler.PureJavaCompiler) PureRuntimeBuilder(org.finos.legend.pure.m3.serialization.runtime.PureRuntimeBuilder) JavaStandaloneLibraryGenerator(org.finos.legend.pure.runtime.java.compiled.generation.JavaStandaloneLibraryGenerator) JavaPackageAndImportBuilder(org.finos.legend.pure.runtime.java.compiled.generation.JavaPackageAndImportBuilder) Path(java.nio.file.Path) ArrayIterate(org.eclipse.collections.impl.utility.ArrayIterate) MalformedURLException(java.net.MalformedURLException) PureRuntime(org.finos.legend.pure.m3.serialization.runtime.PureRuntime) Set(java.util.Set) FileInputStream(java.io.FileInputStream) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ClassLoaderCodeStorage(org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.classpath.ClassLoaderCodeStorage) ListAdapter(org.eclipse.collections.impl.list.mutable.ListAdapter) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ClassLoaderPureGraphCache(org.finos.legend.pure.m3.serialization.runtime.cache.ClassLoaderPureGraphCache) CacheState(org.finos.legend.pure.m3.serialization.runtime.cache.CacheState) PureException(org.finos.legend.pure.m4.exception.PureException) Sets(org.eclipse.collections.impl.factory.Sets) CompiledExtensionLoader(org.finos.legend.pure.runtime.java.compiled.extension.CompiledExtensionLoader) GenericCodeRepository(org.finos.legend.pure.m3.serialization.filesystem.repository.GenericCodeRepository) AbstractMojo(org.apache.maven.plugin.AbstractMojo) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) Generate(org.finos.legend.pure.runtime.java.compiled.generation.Generate) URLClassLoader(java.net.URLClassLoader) ClassLoaderCodeStorage(org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.classpath.ClassLoaderCodeStorage) JavaStandaloneLibraryGenerator(org.finos.legend.pure.runtime.java.compiled.generation.JavaStandaloneLibraryGenerator) PureCodeStorage(org.finos.legend.pure.m3.serialization.filesystem.PureCodeStorage) Path(java.nio.file.Path) PureException(org.finos.legend.pure.m4.exception.PureException) PureRuntime(org.finos.legend.pure.m3.serialization.runtime.PureRuntime) PureJavaCompiler(org.finos.legend.pure.runtime.java.compiled.compiler.PureJavaCompiler) CodeRepository(org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository) GenericCodeRepository(org.finos.legend.pure.m3.serialization.filesystem.repository.GenericCodeRepository) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ClassLoaderPureGraphCache(org.finos.legend.pure.m3.serialization.runtime.cache.ClassLoaderPureGraphCache) MojoFailureException(org.apache.maven.plugin.MojoFailureException) CacheState(org.finos.legend.pure.m3.serialization.runtime.cache.CacheState) PureRuntimeBuilder(org.finos.legend.pure.m3.serialization.runtime.PureRuntimeBuilder) MalformedURLException(java.net.MalformedURLException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileNotFoundException(java.io.FileNotFoundException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) PureException(org.finos.legend.pure.m4.exception.PureException) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Aggregations

PureCodeStorage (org.finos.legend.pure.m3.serialization.filesystem.PureCodeStorage)2 CodeRepository (org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository)2 ClassLoaderCodeStorage (org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.classpath.ClassLoaderCodeStorage)2 PureRuntime (org.finos.legend.pure.m3.serialization.runtime.PureRuntime)2 PureRuntimeBuilder (org.finos.legend.pure.m3.serialization.runtime.PureRuntimeBuilder)2 CacheState (org.finos.legend.pure.m3.serialization.runtime.cache.CacheState)2 ClassLoaderPureGraphCache (org.finos.legend.pure.m3.serialization.runtime.cache.ClassLoaderPureGraphCache)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 Path (java.nio.file.Path)1 Arrays (java.util.Arrays)1 Set (java.util.Set)1 AbstractMojo (org.apache.maven.plugin.AbstractMojo)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 Mojo (org.apache.maven.plugins.annotations.Mojo)1