Search in sources :

Example 1 with SchemaRef

use of org.spf4j.avro.SchemaRef in project spf4j by zolyfarkas.

the class MavenSchemaResolver method resolveSchema.

@Override
@SuppressFBWarnings("PCAIL_POSSIBLE_CONSTANT_ALLOCATION_IN_LOOP")
public Schema resolveSchema(final String id) {
    SchemaRef ref = new SchemaRef(id);
    try {
        File artifact = MavenRepositoryUtils.resolveArtifact(ref.getGroupId(), ref.getArtifactId(), classifier, extension, ref.getVersion(), remotes, repoSystem, repoSystemSession);
        URI zipUri = URI.create("jar:" + artifact.toURI().toURL());
        FileSystem zipFs;
        synchronized (zipUri.toString().intern()) {
            // newFileSystem fails if already one there...
            try {
                zipFs = FileSystems.newFileSystem(zipUri, Collections.emptyMap());
            } catch (FileSystemAlreadyExistsException ex) {
                zipFs = FileSystems.getFileSystem(zipUri);
            } catch (ZipError ze) {
                throw new AvroRuntimeException("Cannot resolve " + id, ze);
            }
        }
        for (Path root : zipFs.getRootDirectories()) {
            Path index = root.resolve("schema_index.properties");
            if (Files.exists(index)) {
                Properties prop = new Properties();
                try (BufferedReader indexReader = Files.newBufferedReader(index)) {
                    prop.load(indexReader);
                }
                String schemaName = prop.getProperty(ref.getRef());
                if (schemaName == null) {
                    throw new AvroRuntimeException("unable to resolve schema: " + id + " missing from index " + index);
                }
                Path schemaPath = root.resolve(schemaName.replace('.', '/') + ".avsc");
                try (BufferedInputStream bis = new BufferedInputStream(Files.newInputStream(schemaPath))) {
                    return new Schema.Parser().parse(bis);
                }
            }
        }
        throw new IOException("unable to resolve schema: " + id);
    } catch (ArtifactResolutionException | IOException ex) {
        throw new AvroRuntimeException("Cannot resolve " + id, ex);
    }
}
Also used : Path(java.nio.file.Path) SchemaRef(org.spf4j.avro.SchemaRef) Schema(org.apache.avro.Schema) AvroRuntimeException(org.apache.avro.AvroRuntimeException) IOException(java.io.IOException) Properties(java.util.Properties) URI(java.net.URI) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) BufferedInputStream(java.io.BufferedInputStream) FileSystem(java.nio.file.FileSystem) BufferedReader(java.io.BufferedReader) FileSystemAlreadyExistsException(java.nio.file.FileSystemAlreadyExistsException) File(java.io.File) ZipError(java.util.zip.ZipError) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with SchemaRef

use of org.spf4j.avro.SchemaRef 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)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Schema (org.apache.avro.Schema)2 SchemaRef (org.spf4j.avro.SchemaRef)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 UncheckedIOException (java.io.UncheckedIOException)1 URI (java.net.URI)1 FileSystem (java.nio.file.FileSystem)1 FileSystemAlreadyExistsException (java.nio.file.FileSystemAlreadyExistsException)1 Properties (java.util.Properties)1 ZipError (java.util.zip.ZipError)1 AvroRuntimeException (org.apache.avro.AvroRuntimeException)1 SchemaResolver (org.apache.avro.SchemaResolver)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)1 MavenSchemaResolver (org.spf4j.maven.MavenSchemaResolver)1