package io.joynr.messaging.websocket;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import io.joynr.messaging.FailureAction;
import io.joynr.messaging.IMessagingSkeleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * #%L
 * %%
 * Copyright (C) 2011 - 2015 BMW Car IT GmbH
 * %%
 * 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.
 * #L%
 */

import com.fasterxml.jackson.databind.ObjectMapper;

import io.joynr.messaging.routing.MessageRouter;
import joynr.JoynrMessage;

/**
 *
 */
public abstract class WebSocketMessagingSkeleton extends MessagingSocket implements IMessagingSkeleton {

    private static final Logger logger = LoggerFactory.getLogger(WebSocketMessagingSkeleton.class);
    private ExecutorService executorThreadPool = Executors.newCachedThreadPool();

    ObjectMapper objectMapper;
    private MessageRouter messageRouter;

    public WebSocketMessagingSkeleton(ObjectMapper objectMapper, MessageRouter messageRouter) {
        this.objectMapper = objectMapper;
        this.messageRouter = messageRouter;
    }

    @Override
    public void onWebSocketText(String json) {
        super.onWebSocketText(json);
        logger.debug("Received TEXT message: " + json);
        try {
            final JoynrMessage message = objectMapper.readValue(json, JoynrMessage.class);
            executorThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        messageRouter.route(message);
                    } catch (IOException e) {
                        logger.error("Error: ", e);
                    }
                }
            });

        } catch (IOException e) {
            logger.error("Failed to parse websocket message", e);
        }
    }

    @Override
    public void transmit(JoynrMessage message, FailureAction failureAction) {
        try {
            messageRouter.route(message);
        } catch (Exception exception) {
            failureAction.execute(exception);
        }
    }

    @Override
    public void transmit(String serializedMessage, FailureAction failureAction) {
        // TODO Auto-generated method stub
    }
}

@@ -36,266 +36,8 @@
 t;%0A%0A
-import java.io.IOException;%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0A%0Aimport io.joynr.messaging.FailureAction;%0Aimport io.joynr.messaging.IMessagingSkeleton;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 /*%0A 
@@ -669,16 +669,189 @@
 L%25%0A */%0A%0A
+import java.io.IOException;%0A%0Aimport io.joynr.messaging.FailureAction;%0Aimport io.joynr.messaging.IMessagingSkeleton;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import c
@@ -1192,90 +1192,8 @@
 ss);
-%0A    private ExecutorService executorThreadPool = Executors.newCachedThreadPool();
 %0A%0A  
@@ -1727,347 +1727,37 @@
     
-executorThreadPool.submit(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A                    try %7B%0A                        messageRouter.route(message);%0A                    %7D catch (IOException e) %7B%0A                        logger.error(%22Error: %22, e);%0A                    %7D%0A                %7D%0A            %7D);%0A
+messageRouter.route(message);
 %0A   
@@ -1828,12 +1828,14 @@
 to p
-arse
+rocess
  web
@@ -1852,12 +1852,29 @@
 sage
-%22, e
+: %7B%7D%22, e.getMessage()
 );%0A 

Remove extra thread executor from WebSockets skeleton