import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.jms.JMSException;
import javax.jms.Destination;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.Session;

public class Listener 
{
    public static void main(String[] args) 
    {
        Context context = null;
        ConnectionFactory factory = null;
        Connection connection = null;
        String factoryName = "ConnectionFactory";
        String destName = null;
        Destination dest = null;
        Session session = null;
        MessageConsumer receiver = null;
        BufferedReader waiter = null;
	
        if (args.length != 1) 
	    {
		System.out.println("usage: Listener <destination>");
		System.exit(1);
	    }
	
        destName = args[0];
	
        try 
	    {
        //TODO
		// Implementer l'enregistrement du listener

		System.out.println("Waiting for messages...");
		System.out.println("Press [return] to quit");
		
		waiter = new BufferedReader(new InputStreamReader(System.in));
		waiter.readLine();
	    } 
	catch (IOException exception) 
	    {
		exception.printStackTrace();
	    } 
/*
	catch (JMSException exception) 
	    {
		exception.printStackTrace();
	    } 
	catch (NamingException exception) 
	    {
		exception.printStackTrace();
	    } 
*/
	finally 
	    {
		// close the context
		if (context != null) 
		    {
			try 
			    {
				context.close();
			    } 
			catch (NamingException exception) 
			    {
				exception.printStackTrace();
			    }
		    }
		// close the connection
		if (connection != null) 
		    {
			try 
			    {
				connection.close();
			    } 
			catch (JMSException exception) 
			    {
				exception.printStackTrace();
			    }
		    }
	    }
    }
}
