/*
 * Copyright 2015-present Facebook, Inc.
 *
 * 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.
 */

package com.facebook.buck.intellij.ideabuck.ws.buckevents;

import com.facebook.buck.event.external.events.BuckEventExternalInterface;
import com.facebook.buck.intellij.ideabuck.ws.buckevents.consumers.BuckEventsConsumerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.intellij.openapi.application.ApplicationManager;

import java.io.IOException;

public class BuckEventsQueue implements BuckEventsQueueInterface {

    private final BuckEventsConsumerFactory mFactory;
    private ObjectMapper mObjectMapper;
    private BuckEventsAdapter mBuckEventsAdapter;

    public BuckEventsQueue(ObjectMapper objectMapper, BuckEventsConsumerFactory factory) {
        mFactory = factory;
        mObjectMapper = objectMapper;
        mBuckEventsAdapter = new BuckEventsAdapter();
    }

    @Override
    public void add(String rawMessage, BuckEventExternalInterface event) {
        handleEvent(rawMessage, event);
    }

    private void handleEvent(final String rawMessage, final BuckEventExternalInterface event) {
        ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
            @Override
            public void run() {
                synchronized (BuckEventsQueue.this.mFactory) {
                    try {
                        mBuckEventsAdapter.get(
                            event.getEventName()).handleEvent(
                            rawMessage,
                            event,
                            mFactory,
                            mObjectMapper);
                    } catch (IOException e) {
                        // TODO(cosmin1123) Handle exception
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}

@@ -829,16 +829,100 @@
 actory;%0A
+import com.facebook.buck.intellij.ideabuck.ws.buckevents.handlers.BuckEventHandler;%0A
 import c
@@ -961,24 +961,24 @@
 jectMapper;%0A
-
 import com.i
@@ -1024,16 +1024,63 @@
 Manager;
+%0Aimport com.intellij.openapi.diagnostic.Logger;
 %0A%0Aimport
@@ -1170,16 +1170,98 @@
 face %7B%0A%0A
+    private static final Logger LOG = Logger.getInstance(BuckEventsQueue.class);%0A%0A
     priv
@@ -2092,24 +2092,125 @@
             
+ String eventName = event.getEventName();%0A                        BuckEventHandler buckEventHandler =
  mBuckEvents
@@ -2221,16 +2221,27 @@
 ter.get(
+eventName);
 %0A       
@@ -2261,33 +2261,209 @@
     
-    event.getEventName())
+if (buckEventHandler == null) %7B%0A                            LOG.warn(%22Unhandled event '%22 + eventName + %22': %22 + rawMessage);%0A                        %7D else %7B%0A                            buckEventHandler
 .han
@@ -2500,16 +2500,20 @@
         
+    
 rawMessa
@@ -2544,16 +2544,20 @@
         
+    
 event,%0A 
@@ -2575,32 +2575,36 @@
                 
+   
+ 
 mFactory,%0A      
@@ -2597,16 +2597,20 @@
 actory,%0A
+    
         
@@ -2645,16 +2645,42 @@
 apper);%0A
+                        %7D%0A
         

Fix NPE when an unrecognized event was encountered.