FileDocCategorySizeDatePackage
Client_7.javaAPI DocExample2121Sat Mar 06 09:46:30 GMT 1999com.titan.cabin

Client_7.java

package com.titan.cabin;

import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.ejb.CreateException;
import javax.ejb.Handle;
import java.rmi.RemoteException;
import java.util.Properties;

import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


public class Client_7 {

    public static void main(String [] args){
        try{

            // obtain cabin 100
            Context jndiContext = getInitialContext();
            CabinHome home = (CabinHome)jndiContext.lookup("CabinHome");
            CabinPK pk_1 = new CabinPK();
            pk_1.id = 100;
            Cabin cabin_1 = home.findByPrimaryKey(pk_1);

            // serialize the Handle for cabin 100 to a file.
            Handle handle = cabin_1.getHandle();
            FileOutputStream fos = new FileOutputStream("handle100.ser");
            ObjectOutputStream outStream = new ObjectOutputStream(fos);
            outStream.writeObject(handle);
            outStream.flush();
            fos.close();
            handle = null;

            // deserialize the Handle for cabin 100
            FileInputStream fis = new FileInputStream("handle100.ser");
            ObjectInputStream inStream = new ObjectInputStream(fis);
            handle = (Handle)inStream.readObject();
            fis.close();

            // re-obtain a remote refernce to cabin 100 and read its name
            Cabin cabin_2 = (Cabin)handle.getEJBObject();
            System.out.println(cabin_2.getName());

        }catch(java.rmi.RemoteException re){re.printStackTrace();}
         catch(javax.naming.NamingException ne)
              {ne.printStackTrace();}
         catch(Throwable t){t.printStackTrace();}
    }

    public static Context getInitialContext()
       throws javax.naming.NamingException{

        Properties p = new Properties();
        // ... specify the JNDI properties specific to the vendor
        return new javax.naming.InitialContext(p);
    }
}