/*
 * #%L
 * %%
 * Copyright (C) 2011 - 2017 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%
 */
package io.joynr.examples.jee;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;

import io.joynr.jeeintegration.api.ServiceLocator;
import joynr.exceptions.ApplicationException;
import joynr.vehicle.Country;
import joynr.vehicle.GeoPosition;
import joynr.vehicle.RadioStation;
import joynr.vehicle.RadioSync;

@Path("/radio-stations")
@Produces(MediaType.APPLICATION_JSON)
public class RadioConsumerRestEndpoint {

    private ServiceLocator serviceLocator;

    private RadioSync radioClient;

    @Inject
    public RadioConsumerRestEndpoint(ServiceLocator serviceLocator) {
        this.serviceLocator = serviceLocator;
    }

    @GET
    @Path("/current-station")
    public RadioStation getCurrentRadioStation() {
        try {
            return getRadioClient().getCurrentStation();
        } catch (Exception e) {
            throw new WebApplicationException(e);
        }
    }

    @GET
    @Path("/current-station/location")
    public GeoPosition getLocationOfCurrentStation() {
        return getRadioClient().getLocationOfCurrentStation().location;
    }

    @GET
    @Path("/current-station/country")
    public Country getCountryOfCurrentStation() {
        return getRadioClient().getLocationOfCurrentStation().country;
    }

    @POST
    @Path("/shuffle")
    public void shuffleRadioStations() {
        getRadioClient().shuffleStations();
    }

    @POST
    @Produces({ MediaType.TEXT_PLAIN })
    public boolean addRadioStation(String name) {
        try {
            return getRadioClient().addFavoriteStation(new RadioStation(name, true, Country.GERMANY));
        } catch (ApplicationException e) {
            throw new WebApplicationException(e.getMessage());
        }
    }

    private RadioSync getRadioClient() {
        if (radioClient == null) {
            radioClient = serviceLocator.get(RadioSync.class, "io.joynr.examples.jee.provider");
        }
        return radioClient;
    }

}

@@ -655,24 +655,121 @@
 mples.jee;%0A%0A
+import java.util.concurrent.CompletableFuture;%0Aimport java.util.concurrent.locks.ReentrantLock;%0A%0A
 import javax
@@ -963,24 +963,173 @@
 MediaType;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport io.joynr.arbitration.ArbitrationStrategy;%0Aimport io.joynr.arbitration.DiscoveryQos;%0A
 import io.jo
@@ -1450,16 +1450,116 @@
 oint %7B%0A%0A
+    private final static Logger LOGGER = LoggerFactory.getLogger(RadioConsumerRestEndpoint.class);%0A%0A
     priv
@@ -1598,32 +1598,41 @@
 r;%0A%0A    private 
+volatile 
 RadioSync radioC
@@ -1639,16 +1639,82 @@
 lient;%0A%0A
+    private ReentrantLock radioClientLock = new ReentrantLock();%0A%0A
     @Inj
@@ -3020,16 +3020,145 @@
 ioClient
+Lock.lock();%0A            try %7B%0A                if (radioClient == null) %7B%0A                    CompletableFuture%3CRadioSync%3E future
  = servi
@@ -3167,19 +3167,23 @@
 Locator.
-get
+builder
 (RadioSy
@@ -3195,43 +3195,1109 @@
 ass,
- %22io.joynr.examples.jee.provider%22);
+%0A                                                                                 %22io.joynr.examples.jee.provider%22)%0A                                                                        .withDiscoveryQos(new DiscoveryQos(10000L,%0A                                                                                                           ArbitrationStrategy.HighestPriority,%0A                                                                                                           360000L))%0A                                                                        .useFuture()%0A                                                                        .build();%0A                    try %7B%0A                        radioClient = future.get();%0A                    %7D catch (Exception e) %7B%0A                        LOGGER.error(%22Unable to create a proxy for the radio provider. All calls will fail.%22, e);%0A                        throw new RuntimeException(%22Radio proxy creation failed.%22, e);%0A                    %7D%0A                %7D%0A            %7D finally %7B%0A                radioClientLock.unlock();%0A            %7D
 %0A   

use new service locator futures in radio JEE