jtaxisservice.java

来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 206 行

JAVA
206
字号
package Jt.axis;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

import Jt.xml.*;
import Jt.*;


/**
 * Axis Web Service used to interface with the Jt Framework. 
 */

public class JtAxisService extends JtObject {


  public static final String JtCLASS_NAME = JtAxisService.class.getName(); 
  private static final long serialVersionUID = 1L;
  private transient JtScriptInterpreter reader = null;
  private transient JtXMLHelper xmlHelper = null;
  private transient JtFactory factory = new JtFactory ();

  public JtAxisService() {
  }


  private String stackTrace (Exception ex ) {
      ByteArrayOutputStream bstream;


      if (ex == null)
          return (null);

      bstream = new ByteArrayOutputStream ();
      
      //ex = (Exception) getValue (this, "objException");

      ex.printStackTrace (new PrintWriter (bstream, true));


      return (bstream.toString ());


  }
  

  // convertToXML: convert script output to XML

  String convertToXML (Object scriptOutput) {

      Exception ex;
      JtMessage msg = new JtMessage ();
      JtList lst;
      JtRemoteException jre;

      ex = (Exception) 
      getValue (reader, "objException");

      if (xmlHelper == null)
          xmlHelper = new JtXMLHelper ();

      if (ex != null)
          handleTrace ("JtAxisService.convertToXML(Exception):"  + ex);
      /*   
    if (ex == null) {
      msg.setMsgId ("JtCONVERT_OBJECT_TO_XML");
      msg.setMsgContent (scriptOutput);
      return ((String) sendMessage (xmlHelper, msg));
    }
       */
      lst = new JtList ();

      msg.setMsgId (JtList.JtADD);
      if (scriptOutput != null) {
          msg.setMsgContent (scriptOutput);
          //sendMessage (lst, msg);
          lst.processMessage (msg);
      } else {
          msg.setMsgContent (new JtNull ());
          //sendMessage (lst, msg);
          lst.processMessage (msg);       
      }
      //msg.setMsgContent (new JtException (ex.getMessage ())); // check

      // Remote Exception

      if (ex != null) {
          jre = new JtRemoteException (ex.getMessage());
          jre.setTrace (stackTrace (ex));

          msg.setMsgContent (jre);
          //sendMessage (lst, msg);
          lst.processMessage (msg);  
      }

      msg.setMsgId (JtObject.JtXML_ENCODE);
      msg.setMsgContent (lst);
      return ((String) sendMessage (xmlHelper, msg));

  }


   /**
     * Process Jt Messages that have been previously converted to the XML format.
     * 
     * @param xmlMessage  Jt Message in XML format
     * @return reply in XML format
     */


  public String processMessage (String xmlMessage) {
  JtMessage msg;
  Object scriptOutput = null;
      //MessageContext mc = MessageContext.
         //getCurrentContext();
      //Session session = mc.getSession();
      //String name = (String)session.get("name");

    if (xmlMessage == null)
      return (null);

    //setLogFile ("c:\\log.txt"); 
    this.setLogging(true);
    this.setLogLevel(JtObject.JtMIN_LOG_LEVEL+1);

    handleTrace ("JtAxisService.processMessage (input) ...\n"  + xmlMessage,
            JtObject.JtMIN_LOG_LEVEL+1);
    
    
    //msg = new JtMessage ("JtPARSE");
    msg = new JtMessage (JtInterpreter.JtINTERPRET);

    // Send Jt script to the Message Interpreter for execution


    if (reader == null) {
      reader = (JtScriptInterpreter) factory.createObject (JtScriptInterpreter.JtCLASS_NAME, 
        "root");
      //setValue (reader, "remoteInvocation", "true");
      reader.setRemoteInvocation(true);
    }

    //setValue (reader, "content", xmlMessage);
    //setValue (reader, "objException", null);
    reader.setContent(xmlMessage);
    reader.setObjException(null);

    scriptOutput = factory.sendMessage (reader, msg);
    handleTrace ("JtAxisService.processMessage (output) ...\n" + convertToXML (scriptOutput), 
            JtObject.JtMIN_LOG_LEVEL+1);

    return (convertToXML (scriptOutput));


  }


 /**
   * Demonstrates all the messages processed by JtAxisService. 
   */


  public static void main(String[] args) {

    JtObject main = new JtObject ();
    JtMessage msg1, msg2;
    JtAxisService adapter;
    String str;

    //main.setLogFile ("log.txt");


    if (args.length < 1) {
      System.err.println ("Usage: java JtAxisService uri");
      System.exit (1);
    }

    msg1 = new JtMessage ();
    msg2 = new JtMessage ();


    main.createObject (JtFile.JtCLASS_NAME, "file");
    main.setValue ("file", "name", args[0]);    
    main.setValue (msg1, "msgId", JtFile.JtCONVERT_TO_STRING);

    str = (String) main.sendMessage ("file", msg1);

    msg1.setMsgId ("JtMessage1");
    msg2.setMsgId ("JtMessage2");


    // Create XML adapter 


    adapter = (JtAxisService) main.createObject (JtAxisService.JtCLASS_NAME, 
     "adapter");

    adapter.processMessage (str);
      

  }

}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?