Search in sources :

Example 1 with SpecificCompiler

use of org.apache.avro.compiler.specific.SpecificCompiler in project spf4j by zolyfarkas.

the class GenericRecordBuilder method generateClasses.

private void generateClasses(final GenericData.StringType st, final Schema... schemas) {
    String[] namespaces = new String[schemas.length];
    for (int i = 0; i < schemas.length; i++) {
        String namespace = schemas[i].getNamespace();
        if (namespace == null) {
            namespace = "";
        }
        namespaces[i] = namespace;
    }
    String commonPrefix = org.spf4j.base.Strings.commonPrefix(namespaces);
    if (commonPrefix.endsWith(".")) {
        commonPrefix = commonPrefix.substring(0, commonPrefix.length() - 1);
    }
    Protocol proto = new Protocol("generated", commonPrefix);
    proto.setTypes(Arrays.asList(schemas));
    SpecificCompiler sc = new SpecificCompiler(proto);
    sc.setStringType(st);
    // use a custom template that does not contain the builder (janino can't compile builder).
    sc.setTemplateDir("org/spf4j/avro/");
    sc.setFieldVisibility(SpecificCompiler.FieldVisibility.PRIVATE);
    try {
        sc.compileToDestination(null, tmpDir);
    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
    try {
        Files.walkFileTree(tmpDir.toPath(), new SetFilesReadOnlyVisitor());
    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
}
Also used : SpecificCompiler(org.apache.avro.compiler.specific.SpecificCompiler) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) SetFilesReadOnlyVisitor(org.spf4j.io.SetFilesReadOnlyVisitor) Protocol(org.apache.avro.Protocol)

Example 2 with SpecificCompiler

use of org.apache.avro.compiler.specific.SpecificCompiler in project spf4j by zolyfarkas.

the class SchemaCompileMojo method doCompileProtocol.

protected void doCompileProtocol(final String filename, final Path destination) throws IOException {
    File src = new File(sourceDirectory, filename);
    Protocol protocol = Protocol.parse(src);
    publishSchemasAndAttachMvnIdToProtocol(protocol, addMavenId, useSchemaReferencesForAvsc);
    SpecificCompiler compiler = new SpecificCompiler(protocol);
    compiler.setOutputCharacterEncoding(mavenProject.getProperties().getProperty("project.build.sourceEncoding"));
    compiler.setTemplateDir(templateDirectory);
    compiler.setStringType(GenericData.StringType.valueOf(stringType));
    compiler.setFieldVisibility(SpecificCompiler.FieldVisibility.valueOf(fieldVisibility));
    compiler.setCreateSetters(createSetters);
    compiler.compileToDestination(src, generatedJavaTarget);
    Files.write(destination, protocol.toString(true).getBytes(StandardCharsets.UTF_8));
}
Also used : SpecificCompiler(org.apache.avro.compiler.specific.SpecificCompiler) Protocol(org.apache.avro.Protocol) File(java.io.File)

Example 3 with SpecificCompiler

use of org.apache.avro.compiler.specific.SpecificCompiler in project spf4j by zolyfarkas.

the class SchemaCompileMojo method writeSchemaToTarget.

private void writeSchemaToTarget(final Schema schema, final File src) throws IOException {
    String targetName = schema.getFullName().replace('.', File.separatorChar) + ".avsc";
    Path destination = generatedAvscTarget.toPath().resolve(targetName);
    Path parent = destination.getParent();
    if (parent != null) {
        Files.createDirectories(parent);
    }
    Files.write(destination, schema.toString().getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    SpecificCompiler compiler = new SpecificCompiler(schema);
    compiler.setOutputCharacterEncoding(mavenProject.getProperties().getProperty("project.build.sourceEncoding"));
    compiler.setTemplateDir(templateDirectory);
    compiler.setStringType(GenericData.StringType.valueOf(stringType));
    compiler.setFieldVisibility(SpecificCompiler.FieldVisibility.valueOf(fieldVisibility));
    compiler.setCreateSetters(createSetters);
    compiler.compileToDestination(src, generatedJavaTarget);
}
Also used : Path(java.nio.file.Path) SpecificCompiler(org.apache.avro.compiler.specific.SpecificCompiler)

Example 4 with SpecificCompiler

use of org.apache.avro.compiler.specific.SpecificCompiler 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

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