/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.example.jmsexample.requestResponse;

import org.apache.qpid.example.jmsexample.common.BaseExample;

import javax.jms.*;

/**
 * The example creates a MessageConsumer on the specified
 * Destination which is used to synchronously consume messages. If a
 * received message has a ReplyTo header then a new response message is sent
 * to that specified destination.
 */
public class Server extends BaseExample
{
    /* Used in log output. */
    private static final String CLASS = "Server";

    /* The destination type */
    private String _destinationType;

    /* The destination Name */
    private String _destinationName;

    /**
     * Create a Server client.
     *
     * @param args Command line arguments.
     */
    public Server(String[] args)
    {
        super(CLASS, args);
        _destinationType = _argProcessor.getStringArgument("-destinationType");
        _destinationName = _argProcessor.getStringArgument("-destinationName");
    }

    /**
     * Run the message mirror example.
     *
     * @param args Command line arguments.
     */
    public static void main(String[] args)
    {
        _options.put("-destinationType", "Destination Type: queue/topic");
        _defaults.put("-destinationType", "queue");
        _options.put("-destinationName", "Destination Name");
        _defaults.put("-destinationName", "request");
        Server server = new Server(args);
        server.runTest();
    }

    /**
     * Start the example.
     */
    private void runTest()
    {
        try
        {
            // Declare the connection
            Connection connection = getConnection();

            // As this application is using a MessageConsumer we need to set an ExceptionListener on the connection
            // so that errors raised within the JMS client library can be reported to the application
            System.out.println(
                    CLASS + ": Setting an ExceptionListener on the connection as sample uses a MessageConsumer");

            connection.setExceptionListener(new ExceptionListener()
            {
                public void onException(JMSException jmse)
                {
                    // The connection may have broken invoke reconnect code if available.
                    // The connection may have broken invoke reconnect code if available.
                    System.err.println(CLASS + ": The sample received an exception through the ExceptionListener");
                    System.exit(0);
                }
            });

            // Create a session on the connection
            // This session is a default choice of non-transacted and uses
            // the auto acknowledge feature of a session.
            System.out.println(CLASS + ": Creating a non-transacted, auto-acknowledged session");

            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            Destination destination;

            if (_destinationType.equals("queue"))
            {
                // Lookup the queue
                System.out.println(CLASS + ": Looking up queue with name: " + _destinationName);
                destination = session.createQueue(_destinationName);
            }
            else
            {
                // Lookup the topic
                System.out.println(CLASS + ": Looking up topic with name: " + _destinationName);
                destination = session.createTopic(_destinationName);
            }

            // Create a MessageConsumer
            System.out.println(CLASS + ": Creating a MessageConsumer");
            MessageConsumer messageConsumer = session.createConsumer(destination);

            /**
             * Create a MessageProducer - note that although we create the
             */
            System.out.println(CLASS + ": Creating a MessageProducer");
            MessageProducer messageProducer;

            // Now the messageConsumer is set up we can start the connection
            System.out.println(CLASS + ": Starting connection so MessageConsumer can receive messages");
            connection.start();

            // Cycle round until all the messages are consumed.
            Message requestMessage;
            TextMessage responseMessage;
            boolean end = false;
            while (!end)
            {
                System.out.println(CLASS + ": Receiving the message");

                requestMessage = messageConsumer.receive();

                String text;
                if (requestMessage instanceof TextMessage)
                {
                    text = ((TextMessage) requestMessage).getText();
                }
                else
                {
                    byte[] body = new byte[(int) ((BytesMessage) requestMessage).getBodyLength()];
                    ((BytesMessage) requestMessage).readBytes(body);
                    text = new String(body);
                }


                if (text.equals("That's all, folks!"))
                {
                    System.out.println(CLASS + ": Received final message for " + destination);
                    end = true;
                }
                else
                {
                    System.out.println(CLASS + ": \tContents = " + text);
                }

                // Now bounce the message if a ReplyTo header was set.
                if (requestMessage.getJMSReplyTo() != null)
                {
                    System.out.println(CLASS + ": Activating response queue listener for: " + destination);
                    responseMessage = session.createTextMessage();

                    responseMessage.setText(text.toUpperCase());
                    System.out.println(CLASS + ": \tResponse = " + responseMessage.getText());

                    messageProducer = session.createProducer(requestMessage.getJMSReplyTo());
                    messageProducer.send(responseMessage);
                }
                System.out.println();
            }

            // Close the connection to the server
            System.out.println(CLASS + ": Closing connection");
            connection.close();

            // Close the JNDI reference
            System.out.println(CLASS + ": Closing JNDI context");
            getInitialContext().close();
        }
        catch (Exception exp)
        {
            exp.printStackTrace();
            System.err.println(CLASS + ": Caught an Exception: " + exp);
        }
    }
}
