-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSGiGenerator.java
360 lines (312 loc) · 14 KB
/
OSGiGenerator.java
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*******************************************************************************
* Copyright (c) 2020 Composent, Inc. and others. All rights reserved. This
* program and the accompanying materials are made available under the terms of
* the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Composent, Inc. - initial API and implementation
******************************************************************************/
package org.eclipse.ecf.grpc.osgigenerator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import com.google.common.base.Strings;
import com.google.common.html.HtmlEscapers;
import com.google.protobuf.DescriptorProtos.FileDescriptorProto;
import com.google.protobuf.DescriptorProtos.FileOptions;
import com.google.protobuf.DescriptorProtos.MethodDescriptorProto;
import com.google.protobuf.DescriptorProtos.ServiceDescriptorProto;
import com.google.protobuf.DescriptorProtos.SourceCodeInfo.Location;
import com.google.protobuf.GeneratedMessage.GeneratedExtension;
import com.google.protobuf.compiler.PluginProtos;
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest;
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse.File;
import com.salesforce.jprotoc.Generator;
import com.salesforce.jprotoc.GeneratorException;
import com.salesforce.jprotoc.ProtoTypeMap;
import com.salesforce.jprotoc.ProtocPlugin;
public class OSGiGenerator extends Generator {
private static final String JAVA_EXTENSION = ".java";
private static final String SERVICE_CLASS_SUFFIX = "Service";
private static final int SERVICE_NUMBER_OF_PATHS = 2;
private static final int METHOD_NUMBER_OF_PATHS = 4;
private static final String REACTIVE_PACKAGE = String.join(".", "io", "reactivex");
private static final String REACTIVE3_PACKAGE = String.join(".", "io", "reactivex", "rxjava3", "core");
private static final String FLOWABLE_CLASS = "Flowable";
private static final String SINGLE_CLASS = "Single";
private static final String DEFAULT_BODY_NULL_RETURN = "return null";
private boolean useRx3 = false;
protected OSGiGenerator(boolean rx3) {
this.useRx3 = rx3;
}
public static void main(String[] args) throws Exception {
boolean useRx3 = false;
if (args.length > 0) {
List<String> argsList = new ArrayList<String>(Arrays.asList(args));
if (argsList.contains("rxjava3")) {
useRx3 = true;
argsList.remove("rxjava3");
args = argsList.toArray(new String[argsList.size()]);
}
}
List<Generator> generators = new ArrayList<Generator>();
generators.add(new OSGiGenerator(useRx3));
@SuppressWarnings("rawtypes")
List<GeneratedExtension> extensions = new ArrayList<GeneratedExtension>();
extensions.add(OsgiServiceOptionsProto.generationType);
extensions.add(OsgiServiceOptionsProto.interfaceMethodBodyType);
extensions.add(OsgiServiceOptionsProto.fileReactivexVersion);
extensions.add(OsgiServiceOptionsProto.serviceReactivexVersion);
if (args.length == 0) {
ProtocPlugin.generate(generators, extensions);
} else {
ProtocPlugin.debug(generators, extensions, args[0]);
}
}
@Override
public List<File> generateFiles(CodeGeneratorRequest request) throws GeneratorException {
return generateFiles(findServices(request.getProtoFileList().stream()
.filter(protoFile -> request.getFileToGenerateList().contains(protoFile.getName()))
.collect(Collectors.toList()), ProtoTypeMap.of(request.getProtoFileList())));
}
private List<ServiceContext> findServices(List<FileDescriptorProto> protos, ProtoTypeMap typeMap) {
List<ServiceContext> contexts = new ArrayList<>();
protos.forEach(fileProto -> {
for (int serviceNumber = 0; serviceNumber < fileProto.getServiceCount(); serviceNumber++) {
ServiceContext serviceContext = buildServiceContext(fileProto.getService(serviceNumber), typeMap,
fileProto.getSourceCodeInfo().getLocationList(), serviceNumber,
useRx3?ReactiveXVersion.V_3:fileProto.getOptions().getExtension(OsgiServiceOptionsProto.fileReactivexVersion));
serviceContext.protoName = fileProto.getName();
serviceContext.packageName = extractPackageName(fileProto);
contexts.add(serviceContext);
}
});
return contexts;
}
private String extractPackageName(FileDescriptorProto proto) {
FileOptions options = proto.getOptions();
if (options != null) {
String javaPackage = options.getJavaPackage();
if (!Strings.isNullOrEmpty(javaPackage)) {
return javaPackage;
}
}
return Strings.nullToEmpty(proto.getPackage());
}
private ServiceContext buildServiceContext(ServiceDescriptorProto serviceProto, ProtoTypeMap typeMap,
List<Location> locations, int serviceNumber, ReactiveXVersion fileReactiveXVersion) {
String serviceName = serviceProto.getName();
ServiceContext serviceContext = new ServiceContext();
serviceContext.methodTypes = serviceProto.getOptions().getExtension(OsgiServiceOptionsProto.generationType);
serviceContext.className = serviceName + SERVICE_CLASS_SUFFIX;
serviceContext.fileName = serviceContext.className + JAVA_EXTENSION;
serviceContext.serviceName = serviceName;
serviceContext.deprecated = serviceProto.getOptions() != null && serviceProto.getOptions().getDeprecated();
List<Location> allLocationsForService = locations.stream()
.filter(location -> location.getPathCount() >= 2
&& location.getPath(0) == FileDescriptorProto.SERVICE_FIELD_NUMBER
&& location.getPath(1) == serviceNumber)
.collect(Collectors.toList());
Location serviceLocation = allLocationsForService.stream()
.filter(location -> location.getPathCount() == SERVICE_NUMBER_OF_PATHS).findFirst()
.orElseGet(Location::getDefaultInstance);
serviceContext.javaDoc = getJavaDoc(getComments(serviceLocation), getServiceJavaDocPrefix());
String servicePackage = REACTIVE_PACKAGE; // default to version 2 for now
if (fileReactiveXVersion == ReactiveXVersion.V_3 || serviceProto.getOptions()
.getExtension(OsgiServiceOptionsProto.serviceReactivexVersion) == ReactiveXVersion.V_3) {
servicePackage = REACTIVE3_PACKAGE;
}
for (int methodNumber = 0; methodNumber < serviceProto.getMethodCount(); methodNumber++) {
MethodContext methodContext = buildMethodContext(serviceContext, serviceProto.getMethod(methodNumber),
typeMap, allLocationsForService, methodNumber, servicePackage);
if (methodContext != null) {
serviceContext.methods.add(methodContext);
}
}
return serviceContext;
}
private void setImport(ServiceContext serviceContext, String importClass) {
if (!serviceContext.imports.stream().filter(i -> i.importClass.equals(importClass)).findAny().isPresent())
serviceContext.imports.add(new Import(importClass));
}
private MethodContext buildMethodContext(ServiceContext serviceContext, MethodDescriptorProto methodProto,
ProtoTypeMap typeMap, List<Location> locations, int methodNumber, String service_package) {
MethodContext methodContext = new MethodContext();
methodContext.isManyInput = methodProto.getClientStreaming();
methodContext.isManyOutput = methodProto.getServerStreaming();
methodContext.methodName = lowerCaseFirst(methodProto.getName());
// Get base input and output types
String baseInputType = typeMap.toJavaTypeName(methodProto.getInputType());
String baseOutputType = typeMap.toJavaTypeName(methodProto.getOutputType());
// Get defaultBody if present in options and set defaultBody in methodContext
if (methodProto.getOptions()
.getExtension(OsgiServiceOptionsProto.interfaceMethodBodyType) == InterfaceMethodBodyType.THROW) {
methodContext.defaultBody = String.format(
"throw new UnsupportedOperationException(\"%s not implemented yet\")", methodContext.methodName);
} else {
methodContext.defaultBody = DEFAULT_BODY_NULL_RETURN;
}
String fq_flowable_class = String.join(".", service_package, FLOWABLE_CLASS);
String fq_single_class = String.join(".", service_package, SINGLE_CLASS);
// Argument types
if (methodContext.isManyInput) {
setImport(serviceContext, fq_flowable_class);
methodContext.inputType = FLOWABLE_CLASS;
} else {
setImport(serviceContext, fq_single_class);
methodContext.inputType = SINGLE_CLASS;
}
methodContext.inputGenericType = baseInputType;
// return types
if (methodContext.isManyOutput) {
setImport(serviceContext, fq_flowable_class);
methodContext.outputType = FLOWABLE_CLASS;
} else {
setImport(serviceContext, fq_single_class);
methodContext.outputType = SINGLE_CLASS;
}
methodContext.outputGenericType = baseOutputType;
methodContext.deprecated = methodProto.getOptions() != null && methodProto.getOptions().getDeprecated();
Location methodLocation = locations.stream()
.filter(location -> location.getPathCount() == METHOD_NUMBER_OF_PATHS
&& location.getPath(METHOD_NUMBER_OF_PATHS - 1) == methodNumber)
.findFirst().orElseGet(Location::getDefaultInstance);
methodContext.javaDoc = getJavaDoc(getComments(methodLocation), getMethodJavaDocPrefix());
return methodContext;
}
private String lowerCaseFirst(String s) {
return Character.toLowerCase(s.charAt(0)) + s.substring(1);
}
private List<PluginProtos.CodeGeneratorResponse.File> generateFiles(List<ServiceContext> services) {
return services.stream().map(this::buildFile).collect(Collectors.toList());
}
private String getServiceJavaDocPrefix() {
return "";
}
private String getMethodJavaDocPrefix() {
return " ";
}
private PluginProtos.CodeGeneratorResponse.File buildFile(ServiceContext context) {
return PluginProtos.CodeGeneratorResponse.File.newBuilder().setName(absoluteFileName(context))
.setContent(applyTemplate(context.methodTypes == GenerationType.GRPC_UNARY ? "GrpcService.mustache"
: "ReactiveXService.mustache", context))
.build();
}
private String absoluteFileName(ServiceContext ctx) {
String dir = ctx.packageName.replace('.', '/');
if (Strings.isNullOrEmpty(dir)) {
return ctx.fileName;
} else {
return dir + "/" + ctx.fileName;
}
}
private String getComments(Location location) {
return location.getLeadingComments().isEmpty() ? location.getTrailingComments() : location.getLeadingComments();
}
private String getJavaDoc(String comments, String prefix) {
if (!comments.isEmpty()) {
StringBuilder builder = new StringBuilder("/**\n").append(prefix).append(" * <pre>\n");
Arrays.stream(HtmlEscapers.htmlEscaper().escape(comments).split("\n"))
.map(line -> line.replace("*/", "*/").replace("*", "*"))
.forEach(line -> builder.append(prefix).append(" * ").append(line).append("\n"));
builder.append(prefix).append(" * </pre>\n").append(prefix).append(" */");
return builder.toString();
}
return null;
}
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> map = new ConcurrentHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
private class Import {
public String importClass;
Import(String clazzName) {
this.importClass = clazzName;
}
}
/**
* Template class for proto Service objects.
*/
private class ServiceContext {
public GenerationType methodTypes;
public List<Import> imports = new ArrayList<Import>();
// CHECKSTYLE DISABLE VisibilityModifier FOR 8 LINES
public String fileName;
@SuppressWarnings("unused")
public String protoName;
public String packageName;
public String className;
@SuppressWarnings("unused")
public String serviceName;
@SuppressWarnings("unused")
public boolean deprecated;
@SuppressWarnings("unused")
public String javaDoc;
public List<MethodContext> methods = new ArrayList<>();
@SuppressWarnings("unused")
public List<MethodContext> unaryRequestMethods() {
return methods.stream().filter(m -> (!m.isManyInput && !m.isManyOutput)).collect(Collectors.toList());
}
@SuppressWarnings("unused")
public List<MethodContext> serverStreamingRequestMethods() {
return methods.stream().filter(m -> (!m.isManyInput && m.isManyOutput)).collect(Collectors.toList());
}
@SuppressWarnings("unused")
public List<MethodContext> clientStreamingRequestMethods() {
return methods.stream().filter(m -> (m.isManyInput && !m.isManyOutput)).collect(Collectors.toList());
}
@SuppressWarnings("unused")
public List<MethodContext> bidiStreamingRequestMethods() {
return methods.stream().filter(m -> (m.isManyInput && m.isManyOutput)).collect(Collectors.toList());
}
@SuppressWarnings("unused")
public List<Import> importClassNames() {
return imports.stream().filter(distinctByKey(i -> i.importClass)).collect(Collectors.toList());
}
}
/**
* Template class for proto RPC objects.
*/
@SuppressWarnings("unused")
private class MethodContext {
// CHECKSTYLE DISABLE VisibilityModifier FOR 10 LINES
public String methodName;
public String inputType;
public String inputGenericType;
public String outputType;
public String outputGenericType;
public boolean deprecated;
public boolean isManyInput;
public boolean isManyOutput;
public String javaDoc;
public String defaultBody;
// This method mimics the upper-casing method ogf gRPC to ensure compatibility
// See
// https://github.com/grpc/grpc-java/blob/v1.8.0/compiler/src/java_plugin/cpp/java_generator.cpp#L58
public String methodNameUpperUnderscore() {
StringBuilder s = new StringBuilder();
for (int i = 0; i < methodName.length(); i++) {
char c = methodName.charAt(i);
s.append(Character.toUpperCase(c));
if ((i < methodName.length() - 1) && Character.isLowerCase(c)
&& Character.isUpperCase(methodName.charAt(i + 1))) {
s.append('_');
}
}
return s.toString();
}
public String methodNamePascalCase() {
String mn = methodName.replace("_", "");
return String.valueOf(Character.toUpperCase(mn.charAt(0))) + mn.substring(1);
}
public String methodNameCamelCase() {
String mn = methodName.replace("_", "");
return String.valueOf(Character.toLowerCase(mn.charAt(0))) + mn.substring(1);
}
}
}