/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.jms.jndi;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Hashtable;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.jboss.logging.Logger;

/**
 * A JMS provider adapter for <em>JBossMQ</em>.
 *
 * @deprecated use JNDIProviderAdapter
 * @author  <a href="mailto:peter.antman@dn.se">Peter Antman</a>
 * @author  <a href="mailto:cojonudo14@hotmail.com">Hiram Chirino</a>
 * @author  <a href="mailto:jason@planet57.com">Jason Dillon</a>
 * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
 * @version <pre>$Revision: 1.9.4.2 $</pre>
 */
public class JBossMQProvider extends AbstractJMSProviderAdapter
{
   private static final long serialVersionUID = 837382943326224144L;

   /** The initial context factory to use. */
   public static final String INITIAL_CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";

   /** The url package prefixes. */
   public static final String URL_PKG_PREFIXES = "org.jboss.naming";

   /** The security manager to use. */
   private static final String SECURITY_MANAGER = "java.naming.rmi.security.manager";

   /** Instance logger. */
   private transient Logger log = Logger.getLogger(this.getClass());

   /** Flag to enable JNDI security manager. */
   private String hasJndiSecurityManager = "yes";

   private transient boolean warned = false;

   public void setProperties(Properties properties)
   {
      if (properties != null)
         throw new IllegalArgumentException(
               "Properties not supported by this provider adapter - use JNDIProviderAdapter");
   }

   /** Override of standard de-serialization to re-create logger. */
   private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
   {
      in.defaultReadObject();
      this.log = Logger.getLogger(this.getClass());
   }

   public Context getInitialContext() throws NamingException
   {
      if (warned == false)
      {
         log.warn("JBossMQProvider is deprecated use JNDIProviderAdapter");
         warned = true;
      }
      Context ctx = null;
      boolean debug = log.isDebugEnabled();
      if (providerURL == null)
      {
         // Use default
         if (debug)
            log.debug("no provider url; connecting to local JNDI");
         ctx = new InitialContext(); // Only for JBoss embedded now
      }
      else
      {
         // Try another location
         Hashtable props = new Hashtable();
         props.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
         props.put(Context.PROVIDER_URL, providerURL);
         props.put(SECURITY_MANAGER, hasJndiSecurityManager);
         props.put(Context.URL_PKG_PREFIXES, URL_PKG_PREFIXES);
         if (debug)
            log.debug("connecting to remote JNDI with props: " + props);
         ctx = new InitialContext(props);
      }
      if (debug)
      {
         log.debug("created context: " + ctx);
      }
      return ctx;
   }
}