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;
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations