Search in sources :

Example 1 with SParameter

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

the class RealtimeReflectorFactoryBuilder method build1.

private void build1(Class<? extends PublicInterface> interfaceClass, org.bimserver.shared.meta.SService sService) {
    try {
        CtClass reflectorImplClass = pool.makeClass(GENERATED_CLASSES_PACKAGE + "." + interfaceClass.getSimpleName() + "Impl" + implementationCounter);
        reflectorImplClass.addInterface(pool.get(interfaceClass.getName()));
        CtClass reflectorClass = pool.get(Reflector.class.getName());
        CtField reflectorField = new CtField(reflectorClass, "reflector", reflectorImplClass);
        reflectorImplClass.addField(reflectorField);
        CtConstructor constructor = new CtConstructor(new CtClass[] { reflectorClass }, reflectorImplClass);
        StringBuilder sb = new StringBuilder();
        reflectorImplClass.addConstructor(constructor);
        sb.append("{");
        sb.append("this.reflector = $1;");
        sb.append("}");
        constructor.setBody(sb.toString());
        for (SMethod sMethod : sService.getMethods()) {
            CtClass[] parameters = new CtClass[sMethod.getParameters().size()];
            int i = 0;
            for (org.bimserver.shared.meta.SParameter sParameter : sMethod.getParameters()) {
                parameters[i] = pool.get(sParameter.getType().toJavaCode());
                i++;
            }
            CtMethod method = new CtMethod(pool.get(sMethod.getReturnType().toJavaCode()), sMethod.getName(), parameters, reflectorImplClass);
            StringBuilder methodBuilder = new StringBuilder();
            methodBuilder.append("{");
            if (sMethod.getReturnType().isVoid()) {
            } else {
                methodBuilder.append("return (" + sMethod.getReturnType().toJavaCode() + ")");
            }
            methodBuilder.append("reflector.callMethod(\"" + interfaceClass.getSimpleName() + "\", \"" + sMethod.getName() + "\", " + sMethod.getReturnType().toJavaCode() + ".class");
            if (sMethod.getParameters().isEmpty()) {
                methodBuilder.append(", new " + KeyValuePair.class.getName() + "[0]");
            } else {
                methodBuilder.append(", new " + KeyValuePair.class.getName() + "[]{");
                int x = 1;
                for (SParameter sParameter : sMethod.getParameters()) {
                    methodBuilder.append("new " + KeyValuePair.class.getName() + "(\"" + sParameter.getName() + "\", $" + x + ")");
                    if (sMethod.getParameter(sMethod.getParameters().size() - 1) != sParameter) {
                        methodBuilder.append(", ");
                    }
                    x++;
                }
                methodBuilder.append("}");
            }
            methodBuilder.append(");");
            methodBuilder.append("}");
            method.setBody(methodBuilder.toString());
            reflectorImplClass.addMethod(method);
        }
        pool.toClass(reflectorImplClass, getClass().getClassLoader(), getClass().getProtectionDomain());
    } catch (Exception e) {
        LOGGER.error("", e);
    }
}
Also used : CannotCompileException(javassist.CannotCompileException) NotFoundException(javassist.NotFoundException) CtConstructor(javassist.CtConstructor) CtClass(javassist.CtClass) CtField(javassist.CtField) SParameter(org.bimserver.shared.meta.SParameter) SMethod(org.bimserver.shared.meta.SMethod) SParameter(org.bimserver.shared.meta.SParameter) CtMethod(javassist.CtMethod)

Example 2 with SParameter

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

the class RealtimeReflectorFactoryBuilder method build2.

