use of org.bimserver.shared.meta.SMethod 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);
}
}
use of org.bimserver.shared.meta.SMethod 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);
}
}
use of org.bimserver.shared.meta.SMethod in project BIMserver by opensourceBIM.
the class MetricsRegistry method getMetrics.
public synchronized SMetrics getMetrics() {
SMetrics sMetrics = new SMetrics();
for (SService sService : methodStats.keySet()) {
SInterfaceMetric interfaceMetric = new SInterfaceMetric();
interfaceMetric.setName(sService.getSimpleName());
sMetrics.getInterfaces().add(interfaceMetric);
for (SMethod sMethod : methodStats.get(sService).keySet()) {
SMethodMetric sMethodMetric = new SMethodMetric();
sMethodMetric.setName(sMethod.getName());
interfaceMetric.getMethods().add(sMethodMetric);
MethodStats methodStats = getMethodStats(sMethod);
sMethodMetric.setNrCalls(methodStats.getNrCalls());
sMethodMetric.setAverageMs(methodStats.getAverageNanoSecondsPerCall() / 1000000);
}
}
return sMetrics;
}
use of org.bimserver.shared.meta.SMethod 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);
}
}
use of org.bimserver.shared.meta.SMethod in project BIMserver by opensourceBIM.
the class MetaServiceImpl method getServiceMethods.
@Override
public List<SServiceMethod> getServiceMethods(String serviceInterfaceName) throws ServerException, UserException {
List<SServiceMethod> sServiceMethods = new ArrayList<SServiceMethod>();
SService sService = getBimServer().getServicesMap().getByName(serviceInterfaceName);
if (sService == null) {
throw new UserException("Service \"" + serviceInterfaceName + "\" not found");
}
for (SMethod sMethod : sService.getMethods()) {
SServiceMethod sServiceMethod = convertMethod(sMethod);
sServiceMethods.add(sServiceMethod);
}
return sServiceMethods;
}
Aggregations