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

package nyangau.jmsadapter;

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

public class JmsaCFObjectFactory implements ObjectFactory
  {
//...sJmsaCFObjectFactory:2:
public JmsaCFObjectFactory() {}
//...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(JmsaConnectionFactory       .class.getName()) ||
         n.equals(JmsaQueueConnectionFactory  .class.getName()) ||
         n.equals(JmsaTopicConnectionFactory  .class.getName()) ||
         n.equals(JmsaXAConnectionFactory     .class.getName()) ||
         n.equals(JmsaXAQueueConnectionFactory.class.getName()) ||
         n.equals(JmsaXATopicConnectionFactory.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(JmsaConnectionFactory.class.getName()) )
          return new JmsaConnectionFactory(map);
        if ( n.equals(JmsaQueueConnectionFactory.class.getName()) )
          return new JmsaQueueConnectionFactory(map);
        if ( n.equals(JmsaTopicConnectionFactory.class.getName()) )
          return new JmsaTopicConnectionFactory(map);
        if ( n.equals(JmsaXAConnectionFactory.class.getName()) )
          return new JmsaXAConnectionFactory(map);
        if ( n.equals(JmsaXAQueueConnectionFactory.class.getName()) )
          return new JmsaXAQueueConnectionFactory(map);
        if ( n.equals(JmsaXATopicConnectionFactory.class.getName()) )
          return new JmsaXATopicConnectionFactory(map);
        }
      }
    }
  return null;
  }
//...e
  }
