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