use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project ballerina by ballerina-lang.
the class StreamingExecute method execute.
@Override
public void execute(Context context) {
BStruct serviceStub = (BStruct) context.getRefArgument(SERVICE_STUB_REF_INDEX);
if (serviceStub == null) {
notifyErrorReply(context, "Error while getting connector. gRPC Client connector " + "is not initialized properly");
return;
}
Object connectionStub = serviceStub.getNativeData(SERVICE_STUB);
if (connectionStub == null) {
notifyErrorReply(context, "Error while getting connection stub. gRPC Client connector " + "is not initialized properly");
return;
}
String methodName = context.getStringArgument(0);
if (methodName == null) {
notifyErrorReply(context, "Error while processing the request. RPC endpoint doesn't " + "set properly");
return;
}
com.google.protobuf.Descriptors.MethodDescriptor methodDescriptor = MessageRegistry.getInstance().getMethodDescriptor(methodName);
if (methodDescriptor == null) {
notifyErrorReply(context, "No registered method descriptor for '" + methodName + "'");
return;
}
if (connectionStub instanceof GrpcNonBlockingStub) {
GrpcNonBlockingStub grpcNonBlockingStub = (GrpcNonBlockingStub) connectionStub;
BTypeDescValue serviceType = (BTypeDescValue) context.getRefArgument(1);
Service callbackService = BLangConnectorSPIUtil.getServiceFromType(context.getProgramFile(), getTypeField(serviceType));
try {
MethodDescriptor.MethodType methodType = getMethodType(methodDescriptor);
DefaultStreamObserver responseObserver = new DefaultStreamObserver(callbackService);
StreamObserver<Message> requestSender;
if (methodType.equals(MethodDescriptor.MethodType.CLIENT_STREAMING)) {
requestSender = grpcNonBlockingStub.executeClientStreaming(responseObserver, methodName);
} else if (methodType.equals(MethodDescriptor.MethodType.BIDI_STREAMING)) {
requestSender = grpcNonBlockingStub.executeBidiStreaming(responseObserver, methodName);
} else {
notifyErrorReply(context, "Error while executing the client call. Method type " + methodType.name() + " not supported");
return;
}
BStruct connStruct = createStruct(context, CLIENT_CONNECTION);
connStruct.addNativeData(REQUEST_SENDER, requestSender);
connStruct.addNativeData(REQUEST_MESSAGE_DEFINITION, methodDescriptor.getInputType());
BStruct clientEndpoint = (BStruct) serviceStub.getNativeData(CLIENT_END_POINT);
clientEndpoint.addNativeData(CLIENT_CONNECTION, connStruct);
context.setReturnValues(clientEndpoint);
} catch (RuntimeException | GrpcClientException e) {
notifyErrorReply(context, "gRPC Client Connector Error :" + e.getMessage());
}
}
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project motan by weibocom.
the class GrpcUtil method getMethodDescriptorByAnnotation.
@SuppressWarnings("rawtypes")
public static HashMap<String, MethodDescriptor> getMethodDescriptorByAnnotation(Class<?> clazz, String serialization) throws Exception {
String clazzName = getGrpcClassName(clazz);
HashMap<String, MethodDescriptor> result = serviceMap.get(clazzName);
if (result == null) {
synchronized (serviceMap) {
if (!serviceMap.containsKey(clazzName)) {
ServiceDescriptor serviceDesc = getServiceDesc(getGrpcClassName(clazz));
HashMap<String, MethodDescriptor> methodMap = new HashMap<String, MethodDescriptor>();
for (MethodDescriptor<?, ?> methodDesc : serviceDesc.getMethods()) {
Method interfaceMethod = getMethod(methodDesc.getFullMethodName(), clazz);
if (JSON_CODEC.equals(serialization)) {
methodDesc = convertJsonDescriptor(methodDesc, interfaceMethod.getParameterTypes()[0], interfaceMethod.getReturnType());
}
methodMap.put(interfaceMethod.getName(), methodDesc);
}
serviceMap.put(clazzName, methodMap);
}
result = serviceMap.get(clazzName);
}
}
return result;
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project webpieces by deanhiller.
the class GrpcJsonRoutes method loadRoutes.
@SuppressWarnings("rawtypes")
private void loadRoutes(RouteBuilder bldr, String baseUrl, ServiceMetaInfo meta) {
try {
// MUST use Thread context class loader in case we are in DevelopmentServer.
// This class is loaded in AppClassLoader. We need to be using CompilingClassLoader in dev server
ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
String controller = meta.getController().getName();
// NOTE: If we do not do this, it will not recompile in the DevelopmentServer as it will use the original class
Class grpcClazz = threadClassLoader.loadClass(meta.getGrpcService().getName());
@SuppressWarnings("unchecked") Method method = grpcClazz.getMethod("getServiceDescriptor");
ServiceDescriptor descriptor = (ServiceDescriptor) method.invoke(null);
Collection<MethodDescriptor<?, ?>> methods = descriptor.getMethods();
for (MethodDescriptor<?, ?> methodDescriptor : methods) {
String fullMethodName = methodDescriptor.getFullMethodName();
int index = fullMethodName.indexOf("/");
String grpcMethod = fullMethodName.substring(index + 1, fullMethodName.length());
String javaMethodName = grpcMethod.substring(0, 1).toLowerCase() + grpcMethod.substring(1);
// the world is moving ALL to HTTPS so it's not bad to just do it all on https
// POST is fine for rpc calls. YES, some calls may be stricly get BUT this is rpc at this point so POST
// this plugin is EASY to copy and fork and maintain your self so you can easily do that
bldr.addContentRoute(HTTPS, POST, baseUrl + "/" + fullMethodName, controller + "." + javaMethodName);
}
} catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw SneakyThrow.sneak(e);
}
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project alluxio by Alluxio.
the class GrpcSerializationUtils method overrideMethods.
/**
* Creates a service definition that uses custom marshallers.
*
* @param service the service to intercept
* @param marshallers a map that specifies which marshaller to use for each method
* @return the new service definition
*/
public static ServerServiceDefinition overrideMethods(final ServerServiceDefinition service, final Map<MethodDescriptor, MethodDescriptor> marshallers) {
List<ServerMethodDefinition<?, ?>> newMethods = new ArrayList<ServerMethodDefinition<?, ?>>();
List<MethodDescriptor<?, ?>> newDescriptors = new ArrayList<MethodDescriptor<?, ?>>();
// intercepts the descriptors
for (final ServerMethodDefinition<?, ?> definition : service.getMethods()) {
ServerMethodDefinition<?, ?> newMethod = interceptMethod(definition, marshallers);
newDescriptors.add(newMethod.getMethodDescriptor());
newMethods.add(newMethod);
}
// builds the new service descriptor
final ServerServiceDefinition.Builder serviceBuilder = ServerServiceDefinition.builder(new ServiceDescriptor(service.getServiceDescriptor().getName(), newDescriptors));
// creates the new service definition
for (ServerMethodDefinition<?, ?> definition : newMethods) {
serviceBuilder.addMethod(definition);
}
return serviceBuilder.build();
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor in project alluxio by Alluxio.
the class GrpcSerializationUtils method interceptMethod.
private static <ReqT, RespT> ServerMethodDefinition<ReqT, RespT> interceptMethod(final ServerMethodDefinition<ReqT, RespT> definition, final Map<MethodDescriptor, MethodDescriptor> newMethods) {
MethodDescriptor<ReqT, RespT> descriptor = definition.getMethodDescriptor();
MethodDescriptor newMethod = newMethods.get(descriptor);
if (newMethod != null) {
return ServerMethodDefinition.create(newMethod, definition.getServerCallHandler());
}
return definition;
}
Aggregations