/*
 *
 * Copyright (c) 2006 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.apache.qpid.jndi.referenceabletest;

import org.apache.qpid.client.AMQTopic;
import org.apache.qpid.client.AMQConnection;
import org.apache.qpid.client.AMQConnectionFactory;

import javax.naming.*;
import java.util.Hashtable;


/**
 * Usage: To run these you need to have the sun JNDI SPI for the FileSystem.
 * This can be downloaded from sun here:
 * http://java.sun.com/products/jndi/downloads/index.html
 * Click : Download JNDI 1.2.1 & More button
 * Download: File System Service Provider, 1.2 Beta 3
 * and add the two jars in the lib dir to your class path.
 * <p/>
 * Also you need to create the directory /temp/qpid-jndi-test
 */
class Lookup
{
    AMQTopic _topic = null;
    AMQConnection _connection = null;
    AMQConnectionFactory _connectionFactory = null;

    public Lookup()
    {
        // Set up the environment for creating the initial context
        Hashtable env = new Hashtable(11);
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.fscontext.RefFSContextFactory");
        env.put(Context.PROVIDER_URL, "file:/temp/qpid-jndi-test");

        try
        {
            // Create the initial context
            Context ctx = new InitialContext(env);

            _topic = (AMQTopic) ctx.lookup("Topic");

            _connection = (AMQConnection) ctx.lookup("Connection");

            _connectionFactory = (AMQConnectionFactory) ctx.lookup("ConnectionFactory");
            //System.out.println(topic);

            // Close the context when we're done
            ctx.close();
        }
        catch (NamingException e)
        {
            System.out.println("Operation failed: " + e);
        }
    }


    public String connectionFactoryValue()
    {
        return ((AMQConnection) _connectionFactory.getConnectionURL()).toURL();
    }

    public String connectionValue()
    {
        return _connection.toURL();
    }

    public String topicValue()
    {
        return _topic.toURL();
    }


    public static void main(String[] args)
    {
        new Lookup();
    }
}

