jtwebservicesadapter.java

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

JAVA
463
字号
package Jt.axis;
import Jt.*;
import Jt.examples.HelloWorld;
import Jt.xml.*;



/**
  * Jt Adapter for web services
  */ 

public class JtWebServicesAdapter extends JtAdapter  {

  public static final String JtCLASS_NAME = JtWebServicesAdapter.class.getName(); 
  public static final String JtCREATE_PROXY = "JtCREATE_PROXY"; 
  private static final long serialVersionUID = 1L;
  JtXMLConverter xmlConverter = null;  
  JtAxisAdapter axisAdapter = null;
  private String url = null;
  private String remoteLogFile; // deprecated



  public JtWebServicesAdapter () {
  }


 /**
  * Specifies the service URL.
  * @param url service url
  */

  public void setUrl (String url) {
    this.url = url;
  }

 /**
   * Returns the service URL. 
   */

  public String getUrl () {
    return (url);
  }


 /**
   * Returns the name of the remote log file (deprecated)
   */

  public String getRemoteLogFile () {
    return (remoteLogFile);
  }


 /**
  * Specifies the name of the remote log file (deprecated)
  * @param remoteLogFile remote log file
  */

  public void setRemoteLogFile (String remoteLogFile) {
    this.remoteLogFile = remoteLogFile;
  }





  // Propagate Exceptions 

  private Exception propagateException (JtObject obj)
  {
      Exception ex;

      if (obj == null)
          return null;

      ex = (Exception) 
      //super.getValue (obj, "objException");
      obj.getObjException();

      if (ex != null)
          this.setObjException(ex);
      //super.setValue (this, "objException", ex);

      return (ex);
  }


  // Create the object that will perform the service
  // 
 

  public Object createProxy (Object class_name, Object id) {
    String xmlMsg;
    Object output;
    JtAxisProxy proxy;

    //if (class_name == null || id == null) { // check
    if (class_name == null) { // check
      handleError ("JtWebServicesAdapter.createObject: invalid parameters");
      return (null);
    }
    
    if (xmlConverter == null)
      xmlConverter = new JtXMLConverter ();

    xmlConverter.createObject (class_name, id);
  
    xmlMsg = (String) xmlConverter.processMessage (new JtMessage (JtXMLConverter.JtCONVERT));    


    output = doit (xmlMsg);
    xmlConverter = null;

    if (output == null) {
        handleTrace ("JtWebservicesAdapter.createObject: unable to create remote object " + id);
        return (null);
    }    
    
    // Create the proxy 
    
    proxy = new JtAxisProxy ();
    proxy.setSubject(this);
    proxy.setProxyId(output);
      
    return (proxy);
    //return (output);
  }

/*
  private void updateRemoteLogFile () {

    String xmlMsg;
    JtMessage msg;
    JtXMLConverter xmlconverter;


    if (axisAdapter == null)
      return;
    
    xmlConverter = new JtXMLConverter ();

    xmlConverter.setValue ("this", "logFile", remoteLogFile);

    xmlMsg = (String) xmlConverter.processMessage (new JtMessage (JtXMLConverter.JtCONVERT));    


    msg = new JtMessage ("JtSCRIPT");
    msg.setMsgContent (xmlMsg);

    super.sendMessage (axisAdapter, msg);


  }
*/  
/*
  public void destroyObject (Object id) {
    String xmlMsg;
    Object output;

    if (id == null) { // check
      handleError ("JtWebServicesAdapter.createObject: invalid parameters");
      return;
    }
    
    if (xmlConverter == null)
      xmlConverter = new JtXMLConverter ();

    xmlConverter.destroyObject (id);
  
    xmlMsg = (String) xmlConverter.processMessage (new JtMessage (JtXMLConverter.JtCONVERT));    


    output = doit (xmlMsg);
    xmlConverter = null;

  }
*/  
  public void removeRemoteObject (Object id) {
      String xmlMsg;
      //Object output;

      if (id == null) { // check
        handleError ("JtWebServicesAdapter.createObject: invalid parameters");
        return;
      }
      
      if (xmlConverter == null)
        xmlConverter = new JtXMLConverter ();

      xmlConverter.removeObject (id);
   
      xmlMsg = (String) xmlConverter.processMessage (new JtMessage (JtXMLConverter.JtCONVERT));    


      doit (xmlMsg);
      xmlConverter = null;

    }

  private Object doit (String xmlMsg) {

    JtMessage msg;
    Object output;
    JtFactory factory = new JtFactory ();

    if (xmlMsg == null)
     return (null); // check


    handleTrace ("JtWebServicesAdapter.doit() ....\n"
                 + xmlMsg, JtObject.JtMIN_LOG_LEVEL);

    msg = new JtMessage (JtScriptInterpreter.JtSCRIPT);
    msg.setMsgContent (xmlMsg);


    if (url == null) {
      handleError ("JtWebServicesAdapter.sendMessage: invalid url (null)");
      return (null);     
    }

    if (axisAdapter == null) {
      axisAdapter = (JtAxisAdapter) factory.createObject (JtAxisAdapter.JtCLASS_NAME);
      
      //super.setValue (axisAdapter, "url", url);
      axisAdapter.setUrl(url);
      //updateRemoteLogFile ();
    }

    output = factory.sendMessage (axisAdapter, msg);

    //this.setObjException(null);
    propagateException (axisAdapter);
    return (output);


  }