private void build2(Class<? extends PublicInterface> interfaceClass, org.bimserver.shared.meta.SService sService) {
    try {
        CtClass reflectorImplClass = pool.makeClass(GENERATED_CLASSES_PACKAGE + "." + interfaceClass.getSimpleName() + "Reflector" + implementationCounter);
        CtClass reflectorClass = pool.get(Reflector.class.getName());
        CtClass interfaceCtClass = pool.get(interfaceClass.getName());
        reflectorImplClass.addInterface(reflectorClass);
        CtField reflectorField = new CtField(interfaceCtClass, "publicInterface", reflectorImplClass);
        reflectorImplClass.addField(reflectorField);
        CtConstructor constructor = new CtConstructor(new CtClass[] { interfaceCtClass }, reflectorImplClass);
        StringBuilder sb = new StringBuilder();
        reflectorImplClass.addConstructor(constructor);
        sb.append("{");
        sb.append("this.publicInterface = $1;");
        sb.append("}");
        constructor.setBody(sb.toString());
        CtClass[] parameters = new CtClass[4];
        parameters[0] = pool.get(String.class.getName());
        parameters[1] = pool.get(String.class.getName());
        parameters[2] = pool.get(Class.class.getName());
        parameters[3] = pool.get(KeyValuePair.class.getName() + "[]");
        CtMethod method = new CtMethod(pool.get(Object.class.getName()), "callMethod", parameters, reflectorImplClass);
        StringBuilder methodBuilder = new StringBuilder();
        methodBuilder.append("{");
        methodBuilder.append("if  (1==0) {} ");
        for (SMethod sMethod : sService.getMethods()) {
            methodBuilder.append(" else if ($2.equals(\"" + sMethod.getName() + "\")) {");
            if (!sMethod.getReturnType().isVoid()) {
                methodBuilder.append("return ");
            }
            methodBuilder.append("publicInterface." + sMethod.getName() + "(");
            int i = 0;
            for (SParameter sParameter : sMethod.getParameters()) {
                methodBuilder.append("(" + sParameter.getType().toJavaCode() + ")$4[" + i + "].getValue()");
                if (i < sMethod.getParameters().size() - 1) {
                    methodBuilder.append(", ");
                }
                i++;
            }
            methodBuilder.append(");");
            methodBuilder.append("}");
        }
        methodBuilder.append("return null;");
        methodBuilder.append("}");
        method.setBody(methodBuilder.toString());
        reflectorImplClass.addMethod(method);
        pool.toClass(reflectorImplClass, getClass().getClassLoader(), getClass().getProtectionDomain());
    } catch (Exception e) {
        LOGGER.error("", e);
    }
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) SParameter(org.bimserver.shared.meta.SParameter) SMethod(org.bimserver.shared.meta.SMethod) CtMethod(javassist.CtMethod) CannotCompileException(javassist.CannotCompileException) NotFoundException(javassist.NotFoundException) CtConstructor(javassist.CtConstructor)

Example 3 with SParameter

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

the class ProtocolBuffersGenerator method generateServiceInterfaceImplementation.

