/*
 * Copyright (C) 2012-2013 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.nifty.processor;

import com.facebook.nifty.core.RequestContext;
import org.apache.thrift.TException;
import org.apache.thrift.TProcessor;
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TTransport;

public class NiftyProcessorAdapters
{
    public static NiftyProcessor processorFromTProcessor(final TProcessor standardThriftProcessor)
    {
        checkProcessMethodSignature();

        return new NiftyProcessor()
        {
            @Override
            public boolean process(TProtocol in, TProtocol out, RequestContext requestContext) throws TException
            {
                return standardThriftProcessor.process(in, out);
            }
        };
    }

    public static NiftyProcessorFactory factoryFromTProcessor(final TProcessor standardThriftProcessor)
    {
        checkProcessMethodSignature();

        return new NiftyProcessorFactory()
        {
            @Override
            public NiftyProcessor getProcessor(TTransport transport)
            {
                return processorFromTProcessor(standardThriftProcessor);
            }
        };
    }

    public static NiftyProcessorFactory factoryFromTProcessorFactory(final TProcessorFactory standardThriftProcessorFactory)
    {
        checkProcessMethodSignature();

        return new NiftyProcessorFactory()
        {
            @Override
            public NiftyProcessor getProcessor(TTransport transport)
            {
                return processorFromTProcessor(standardThriftProcessorFactory.getProcessor(transport));
            }
        };
    }

    /**
     * Catch the mismatch early if someone tries to pass our internal variant of TProcessor with
     * a different signature on the process() method into these adapters.
     */
    private static void checkProcessMethodSignature()
    {
        try {
            TProcessor.class.getMethod("process", TProtocol.class, TProtocol.class);
        }
        catch (NoSuchMethodException e) {
            // Facebook's TProcessor variant needs processor adapters from a different package
            throw new IllegalStateException("The loaded TProcessor class is not supported by version of the adapters");
        }
    }
}

@@ -931,16 +931,323 @@
 pters%0A%7B%0A
+    /**%0A     * Adapt a %7B@link TProcessor%7D to a standard Thrift %7B@link NiftyProcessor%7D. Nifty uses this%0A     * internally to adapt the processors generated by the standard Thrift code generator into%0A     * instances of %7B@link NiftyProcessor%7D usable by %7B@link com.facebook.nifty.core.NiftyDispatcher%7D%0A     */%0A
     publ
@@ -1329,32 +1329,32 @@
 hriftProcessor)%0A
-
     %7B%0A        ch
@@ -1667,32 +1667,211 @@
       %7D;%0A    %7D%0A%0A
+    /**%0A     * Create a %7B@link NiftyProcessorFactory%7D that always returns the same %7B@link NiftyProcessor%7D%0A     * adapted from the given standard Thrift %7B@link TProcessor%7D%0A     */%0A
     public stati
@@ -2263,24 +2263,235 @@
   %7D;%0A    %7D%0A%0A
+    /**%0A     * Create a %7B@link NiftyProcessorFactory%7D that delegates to a standard Thrift %7B@link TProcessorFactory%7D%0A     * to construct an instance, then adapts each instance to a %7B@link NiftyProcessor%7D%0A     */%0A
     public s
@@ -2901,18 +2901,1537 @@
 ssor
+%0A                        (transport));%0A            %7D%0A        %7D;%0A    %7D%0A%0A    /**%0A     * Adapt a %7B@link NiftyProcessor%7D to a standard Thrift %7B@link TProcessor%7D. The %7B@link%0A     * RequestContext%7D will always be %7B@code null%7D%0A     */%0A    public static TProcessor processorToTProcessor(final NiftyProcessor niftyProcessor)%0A    %7B%0A        return new TProcessor()%0A        %7B%0A            @Override%0A            public boolean process(TProtocol in, TProtocol out) throws TException%0A            %7B%0A                return niftyProcessor.process(in, out, null);%0A            %7D%0A        %7D;%0A    %7D%0A%0A    /**%0A     * Create a standard thrift %7B@link TProcessorFactory%7D that always returns the same%0A     * %7B@link TProcessor%7D adapted from the given %7B@link NiftyProcessor%7D%0A     */%0A    public static TProcessorFactory processorToTProcessorFactory(final NiftyProcessor niftyProcessor)%0A    %7B%0A        return new TProcessorFactory(processorToTProcessor(niftyProcessor));%0A    %7D%0A%0A    /**%0A     * Create a standard thrift %7B@link TProcessorFactory%7D that delegates to a%0A     * %7B@link NiftyProcessorFactory%7D to construct an instance, then adapts each instance to a%0A     * standard Thrift %7B@link TProcessor%7D%0A     */%0A    public static TProcessorFactory processorFactoryToTProcessorFactory(final NiftyProcessorFactory niftyProcessorFactory)%0A    %7B%0A        return new TProcessorFactory(null) %7B%0A            @Override%0A            public TProcessor getProcessor(TTransport trans)%0A            %7B%0A                return processorToTProcessor(niftyProcessorFactory.getProcessor
 (trans
-port
 ));%0A

Add adapters to adapt NiftyProcessors back to TProcessors