/*
 * JBossMQ, the OpenSource JMS implementation
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.mq.referenceable;

import javax.naming.spi.ObjectFactory;

import org.jboss.logging.Logger;

/**
 *  This class is used to create instances of of: SpyTopic SpyQueue classes from
 *  a javax.naming.Reference instance.
 *
 * @author     Hiram Chirino (Cojonudo14@hotmail.com)
 * @created    August 16, 2001
 * @version    $Revision: 1.3 $
 */
public class SpyDestinationObjectFactory implements javax.naming.spi.ObjectFactory {

   static Logger cat = Logger.getLogger( SpyDestinationObjectFactory.class );

   /**
    *  SpyTopicObjectFactory constructor
    */
   public SpyDestinationObjectFactory() {
      super();
   }

   /**
    *  getObjectInstance method
    *
    * @param  reference                Description of Parameter
    * @param  name                     Description of Parameter
    * @param  contex                   Description of Parameter
    * @param  properties               Description of Parameter
    * @return                          The ObjectInstance value
    * @exception  java.lang.Exception  Description of Exception
    */
   public java.lang.Object getObjectInstance( java.lang.Object reference, javax.naming.Name name, javax.naming.Context contex, java.util.Hashtable properties )
      throws java.lang.Exception {

      cat.debug( "SpyDestinationObjectFactory->getObjectInstance()" );

      try {

         javax.naming.Reference ref = ( javax.naming.Reference )reference;
         if ( ref.getClassName().equals( "org.jboss.mq.SpyTopic" ) ) {

            String dest = ( String )ref.get( "name" ).getContent();
            return new org.jboss.mq.SpyTopic( dest );
         } else if ( ref.getClassName().equals( "org.jboss.mq.SpyQueue" ) ) {

            String dest = ( String )ref.get( "name" ).getContent();
            return new org.jboss.mq.SpyQueue( dest );
         }
      } catch ( RuntimeException ignore ) {
      } catch ( Exception ignore ) {
         // This method should not throw an exception since
         // It would prevent another ObjectFactory from attempting
         // to create an instance of the object.
      }
      return null;
   }
}
