Search in sources :

Example 6 with SMethod

use of org.bimserver.shared.meta.SMethod 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 7 with SMethod

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

the class RealtimeReflectorFactoryBuilder method build1.

private void build1(String newClassPrefix, Class<? extends PublicInterface> interfaceClass, org.bimserver.shared.meta.SService sService) {
    try {
        CtClass reflectorImplClass = pool.makeClass(GENERATED_CLASSES_PACKAGE + "." + interfaceClass.getSimpleName() + "Impl" + newClassPrefix);
        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, GeneratedNeighbourClass.class, 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 8 with SMethod

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

the class RealtimeReflectorFactoryBuilder method build2.

private void build2(String newClassPrefix, Class<? extends PublicInterface> interfaceClass, org.bimserver.shared.meta.SService sService) {
    try {
        CtClass reflectorImplClass = pool.makeClass(GENERATED_CLASSES_PACKAGE + "." + interfaceClass.getSimpleName() + "Reflector" + newClassPrefix);
        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, GeneratedNeighbourClass.class, 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 9 with SMethod

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

the class JsonReflector method callMethod.

@Override
public Object callMethod(String interfaceName, String methodName, Class<?> definedReturnType, KeyValuePair... args) throws ServerException, UserException {
    try {
        ObjectNode request = OBJECT_MAPPER.createObjectNode();
        request.put("interface", interfaceName);
        request.put("method", methodName);
        ObjectNode parameters = OBJECT_MAPPER.createObjectNode();
        for (KeyValuePair arg : args) {
            parameters.set(arg.getFieldName(), converter.toJson(arg.getValue()));
        }
        request.set("parameters", parameters);
        ObjectNode requestObject = OBJECT_MAPPER.createObjectNode();
        requestObject.set("request", request);
        JsonNode jsonResult = call(requestObject);
        if (!isOneWay()) {
            if (jsonResult == null) {
                return null;
            }
            ObjectNode response = (ObjectNode) jsonResult.get("response");
            if (response.has("exception")) {
                ObjectNode exceptionJson = (ObjectNode) response.get("exception");
                String exceptionType = exceptionJson.get("__type").asText();
                String message = exceptionJson.has("message") ? exceptionJson.get("message").asText() : "unknown";
                if (exceptionType.equals(UserException.class.getSimpleName())) {
                    if (exceptionJson.has("errorCode")) {
                        String errorType = exceptionJson.get("errorType").asText();
                        if (errorType.equals("DefaultErrorCode")) {
                            throw new UserException(message, DefaultErrorCode.fromCode(exceptionJson.get("errorCode").asInt()));
                        } else if (errorType.contentEquals("DeserializerErrorCode")) {
                            throw new UserException(message, DeserializerErrorCode.fromCode(exceptionJson.get("errorCode").asInt()));
                        } else {
                            throw new UserException(message, ErrorCode.fromCode(exceptionJson.get("errorCode").asInt()));
                        }
                    } else {
                        throw new UserException(message);
                    }
                } else if (exceptionType.equals(ServerException.class.getSimpleName())) {
                    String errorType = exceptionJson.get("errorType").asText();
                    if (errorType.equals("DefaultErrorCode")) {
                        throw new ServerException(message, DefaultErrorCode.fromCode(exceptionJson.get("errorCode").asInt()));
                    } else if (errorType.contentEquals("DeserializerErrorCode")) {
                        throw new ServerException(message, DeserializerErrorCode.fromCode(exceptionJson.get("errorCode").asInt()));
                    } else {
                        throw new ServerException(message, ErrorCode.fromCode(exceptionJson.get("errorCode").asInt()));
                    }
                } else {
                    if (exceptionJson.has("errorCode")) {
                        throw new ServerException(message, ErrorCode.fromCode(exceptionJson.get("errorCode").asInt()));
                    } else {
                        throw new ServerException(message);
                    }
                }
            } else if (response.has("result")) {
                Object result = response.get("result");
                SMethod method = servicesMap.getBySimpleName(interfaceName).getSMethod(methodName);
                return converter.fromJson(method.getReturnType(), method.getGenericReturnType(), result);
            } else {
                return null;
            }
        } else {
            return null;
        }
    } catch (HttpHostConnectException e) {
        throw new UserException(e.getMessage(), DefaultErrorCode.HTTP_CONNECTION_REFUSED);
    } catch (UserException e) {
        throw e;
    } catch (ServerException e) {
        throw e;
    } catch (Exception e) {
        throw new UserException(e);
    }
}
Also used : ServerException(org.bimserver.shared.exceptions.ServerException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) KeyValuePair(org.bimserver.shared.reflector.KeyValuePair) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) JsonNode(com.fasterxml.jackson.databind.JsonNode) UserException(org.bimserver.shared.exceptions.UserException) SMethod(org.bimserver.shared.meta.SMethod) ReflectorException(org.bimserver.shared.reflector.ReflectorException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException)

Example 10 with SMethod

use of org.bimserver.shared.meta.SMethod 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

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