Search in sources :

Example 1 with Idl

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

the class SchemaUtilsTest method testIdlGenerator.

@Test
public void testIdlGenerator() throws IOException, ParseException {
    StringWriter idlStrWriter = new StringWriter();
    Schema rs = SchemaBuilder.builder().record("TestRecord2").namespace("test").doc("record doc").aliases("recordAlias").prop("propKey", "propVal").fields().requiredBoolean("isThere").nullableString("strField", null).name("customField").orderAscending().aliases("blaAlias").prop("fprop", "fval").prop("fprp2", "fval2").type(Schema.createArray(Schema.createEnum("someEnum", "enum doc", "test", Arrays.asList("A", "B")))).withDefault(Collections.EMPTY_LIST).name("fixedField").doc("fixedField doc").type(Schema.createFixed("Myfixed", "fixed doc", "test2", 16)).noDefault().endRecord();
    SchemaUtils.writeIdlProtocol("TestProtocol", "test", idlStrWriter, rs);
    LOG.debug("writer = {}", idlStrWriter);
    String idlStr = idlStrWriter.toString();
    Assert.assertFalse(idlStr.isEmpty());
    Idl idl = new Idl(new StringReader(idlStr));
    idl.setSource("");
    Protocol protocol = idl.CompilationUnit();
    LOG.debug("Protocol = {}", protocol);
    Schema rs2 = protocol.getType("test.TestRecord2");
    Assert.assertEquals(rs, rs2);
}
Also used : Idl(org.apache.avro.compiler.idl.Idl) StringWriter(java.io.StringWriter) Schema(org.apache.avro.Schema) StringReader(java.io.StringReader) Protocol(org.apache.avro.Protocol) Test(org.junit.Test)

Example 2 with Idl

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

the class SchemaCompileMojo method addMvnIdsToIdl.

private File addMvnIdsToIdl(final File idl, final URLClassLoader cl) throws IOException, ParseException {
    if (!addMavenId) {
        return idl;
    }
    String charsetStr = mavenProject.getProperties().getProperty("project.build.sourceEncoding");
    Charset charset = charsetStr == null ? Charset.defaultCharset() : Charset.forName(charsetStr);
    List<String> readAllLines = Files.readAllLines(idl.toPath(), charset);
    Idl parser = new Idl(idl, cl);
    Protocol protocol = parser.CompilationUnit();
    // hack uses the same logic and Idl...
    String idlSource = new File(".").toURI().relativize(idl.toURI()).toString();
    Log log = getLog();
    log.debug("Injecting mvnIds to " + idlSource);
    for (Schema s : protocol.getTypes()) {
        if (s.getProp("mvnId") != null) {
            continue;
        }
        String sourceIdl = s.getProp("sourceIdl");
        if (sourceIdl == null) {
            log.warn("sourceIdl not available, will not attach mvnId for IDLs");
            continue;
        }
        SourceLocation sl = new SourceLocation(sourceIdl);
        if (!idlSource.equals(sl.getFilePath())) {
            continue;
        }
        int zbLineNr = sl.getLineNr() - 1;
        String line = readAllLines.get(zbLineNr);
        String sMvnId = genMnvId(s);
        log.debug("inserting mvnId: " + sMvnId + " at " + sl + " for line \"" + line + "\" schema: " + s.getFullName());
        int zbColNr = sl.getColNr() - 1;
        readAllLines.set(zbLineNr, line.substring(0, zbColNr) + " @mvnId(\"" + sMvnId + "\") " + line.substring(zbColNr, line.length()));
    }
    Path tempIdl = Files.createTempFile(this.target.toPath(), idl.getName(), ".tmp");
    Files.write(tempIdl, readAllLines, charset);
    return tempIdl.toFile();
}
Also used : Idl(org.apache.avro.compiler.idl.Idl) Path(java.nio.file.Path) Log(org.apache.maven.plugin.logging.Log) Schema(org.apache.avro.Schema) Charset(java.nio.charset.Charset) Protocol(org.apache.avro.Protocol) File(java.io.File)

Example 3 with Idl

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

the class SchemaUtilsTest method testIdlGenerator2.

@Test
public void testIdlGenerator2() throws IOException, ParseException {
    StringWriter idlStrWriter = new StringWriter();
    Schema rs = SchemaBuilder.builder().record("TestRecord2").namespace("test").doc("record doc").aliases("recordAlias").prop("propKey", "propVal").fields().requiredBoolean("isThere").nullableString("strField", null).endRecord();
    SchemaUtils.writeIdlProtocol("TestProtocol", "test", idlStrWriter, rs);
    LOG.debug("writer = {}", idlStrWriter);
    String idlStr = idlStrWriter.toString();
    Assert.assertFalse(idlStr.isEmpty());
    Idl idl = new Idl(new StringReader(idlStr));
    idl.setSource("");
    Protocol protocol = idl.CompilationUnit();
    LOG.debug("Protocol = {}", protocol);
    Schema rs2 = protocol.getType("test.TestRecord2");
    Assert.assertEquals(rs, rs2);
}
Also used : Idl(org.apache.avro.compiler.idl.Idl) StringWriter(java.io.StringWriter) Schema(org.apache.avro.Schema) StringReader(java.io.StringReader) Protocol(org.apache.avro.Protocol) Test(org.junit.Test)

Example 4 with Idl

use of org.apache.avro.compiler.idl.Idl 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

Protocol (org.apache.avro.Protocol)4 Idl (org.apache.avro.compiler.idl.Idl)4 Schema (org.apache.avro.Schema)3 File (java.io.File)2 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 Charset (java.nio.charset.Charset)1 Path (java.nio.file.Path)1 ParseException (org.apache.avro.compiler.idl.ParseException)1 SpecificCompiler (org.apache.avro.compiler.specific.SpecificCompiler)1 DependencyResolutionRequiredException (org.apache.maven.artifact.DependencyResolutionRequiredException)1 Log (org.apache.maven.plugin.logging.Log)1