Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add descriptor loader #87

Merged
merged 10 commits into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions protobuf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ abstractions.
<artifactId>jackson-annotations</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.fasterxml.jackson.dataformat.protobuf.schema;


import com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
* Class used for loading protobuf descriptors (from .desc files
* or equivalent sources), to construct FileDescriptorSet.
*/
public class DescriptorLoader
{
private final String DESCRIPTOR_PROTO = "/descriptor.proto";
private ProtobufMapper descriptorMapper;
private ProtobufSchema descriptorFileSchema;

/**
* Standard loader instance that is usually used for loading descriptor file.
*/
public final static DescriptorLoader std = new DescriptorLoader();

public DescriptorLoader() {}


/**
* Public API
*/

public FileDescriptorSet load(URL url) throws IOException
{
return _loadFileDescriptorSet(url.openStream());
}

public FileDescriptorSet load(File f) throws IOException
{
return _loadFileDescriptorSet(new FileInputStream(f));
}

public FileDescriptorSet load(InputStream in) throws IOException
{
return _loadFileDescriptorSet(in);
}

public FileDescriptorSet fromBytes(byte[] descriptorBytes) throws IOException
{
return _loadFileDescriptorSet(new ByteArrayInputStream(descriptorBytes));
}

protected FileDescriptorSet _loadFileDescriptorSet(InputStream in) throws IOException
{
try {
if (descriptorMapper == null) {
createDescriptorMapper();
}
return descriptorMapper.readerFor(FileDescriptorSet.class)
.with(descriptorFileSchema)
.readValue(in);
}
finally {
try {
in.close();
}
catch (IOException e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why catch? Shouldn't it just be propagated as-is?

}
}
}

private void createDescriptorMapper() throws IOException
{
// read Descriptor Proto
descriptorMapper = new ProtobufMapper();
InputStream in = getClass().getResourceAsStream(DESCRIPTOR_PROTO);
descriptorFileSchema = ProtobufSchemaLoader.std.load(in, "FileDescriptorSet");
in.close();
}
}
Loading