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);
}
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();
}
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);
}
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);
}
}
Aggregations