package io.joynr.messaging.routing;

/*
 * #%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.google.inject.Inject;
import io.joynr.exceptions.JoynrMessageNotSentException;
import io.joynr.messaging.AbstractMessagingStubFactory;
import io.joynr.messaging.IMessaging;
import io.joynr.messaging.inprocess.InProcessAddress;
import io.joynr.messaging.inprocess.InProcessMessagingStub;
import joynr.system.RoutingTypes.Address;
import joynr.system.RoutingTypes.ChannelAddress;
import joynr.system.RoutingTypes.WebSocketAddress;
import joynr.system.RoutingTypes.WebSocketClientAddress;

import java.util.Map;

public class MessagingStubFactory {

    private Map<Class<? extends Address>, AbstractMessagingStubFactory> messagingStubFactories;

    @Inject
    public MessagingStubFactory(Map<Class<? extends Address>, AbstractMessagingStubFactory> messagingStubFactories) {
        this.messagingStubFactories = messagingStubFactories;
    }

    public IMessaging create(Address address) {
        IMessaging messagingStub;

        if (address instanceof ChannelAddress) {
            messagingStub = messagingStubFactories.get(ChannelAddress.class).create(address);
        } else if (address instanceof InProcessAddress) {
            messagingStub = new InProcessMessagingStub(((InProcessAddress) address).getSkeleton());
        } else if (address instanceof WebSocketAddress) {
            messagingStub = messagingStubFactories.get(WebSocketAddress.class).create(address);
        } else if (address instanceof WebSocketClientAddress) {
            messagingStub = messagingStubFactories.get(WebSocketClientAddress.class).create(address);
        } else {
            throw new JoynrMessageNotSentException("Failed to send Request: Address type not supported");
        }
        return messagingStub;
    }

    public void shutdown() {
        for (AbstractMessagingStubFactory messagingStubFactory : messagingStubFactories.values()) {
            messagingStubFactory.shutdown();
        }
    }
}

@@ -1010,165 +1010,8 @@
 ess;
-%0Aimport joynr.system.RoutingTypes.ChannelAddress;%0Aimport joynr.system.RoutingTypes.WebSocketAddress;%0Aimport joynr.system.RoutingTypes.WebSocketClientAddress;
 %0A%0Aim
@@ -1365,139 +1365,114 @@
     
-%7D%0A%0A
     
-public IMessaging create(Address address) %7B%0A        IMessaging messagingStub;%0A%0A        if (address instanceof Channel
+messagingStubFactories.put(InProcessAddress.class, new AbstractMessagingStubFactory%3CInProcess
 Address
+%3E(
 ) %7B%0A
@@ -1487,128 +1487,66 @@
     
-messagingStub = messagingStubFactories.get(ChannelAddress.class).create(address);%0A        %7D else if (address instanceof 
+@Override%0A            protected IMessaging createInternal(
 InPr
@@ -1553,24 +1553,32 @@
 ocessAddress
+ address
 ) %7B%0A        
@@ -1581,31 +1581,26 @@
         
-messagingStub =
+    return
  new InP
@@ -1624,27 +1624,8 @@
 ub((
-(InProcessAddress) 
 addr
@@ -1657,152 +1657,184 @@
     
-%7D else if (address instanceof WebSocketAddress) %7B%0A            messagingStub = messagingStubFactories.get(WebSocketAddress.class).create(
+    %7D%0A%0A            @Override%0A            public void shutdown() %7B%0A                //nothing to do%0A            %7D%0A        %7D);%0A    %7D%0A%0A    public IMessaging create(Address 
 address)
 ;%0A  
@@ -1833,85 +1833,47 @@
 ess)
-;%0A 
+ %7B%0A
        
-%7D else if (address instanceof WebSocketClientAddress) %7B%0A           
+ AbstractMessagingStubFactory
  mes
@@ -1882,16 +1882,23 @@
 gingStub
+Factory
  = messa
@@ -1923,69 +1923,70 @@
 get(
-WebSocketClientAddress.class).create(address);%0A        %7D else
+address.getClass());%0A        if (messagingStubFactory == null)
  %7B%0A 
@@ -2128,16 +2128,39 @@
 gingStub
+Factory.create(address)
 ;%0A    %7D%0A

Make MessagingStubFactory.create generic