public void generateServiceInterfaceImplementation(String name, File file, SService sService) {
    try {
        PrintWriter out = new PrintWriter(file);
        out.println("package org.bimserver.pb;\n");
        out.println(Licenser.getCommentedLicenseText(new File("license.txt")));
        out.println("import java.util.*;");
        out.println("import com.google.protobuf.*;");
        out.println("import org.bimserver.utils.*;");
        out.println("import org.bimserver.pb.ProtocolBuffersService.*;");
        out.println("import com.google.protobuf.BlockingRpcChannel;");
        out.println("import org.bimserver.pb.ProtocolBuffersService.ServiceInterface.BlockingInterface;");
        out.println("import com.googlecode.protobuf.socketrpc.SocketRpcController;");
        out.println("import com.googlecode.protobuf.socketrpc.RpcChannels;");
        out.println("import com.googlecode.protobuf.socketrpc.SocketRpcConnectionFactories;");
        out.println("import javax.mail.util.ByteArrayDataSource;");
        out.println("import javax.activation.DataHandler;");
        out.println();
        out.println("@SuppressWarnings(\"unused\")");
        out.println("public class " + name + " implements org.bimserver.shared.ServiceInterface {\n");
        out.println("\tprivate BlockingInterface service;\n");
        out.println("\tprivate SocketRpcController rpcController;\n");
        out.println("\tpublic ProtocolBuffersServiceInterfaceImplementation(String address, int port) {");
        out.println("\t\tBlockingRpcChannel rpcChannel = RpcChannels.newBlockingRpcChannel(SocketRpcConnectionFactories.createRpcConnectionFactory(address, port));");
        out.println("\t\trpcController = new SocketRpcController();");
        out.println("\t\tservice = ServiceInterface.newBlockingStub(rpcChannel);");
        out.println("\t}\n");
        for (SMethod sMethod : sService.getMethods()) {
            String fullResultType = "void";
            if (sMethod.returnsVoid()) {
                out.print("\tpublic void " + sMethod.getName() + "(");
            } else {
                fullResultType = sMethod.getReturnType().getName();
                if (sMethod.isAggregateReturnType()) {
                    fullResultType = sMethod.getReturnType().getName() + "<" + sMethod.getGenericReturnType().getName() + ">";
                }
                out.print("\tpublic " + fullResultType + " " + sMethod.getName() + "(");
            }
            for (SParameter parameter : sMethod.getParameters()) {
                if (parameter.isAggregate()) {
                    out.print(parameter.getType().getName() + "<" + parameter.getGenericType().getName() + "> " + parameter.getName() + (parameter.isLast() ? "" : ", "));
                } else {
                    out.print(parameter.getType().getName() + " " + parameter.getName() + (parameter.isLast() ? "" : ", "));
                }
            }
            out.println(") {");
            out.println("\t\ttry {");
            String requestClassName = StringUtils.firstUpperCase(sMethod.getName()) + "Request";
            String responseClassName = StringUtils.firstUpperCase(sMethod.getName()) + "Response";
            out.println("\t\t\t" + requestClassName + ".Builder requestBuilder = " + requestClassName + ".newBuilder();");
            for (SParameter parameter : sMethod.getParameters()) {
                if (parameter.isAggregate()) {
                    out.println("\t\t\tfor (" + parameter.getGenericType() + " val : " + parameter.getName() + ") {");
                    if (parameter.getType().isPrimitive() || parameter.getType().getInstanceClass() == String.class) {
                        out.println("\t\t\t\t" + parameter.getGenericType().getInstanceClass().getSimpleName() + " v = val;");
                    } else {
                        out.println("\t\t\t\tProtocolBuffersService." + parameter.getGenericType().getInstanceClass().getSimpleName() + " v = null;");
                    }
                    out.println("\t\t\t\trequestBuilder.add" + StringUtils.firstUpperCase(parameter.getName()) + "(v);");
                    out.println("\t\t\t}");
                } else if (parameter.getType().getInstanceClass().isAssignableFrom(DataHandler.class)) {
                    out.println("\t\t\tByteString bs = ByteString.copyFrom(BinUtils.readInputStream(" + parameter.getName() + ".getInputStream()));");
                    out.println("\t\t\trequestBuilder.set" + StringUtils.firstUpperCase(parameter.getName()) + "(bs);");
                } else if (parameter.getType().getInstanceClass().isPrimitive()) {
                    out.println("\t\t\trequestBuilder.set" + StringUtils.firstUpperCase(parameter.getName()) + "(" + parameter.getName() + ");");
                } else if (parameter.getType().getInstanceClass().isEnum()) {
                    out.println("\t\t\trequestBuilder.set" + StringUtils.firstUpperCase(parameter.getName()) + "(ProtocolBuffersService." + parameter.getType().getInstanceClass().getSimpleName() + ".values()[" + parameter.getName() + ".ordinal()]);");
                } else if (parameter.getType().getInstanceClass().isPrimitive() || parameter.getType().getInstanceClass() == String.class) {
                    out.println("\t\t\trequestBuilder.set" + StringUtils.firstUpperCase(parameter.getName()) + "(" + parameter.getName() + ");");
                } else {
                    out.println("\t\t\tProtocolBuffersService." + parameter.getType().getInstanceClass().getSimpleName() + ".Builder newVal = " + parameter.getType().getInstanceClass().getSimpleName() + ".newBuilder();");
                    genServiceInterfaceToProtocolBuffers(out, parameter.getName(), "newVal", parameter.getType());
                    out.println("\t\t\trequestBuilder.set" + StringUtils.firstUpperCase(parameter.getName()) + "(newVal.build());");
                }
            }
            out.println("\t\t\t" + requestClassName + " request = requestBuilder.build();");
            if (sMethod.returnsVoid()) {
                out.println("\t\t\tservice." + sMethod.getName() + "(rpcController, request);");
            } else {
                out.println("\t\t\t" + responseClassName + " response = service." + sMethod.getName() + "(rpcController, request);");
                if (sMethod.isListReturnType()) {
                    out.println("\t\t\t" + fullResultType + " realResult = new ArrayList<" + sMethod.getGenericReturnType().getName() + ">();");
                    String fullTypeName = sMethod.getGenericReturnType().getName();
                    if (sMethod.getGenericReturnType().isPrimitive() || sMethod.getGenericReturnType().getInstanceClass() == String.class) {
                    } else {
                        fullTypeName = "ProtocolBuffersService." + sMethod.getGenericReturnType().getName();
                    }
                    out.println("\t\t\tList<" + fullTypeName + "> originalList = response.getValueList();");
                    out.println("\t\t\tfor (" + fullTypeName + " val : originalList) {");
                    if (sMethod.getGenericReturnType().getInstanceClass() == String.class) {
                        out.println("\t\t\t\trealResult.add(val);");
                    } else if (sMethod.getGenericReturnType().isEnum()) {
                        out.println("\t\t\t\trealResult.add(" + sMethod.getGenericReturnType().getName() + ".values()[val.ordinal()]);");
                    } else {
                        out.println("\t\t\t\t" + sMethod.getGenericReturnType().getName() + " v = " + genInitializerCode(sMethod.getGenericReturnType().getInstanceClass()) + ";");
                        genProtocolBuffersToServiceInterface(out, "val", "v", sMethod.getGenericReturnType(), sMethod);
                        out.println("\t\t\t\trealResult.add(v);");
                    }
                    out.println("\t\t\t}");
                    out.println("\t\treturn realResult;");
                } else if (sMethod.getGenericReturnType().isSet()) {
                    out.println("\t\t\t" + fullResultType + " realResult = new HashSet<" + sMethod.getGenericReturnType().getName() + ">();");
                    String fullTypeName = "";
                    if (sMethod.getGenericReturnType().isPrimitive() || sMethod.getGenericReturnType().isString()) {
                    } else {
                        fullTypeName = "ProtocolBuffersService." + sMethod.getGenericReturnType().getInstanceClass().getSimpleName();
                    }
                    out.println("\t\t\tList<" + fullTypeName + "> originalList = response.getValueList();");
                    out.println("\t\t\tfor (" + fullTypeName + " val : originalList) {");
                    if (sMethod.getGenericReturnType().isString()) {
                        out.println("\t\t\t\trealResult.add(val);");
                    } else if (sMethod.getGenericReturnType().isEnum()) {
                        out.println("\t\t\t\trealResult.add(" + sMethod.getGenericReturnType().getName() + ".values()[val.ordinal()]);");
                    } else {
                        out.println("\t\t\t\t" + sMethod.getGenericReturnType().getName() + " v = " + genInitializerCode(sMethod.getGenericReturnType().getInstanceClass()) + ";");
                        genProtocolBuffersToServiceInterface(out, "val", "v", sMethod.getGenericReturnType(), sMethod);
                        out.println("\t\t\t\trealResult.add(v);");
                    }
                    out.println("\t\t\t}");
                    out.println("\t\treturn realResult;");
                } else if (sMethod.getReturnType().isPrimitive() || sMethod.getReturnType().isString()) {
                    out.println("\t\t\treturn response.getValue();");
                } else if (sMethod.getReturnType().isDate()) {
                    out.println("\t\t\treturn new java.util.Date(response.getValue());");
                } else if (sMethod.getGenericReturnType().isEnum()) {
                    out.println("\t\t\treturn null;");
                } else {
                    out.println("\t\t\t" + sMethod.getGenericReturnType().getName() + " realResult = new " + sMethod.getReturnType().getName() + "();");
                    out.println("\t\t\t" + "ProtocolBuffersService." + sMethod.getReturnType().getName() + " val = response.getValue();");
                    genProtocolBuffersToServiceInterface(out, "val", "realResult", sMethod.getReturnType(), sMethod);
                    out.println("\t\t\treturn realResult;");
                }
            }
            out.println("\t\t} catch (Exception e) {}");
            if (!sMethod.returnsVoid()) {
                out.println("\t\treturn " + getDefaultLiteralCode(sMethod.getGenericReturnType().getInstanceClass()) + ";");
            }
            out.println("\t}\n");
        }
        out.println('}');
        out.close();
    } catch (FileNotFoundException e) {
        LOGGER.error("", e);
    }
}
Also used : SParameter(org.bimserver.shared.meta.SParameter) FileNotFoundException(java.io.FileNotFoundException) SMethod(org.bimserver.shared.meta.SMethod) DataHandler(javax.activation.DataHandler) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 4 with SParameter

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

