package org.openengsb.loom.java.impl;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.LinkedBlockingQueue;

public class QueueMap<K, V> {
    private ConcurrentMap<K, BlockingQueue<V>> map = new ConcurrentHashMap<K, BlockingQueue<V>>();

    public void put(K key, V value) {
        synchronized (map) {
            if (!map.containsKey(key)) {
                map.put(key, new LinkedBlockingQueue<V>());
                map.notifyAll();
            }
            map.get(key).add(value);
        }
    }

    public V poll(K key) throws InterruptedException {
        BlockingQueue<V> queue;
        synchronized (map) {
            while (!map.containsKey(key)) {
                map.wait();
            }
            queue = map.get(key);
        }
        return queue.poll();
    }
}

@@ -219,37 +219,179 @@
 e;%0A%0A
-public class QueueMap%3CK, V%3E %7B
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Apublic class QueueMap%3CK, V%3E %7B%0A%0A    private static final Logger LOGGER = LoggerFactory.getLogger(QueueMap.class);%0A
 %0A   
@@ -698,32 +698,95 @@
           %7D%0A    
+    %7D%0A        LOGGER.info(%22putting message in queue %7B%7D%22, key);%0A
         map.get(
@@ -802,26 +802,16 @@
 value);%0A
-        %7D%0A
     %7D%0A%0A 
@@ -896,16 +896,81 @@
  queue;%0A
+        LOGGER.info(%22looking for message for corr-id: %7B%7D%22, key);%0A
         
@@ -1054,77 +1054,298 @@
     
-map.wait();%0A            %7D%0A            queue = map.get(key);%0A        %7D
+LOGGER.info(%22waiting for corr-id: %7B%7D%22, key);%0A                map.wait();%0A            %7D%0A            LOGGER.info(%22queue for corr-id found: %7B%7D%22, key);%0A            queue = map.get(key);%0A            LOGGER.info(%22-SYNC: poll%22);%0A        %7D%0A        LOGGER.info(%22polling queue for corr-id: %7B%7D%22, key);
 %0A   
@@ -1362,20 +1362,20 @@
 n queue.
-poll
+take
 ();%0A    

fix concurrency-issue in queuemap