//
// JmsaDObjectFactory.java - JMS Adapter, reconstruct JmsaDs
//
// ObjectFactory capable of reconstructing JmsaDs from References in JNDI.
//

package nyangau.jmsadapter;

import java.util.*;
import java.io.*;
import javax.naming.*;
import javax.naming.spi.ObjectFactory;

public class JmsaDObjectFactory implements ObjectFactory
  {
//...sJmsaDObjectFactory:2:
public JmsaDObjectFactory() {}
//...e
//...sgetObjectInstance:2:
public Object getObjectInstance(
  Object obj, Name name, Context ctx, Hashtable env
  )
  throws Exception
  {
  if ( obj instanceof Reference )
    {
    Reference r = (Reference) obj;
    String n = r.getClassName();
    if ( n.equals(JmsaQueue.class.getName()) ||
         n.equals(JmsaTopic.class.getName()) )
      {
      RefAddr ra = r.get("properties");
      if ( ra != null )
        {
        byte[] b = (byte[]) ra.getContent();
        ByteArrayInputStream bais = new ByteArrayInputStream(b);
        ObjectInputStream ois = new ObjectInputStream(bais);
        @SuppressWarnings({"unchecked"}) // ugly, but necessary
        Map<String,Object> map = (Map<String,Object>) ois.readObject();
        if ( n.equals(JmsaQueue.class.getName()) )
          return new JmsaQueue(map);
        if ( n.equals(JmsaTopic.class.getName()) )
          return new JmsaTopic(map);
        }
      }
    }
  return null;
  }
//...e
  }
