/
file_server_wrapper.go
291 lines (271 loc) · 11.6 KB
/
file_server_wrapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package java
import (
"bytes"
"log"
"path"
"strings"
"v.io/x/ref/lib/vdl/compile"
"v.io/x/ref/lib/vdl/vdlutil"
)
const serverWrapperTmpl = header + `
// Source(s): {{ .Source }}
package {{ .PackagePath }};
/**
* Wrapper for {@link {{ .ServiceName }}Server}. This wrapper is used by
* {@link io.v.v23.rpc.ReflectInvoker} to indirectly invoke server methods.
*/
public final class {{ .ServiceName }}ServerWrapper {
private final {{ .FullServiceName }}Server server;
{{/* Define fields to hold each of the embedded server wrappers*/}}
{{ range $embed := .Embeds }}
{{/* e.g. private final com.somepackage.gen_impl.ArithStub stubArith; */}}
private final {{ $embed.FullName }}ServerWrapper wrapper{{ $embed.Name }};
{{ end }}
/**
* Creates a new {@link {{ .ServiceName }}ServerWrapper} to invoke the methods of the
* provided server.
*
* @param server server whose methods are to be invoked
*/
public {{ .ServiceName }}ServerWrapper({{ .FullServiceName }}Server server) {
this.server = server;
{{/* Initialize the embedded server wrappers */}}
{{ range $embed := .Embeds }}
this.wrapper{{ $embed.Name }} = new {{ $embed.FullName }}ServerWrapper(server);
{{ end }}
}
/**
* Returns a description of this server.
*/
public io.v.v23.vdlroot.signature.Interface signature() {
java.util.List<io.v.v23.vdlroot.signature.Embed> embeds = new java.util.ArrayList<io.v.v23.vdlroot.signature.Embed>();
java.util.List<io.v.v23.vdlroot.signature.Method> methods = new java.util.ArrayList<io.v.v23.vdlroot.signature.Method>();
{{ range $method := .Methods }}
{
java.util.List<io.v.v23.vdlroot.signature.Arg> inArgs = new java.util.ArrayList<io.v.v23.vdlroot.signature.Arg>();
{{ range $arg := $method.CallingArgTypes }}
inArgs.add(new io.v.v23.vdlroot.signature.Arg("", "", new io.v.v23.vdl.VdlTypeObject({{ $arg }})));
{{ end }}
java.util.List<io.v.v23.vdlroot.signature.Arg> outArgs = new java.util.ArrayList<io.v.v23.vdlroot.signature.Arg>();
{{ range $arg := $method.RetJavaTypes }}
outArgs.add(new io.v.v23.vdlroot.signature.Arg("", "", new io.v.v23.vdl.VdlTypeObject({{ $arg }})));
{{ end }}
java.util.List<io.v.v23.vdl.VdlAny> tags = new java.util.ArrayList<io.v.v23.vdl.VdlAny>();
{{ range $tag := .Tags }}
tags.add(new io.v.v23.vdl.VdlAny(io.v.v23.vdl.VdlValue.valueOf({{ $tag.Value }}, {{ $tag.Type }})));
{{ end }}
methods.add(new io.v.v23.vdlroot.signature.Method(
"{{ $method.Name }}",
"{{ $method.Doc }}",
inArgs,
outArgs,
null,
null,
tags));
}
{{ end }}
return new io.v.v23.vdlroot.signature.Interface("{{ .ServiceName }}", "{{ .PackagePath }}", "{{ .Doc }}", embeds, methods);
}
/**
* Returns all tags associated with the provided method or {@code null} if the method isn't
* implemented by this server.
*
* @param method method whose tags are to be returned
*/
@SuppressWarnings("unused")
public io.v.v23.vdl.VdlValue[] getMethodTags(java.lang.String method) throws io.v.v23.verror.VException {
{{ range $methodName, $tags := .MethodTags }}
if ("{{ $methodName }}".equals(method)) {
try {
return new io.v.v23.vdl.VdlValue[] {
{{ range $tag := $tags }} io.v.v23.vdl.VdlValue.valueOf({{ $tag.Value }}, {{ $tag.Type }}), {{ end }}
};
} catch (IllegalArgumentException e) {
throw new io.v.v23.verror.VException(String.format("Couldn't get tags for method \"{{ $methodName }}\": %s", e.getMessage()));
}
}
{{ end }}
{{ range $embed := .Embeds }}
{
io.v.v23.vdl.VdlValue[] tags = this.wrapper{{ $embed.Name }}.getMethodTags(method);
if (tags != null) {
return tags;
}
}
{{ end }}
return null; // method not found
}
{{/* Iterate over methods defined directly in the body of this server */}}
{{ range $method := .Methods }}
{{ $method.JavaDoc }}
public com.google.common.util.concurrent.ListenableFuture<{{ $method.RetType }}> {{ $method.Name }}(io.v.v23.context.VContext _ctx, final io.v.v23.rpc.StreamServerCall _call{{ $method.DeclarationArgs }}) {
{{ if $method.IsStreaming }}
io.v.v23.vdl.ServerStream<{{ $method.SendType }}, {{ $method.RecvType }}> _stream = new io.v.v23.vdl.ServerStream<{{ $method.SendType }}, {{ $method.RecvType }}>() {
@Override
public com.google.common.util.concurrent.ListenableFuture<Void> send({{ $method.SendType }} _item) {
java.lang.reflect.Type _type = {{ $method.StreamSendReflectType }};
return _call.send(_item, _type);
}
@Override
public com.google.common.util.concurrent.ListenableFuture<{{ $method.RecvType }}> recv() {
java.lang.reflect.Type _type = {{ $method.StreamRecvReflectType }};
return com.google.common.util.concurrent.Futures.transform(_call.recv(_type), new com.google.common.base.Function<Object, {{ $method.RecvType }}>() {
@Override
public {{ $method.RecvType }} apply(Object result) {
return ({{ $method.RecvType }}) result;
}
});
}
};
{{ end }} {{/* end if $method.IsStreaming */}}
return this.server.{{ $method.Name }}(_ctx, _call {{ $method.CallingArgs }} {{ if $method.IsStreaming }} ,_stream {{ end }} );
}
{{end}}
{{/* Iterate over methods from embedded servers and generate code to delegate the work */}}
{{ range $eMethod := .EmbedMethods }}
{{ $eMethod.JavaDoc }}
public com.google.common.util.concurrent.ListenableFuture<{{ $eMethod.RetType }}> {{ $eMethod.Name }}(io.v.v23.context.VContext ctx, io.v.v23.rpc.StreamServerCall call{{ $eMethod.DeclarationArgs }}) throws io.v.v23.verror.VException {
{{/* e.g. return this.stubArith.cosine(ctx, call, [args], options) */}}
return this.wrapper{{ $eMethod.IfaceName }}.{{ $eMethod.Name }}(ctx, call{{ $eMethod.CallingArgs }});
}
{{ end }} {{/* end range .EmbedMethods */}}
}
`
type serverWrapperMethod struct {
CallingArgs string
CallingArgTypes []string
DeclarationArgs string
Doc string
IsStreaming bool
JavaDoc string
Name string
RecvType string
RetType string
RetJavaTypes []string
SendType string
StreamRecvReflectType string
StreamSendReflectType string
Tags []methodTag
}
type serverWrapperEmbedMethod struct {
CallingArgs string
DeclarationArgs string
Doc string
IfaceName string
JavaDoc string
Name string
RetType string
}
type serverWrapperEmbed struct {
Name string
FullName string
}
type methodTag struct {
Value string
Type string
}
// TODO(sjr): move this to somewhere in util_*.
func toJavaString(goString string) string {
result := strings.ReplaceAll(goString, "\"", "\\\"")
result = strings.ReplaceAll(result, "\n", "\" + \n\"")
return result
}
func processServerWrapperMethod(iface *compile.Interface, method *compile.Method, env *compile.Env, tags []methodTag) serverWrapperMethod {
callArgTypes := make([]string, len(method.InArgs))
for i, arg := range method.InArgs {
callArgTypes[i] = javaReflectType(arg.Type, env)
}
retArgTypes := make([]string, len(method.OutArgs))
for i, arg := range method.OutArgs {
retArgTypes[i] = javaReflectType(arg.Type, env)
}
return serverWrapperMethod{
CallingArgs: javaCallingArgStr(method.InArgs, true),
CallingArgTypes: callArgTypes,
DeclarationArgs: javaDeclarationArgStr(method.InArgs, env, true),
Doc: toJavaString(method.Doc),
IsStreaming: isStreamingMethod(method),
JavaDoc: javaDoc(method.Doc, method.DocSuffix),
Name: vdlutil.FirstRuneToLower(method.Name),
RecvType: javaType(method.InStream, true, env),
RetType: serverInterfaceOutArg(iface, method, env),
RetJavaTypes: retArgTypes,
SendType: javaType(method.OutStream, true, env),
StreamRecvReflectType: javaReflectType(method.InStream, env),
StreamSendReflectType: javaReflectType(method.OutStream, env),
Tags: tags,
}
}
func processServerWrapperEmbedMethod(iface *compile.Interface, embedMethod *compile.Method, env *compile.Env) serverWrapperEmbedMethod {
return serverWrapperEmbedMethod{
CallingArgs: javaCallingArgStr(embedMethod.InArgs, true),
DeclarationArgs: javaDeclarationArgStr(embedMethod.InArgs, env, true),
IfaceName: vdlutil.FirstRuneToUpper(iface.Name),
JavaDoc: javaDoc(embedMethod.Doc, embedMethod.DocSuffix),
Name: vdlutil.FirstRuneToLower(embedMethod.Name),
RetType: serverInterfaceOutArg(iface, embedMethod, env),
}
}
// genJavaServerWrapperFile generates a java file containing a server wrapper for the specified
// interface.
func genJavaServerWrapperFile(iface *compile.Interface, env *compile.Env) JavaFileInfo {
embeds := []serverWrapperEmbed{}
for _, embed := range allEmbeddedIfaces(iface) {
embeds = append(embeds, serverWrapperEmbed{
Name: vdlutil.FirstRuneToUpper(embed.Name),
FullName: javaPath(javaGenPkgPath(path.Join(embed.File.Package.GenPath, vdlutil.FirstRuneToUpper(embed.Name)))),
})
}
methodTags := make(map[string][]methodTag)
// Copy method tags off of the interface.
methods := make([]serverWrapperMethod, len(iface.Methods))
for i, method := range iface.Methods {
tags := make([]methodTag, len(method.Tags))
for j, tag := range method.Tags {
tags[j].Value = javaConstVal(tag, env)
tags[j].Type = javaReflectType(tag.Type(), env)
}
methodTags[vdlutil.FirstRuneToLower(method.Name)] = tags
methods[i] = processServerWrapperMethod(iface, method, env, tags)
}
embedMethods := []serverWrapperEmbedMethod{}
for _, embedMao := range dedupedEmbeddedMethodAndOrigins(iface) {
embedMethods = append(embedMethods, processServerWrapperEmbedMethod(embedMao.Origin, embedMao.Method, env))
}
javaServiceName := vdlutil.FirstRuneToUpper(iface.Name)
data := struct {
FileDoc string
EmbedMethods []serverWrapperEmbedMethod
Embeds []serverWrapperEmbed
FullServiceName string
Methods []serverWrapperMethod
MethodTags map[string][]methodTag
PackagePath string
ServiceName string
Source string
Doc string
}{
FileDoc: iface.File.Package.FileDoc,
EmbedMethods: embedMethods,
Embeds: embeds,
FullServiceName: javaPath(interfaceFullyQualifiedName(iface)),
Methods: methods,
MethodTags: methodTags,
PackagePath: javaPath(javaGenPkgPath(iface.File.Package.GenPath)),
ServiceName: javaServiceName,
Source: iface.File.BaseName,
Doc: toJavaString(iface.NamePos.Doc),
}
var buf bytes.Buffer
err := parseTmpl("server wrapper", serverWrapperTmpl).Execute(&buf, data)
if err != nil {
log.Fatalf("vdl: couldn't execute server wrapper template: %v", err)
}
return JavaFileInfo{
Name: javaServiceName + "ServerWrapper.java",
Data: buf.Bytes(),
}
}