Search in sources :

Example 1 with ParseException

use of org.apache.avro.compiler.idl.ParseException in project spf4j by zolyfarkas.

the class SchemaCompileMojo method addMvnIdToIdlsAndMoveToDestination.

public void addMvnIdToIdlsAndMoveToDestination(final Path destPath) throws MojoExecutionException {
    Thread currentThread = Thread.currentThread();
    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
    try {
        List<URL> runtimeUrls = createPathUrls(this.sourceDirectory);
        getLog().info("Compile classpath: " + runtimeUrls);
        URLClassLoader projPathLoader = AccessController.doPrivileged((PrivilegedAction<URLClassLoader>) () -> new URLClassLoader(runtimeUrls.toArray(new URL[runtimeUrls.size()]), contextClassLoader));
        currentThread.setContextClassLoader(projPathLoader);
        for (String file : getSourceFiles("**/*.avdl")) {
            Path destination = destPath.resolve(file);
            Path parent = destination.getParent();
            if (parent != null) {
                Files.createDirectories(parent);
            }
            File idlFile = new File(sourceDirectory, file);
            try {
                idlFile = addMvnIdsToIdl(idlFile, projPathLoader);
            } catch (ParseException | IOException | RuntimeException ex) {
                throw new MojoExecutionException("cannot add mvnId to  IDL " + idlFile + ", " + ex.getMessage(), ex);
            }
            Files.copy(idlFile.toPath(), destination, StandardCopyOption.REPLACE_EXISTING);
        }
    } catch (IOException | DependencyResolutionRequiredException ex) {
        throw new MojoExecutionException("cannot add mvnId to  IDL " + this, ex);
    } finally {
        currentThread.setContextClassLoader(contextClassLoader);
    }
}
Also used : Path(java.nio.file.Path) DependencyResolutionRequiredException(org.apache.maven.artifact.DependencyResolutionRequiredException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) ParseException(org.apache.avro.compiler.idl.ParseException) File(java.io.File)

Example 2 with ParseException

use of org.apache.avro.compiler.idl.ParseException in project spf4j by zolyfarkas.

the class SchemaCompileMojo method doCompileIDL.

protected void doCompileIDL(final File sourceDir, final String filename) throws IOException {
    Thread currentThread = Thread.currentThread();
    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
    try {
        List<URL> runtimeUrls = createPathUrls(sourceDir);
        getLog().info("IDL Compile classpath: " + runtimeUrls);
        URLClassLoader projPathLoader = AccessController.doPrivileged((PrivilegedAction<URLClassLoader>) () -> new URLClassLoader(runtimeUrls.toArray(new URL[runtimeUrls.size()]), contextClassLoader));
        currentThread.setContextClassLoader(projPathLoader);
        File file = new File(sourceDir, filename);
        String sourceAbsolutePath = sourceDir.getAbsolutePath();
        // set the current dir do that sourceIdl will be computed relative to it.
        // This makes this plugin not thread safe.
        Idl parser;
        String origCurrentDir = org.spf4j.base.Runtime.getCurrentDir();
        org.spf4j.base.Runtime.setCurrentDir(sourceAbsolutePath);
        try {
            parser = new Idl(file, projPathLoader);
        } finally {
            org.spf4j.base.Runtime.setCurrentDir(origCurrentDir);
        }
        Protocol protocol = parser.CompilationUnit();
        publishSchemasAndAttachMvnIdToProtocol(protocol, false, useSchemaReferencesForAvsc);
        SpecificCompiler compiler = new SpecificCompiler(protocol);
        compiler.setOutputCharacterEncoding(mavenProject.getProperties().getProperty("project.build.sourceEncoding"));
        compiler.setStringType(GenericData.StringType.valueOf(stringType));
        compiler.setTemplateDir(templateDirectory);
        compiler.setFieldVisibility(SpecificCompiler.FieldVisibility.valueOf(fieldVisibility));
        compiler.setCreateSetters(createSetters);
        compiler.compileToDestination(null, generatedJavaTarget);
    } catch (ParseException e) {
        throw new IOException(e);
    } catch (DependencyResolutionRequiredException drre) {
        throw new IOException(drre);
    } finally {
        currentThread.setContextClassLoader(contextClassLoader);
    }
}
Also used : DependencyResolutionRequiredException(org.apache.maven.artifact.DependencyResolutionRequiredException) IOException(java.io.IOException) URL(java.net.URL) Idl(org.apache.avro.compiler.idl.Idl) URLClassLoader(java.net.URLClassLoader) SpecificCompiler(org.apache.avro.compiler.specific.SpecificCompiler) URLClassLoader(java.net.URLClassLoader) ParseException(org.apache.avro.compiler.idl.ParseException) Protocol(org.apache.avro.Protocol) File(java.io.File)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 URL (java.net.URL)2 URLClassLoader (java.net.URLClassLoader)2 ParseException (org.apache.avro.compiler.idl.ParseException)2 DependencyResolutionRequiredException (org.apache.maven.artifact.DependencyResolutionRequiredException)2 Path (java.nio.file.Path)1 Protocol (org.apache.avro.Protocol)1 Idl (org.apache.avro.compiler.idl.Idl)1 SpecificCompiler (org.apache.avro.compiler.specific.SpecificCompiler)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1