/*
 * Copyright 2010-2011 Ning, Inc.
 *
 * Ning licenses this file to you 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.ning.metrics.collector.events.processing;

import com.google.inject.Inject;
import com.ning.metrics.collector.binder.annotations.ScribeStats;
import com.ning.metrics.collector.binder.config.CollectorConfig;
import com.ning.metrics.collector.endpoint.EventEndPointStats;
import com.ning.metrics.collector.endpoint.EventStats;
import com.ning.metrics.serialization.event.Event;
import org.apache.log4j.Logger;
import org.weakref.jmx.Managed;
import scribe.thrift.LogEntry;

public class ScribeEventHandlerImpl implements ScribeEventHandler
{
    private static final Logger logger = Logger.getLogger(ScribeEventHandlerImpl.class);

    private final EventCollector collector;
    private volatile boolean scribeCollectionEnabled;
    private final EventEndPointStats stats;

    @Inject
    public ScribeEventHandlerImpl(
        EventCollector collector,
        CollectorConfig config,
        @ScribeStats EventEndPointStats endPointStats
    )
    {
        this(collector, config.isScribeCollectionEnabled(), endPointStats);
    }

    public ScribeEventHandlerImpl(
        EventCollector collector,
        boolean enabled,
        EventEndPointStats stats)
    {
        this.collector = collector;
        this.scribeCollectionEnabled = enabled;
        this.stats = stats;
    }

    @Override
    public boolean processEvent(Event event, EventStats eventStats)
    {
        assert event != null;
        boolean success;

        stats.updateTotalEvents();

        if (scribeCollectionEnabled) {
            logger.debug(String.format("Processing event of type [%s], collection enabled", event.getName()));

            success = collector.collectEvent(event, eventStats);
            if (success) {
                stats.updateSuccesfulEventCounters(event);
                logger.debug(String.format("Event accepted: %s", event.getData()));
            }
            else {
                stats.updateRejectedEvents();
                logger.warn(String.format("Event rejected: %s", event.getData()));
            }
        }
        else {
            stats.updateRejectedEvents();
            logger.debug(String.format("Rejecting event [%s], collection disabled", event));
            success = true;
        }
        return success;
    }

    @Override
    public void handleFailure(LogEntry l, EventStats eventStats)
    {
        logger.warn(String.format("Error parsing request type: %s", l));
        stats.updateTotalEvents();
        stats.updateFailedEvents();
    }

    @Managed(description = "enable/disable collection of events")
    public void setScribeCollectionEnabled(boolean value)
    {
        scribeCollectionEnabled = value;
    }

    @Managed(description = "event collection enabled?")
    public boolean getScribeCollectionEnabled()
    {
        return scribeCollectionEnabled;
    }
}

@@ -1319,92 +1319,93 @@
 ate 
-volatile boolean scribeCollectionEnabled;%0A    private final EventEndPointStats stats
+final EventEndPointStats stats;%0A%0A    private volatile boolean scribeCollectionEnabled
 ;%0A%0A 
@@ -1594,17 +1594,17 @@
     this
-(
+.
 collecto
@@ -1608,253 +1608,71 @@
 ctor
-, config.isScribeCollectionEnabled(), endPointStats)
+ = collector
 ;%0A    
-%7D%0A%0A
     
-public ScribeEventHandlerImpl(%0A        EventCollector collector,%0A        boolean enabled,%0A        EventEndPointStats stats)%0A    %7B%0A        this.collector = collector;%0A        this.s
+this.scribeCollectionEnabled = config.isS
 crib
@@ -1693,18 +1693,10 @@
 bled
- = enabled
+()
 ;%0A  
@@ -1714,17 +1714,25 @@
 stats = 
-s
+endPointS
 tats;%0A  

simplify ScribeEventHandlerImpl class