コード例 #1
0
 /**
  * Private helper method to indent each line of a code block. This method indents code by one tab.
  * Internally, it calls the indentCode(code, depth) method and is provided for convenience only.
  *
  * @param code Code to be indented
  * @return Code indented by one tab
  */
 private static String indentCode(final String code) {
   return WorkerThreadGenerator.indentCode(code, 1);
 }
コード例 #2
0
  /**
   * Create code for method body of the run() method that is generated in each of the child thread
   * worker classes. This function will generate code to unmarshal a request message, call the RPC
   * method and finally marshal the response message (if RPC method has any output).
   *
   * <p>The code generation was moved to this method for better readability.
   *
   * @param operation FOperation object from WSDL parser
   * @param rpcMethodName Name of RPC method
   * @return Code for method body of run() method
   */
  private String createRunMethodBody(final FOperation operation, final String rpcMethodName) {
    String methodBody = "";

    // Create JSON marshaller object
    if (null != operation.getInputMessage() || null != operation.getOutputMessage()) {
      methodBody +=
          String.format(
              "// Create JSON marshaller\n" + "%s marshaller = new %s();\n\n",
              JSONMarshallerGenerator.MARSHALLER_CLASS_NAME,
              JSONMarshallerGenerator.MARSHALLER_CLASS_NAME);
    }

    // Operation has input message?
    if (null != operation.getInputMessage()) {
      String inputMessageClassName = this.getInputMessageName(operation);

      // Create code to convert JSON code to a bean object
      methodBody +=
          String.format(
              "// Unmarshal JSON code from request\n"
                  + "%s requestBeanObject = (%s)marshaller.jsonToInstance(%s.class, this.requestMessage.payload());\n\n",
              inputMessageClassName, inputMessageClassName, inputMessageClassName);
    }

    // Get name of output message (if any)
    String outputMessageClassName = "";
    if (null != operation.getOutputMessage()) {
      outputMessageClassName = this.getOutputMessageName(operation);
    }

    // Create code to call RPC method
    methodBody +=
        String.format(
            "// Call service operation\n"
                + "LOGGER.info(\"Processing '%s()' request...\");\n"
                + "%sthis.serviceProvider.%s(%s);",
            WorkerThreadGenerator.firstLetterLowercase(rpcMethodName),
            (null != operation.getOutputMessage()
                ? String.format("%s responseBeanObject = ", outputMessageClassName)
                : ""), // Method has return value?
            operation.getOperationName(),
            (null != operation.getInputMessage() ? "requestBeanObject" : "")); // Method has input?

    // Operation has output message?
    if (null != operation.getOutputMessage()) {
      // Create code to convert bean object to JSON code
      methodBody +=
          String.format(
              "\n\n"
                  + "// Marshal bean and create response message\n"
                  + "String jsonResponse = marshaller.instanceToJSON(responseBeanObject);\n"
                  + "%s responseMessage = new %s(this.requestMessage.uuid(), this.requestMessage.method(), jsonResponse);\n\n",
              this.messageClassFullName, this.messageClassFullName);

      // Create code to send response
      methodBody +=
          String.format(
              "// Send response to client\n"
                  + "LOGGER.info(\"Responding to '%s()' request...\");\n"
                  + "%s.sendMessage(this.webSocket, responseMessage.asString());",
              WorkerThreadGenerator.firstLetterLowercase(rpcMethodName), this.interfaceName);
    }

    // Surround code with try..catch-block
    methodBody =
        String.format(
            "try {\n"
                + WorkerThreadGenerator.indentCode(methodBody)
                + "}\n"
                + "catch (Exception e) {\n"
                + "\tString jsonResponse = String.format(\"{ \\\"Error\\\": \\\"%%s\\\" }\", e.getMessage());\n"
                + "\t%s responseMessage = new %s(this.requestMessage.uuid(), this.requestMessage.method(), jsonResponse);\n\n"
                + "\tLOGGER.error(\"Error: \" + e.getMessage());\n"
                + "\t%s.sendMessage(this.webSocket, responseMessage.asString());\n"
                + "}",
            this.messageClassFullName,
            this.messageClassFullName,
            this.interfaceName);

    return methodBody;
  }