the class MetaServiceImpl method getServiceMethodParameters.

@Override
public List<SServiceParameter> getServiceMethodParameters(String serviceInterfaceName, String serviceMethodName) throws ServerException, UserException {
    List<SServiceParameter> sServiceParameters = new ArrayList<SServiceParameter>();
    SService serviceInterface = getBimServer().getServicesMap().getByName(serviceInterfaceName);
    if (serviceInterface == null) {
        throw new UserException("Service \"" + serviceInterfaceName + "\" not found");
    }
    SMethod sMethod = serviceInterface.getSMethod(serviceMethodName);
    if (sMethod == null) {
        throw new UserException("Method \"" + serviceMethodName + "\" not found in \"" + serviceInterfaceName + "\"");
    }
    for (SParameter sParameter : sMethod.getParameters()) {
        SServiceParameter sServiceParameter = new SServiceParameter();
        sServiceParameter.setName(sParameter.getName());
        sServiceParameter.setDoc(sParameter.getDoc());
        sServiceParameter.setType(createSServiceType(sParameter.getType(), false));
        sServiceParameter.setGenericType(createSServiceType(sParameter.getGenericType(), false));
        sServiceParameters.add(sServiceParameter);
    }
    return sServiceParameters;
}
Also used : SParameter(org.bimserver.shared.meta.SParameter) SServiceParameter(org.bimserver.interfaces.objects.SServiceParameter) ArrayList(java.util.ArrayList) UserException(org.bimserver.shared.exceptions.UserException) SMethod(org.bimserver.shared.meta.SMethod) SService(org.bimserver.shared.meta.SService)

