/*
 * #%L
 * %%
 * Copyright (C) 2018 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.jeeintegration;

import java.util.concurrent.ConcurrentHashMap;

import javax.ejb.Singleton;

import io.joynr.messaging.mqtt.statusmetrics.MqttStatusReceiver;
import io.joynr.statusmetrics.MessageWorkerStatus;
import io.joynr.statusmetrics.StatusReceiver;

@Singleton
public class JoynrStatusMetricsAggregator implements JoynrStatusMetrics, MqttStatusReceiver, StatusReceiver {
    private int numDiscardedMqttRequest = 0;
    private boolean isConnectedToMqttBroker = false;
    private long disconnectedFromMqttBrokerSinceTimestamp;
    private ConcurrentHashMap<Integer, MessageWorkerStatus> messageWorkersStatus = new ConcurrentHashMap<Integer, MessageWorkerStatus>();

    @Override
    public synchronized void notifyMessageDropped() {
        numDiscardedMqttRequest++;
    }

    @Override
    public synchronized void notifyConnectionStatusChanged(ConnectionStatus connectionStatus) {
        switch (connectionStatus) {
        case CONNECTED:
            isConnectedToMqttBroker = true;
            disconnectedFromMqttBrokerSinceTimestamp = -1;
            break;
        case NOT_CONNECTED:
            if (isConnectedToMqttBroker) {
                isConnectedToMqttBroker = false;
                disconnectedFromMqttBrokerSinceTimestamp = System.currentTimeMillis();
            }
            break;
        }
    }

    @Override
    public synchronized int getNumDiscardedMqttRequests() {
        return numDiscardedMqttRequest;
    }

    @Override
    public synchronized boolean isConnectedToMqttBroker() {
        return isConnectedToMqttBroker;
    }

    @Override
    public synchronized long getDisconnectedFromMqttBrokerSinceTimestamp() {
        return disconnectedFromMqttBrokerSinceTimestamp;
    }

    @Override
    public void updateMessageWorkerStatus(int messageWorkerId, MessageWorkerStatus messageWorkerStatus) {
        this.messageWorkersStatus.put(messageWorkerId, messageWorkerStatus);
    }
}

@@ -701,16 +701,247 @@
 ashMap;%0A
+import java.util.concurrent.atomic.AtomicBoolean;%0Aimport java.util.concurrent.atomic.AtomicInteger;%0Aimport java.util.concurrent.atomic.AtomicLong;%0A%0Aimport javax.ejb.ConcurrencyManagement;%0Aimport javax.ejb.ConcurrencyManagementType;
 %0Aimport 
@@ -1136,16 +1136,71 @@
 ngleton%0A
+@ConcurrencyManagement(ConcurrencyManagementType.BEAN)%0A
 public c
@@ -1313,19 +1313,29 @@
 private 
-int
+AtomicInteger
  numDisc
@@ -1357,24 +1357,111 @@
 t = 
-0;%0A    private b
+new AtomicInteger();%0A    private Object connectionStatusChangedLock = new Object();%0A    private AtomicB
 oole
@@ -1485,29 +1485,43 @@
 qttBroker = 
-false
+new AtomicBoolean()
 ;%0A    privat
@@ -1522,17 +1522,23 @@
 private 
-l
+AtomicL
 ong disc
@@ -1569,24 +1569,43 @@
 nceTimestamp
+ = new AtomicLong()
 ;%0A    privat
@@ -1754,37 +1754,24 @@
 %0A    public 
-synchronized 
 void notifyM
@@ -1823,10 +1823,26 @@
 uest
-++
+.incrementAndGet()
 ;%0A  
@@ -1871,29 +1871,16 @@
  public 
-synchronized 
 void not
@@ -1995,32 +1995,93 @@
 case CONNECTED:%0A
+            synchronized (connectionStatusChangedLock) %7B%0A    
             isCo
@@ -2103,17 +2103,24 @@
 oker
- = 
+.set(
 true
+)
 ;%0A
+    
     
@@ -2171,18 +2171,32 @@
 tamp
- = 
+.set(
 -1
+)
 ;%0A   
+ 
+        %7D%0A
     
@@ -2192,32 +2192,35 @@
      %7D%0A         
+   
 break;%0A        c
@@ -2230,32 +2230,93 @@
  NOT_CONNECTED:%0A
+            synchronized (connectionStatusChangedLock) %7B%0A    
             if (
@@ -2338,16 +2338,22 @@
 ttBroker
+.get()
 ) %7B%0A    
@@ -2356,32 +2356,36 @@
                 
+  
+  
 isConnectedToMqt
@@ -2395,16 +2395,19 @@
 oker
- = 
+.set(
 false
+)
 ;%0A  
@@ -2412,32 +2412,36 @@
                 
+   
+ 
 disconnectedFrom
@@ -2464,19 +2464,21 @@
 imestamp
- = 
+.set(
 System.c
@@ -2495,17 +2495,36 @@
 Millis()
-;
+);%0A                %7D
 %0A       
@@ -2591,29 +2591,16 @@
  public 
-synchronized 
 int getN
@@ -2665,16 +2665,22 @@
 tRequest
+.get()
 ;%0A    %7D%0A
@@ -2705,29 +2705,16 @@
  public 
-synchronized 
 boolean 
@@ -2779,16 +2779,22 @@
 ttBroker
+.get()
 ;%0A    %7D%0A
@@ -2819,29 +2819,16 @@
  public 
-synchronized 
 long get
@@ -2927,16 +2927,22 @@
 imestamp
+.get()
 ;%0A    %7D%0A

Improve concurrency management in JoynrStatusMetricsAggregator