/*
 * 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.endpoint.servers;

import com.google.inject.Inject;
import com.ning.metrics.collector.binder.config.CollectorConfig;
import com.ning.metrics.collector.util.NamedThreadFactory;
import org.apache.log4j.Logger;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TNonblockingServerTransport;
import org.apache.thrift.transport.TTransportException;
import scribe.thrift.scribe.Iface;
import scribe.thrift.scribe.Processor;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * Thrift service. Contacted usually by Scribe client.
 */
public class ScribeServer
{
    private final Iface eventRequestHandler;
    private final int port;
    private TNonblockingServer server = null;

    private static final Logger log = Logger.getLogger(ScribeServer.class);

    @Inject
    public ScribeServer(Iface eventRequestHandler, CollectorConfig config) throws TTransportException
    {
        this(eventRequestHandler, config.getScribePort());
    }

    public ScribeServer(Iface eventRequestHandler, int port) throws TTransportException
    {
        this.eventRequestHandler = eventRequestHandler;
        this.port = port;

        ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("ScribeServer"));
        executor.execute(new Runnable()
        {
            @Override
            public void run()
            {
                try {
                    start();

                    Runtime.getRuntime().addShutdownHook(new Thread()
                    {
                        @Override
                        public void run()
                        {
                            shutdown();
                        }
                    });
                }
                catch (TTransportException e) {
                    log.warn("Unable to start the Scribe server", e);
                    Thread.currentThread().interrupt();
                }
            }
        });
    }

    /**
     * Start the terminal Scribe server
     *
     * @throws TTransportException if the TNonblockingServerSocket cannot be instantiated
     */
    private void start() throws TTransportException
    {
        TNonblockingServerTransport socket = new TNonblockingServerSocket(port);
        Processor processor = new Processor(eventRequestHandler);

        server = new TNonblockingServer(new TNonblockingServer.Args(socket).processor(processor).protocolFactory(new TBinaryProtocol.Factory()));
        log.info(String.format("Starting terminal Scribe server on port %d", port));
        server.serve();
    }

    /**
     * Stop the terminal Scribe server
     */
    private void shutdown()
    {
        if (server != null) {
            server.stop();
        }
    }
}


@@ -1426,128 +1426,8 @@
 r%0A%7B%0A
-    private final Iface eventRequestHandler;%0A    private final int port;%0A    private TNonblockingServer server = null;%0A%0A
     
@@ -1507,40 +1507,22 @@
     
-@Inject%0A    public ScribeServer(
+private final 
 Ifac
@@ -1542,17 +1542,35 @@
 tHandler
-,
+;%0A    private final
  Collect
@@ -1588,108 +1588,69 @@
 nfig
-) throws TTransportException%0A    %7B%0A        this(eventRequestHandler, config.getScribePort())
+;%0A%0A    private TNonblockingServer server = null
 ;%0A
+%0A
     
-%7D%0A
+@Inject
 %0A   
@@ -1701,16 +1701,30 @@
 er, 
-int port
+CollectorConfig config
 ) th
@@ -1827,19 +1827,23 @@
 his.
-port = port
+config = config
 ;%0A%0A 
@@ -2941,20 +2941,38 @@
 rSocket(
-p
+config.getScribeP
 ort
+()
 );%0A     

simplify ScribeServer class