⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jtejbadapter.java

📁 Java Pattern Oriented Framework (Jt) 是为了实现Java快速开发的面向模式的框架。
💻 JAVA
字号:

package Jt.ejb;


//import javax.ejb.*;
//import javax.naming.*;
//import java.rmi.*;
//import java.beans.*;
//import java.io.*;
//import java.util.*;
import java.util.Hashtable;

import Jt.*;
import Jt.axis.JtWebServicesAdapter;
import Jt.examples.HelloWorld;



/**
 * Jt EJB Adapter. 
 */ 

public class JtEJBAdapter extends JtAdapter  {

    public static final String JtCLASS_NAME = JtEJBAdapter.class.getName(); 
    public static final String JtCREATE_PROXY = "JtCREATE_PROXY";   
    private static final long serialVersionUID = 1L;
    //private String remoteLogFile;
    private transient JtSessionFacadeHome dhome = null;
    private transient JtSessionFacade sejb = null;
    private transient Object session = null; //check - this attribute is being deprecated


    public JtEJBAdapter () {
    }




    /**
     * Returns the name of the remote log file
     */
/*
    public String getRemoteLogFile () {
        return (remoteLogFile);
    }
*/

    /**
     * Specifies the name of the remote log file
     * @param remoteLogFile remote log file
     */
/*
    public void setRemoteLogFile (String remoteLogFile) {
        this.remoteLogFile = remoteLogFile;

        if (session == null)
            initializeEJB ();
        if (session == null)
            return;
        try { 
            sejb.setLogFile(session, remoteLogFile);
        } catch (Exception ex) {
            handleException (ex);
        }    


    }
*/


    // Propagate Exceptions 

    /*
  private Exception propagateException (Object obj)
  {
    Exception ex;

    if (obj == null)
      return null;

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

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

    return (ex);
  }
     */


    private void getEJBHandle () {

        if (sejb == null) {

            dhome = (JtSessionFacadeHome) locateJtService ();
            if (dhome == null)
                return;
            try {
                sejb = dhome.create();
            } catch (Exception ex) {
                handleException (ex);
            }
        } 
    }


    private Object getSessionHandle () {

        if (sejb == null)
            return (null); 
        /*
        try {
            return (sejb.createSession ());
        } catch (Exception ex) {
            handleException (ex);
        }
         */
        return new JtFactory ();
        //return (null);

    }
    
    
    // Initialize the remote resource table
    
    private void initializeRemoteResourceTable () {
        String handle;
        Hashtable table = (new JtFactory ()).getResTable();
        
        try { 
            handle = (String) sejb.createObject (session, JtFactory.JtCLASS_NAME, null); 
        } catch (Exception ex) {
            handleException (ex);
            return;
        }  
        
        if (handle == null)
            return;
        //setRemoteValue (handle, "logging", "true");        
        setRemoteValue (handle, "resTable", table);
    }

    // Intialialize the Jt EJB

    private void initializeEJB () {


        if (sejb == null)
            getEJBHandle ();    

        if (sejb == null)
            return;

        if (session == null)
            session = getSessionHandle ();
        
        //initializeRemoteResourceTable ();

        return;


    }


    /**
     * Creates a remote instance of the specified class. 
     * @param class_name class name
     * @param id  object name
     * @return object created or  null
     */

    private Object createProxy (Object class_name, Object id) {

        JtEJBProxy proxy;
        Object output = null;

        if (class_name == null) { // check
            handleError ("JtEJBAdapter.createProxy: invalid parameters");
            return (null);
        }

        handleTrace ("JtEJBAdapter.createProxy:" + class_name + "," + id);

        // Reset the object exception
        setObjException (null);


        if (session == null)
            initializeEJB ();
        if (session == null)
            return (null);
        try { 
            output = sejb.createObject (session, class_name, id); 
        } catch (Exception ex) {
            handleException (ex);
        }    
        // Create the proxy 

        if (output == null)
            return (null);
        
        proxy = new JtEJBProxy ();
        proxy.setSubject(this);
        proxy.setProxyId(output);
        //proxy.setProxyId(id); //check     
        return (proxy);

    }

    /**
     * Invokes the service locator
     */

    private Object locateJtService () {
        Object jtservice;
        JtServiceLocator locator;
        JtFactory tmp;

        tmp = new JtFactory ();

        // Create ServiceLocator

        locator = (JtServiceLocator) tmp.createObject (JtServiceLocator.JtCLASS_NAME);


        // Activate Service Locator

        jtservice = (Object) tmp.sendMessage (locator, new JtMessage (JtObject.JtACTIVATE));
        tmp.removeObject (locator);
        return (jtservice);
    }

    /*
  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 ("JtCONVERT"));    


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

    super.sendMessage (axisAdapter, msg);


  }
     */

    /**
     * Removes an object. 
     * @param id  object id
     */