Example 5 with SParameter

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

the class NotificationsManager method handleIncoming.

public void handleIncoming(JsonObject request) throws UserException, ConvertException, IOException {
    // TODO copied code from JsonHandler
    String interfaceName = request.get("interface").getAsString();
    String methodName = request.get("method").getAsString();
    SService sService = servicesMap.getByName(interfaceName);
    if (sService == null) {
        sService = servicesMap.getBySimpleName(interfaceName);
    }
    if (sService == null) {
        throw new UserException("No service found with name " + interfaceName);
    }
    SMethod method = sService.getSMethod(methodName);
    if (method == null) {
        SMethod alternative = servicesMap.findMethod(methodName);
        if (alternative == null) {
            throw new UserException("Method " + methodName + " not found on " + interfaceName);
        } else {
            throw new UserException("Method " + methodName + " not found on " + interfaceName + " (suggestion: " + alternative.getService().getSimpleName() + ")");
        }
    }
    KeyValuePair[] parameters = new KeyValuePair[method.getParameters().size()];
    if (request.has("parameters")) {
        JsonObject parametersJson = request.getAsJsonObject("parameters");
        for (int i = 0; i < method.getParameters().size(); i++) {
            SParameter parameter = method.getParameter(i);
            if (parametersJson.has(parameter.getName())) {
                parameters[i] = new KeyValuePair(parameter.getName(), converter.fromJson(parameter.getType(), parameter.getGenericType(), parametersJson.get(parameter.getName())));
            } else {
                LOGGER.error("Missing parameters: " + method.getName() + " -> " + parameter.getName());
            }
        }
    } else {
        throw new UserException("Missing 'parameters' field");
    }
    try {
        method.invoke(sService.getInterfaceClass(), service, parameters);
    } catch (ServiceException e) {
        LOGGER.error("", e);
    } catch (ReflectorException e) {
        LOGGER.error("", e);
    }
}
Also used : KeyValuePair(org.bimserver.shared.reflector.KeyValuePair) ServiceException(org.bimserver.shared.exceptions.ServiceException) SParameter(org.bimserver.shared.meta.SParameter) ReflectorException(org.bimserver.shared.reflector.ReflectorException) JsonObject(com.google.gson.JsonObject) UserException(org.bimserver.shared.exceptions.UserException) SMethod(org.bimserver.shared.meta.SMethod) SService(org.bimserver.shared.meta.SService)

Aggregations

SParameter (org.bimserver.shared.meta.SParameter)8 SMethod (org.bimserver.shared.meta.SMethod)7 UserException (org.bimserver.shared.exceptions.UserException)3 SService (org.bimserver.shared.meta.SService)3 KeyValuePair (org.bimserver.shared.reflector.KeyValuePair)3 JsonObject (com.google.gson.JsonObject)2 ArrayList (java.util.ArrayList)2 CannotCompileException (javassist.CannotCompileException)2 CtClass (javassist.CtClass)2 CtConstructor (javassist.CtConstructor)2 CtField (javassist.CtField)2 CtMethod (javassist.CtMethod)2 NotFoundException (javassist.NotFoundException)2 DataHandler (javax.activation.DataHandler)2 ByteString (com.google.protobuf.ByteString)1 Descriptor (com.google.protobuf.Descriptors.Descriptor)1 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 DynamicMessage (com.google.protobuf.DynamicMessage)1 Builder (com.google.protobuf.Message.Builder)1