/**

   Copyright 2009 OpenEngSB Division, Vienna University of Technology

   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.openengsb.link.http;

import java.io.IOException;

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

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;

public class JmsListener extends Thread {

    private boolean running;
    MessageConsumer mc;

    public JmsListener(String ip) throws JMSException {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        connection.start();
        Destination dest = new ActiveMQTopic("org.openengsb.link.http");
        System.out.println("using IP: " + ip);
        mc = session.createConsumer(dest, String.format("ip = \'%s\'", ip));
        running = true;

    }

    @Override
    public void run() {
        while (running) {
            try {
                Message msg = mc.receive();
                System.out.println("message recieved");
                System.out.println(msg.toString());

            } catch (JMSException e) {
                running = false;
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {

        HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);

        GetMethod get = new GetMethod("http://localhost:8192/Link/");
        get.setQueryString("whoami");

        try {
            if (httpClient.executeMethod(get) != 200) {
                throw new RuntimeException("unable to determine ip for response");
            }
        } catch (HttpException e2) {
            throw new RuntimeException(e2);
        } catch (IOException e2) {
            throw new RuntimeException(e2);
        }

        String response;
        try {
            response = get.getResponseBodyAsString();
        } catch (IOException e1) {
            throw new RuntimeException(e1);
        }
        int pos = response.indexOf("You are ");
        int pos2 = response.indexOf("</h1>");
        String ip = response.substring(pos + 8, pos2);
        try {
            new JmsListener(ip).start();
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return;
        }

    }
}

@@ -1275,24 +1275,179 @@
 s Thread %7B%0A%0A
+    private static final String LINK_HTTP_TOPIC = %22org.openengsb.link.http%22;%0A    private static final String JMS_CONNECTION_URL = %22tcp://localhost:61616%22;%0A
     private 
@@ -1544,16 +1544,55 @@
 ption %7B%0A
+        /* create a new JMS-session */%0A
         
@@ -1659,39 +1659,34 @@
 Factory(
-%22tcp://localhost:61616%22
+JMS_CONNECTION_URL
 );%0A     
@@ -1847,27 +1847,130 @@
     
-connection.start();
+/* start the connection to be enable to receive messages */%0A        connection.start();%0A%0A        /* set the destination */
 %0A   
@@ -2015,33 +2015,23 @@
 pic(
-%22org.openengsb.link.http%22
+LINK_HTTP_TOPIC
 );%0A 
@@ -2177,17 +2177,16 @@
 = true;%0A
-%0A
     %7D%0A%0A 
@@ -2417,16 +2417,71 @@
 ring());
+%0A                /* add message-processing code here */
 %0A%0A      
@@ -2656,24 +2656,59 @@
 %5B%5D args) %7B%0A%0A
+        /* create a HTTP-client */%0A
         Http
@@ -2981,16 +2981,53 @@
 oami%22);%0A
+        /* send the whoami-request */
 %0A       
@@ -3388,32 +3388,116 @@
 ;%0A        try %7B%0A
+            /* the response-body contains a HTML-page with the remote IP-address */%0A
             resp
@@ -3615,32 +3615,67 @@
 (e1);%0A        %7D%0A
+        /* parsing the response */%0A
         int pos 
@@ -3813,32 +3813,78 @@
 ;%0A        try %7B%0A
+            /* start a new listener-thread */%0A
             new 

add more doc to the sample JmsListener