Search in sources :

Example 1 with SetFilesReadOnlyVisitor

use of org.spf4j.io.SetFilesReadOnlyVisitor 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)

Aggregations

IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 Protocol (org.apache.avro.Protocol)1 SpecificCompiler (org.apache.avro.compiler.specific.SpecificCompiler)1 SetFilesReadOnlyVisitor (org.spf4j.io.SetFilesReadOnlyVisitor)1