use of org.ballerinalang.net.grpc.exception.GrpcServerException in project ballerina by ballerina-lang.
the class ServiceProtoBuilder method process.
@Override
public void process(ServiceNode serviceNode, List<AnnotationAttachmentNode> annotations) {
for (AnnotationAttachmentNode annotationNode : annotations) {
if (ANN_MESSAGE_LISTENER.equals(annotationNode.getAnnotationName().getValue())) {
return;
}
}
try {
File fileDefinition = ServiceProtoUtils.generateProtoDefinition(serviceNode);
ServiceConfiguration serviceConfig = ServiceProtoUtils.getServiceConfiguration(serviceNode);
ServiceProtoUtils.writeServiceFiles(fileDefinition, serviceNode.getName().getValue(), serviceConfig.isGenerateClientConnector());
} catch (GrpcServerException e) {
dlog.logDiagnostic(Diagnostic.Kind.WARNING, serviceNode.getPosition(), e.getMessage());
}
}
use of org.ballerinalang.net.grpc.exception.GrpcServerException in project ballerina by ballerina-lang.
the class ServiceProtoUtils method writeServiceFiles.
/**
* Write protobuf file definition content to the filename.
*
* @param protoFileDefinition protobuf file definition.
* @param filename filename
* @throws GrpcServerException when error occur while writing content to file.
*/
static void writeServiceFiles(File protoFileDefinition, String filename, boolean generateClientConnector) throws GrpcServerException {
try {
// write the proto string to the file in protobuf contract directory
Path protoFilePath = Paths.get(filename + ServiceProtoConstants.PROTO_FILE_EXTENSION);
Files.write(protoFilePath, protoFileDefinition.getFileDefinition().getBytes(ServiceProtoConstants.UTF_8_CHARSET));
// write the proto descriptor byte array to the file in protobuf contract directory
byte[] fileDescriptor = protoFileDefinition.getFileDescriptorProto().toByteArray();
Path descFilePath = Paths.get(filename + ServiceProtoConstants.DESC_FILE_EXTENSION);
Files.write(descFilePath, fileDescriptor);
if (generateClientConnector) {
List<byte[]> dependentDescriptorsList = new ArrayList<>();
dependentDescriptorsList.add(com.google.protobuf.WrappersProto.getDescriptor().toProto().toByteArray());
// Path path = Paths.get("");
BallerinaFileBuilder ballerinaFileBuilder = new BallerinaFileBuilder(dependentDescriptorsList);
ballerinaFileBuilder.setRootDescriptor(fileDescriptor);
ballerinaFileBuilder.build();
}
} catch (IOException e) {
throw new GrpcServerException("Error while writing file descriptor to file.", e);
}
}
use of org.ballerinalang.net.grpc.exception.GrpcServerException in project ballerina by ballerina-lang.
the class GetClient method execute.
@Override
public void execute(Context context) {
BStruct serviceEndpoint = (BStruct) context.getRefArgument(SERVICE_ENDPOINT_INDEX);
// Service client responder is populated in method listener. There we create new service endpoint instance
// and bind client responder instance.
BRefType clientType = serviceEndpoint.getRefField(CLIENT_RESPONDER_REF_INDEX);
if (clientType instanceof BStruct) {
BStruct endpointClient = (BStruct) clientType;
context.setReturnValues(endpointClient);
} else {
context.setError(MessageUtils.getConnectorError(context, new GrpcServerException("Error while " + "retrieving endpoint client.")));
}
}
use of org.ballerinalang.net.grpc.exception.GrpcServerException in project ballerina by ballerina-lang.
the class Register method execute.
@Override
public void execute(Context context) {
BStruct serviceEndpoint = (BStruct) context.getRefArgument(SERVICE_ENDPOINT_INDEX);
Service service = BLangConnectorSPIUtil.getServiceRegistered(context);
io.grpc.ServerBuilder serverBuilder = getServiceBuilder(serviceEndpoint);
try {
registerService(serverBuilder, service);
context.setReturnValues();
} catch (GrpcServerException e) {
context.setError(MessageUtils.getConnectorError(context, e));
}
}
use of org.ballerinalang.net.grpc.exception.GrpcServerException in project ballerina by ballerina-lang.
the class Start method execute.
@Override
public void execute(Context context) {
BStruct serviceEndpoint = (BStruct) context.getRefArgument(SERVICE_ENDPOINT_INDEX);
io.grpc.ServerBuilder serverBuilder = getServiceBuilder(serviceEndpoint);
try {
Server server = GrpcServicesBuilder.start(serverBuilder);
serviceEndpoint.addNativeData(GRPC_SERVER, server);
Runtime.getRuntime().addShutdownHook(new Thread(() -> stop(server)));
console.println("ballerina: started gRPC server connector on port " + server.getPort());
GrpcServicesBuilder.blockUntilShutdown(server);
} catch (GrpcServerException e) {
context.setError(MessageUtils.getConnectorError(context, new GrpcServerException("Error in starting gRPC " + "service.", e)));
} catch (InterruptedException e) {
context.setError(MessageUtils.getConnectorError(context, new GrpcServerException("gRPC server " + "interrupted", e)));
}
context.setReturnValues();
}
Aggregations