Search in sources :

Example 16 with SMethod

use of org.bimserver.shared.meta.SMethod in project BIMserver by opensourceBIM.

the class ReflectiveRpcChannel method callBlockingMethod.

@SuppressWarnings({ "unchecked", "rawtypes" })
public Message callBlockingMethod(MethodDescriptorContainer methodDescriptor, Message request) {
    FieldDescriptor errorMessageField = methodDescriptor.getOutputField("errorMessage");
    DynamicMessage response = DynamicMessage.getDefaultInstance(methodDescriptor.getOutputDescriptor());
    Descriptor inputType = methodDescriptor.getInputDescriptor();
    SMethod sMethod = servicesMap.getBySimpleName(methodDescriptor.getServiceDescriptorContainer().getName()).getSMethod(methodDescriptor.getName());
    if (sMethod == null) {
        LOGGER.info("Method " + methodDescriptor.getName() + " not found");
        return null;
    } else {
        try {
            KeyValuePair[] arguments = new KeyValuePair[inputType.getFields().size()];
            int i = 0;
            for (FieldDescriptor fieldDescriptor : inputType.getFields()) {
                SParameter sParameter = sMethod.getParameter(i);
                Object value = request.getField(fieldDescriptor);
                if (value instanceof EnumValueDescriptor) {
                    EnumValueDescriptor enumValueDescriptor = (EnumValueDescriptor) value;
                    Class en = getJavaType(fieldDescriptor);
                    arguments[i] = new KeyValuePair(fieldDescriptor.getName(), en.getEnumConstants()[enumValueDescriptor.getIndex()]);
                } else if (value instanceof ByteString) {
                    ByteString byteString = (ByteString) value;
                    DataSource dataSource = new org.bimserver.utils.ByteArrayDataSource("bytes", byteString.toByteArray());
                    DataHandler dataHandler = new DataHandler(dataSource);
                    arguments[i] = new KeyValuePair(fieldDescriptor.getName(), dataHandler);
                } else if (value instanceof DynamicMessage) {
                    arguments[i] = new KeyValuePair(fieldDescriptor.getName(), convertProtocolBuffersMessageToSObject((DynamicMessage) value, null, sParameter.getType()));
                } else if (value instanceof Collection) {
                    Collection col = (Collection) value;
                    if (sParameter.getType().isList()) {
                        List list = new ArrayList(col);
                        arguments[i] = new KeyValuePair(fieldDescriptor.getName(), list);
                    } else if (sParameter.getType().isSet()) {
                        Set set = new HashSet(col);
                        arguments[i] = new KeyValuePair(fieldDescriptor.getName(), set);
                    }
                } else {
                    arguments[i] = new KeyValuePair(fieldDescriptor.getName(), value);
                }
                i++;
            }
            Object result = sMethod.invoke(publicInterface, service, arguments);
            Builder builder = response.newBuilderForType();
            if (methodDescriptor.getOutputDescriptor().getName().equals("VoidResponse")) {
                builder.setField(errorMessageField, "OKE");
            } else {
                FieldDescriptor valueField = protocolBuffersMetaData.getMessageDescriptor(response.getDescriptorForType().getName()).getField("value");
                if (result != null) {
                    if (valueField.getType().getJavaType() == JavaType.ENUM) {
                        builder.setField(valueField, valueField.getEnumType().findValueByName(result.toString()));
                    } else if (valueField.getType().getJavaType() != JavaType.MESSAGE) {
                        if (result instanceof Date) {
                            builder.setField(valueField, ((Date) result).getTime());
                        } else {
                            if (valueField.isRepeated()) {
                                builder.setField(valueField, new ArrayList());
                            } else {
                                builder.setField(valueField, result);
                            }
                        }
                    } else if (result instanceof List) {
                        List list = new ArrayList();
                        List originalList = (List) result;
                        for (Object object : originalList) {
                            list.add(convertSObjectToProtocolBuffersObject((SBase) object, sMethod.getBestReturnType()));
                        }
                        builder.setField(valueField, list);
                    } else if (result instanceof Set) {
                        List list = new ArrayList();
                        Set originalSet = (Set) result;
                        for (Object object : originalSet) {
                            list.add(convertSObjectToProtocolBuffersObject((SBase) object, sMethod.getBestReturnType()));
                        }
                        builder.setField(valueField, list);
                    } else {
                        builder.setField(valueField, convertSObjectToProtocolBuffersObject((SBase) result, sMethod.getBestReturnType()));
                    }
                }
                builder.setField(errorMessageField, "OKE");
            }
            return builder.build();
        // } catch (InvocationTargetException e) {
        // Builder errorMessage = response.newBuilderForType();
        // if (e.getTargetException() != null && e.getTargetException().getMessage() != null) {
        // errorMessage.setField(errorMessageField, e.getTargetException().getMessage());
        // } else {
        // if (e.getMessage() != null) {
        // errorMessage.setField(errorMessageField, e.getMessage());
        // } else {
        // errorMessage.setField(errorMessageField, "Unknown error");
        // }
        // }
        // return errorMessage.build();
        } catch (Exception e) {
            if (!(e instanceof org.bimserver.shared.exceptions.ServiceException)) {
                LOGGER.error("", e);
            }
            Builder errorMessage = response.newBuilderForType();
            if (e.getMessage() != null) {
                errorMessage.setField(errorMessageField, e.getMessage());
            } else {
                LOGGER.error("", e);
                errorMessage.setField(errorMessageField, "Unknown error");
            }
            return errorMessage.build();
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ByteString(com.google.protobuf.ByteString) Builder(com.google.protobuf.Message.Builder) ArrayList(java.util.ArrayList) DynamicMessage(com.google.protobuf.DynamicMessage) DataHandler(javax.activation.DataHandler) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) SParameter(org.bimserver.shared.meta.SParameter) ArrayList(java.util.ArrayList) List(java.util.List) SMethod(org.bimserver.shared.meta.SMethod) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) HashSet(java.util.HashSet) KeyValuePair(org.bimserver.shared.reflector.KeyValuePair) Date(java.util.Date) DataSource(javax.activation.DataSource) SBase(org.bimserver.shared.meta.SBase) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) Collection(java.util.Collection)