  public Object sendRemoteMessage (Object id, Object msgid) {

    String xmlMsg;
    //JtMessage msg;
    Object output;
    //Exception ex;

    if (id == null || msgid == null) {
      handleError ("JtWebServicesAdapter.sendMessage: invalid parameters");
      return (null);
    }


    if (xmlConverter == null)
      xmlConverter = new JtXMLConverter ();


    // Convert Jt API calls to XML calls

    xmlConverter.sendMessage (id, msgid);

    //xmlMsg = (String) super.sendMessage (xmlConverter, new JtMessage ("JtCONVERT"));    
    xmlMsg = (String) xmlConverter.processMessage (new JtMessage (JtXMLConverter.JtCONVERT));    

    xmlConverter = null;

    output = doit (xmlMsg);

    //propagateException (axisAdapter);

    return (output);

  } 

 
  public void setRemoteValue (Object id, Object att, 
    Object value) {
    String xmlMsg;

    if (id == this) {
       super.setValue (id, att, value);
       return;
    }
    if (id == null || att == null) { // check null value
      handleError ("JtWebServicesAdapter.setValue: invalid parameters");
      return;
    }


    if (xmlConverter == null)
      xmlConverter = new JtXMLConverter ();

    // Convert Jt call to XML

    xmlConverter.setValue (id, att, value);
    //xmlMsg = (String) super.sendMessage (xmlConverter, new JtMessage ("JtCONVERT"));    
    xmlMsg = (String) xmlConverter.processMessage (new JtMessage (JtXMLConverter.JtCONVERT));    

    xmlConverter = null;

    doit (xmlMsg);
    //propagateException (axisAdapter);
  }


  public Object getRemoteValue (Object id, Object att) {
      String xmlMsg;

      //if (id == this) {
      //    super.setValue (id, att, value);
      //    return;
      //}
      
      if (id == null || att == null) { // check null value
          handleError ("JtWebServicesAdapter.getValue: invalid parameters");
          return (null);
      }


      if (xmlConverter == null)
          xmlConverter = new JtXMLConverter ();

      // Convert Jt call to XML

      xmlConverter.getValue (id, att);
      //xmlMsg = (String) super.sendMessage (xmlConverter, new JtMessage ("JtCONVERT"));    
      xmlMsg = (String) xmlConverter.processMessage (new JtMessage (JtXMLConverter.JtCONVERT));    

      xmlConverter = null;

      return (doit (xmlMsg));
      //propagateException (axisAdapter);
  }


  
  
  // Process object messages

  public Object processMessage (Object event) {

      String msgid = null;
      JtMessage e = (JtMessage) event;
      //Object content;

      if (e == null)
          return null;

      msgid = (String) e.getMsgId ();

      if (msgid == null)
          return null;

      //content = e.getMsgContent();
      
      this.setObjException(null);

      if (msgid.equals (JtObject.JtREMOVE)) {
          return (null);     
      }
      
      if (msgid.equals (JtWebServicesAdapter.JtCREATE_PROXY)) {
          return (createProxy(e.getMsgContent(), e.getMsgData()));
      }
      
      if (msgid.equals (JtObject.JtSEND_MESSAGE)) {
          return (sendRemoteMessage(e.getMsgContent(), e.getMsgData()));     
      }

      if (msgid.equals (JtObject.JtSET_VALUE)) {
          setRemoteValue(e.getMsgSubject(), e.getMsgContent(), e.getMsgData());
          return (null);
      }
      
      if (msgid.equals (JtObject.JtGET_VALUE)) {
          return (getRemoteValue(e.getMsgSubject(), e.getMsgContent()));     
      }
      
      if (msgid.equals (JtObject.JtREMOVE_OBJECT)) {
          removeRemoteObject(e.getMsgContent());
          return (null);
      }
      
      return (super.processMessage (event));
      //handleError ("processMessage: invalid message ID:" + msgid);
      //return (null);
  }



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

  public static void main(String[] args) {

    JtObject factory = new JtFactory ();

    JtWebServicesAdapter adapter;
    String reply = null;
    Exception ex;
    JtMessage msg;
    JtProxy proxy;

    // Create the Jt Web service Adapter

    adapter = (JtWebServicesAdapter) factory.createObject (JtWebServicesAdapter.JtCLASS_NAME);

    // Set the service url property (if it is not present in the resource file)

    if (adapter.getUrl() == null)
        adapter.setUrl("http://localhost:8080/axis/services/JtAxisService");
    //factory.setValue (adapter, "url", 
    // "http://localhost:8080/axis/services/JtAxisService");


    // Create a remote instance of the HelloWorld class. The Jt Web service adapter
    // can be used to create remote objects. A local proxy is returned.


    msg = new JtMessage (JtWebServicesAdapter.JtCREATE_PROXY);
    msg.setMsgContent(HelloWorld.JtCLASS_NAME);
    //msg.setMsgData("helloWorld");
    
    proxy = (JtProxy) factory.sendMessage (adapter, msg);
    

    //factory.setValue (proxy, "greetingMessage", "Hello there...");
    //factory.setValue (proxy, "logLevel", "0");
    //factory.setValue (proxy, "logging", "true");
    
    //System.out.println (factory.getValue (proxy, "greetingMessage"));       

    msg = new JtMessage (HelloWorld.JtHELLO);

    // Send JtHello to the remote helloWorld object

    reply = (String) factory.sendMessage (proxy, msg);

    // Display the reply unless an exception is detected.
    // The remote exception gets propagated from the
    // remote object to the local proxy
    
    //ex = (Exception) factory.getValue (proxy, "objException"); 
    ex = (Exception) proxy.getObjException();

    if (ex == null)    
      System.out.println (reply);

    

    // Remove the remote instance and the adapter

    //myService.removeObject ("helloWorld");
    factory.removeObject (proxy);
    factory.removeObject(adapter);

  }

}


⌨️ 快捷键说明

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