Search in sources :

Example 1 with SchemaResolver

use of org.apache.avro.SchemaResolver in project spf4j by zolyfarkas.

the class SchemaMojoBase method execute.

/**
 * Children must call this.
 * @throws MojoExecutionException
 * @throws MojoFailureException
 */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (useSchemaReferencesForAvsc && SchemaRefWriter.isSchemaRefsSupported()) {
        Map<String, String> currentMappings;
        try {
            currentMappings = idx2Name();
        } catch (IOException ex) {
            throw new MojoExecutionException("Exception loading schema index: " + generatedAvscTarget.toPath().resolve(SCHEMA_INDEX_FILENAME), ex);
        }
        MavenSchemaResolver res = new MavenSchemaResolver(repoSystem, getMavenSession().getRepositorySession(), mavenProject.getRemoteProjectRepositories(), schemaArtifactClassifier, schemaArtifactExtension);
        SchemaResolvers.registerDefault(new SchemaResolver() {

            @Override
            public Schema resolveSchema(final String id) {
                SchemaRef ref = new SchemaRef(id);
                if (mavenProject.getGroupId().equals(ref.getGroupId()) && mavenProject.getArtifactId().equals(ref.getArtifactId()) && mavenProject.getVersion().equals(ref.getVersion())) {
                    String name = currentMappings.get(ref.getRef());
                    Path schemaPath = generatedAvscTarget.toPath().resolve(name.replace('.', '/') + ".avsc");
                    try (BufferedInputStream bis = new BufferedInputStream(Files.newInputStream(schemaPath))) {
                        return new Schema.Parser().parse(bis);
                    } catch (IOException ex) {
                        throw new UncheckedIOException(ex);
                    }
                } else {
                    return res.resolveSchema(id);
                }
            }

            @Override
            public String getId(final Schema schema) {
                return res.getId(schema);
            }
        });
    }
}
Also used : Path(java.nio.file.Path) SchemaRef(org.spf4j.avro.SchemaRef) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenSchemaResolver(org.spf4j.maven.MavenSchemaResolver) Schema(org.apache.avro.Schema) MavenSchemaResolver(org.spf4j.maven.MavenSchemaResolver) SchemaResolver(org.apache.avro.SchemaResolver) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) BufferedInputStream(java.io.BufferedInputStream)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 Path (java.nio.file.Path)1 Schema (org.apache.avro.Schema)1 SchemaResolver (org.apache.avro.SchemaResolver)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 SchemaRef (org.spf4j.avro.SchemaRef)1 MavenSchemaResolver (org.spf4j.maven.MavenSchemaResolver)1