Skip to content

Commit 3888345

Browse files
committed
Some post-merge refactoring for #87
1 parent e1a976f commit 3888345

File tree

4 files changed

+191
-110
lines changed

4 files changed

+191
-110
lines changed

protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufMapper.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package com.fasterxml.jackson.dataformat.protobuf;
22

3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.net.URL;
7+
38
import com.fasterxml.jackson.core.Version;
49
import com.fasterxml.jackson.databind.JavaType;
510
import com.fasterxml.jackson.databind.JsonMappingException;
611
import com.fasterxml.jackson.databind.ObjectMapper;
12+
import com.fasterxml.jackson.dataformat.protobuf.schema.DescriptorLoader;
13+
import com.fasterxml.jackson.dataformat.protobuf.schema.FileDescriptorSet;
714
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema;
815
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchemaLoader;
916
import com.fasterxml.jackson.dataformat.protobuf.schemagen.ProtobufSchemaGenerator;
@@ -14,6 +21,14 @@ public class ProtobufMapper extends ObjectMapper
1421

1522
protected ProtobufSchemaLoader _schemaLoader = ProtobufSchemaLoader.std;
1623

24+
/**
25+
* Lazily constructed instance of {@link DescriptorLoader}, used for loading
26+
* structured protoc definitions from multiple files.
27+
*
28+
* @since 2.9
29+
*/
30+
protected DescriptorLoader _descriptorLoader;
31+
1732
/*
1833
/**********************************************************
1934
/* Life-cycle
@@ -51,7 +66,7 @@ public ProtobufFactory getFactory() {
5166

5267
/*
5368
/**********************************************************
54-
/* Schema access
69+
/* Schema access, single protoc source
5570
/**********************************************************
5671
*/
5772

@@ -95,4 +110,46 @@ public ProtobufSchema generateSchemaFor(Class<?> type) throws JsonMappingExcepti
95110
acceptJsonFormatVisitor(type, gen);
96111
return gen.getGeneratedSchema();
97112
}
113+
114+
/*
115+
/**********************************************************
116+
/* Schema access, FileDescriptorSets (since 2.9)
117+
/**********************************************************
118+
*/
119+
120+
/**
121+
* @since 2.9
122+
*/
123+
public FileDescriptorSet loadDescriptorSet(URL src) throws IOException {
124+
return descriptorLoader().load(src);
125+
}
126+
127+
/**
128+
* @since 2.9
129+
*/
130+
public FileDescriptorSet loadDescriptorSet(File src) throws IOException {
131+
return descriptorLoader().load(src);
132+
}
133+
134+
/**
135+
* @since 2.9
136+
*/
137+
public FileDescriptorSet loadDescriptorSet(InputStream src) throws IOException {
138+
return descriptorLoader().load(src);
139+
}
140+
141+
/**
142+
* Accessors that may be used instead of convenience <code>loadDescriptorSet</code>
143+
* methods, if alternate sources need to be used.
144+
*
145+
* @since 2.9
146+
*/
147+
public synchronized DescriptorLoader descriptorLoader() throws IOException
148+
{
149+
DescriptorLoader l = _descriptorLoader;
150+
if (l == null) {
151+
_descriptorLoader = l = DescriptorLoader.construct(this);
152+
}
153+
return l;
154+
}
98155
}
Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,72 @@
11
package com.fasterxml.jackson.dataformat.protobuf.schema;
22

3-
4-
import com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper;
5-
6-
import java.io.ByteArrayInputStream;
73
import java.io.File;
8-
import java.io.FileInputStream;
94
import java.io.IOException;
105
import java.io.InputStream;
6+
import java.io.Reader;
117
import java.net.URL;
128

9+
import com.fasterxml.jackson.databind.ObjectReader;
10+
import com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper;
11+
1312
/**
1413
* Class used for loading protobuf descriptors (from .desc files
1514
* or equivalent sources), to construct FileDescriptorSet.
15+
*
16+
* @since 2.9
1617
*/
1718
public class DescriptorLoader
1819
{
19-
private final String DESCRIPTOR_PROTO = "/descriptor.proto";
20-
private ProtobufMapper descriptorMapper;
21-
private ProtobufSchema descriptorFileSchema;
20+
protected final static String DESCRIPTOR_PROTO = "/descriptor.proto";
2221

2322
/**
24-
* Standard loader instance that is usually used for loading descriptor file.
23+
* Fully configured reader for {@link FileDescriptorSet} objects.
2524
*/
26-
public final static DescriptorLoader std = new DescriptorLoader();
27-
28-
public DescriptorLoader() {}
25+
protected final ObjectReader _reader;
2926

30-
31-
/**
32-
* Public API
33-
*/
34-
35-
public FileDescriptorSet load(URL url) throws IOException
36-
{
37-
return _loadFileDescriptorSet(url.openStream());
27+
public DescriptorLoader(ObjectReader reader) {
28+
_reader = reader;
3829
}
3930

40-
public FileDescriptorSet load(File f) throws IOException
31+
public static DescriptorLoader construct(ProtobufMapper mapper) throws IOException
4132
{
42-
return _loadFileDescriptorSet(new FileInputStream(f));
33+
ProtobufSchema schema;
34+
try (InputStream in = DescriptorLoader.class.getResourceAsStream(DESCRIPTOR_PROTO)) {
35+
schema = mapper.schemaLoader().load(in, "FileDescriptorSet");
36+
}
37+
return new DescriptorLoader(mapper.readerFor(FileDescriptorSet.class)
38+
.with(schema));
4339
}
4440

45-
public FileDescriptorSet load(InputStream in) throws IOException
41+
/*
42+
/**********************************************************************
43+
/* Public API
44+
/**********************************************************************
45+
*/
46+
47+
public FileDescriptorSet load(URL src) throws IOException
4648
{
47-
return _loadFileDescriptorSet(in);
49+
return _reader.readValue(src);
4850
}
4951

50-
public FileDescriptorSet fromBytes(byte[] descriptorBytes) throws IOException
52+
public FileDescriptorSet load(File src) throws IOException
5153
{
52-
return _loadFileDescriptorSet(new ByteArrayInputStream(descriptorBytes));
54+
return _reader.readValue(src);
5355
}
5456

55-
protected FileDescriptorSet _loadFileDescriptorSet(InputStream in) throws IOException
57+
/**
58+
* Note: passed {@link java.io.InputStream} will be closed by this method.
59+
*/
60+
public FileDescriptorSet load(InputStream in) throws IOException
5661
{
57-
try {
58-
if (descriptorMapper == null) {
59-
createDescriptorMapper();
60-
}
61-
return descriptorMapper.readerFor(FileDescriptorSet.class)
62-
.with(descriptorFileSchema)
63-
.readValue(in);
64-
}
65-
finally {
66-
try {
67-
in.close();
68-
}
69-
catch (IOException e) {
70-
}
71-
}
62+
return _reader.readValue(in);
7263
}
7364

74-
private void createDescriptorMapper() throws IOException
65+
/**
66+
* Note: passed {@link java.io.Reader} will be closed by this method.
67+
*/
68+
public FileDescriptorSet load(Reader r) throws IOException
7569
{
76-
// read Descriptor Proto
77-
descriptorMapper = new ProtobufMapper();
78-
InputStream in = getClass().getResourceAsStream(DESCRIPTOR_PROTO);
79-
descriptorFileSchema = ProtobufSchemaLoader.std.load(in, "FileDescriptorSet");
80-
in.close();
70+
return _reader.readValue(r);
8171
}
8272
}

0 commit comments

Comments
 (0)