/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2010, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file 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.as.demos.jms.mbean;

import java.util.ArrayList;
import java.util.List;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.InitialContext;

import org.jboss.logging.Logger;

/**
 * JMS Example (waiting for JMS to be implemented)
 *
 * @author <a href="kabir.khan@jboss.com">Kabir Khan</a>
 * @version $Revision: 1.1 $
 */
public class Test implements TestMBean {

    static final Logger log = Logger.getLogger(Test.class);

    private QueueConnection conn;
    private Queue queue;
    private QueueSession session;

    private final List<String> receivedMessages = new ArrayList<String>();

    public void start() throws Exception {
        InitialContext ctx = new InitialContext();

        System.out.println(ctx.lookup("ConnectionFactory"));
        System.out.println(ctx.lookup("queue/test"));

        QueueConnectionFactory qcf = (QueueConnectionFactory)ctx.lookup("ConnectionFactory");
        conn = qcf.createQueueConnection();
        queue = (Queue)ctx.lookup("queue/test");
        session = conn.createQueueSession(true, QueueSession.AUTO_ACKNOWLEDGE);

        // Set the async listener
        QueueReceiver recv = session.createReceiver(queue);
        recv.setMessageListener(new ExampeMessageListener());
    }

    public void stop() throws Exception {
        conn.stop();
        session.close();
        conn.close();
    }

    public void sendMessage(String txt) throws Exception {
        QueueSender send = null;
        try {
            // Send a text msg
            send = session.createSender(queue);
            TextMessage tm = session.createTextMessage(txt);
            send.send(tm);
            log.info("-----> sent text=" + tm.getText());
        } finally {
            send.close();
        }
    }

    public List<String> readMessages() {
        synchronized (receivedMessages) {
            List<String> list = new ArrayList<String>(receivedMessages);
            receivedMessages.clear();
            return list;
        }
    }

    private class ExampeMessageListener implements MessageListener {

        @Override
        public void onMessage(Message message) {
            TextMessage msg = (TextMessage)message;
            try {
                log.info("-----> on message: " + msg.getText());
                synchronized (receivedMessages) {
                    receivedMessages.add(msg.getText());
                }
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }
}

@@ -1458,16 +1458,49 @@
 .Logger;
+%0Aimport org.jboss.modules.Module;
 %0A%0A/**%0A *
@@ -1969,52 +1969,414 @@
     
-InitialContext ctx = new InitialContext();%0A%0A
+//HornetQ needs the proper TCL%0A        ClassLoader cl = Thread.currentThread().getContextClassLoader();%0A        Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());%0A        try %7B%0A            InitialContext ctx = new InitialContext();%0A%0A            Module module = Module.forClass(Test.class);%0A            Thread.currentThread().setContextClassLoader(module.getClassLoader());%0A    
     
@@ -2432,32 +2432,36 @@
 ory%22));%0A        
+   
+ 
 System.out.print
@@ -2487,24 +2487,28 @@
 e/test%22));%0A%0A
+    
         Queu
@@ -2593,24 +2593,28 @@
 %22);%0A        
+    
 conn = qcf.c
@@ -2633,24 +2633,54 @@
 nnection();%0A
+            conn.start();%0A    
         queu
@@ -2720,24 +2720,28 @@
 %22);%0A        
+ 
+   
 session = co
@@ -2766,11 +2766,12 @@
 ion(
-tru
+fals
 e, Q
@@ -2806,24 +2806,28 @@
 );%0A%0A        
+ 
+   
 // Set the a
@@ -2840,16 +2840,20 @@
 istener%0A
+    
         
@@ -2912,16 +2912,20 @@
         
+   
+ 
 recv.set
@@ -2978,99 +2978,310 @@
     
-%7D%0A%0A    public void stop() throws Exception %7B%0A        conn.stop();%0A        session.close();%0A
+    %7D finally %7B%0A            Thread.currentThread().setContextClassLoader(cl);%0A        %7D%0A    %7D%0A%0A    public void stop() throws Exception %7B%0A        if (conn != null) %7B%0A            conn.stop();%0A        %7D%0A        if (session != null) %7B%0A            session.close();%0A        %7D%0A        if (conn != null) %7B%0A    
     
@@ -3294,24 +3294,34 @@
 nn.close();%0A
+        %7D%0A
     %7D%0A%0A    p

Fix jms example