Example 17 with SMethod

use of org.bimserver.shared.meta.SMethod in project BIMserver by opensourceBIM.

the class ProtocolBuffersConnectionHandler method run.

@Override
public void run() {
    try {
        running = true;
        while (running) {
            String serviceName = dataInputStream.readUTF();
            String methodName = dataInputStream.readUTF();
            String token = dataInputStream.readUTF();
            ProtocolBuffersMetaData protocolBuffersMetaData = protocolBuffersServer.getProtocolBuffersMetaData();
            SService sService = servicesMap.getBySimpleName(serviceName);
            if (sService == null) {
                throw new UserException("No service found with name " + serviceName);
            }
            SMethod method = sService.getSMethod(methodName);
            if (method == null) {
                throw new UserException("Method " + methodName + " not found on " + serviceName);
            }
            Class<? extends PublicInterface> clazz = sService.getInterfaceClass();
            PublicInterface service = null;
            if (token.equals("")) {
                service = serviceFactory.get(AccessMethod.PROTOCOL_BUFFERS).get(clazz);
            } else {
                service = serviceFactory.get(token, AccessMethod.PROTOCOL_BUFFERS).get(clazz);
            }
            ReflectiveRpcChannel reflectiveRpcChannel = new ReflectiveRpcChannel(clazz, service, protocolBuffersMetaData, servicesMap);
            MethodDescriptorContainer pbMethod = protocolBuffersMetaData.getMethod(serviceName, methodName);
            Builder requestBuilder = DynamicMessage.getDefaultInstance(pbMethod.getInputDescriptor()).newBuilderForType();
            requestBuilder.mergeDelimitedFrom(dataInputStream);
            Message response = reflectiveRpcChannel.callBlockingMethod(pbMethod, requestBuilder.build());
            response.writeDelimitedTo(outputStream);
            outputStream.flush();
        }
    } catch (SocketException e) {
    } catch (EOFException e) {
    } catch (Exception e) {
        if (running) {
            LOGGER.error("", e);
        }
    }
    protocolBuffersServer.unregister(this);
}
Also used : SocketException(java.net.SocketException) MethodDescriptorContainer(org.bimserver.shared.pb.ProtocolBuffersMetaData.MethodDescriptorContainer) DynamicMessage(com.google.protobuf.DynamicMessage) Message(com.google.protobuf.Message) Builder(com.google.protobuf.DynamicMessage.Builder) ProtocolBuffersMetaData(org.bimserver.shared.pb.ProtocolBuffersMetaData) IOException(java.io.IOException) EOFException(java.io.EOFException) SocketException(java.net.SocketException) UserException(org.bimserver.shared.exceptions.UserException) ReflectiveRpcChannel(org.bimserver.shared.pb.ReflectiveRpcChannel) PublicInterface(org.bimserver.shared.interfaces.PublicInterface) EOFException(java.io.EOFException) UserException(org.bimserver.shared.exceptions.UserException) SMethod(org.bimserver.shared.meta.SMethod) SService(org.bimserver.shared.meta.SService)

Aggregations

SMethod (org.bimserver.shared.meta.SMethod)17 SParameter (org.bimserver.shared.meta.SParameter)11 UserException (org.bimserver.shared.exceptions.UserException)9 SService (org.bimserver.shared.meta.SService)8 KeyValuePair (org.bimserver.shared.reflector.KeyValuePair)6 ArrayList (java.util.ArrayList)4 CannotCompileException (javassist.CannotCompileException)4 CtClass (javassist.CtClass)4 CtConstructor (javassist.CtConstructor)4 CtField (javassist.CtField)4 CtMethod (javassist.CtMethod)4 NotFoundException (javassist.NotFoundException)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 DynamicMessage (com.google.protobuf.DynamicMessage)3 DataHandler (javax.activation.DataHandler)3 PublicInterface (org.bimserver.shared.interfaces.PublicInterface)3 ReflectorException (org.bimserver.shared.reflector.ReflectorException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 JsonObject (com.google.gson.JsonObject)2 ByteString (com.google.protobuf.ByteString)2