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