package com.javahonk;

import javax.jms.*;
import javax.naming.*;

import java.io.*;
import java.util.Properties;

public class ChatJMSApplication implements MessageListener {

	private TopicSession pubSession;
	private TopicSession subSession;
	private TopicConnectionFactory connectionFactory = null;
	private TopicPublisher publisher;
	private TopicSubscriber subscriber;
	private TopicConnection connection;
	
	public static Context getInitialContext() throws javax.naming.NamingException {

		Properties p = new Properties();
		p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		p.put(Context.URL_PKG_PREFIXES, " org.jboss.naming:org.jnp.interfaces");
		p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		return new javax.naming.InitialContext(p);
	}
	

	public ChatJMSApplication() throws Exception {
		
		Context ic = getInitialContext();
		
		// Look up a JMS connection factory
		connectionFactory = (TopicConnectionFactory) ic.lookup("/ConnectionFactory");

		// Create a JMS connection
		connection = connectionFactory.createTopicConnection();

		// Create two JMS session objects
		pubSession = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
		subSession = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);

		// Look up a JMS topic
		Topic ChatJMSApplicationTopic = (Topic) ic.lookup("javahonk/topic");

		// Create a JMS publisher and subscriber
		publisher = pubSession.createPublisher(ChatJMSApplicationTopic);
		subscriber = subSession.createSubscriber(ChatJMSApplicationTopic);

		// Set a JMS message listener
		subscriber.setMessageListener(this);

		// Initialize the ChatJMSApplication application
		set(connection, pubSession, subSession, publisher);

		// Start the JMS connection; allows messages to be delivered
		connection.start();

	}

	/* Initialize the instance variables */
	public void set(TopicConnection con, TopicSession pubSess, TopicSession subSess, TopicPublisher pub) {
		this.connection = con;
		this.publisher = pub;		
	}

	/* Receive message from topic subscriber */
	public void onMessage(Message message) {
		try {
			TextMessage textMessage = (TextMessage) message;
			String text = textMessage.getText();
			System.out.println(text);
		} catch (JMSException jmse) {
			jmse.printStackTrace();
		}
	}

	/* Create and send message using topic publisher */
	protected void writeMessage(String text) throws JMSException {
		
		TextMessage message = pubSession.createTextMessage();
		message.setText("Message received: " + " : " + text);
		publisher.publish(message);
	}

	/* Close the JMS connection */
	public void close() throws JMSException {
		connection.close();
	}

	/* Run the ChatJMSApplication client */
	public static void main(String[] args) {
		try {
			
			ChatJMSApplication ChatJMSApplication = new ChatJMSApplication();

			// Read input from command line
			BufferedReader commandLine = new java.io.BufferedReader(new InputStreamReader(System.in));

			// Continue while loop until user type word "exit"
			while (true) {
				String s = commandLine.readLine();
				if (s.equalsIgnoreCase("exit")) {
					ChatJMSApplication.close(); // close the connection
					System.exit(0);// exit from program
				} else
					ChatJMSApplication.writeMessage(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
