FileDocCategorySizeDatePackage
Client_5.javaAPI DocExample2125Sat Mar 06 09:44:42 GMT 1999com.titan.cabin

Client_5.java

package com.titan.cabin;

import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.ejb.CreateException;
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_5 {

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

            // obtain cabin 101 and set its name
            Context jndiContext = getInitialContext();
            CabinHome home = (CabinHome)jndiContext.lookup("CabinHome");
            CabinPK pk_1 = new CabinPK();
            pk_1.id = 101;
            Cabin cabin_1 = home.findByPrimaryKey(pk_1);
            cabin_1.setName("Presidential Suite");

            // serialize the primary key for cabin 101 to a file.
            FileOutputStream fos = new FileOutputStream("pk101.ser");
            ObjectOutputStream outStream = new ObjectOutputStream(fos);
            outStream.writeObject(pk_1);
            outStream.flush();
            fos.close();
            pk_1 = null;

            // deserialize the primary key for cabin 101
            FileInputStream fis = new FileInputStream("pk101.ser");
            ObjectInputStream inStream = new ObjectInputStream(fis);
            CabinPK pk_2 = (CabinPK)inStream.readObject();
            fis.close();

            // re-obtain a remote refernce to cabin 101 and read its name
            Cabin cabin_2 = home.findByPrimaryKey(pk_2);
            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);
    }

  }
}