Skip to content

Commit c618360

Browse files
committed
use POJOs instead of JsonNode for the descriptor PB
- rename DescriptorReader to DescriptorLoader - return ProtobufSchema instead of NativeProtobufSchema - find the main .proto file by message type. - remove "main .proto file" concept. - introduce FileDescriptorSet POJO
1 parent 75ec330 commit c618360

File tree

6 files changed

+608
-226
lines changed

6 files changed

+608
-226
lines changed

protobuf/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ abstractions.
4747
<artifactId>jackson-annotations</artifactId>
4848
<scope>test</scope>
4949
</dependency>
50+
<dependency>
51+
<groupId>com.fasterxml.jackson.core</groupId>
52+
<artifactId>jackson-annotations</artifactId>
53+
</dependency>
5054
</dependencies>
5155

5256
<build>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.fasterxml.jackson.dataformat.protobuf.schema;
2+
3+
4+
import com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper;
5+
import com.squareup.protoparser.ProtoFile;
6+
7+
import java.io.ByteArrayInputStream;
8+
import java.io.File;
9+
import java.io.FileInputStream;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.net.URL;
13+
import java.util.Map;
14+
15+
/**
16+
* Class used for loading protobuf descriptors (from .desc files
17+
* or equivalent sources), to construct native schema.
18+
* <p>
19+
* Note that proto name argument is optional if (and only if) desired
20+
* root file is the first file in definition; otherwise an
21+
* exception will be thrown.
22+
*/
23+
public class DescriptorLoader
24+
{
25+
private final String DESCRIPTOR_PROTO = "/descriptor.proto";
26+
private ProtobufMapper descriptorMapper;
27+
private ProtobufSchema descriptorFileSchema;
28+
private Map<String, ProtoFile> protoFileMap;
29+
30+
/**
31+
* Standard loader instance that is usually used for loading descriptor file.
32+
*/
33+
public final static DescriptorLoader std = new DescriptorLoader();
34+
35+
public DescriptorLoader() {}
36+
37+
38+
/**
39+
* Public API
40+
*/
41+
42+
public FileDescriptorSet load(URL url) throws IOException
43+
{
44+
return _loadFileDescriptorSet(url.openStream());
45+
}
46+
47+
public FileDescriptorSet load(File f) throws IOException
48+
{
49+
return _loadFileDescriptorSet(new FileInputStream(f));
50+
}
51+
52+
public FileDescriptorSet load(InputStream in) throws IOException
53+
{
54+
return _loadFileDescriptorSet(in);
55+
}
56+
57+
public FileDescriptorSet fromBytes(byte[] descriptorBytes) throws IOException
58+
{
59+
return _loadFileDescriptorSet(new ByteArrayInputStream(descriptorBytes));
60+
}
61+
62+
protected FileDescriptorSet _loadFileDescriptorSet(InputStream in) throws IOException
63+
{
64+
try {
65+
if (descriptorMapper == null) {
66+
createDescriptorMapper();
67+
}
68+
return descriptorMapper.readerFor(FileDescriptorSet.class)
69+
.with(descriptorFileSchema)
70+
.readValue(in);
71+
}
72+
finally {
73+
try {
74+
in.close();
75+
}
76+
catch (IOException e) {
77+
}
78+
}
79+
}
80+
81+
private void createDescriptorMapper() throws IOException
82+
{
83+
// read Descriptor Proto
84+
descriptorMapper = new ProtobufMapper();
85+
InputStream in = getClass().getResourceAsStream(DESCRIPTOR_PROTO);
86+
descriptorFileSchema = ProtobufSchemaLoader.std.load(in, "FileDescriptorSet");
87+
in.close();
88+
}
89+
}

protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schema/DescriptorReader.java

Lines changed: 0 additions & 200 deletions
This file was deleted.

0 commit comments

Comments
 (0)