0 Replies Latest reply on May 18, 2006 1:22 AM by huishuai

    help me: applet & ejb communication

    huishuai

      I expect to show weightLabel, heightLabel when click the botton1 in EJBClientApplet,but not,why?

      I hope "pushEJB" push weight and height data to applet and show them...

      /***** DeviceDataListener ******/
      import com.thtf.ezone.ibsserver.bean.IDeviceDataBean;
      import java.rmi.*;

      public interface DeviceDataListener extends Remote
      {
      void onData( IDeviceDataBean bean ) throws RemoteException;
      }


      /***** PushEJBBean ******/

      public class PushEJBBean extends Observable implements SessionBean {
      IDeviceDataBean deviceData;
      //
      private ArrayList listeners = new ArrayList();

      public void setDeviceData(IDeviceDataBean deviceData) {
      this.deviceData = deviceData;
      //notify all listener
      DeviceDataListener listener = null;
      try {
      for (Iterator iter = listeners.iterator(); iter.hasNext(); ) {
      listener = (DeviceDataListener) iter.next();
      listener.onData(deviceData);
      }
      } catch ( Exception ex) {
      ex.printStackTrace();
      }
      }

      public void registerListener(DeviceDataListener listener) {
      listeners.add( listener );
      }
      ......//other code
      }


      /***** IDeviceDataBean ******/
      public interface IDeviceDataBean extends Serializable
      {
      public void setHeight(String height);

      public void setWeight(String weight);

      public String getHeight();

      public String getWeight();
      }



      /***** EJBClientApplet ******/
      import java.awt.*;
      import java.awt.event.*;
      import java.applet.*;
      import com.borland.jbcl.layout.*;
      //
      import IDeviceDataBean;
      import DeviceDataBean;
      import java.rmi.*;
      import javax.ejb.*;
      import java.io.IOException;
      import java.io.ObjectOutputStream;
      import java.io.ObjectInputStream;
      import javax.naming.Context;
      import javax.rmi.PortableRemoteObject;
      import java.util.Hashtable;
      import javax.naming.InitialContext;

      public class EJBClientApplet extends Applet implements java.io.Serializable, java.rmi.Remote,com.thtf.ezone.ibsserver.DeviceDataListener
      {
      boolean isStandalone = true;
      BorderLayout borderLayout1 = new BorderLayout();
      String h;
      String w;
      XYLayout xYLayout1 = new XYLayout();
      TextField weightText = new TextField();
      TextField heightText = new TextField();
      Button button1 = new Button();
      Button button2 = new Button();
      Label label1 = new Label();
      Label label2 = new Label();
      Label weightLabel = new Label();
      Label heightLabel = new Label();
      Label label3 = new Label();
      Label label4 = new Label();
      private PushEJB pushEJB = null;

      //Get a parameter value
      public String getParameter(String key, String def) {
      return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
      }

      //Construct the applet
      public EJBClientApplet() {
      //init ejb reference
      try {
      pushEJB = ((PushEJBHome)initializeEJBHome( "PushEJB", PushEJBHome.class)).create();
      System.err.println("pushEJB :: " + pushEJB );
      } catch (RemoteException ex) {
      ex.printStackTrace();
      } catch (CreateException ex) {
      ex.printStackTrace();
      }
      }


      //Initialize the applet
      public void init() {
      //register me as listener
      try {
      pushEJB.registerListener( this );
      pushEJB.registerObserver( this );
      System.err.println("register me OK!!");
      } catch (RemoteException ex) {
      ex.printStackTrace();
      }
      //generator by jb
      try {
      h = this.getParameter("height", "");
      } catch (Exception e) {
      e.printStackTrace();
      }
      try {
      w = this.getParameter("weight", "");
      } catch (Exception e) {
      e.printStackTrace();
      }
      try {
      jbInit();
      } catch (Exception e) {
      e.printStackTrace();
      }
      }

      //Component initialization
      private void jbInit() throws Exception {
      this.setLayout(xYLayout1);
      weightText.setText("0");
      heightText.setText("0");
      button1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
      button1.setLabel("????");
      button1.addActionListener(new EJBClientApplet_button1_actionAdapter(this));

      button2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
      button2.setLabel("??");
      button2.addActionListener(new EJBClientApplet_button2_actionAdapter(this));


      label1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
      label1.setText("?????");
      label2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
      label2.setText("?????");

      label3.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
      label3.setText("???");
      label4.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
      label4.setText("???");
      this.add(button1, new XYConstraints(268, 68, -1, -1));
      this.add(button2, new XYConstraints(268, 88, -1, -1));
      this.add(label1, new XYConstraints(56, 37, -1, -1));
      this.add(weightText, new XYConstraints(95, 70, 58, -1));
      this.add(label2, new XYConstraints(58, 159, -1, -1));
      this.add(heightText, new XYConstraints(187, 69, 56, -1));
      this.add(weightLabel, new XYConstraints(167, 185, -1, -1));
      this.add(heightLabel, new XYConstraints(167, 219, -1, -1));
      this.add(label3, new XYConstraints(124, 185, -1, -1));
      this.add(label4, new XYConstraints(122, 217, -1, -1));
      //
      weightLabel.setText( "0000" );
      heightLabel.setText( "0000" );
      }

      //Get Applet information
      public String getAppletInfo() {
      return "Applet Information";
      }

      //Get parameter info
      public String[][] getParameterInfo() {
      java.lang.String[][] pinfo = { {"height", "String", ""}, {"weight",
      "String", ""},
      };
      return pinfo;
      }
      /**
      * display device data
      * @param bean DeviceDataBean
      */
      public void onData( IDeviceDataBean bean ) throws RemoteException
      {
      System.out.println( "height:: " + bean.getHeight() );
      System.out.println( "weight:: " + bean.getWeight() );
      weightLabel.setText( ""+bean.getHeight() );
      heightLabel.setText( ""+bean.getWeight() );
      }
      //Main method
      public static void main(String[] args)
      {
      EJBClientApplet applet = new EJBClientApplet();
      applet.isStandalone = true;

      Frame frame;
      frame = new Frame();
      frame.setTitle("Applet Frame");

      frame.add(applet, BorderLayout.CENTER);

      applet.init();
      applet.start();
      frame.setSize(400, 320);
      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      frame.setLocation((d.width - frame.getSize().width) / 2,
      (d.height - frame.getSize().height) / 2);
      frame.setVisible(true);
      }

      public void button1_actionPerformed(ActionEvent actionEvent)
      {
      DeviceDataBean bean = new DeviceDataBean();
      bean.setHeight( heightText.getText() );
      bean.setWeight( weightText.getText() );
      //put to ejb
      try {
      pushEJB.setDeviceData(bean);
      } catch (RemoteException ex) {
      ex.printStackTrace();
      }
      }

      public void button2_actionPerformed(ActionEvent actionEvent)
      {
      System.exit(0);
      }

      public EJBHome initializeEJBHome( String jndiName, Class aclass )
      {
      try {

      //get naming context
      Context context = getInitialContext();
      //look up jndi name
      Object ref = context.lookup( jndiName );
      //look up jndi name and cast to Home interface
      return (EJBHome)PortableRemoteObject.narrow( ref, aclass );
      } catch (Exception e) {
      e.printStackTrace();
      }
      return null;
      }

      public Context getInitialContext() throws Exception {
      Hashtable environment = new Hashtable();

      environment.put(Context.INITIAL_CONTEXT_FACTORY,
      "org.jnp.interfaces.NamingContextFactory");
      environment.put(Context.URL_PKG_PREFIXES,
      "org.jboss.naming:org.jnp.interfaces");
      environment.put(Context.PROVIDER_URL, "jnp://localhost:1099");
      return new InitialContext(environment);

      }
      }


      class EJBClientApplet_button1_actionAdapter implements ActionListener
      {
      private EJBClientApplet adaptee;
      EJBClientApplet_button1_actionAdapter(EJBClientApplet adaptee)
      {
      this.adaptee = adaptee;
      }

      public void actionPerformed(ActionEvent actionEvent)
      {
      adaptee.button1_actionPerformed(actionEvent);
      }
      }

      class EJBClientApplet_button2_actionAdapter implements ActionListener
      {
      private EJBClientApplet adaptee;
      EJBClientApplet_button2_actionAdapter(EJBClientApplet adaptee)
      {
      this.adaptee = adaptee;
      }

      public void actionPerformed(ActionEvent actionEvent)
      {
      adaptee.button2_actionPerformed(actionEvent);
      }
      }