    private void removeRemoteObject (Object id) {
        //String xmlMsg;
        //Object output;

        if (id == null) { // check
            handleError ("JtEJBAdapter.removeRemoteObject: invalid parameters");
            return;
        }

        handleTrace ("JtEJBAdapter.removeRemoteObject: " + id);

        // Reset the object exception
        setObjException (null);

        /*
    if (sejb == null)
      getEJBHandle ();    

    if (sejb == null)
      return;
         */

        if (session == null)
            initializeEJB ();
        if (session == null)
            return;

        try { 
            sejb.removeObject (session, id);  // check
        } catch (Exception ex) {
            handleException (ex);
        } 
    }

    /** 
     * Gets the value of an attribute. An object is always returned.  
     * For primitive types, the corresponding object type is returned.
     * For instance, Integer is returned if
     * the attribute is of type int. 
     *
     * @param id object id
     * @param att attribute name
     * @return attribute value
     */

    private Object getRemoteValue (Object id, Object att) {
        //Object obj;


        if (id == null || att == null) {
            handleError ("JtEJBAdapter.getRemoteValue: invalid paramenters");
            return (null);
        }

        handleTrace ("JtEJBAdapter.getRemoteValue: " + id + "," + att);

        // Reset the object exception
        setObjException (null);

        if (session == null)
            initializeEJB ();
        if (session == null)
            return (null);
        try { 
            return (sejb.getValue (session, id, att)); 
        } catch (Exception ex) {
            handleException (ex);
        }
        return (null);
    }



    /**
     * Sends a Jt Message to another object. 
     * @param id    object ID
     * @param msgid message ID
     */

    private Object sendRemoteMessage (Object id, Object msgid) {

        //String xmlMsg;
        //JtMessage msg;
        Object output = null;


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

        handleTrace ("JtEJBAdapter.sendRemoteMessage:" + id  + ", "+ msgid);
        /*
    if (sejb == null)
      getEJBHandle ();    

    if (sejb == null)
      return (null);
         */

        // Reset the object exception
        setObjException (null);

        if (session == null)
            initializeEJB ();
        if (session == null)
            return (null);
        try { 
            output = sejb.sendMessage (session, id, msgid); 
        } catch (Exception ex) {
            handleException (ex);
        }    

        return (output);

    } 

    /** 
     * Sets the value of an attribute. 
     * @param id object id
     * @param att attribute name
     * @param value attribute value
     */

    private void setRemoteValue (Object id, Object att, 
            Object value) {



        if (id == null || att == null) { // check null value
            handleError ("JtEJBAdapter.setRemoteValue: invalid parameters");
            return;
        }

        handleTrace ("JtEJBAdapter.setRemoteValue:" + id + "," + att + "," + value);

        // Reset the object exception
        setObjException (null);


        if (session == null)
            initializeEJB ();
        if (session == null)
            return;

        try { 
            sejb.setValue (session, id, att, value); 
        } catch (Exception ex) {
            handleException (ex);
        }    


    }


    /**
     * Process object messages.
     * <ul>
     * <li> JtTEST - Tests the messages processes by JtEJBAdapter
     * </ul>
     * @param event message
     */

    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();

        //if (msgid.equals (JtObject.JtTEST)) {            
        //    return (test ());
        //}

        if (msgid.equals (JtEJBAdapter.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);
        }

        if (msgid.equals (JtObject.JtREMOVE)) { 

            // Removes the session
            /*
            if (sejb != null && session != null)

                try {
                    sejb.removeSession (session);
                } catch (Exception ex) {
                    handleException (ex); 
                }
            */            
            return (null);
        }

        return (super.processMessage(event));
        //handleError ("processMessage: invalid message ID:" + msgid);
        //return (null);

    }




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

    public static void main(String[] args) {

        JtObject factory = new JtFactory ();
        JtEJBAdapter adapter;
        String reply = null, tmp;
        Exception ex;
        JtMessage msg;
        JtProxy proxy;

        //main.setLogFile ("log.txt");
        // Create the Jt EJB Adapter

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


        // send a test message (JtTEST) to the adapter

        msg = new JtMessage (JtWebServicesAdapter.JtCREATE_PROXY);
        msg.setMsgContent(HelloWorld.JtCLASS_NAME);
        //msg.setMsgData("helloWorld");

        proxy = (JtProxy) factory.sendMessage (adapter, msg);
        factory.setValue (proxy, "logLevel", "0");
        factory.setValue (proxy, "logging", "true");

        //factory.setValue (proxy, "greetingMessage", "Hello there...");

        tmp = (String) factory.getValue (proxy, "greetingMessage");

        System.out.println ("greetingMessage:" + tmp);

        msg = new JtMessage (HelloWorld.JtHELLO);

        // Send JtHello to the remote helloWorld object

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

        //ex = (Exception) factory.getValue (ejbAdapter, "objException");
        ex = (Exception) proxy.getObjException(); // test


        // Display the reply unless an exception is detected

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

        // Remove the EJB adapter and the proxy

        factory.removeObject (proxy);
        factory.removeObject (adapter);

    }

}


⌨️ 快捷键说明

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