To what kind of bean is this guy passing a handle? If it is not a stateful
entity bean, are we down with this?

-----Original Message-----
From: Dave Ford [mailto:[EMAIL PROTECTED]]
Sent: Thursday, August 31, 2000 10:54 AM
To: [EMAIL PROTECTED]
Subject: Re: Encoding javax.ejb.Handle as String


Never mind my last message. I wrote the code myself. Here it is if anyone
wants it. Perhaps someone can review it and make suggestions. Here is the
class:

/**
This class will convert a javax.ejb.Handle object to a java.lang.String and
visa versa,
thus allowing an an ejb handle can be passed as cookie.

Dave Ford
http//www.smart-soft.com
*/

package ss.util;

import java.io.ObjectOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.net.URLDecoder;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
import javax.ejb.Handle;

public class HandleStringConverter{

  public static String handleToString(Handle handle){
    if(handle==null) throw new NullPointerException("handle can not be
null");
    try{
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(baos);
      oos.writeObject(handle);
      byte[] b = baos.toByteArray();
      String s = new BASE64Encoder().encode(b);
      s = URLEncoder.encode(s);
      System.out.println("Ecoded handle: " + s);
      return s;
    }
    catch(IOException ex){
      throw new IllegalStateException(ex.toString());
    }
  }

  public static Handle stringToHandle(String handleAsString){
    try{
      handleAsString = URLDecoder.decode(handleAsString);
      byte[] b = new BASE64Decoder().decodeBuffer(handleAsString);
      ByteArrayInputStream bais = new ByteArrayInputStream(b);
      ObjectInputStream ois = new ObjectInputStream(bais);
      return (Handle)ois.readObject();
    }
    catch(Exception ex){
      throw new IllegalStateException(ex.toString());
    }
  }

}//endClass


Dave Ford
Smart Soft - The Java Training Company
http://www.SmartSoftTraining.com

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to