Created
March 19, 2013 19:22
-
-
Save splatch/5199297 to your computer and use it in GitHub Desktop.
Small utility class which gets rid of "necessary" processors from stack frame.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package experiments; | |
| import java.lang.reflect.Field; | |
| import org.apache.camel.AsyncProcessor; | |
| import org.apache.camel.Processor; | |
| import org.apache.camel.management.InstrumentationProcessor; | |
| import org.apache.camel.processor.DelegateAsyncProcessor; | |
| import org.apache.camel.processor.InterceptorToAsyncProcessorBridge; | |
| import org.apache.camel.processor.SendProcessor; | |
| import org.apache.camel.processor.interceptor.TraceInterceptor; | |
| public class UnwrapUtility { | |
| public static Processor unwrap(Processor processor) { | |
| if (processor instanceof TraceInterceptor) { | |
| System.err.println("Skipping TraceInterceptor"); | |
| return unwrap(((TraceInterceptor) processor).getProcessor()); | |
| } else if (processor instanceof InstrumentationProcessor) { | |
| System.err.println("Skipping InstrumentationProcessor"); | |
| return unwrap(((InstrumentationProcessor) processor).getProcessor()); | |
| } else if (processor instanceof DelegateAsyncProcessor) { | |
| System.err.println("Skipping DelegateAsyncProcessor"); | |
| return unwrap(((DelegateAsyncProcessor) processor).getProcessor()); | |
| } else if (processor instanceof InterceptorToAsyncProcessorBridge) { | |
| return unwrapAsyncX((InterceptorToAsyncProcessorBridge) processor); | |
| } else if (processor instanceof AsyncProcessor && !(processor instanceof SendProcessor)) { | |
| System.err.println("Skipping AsyncProcessor " + processor); | |
| return unwrapAsync((AsyncProcessor) processor); | |
| } | |
| System.err.println("Creating " + processor.getClass()); | |
| return processor; | |
| } | |
| private static Processor unwrapAsyncX(InterceptorToAsyncProcessorBridge processor) { | |
| return fieldProcessor(processor, "interceptor"); | |
| } | |
| private static Processor unwrapAsync(AsyncProcessor processor) { | |
| return fieldProcessor(processor, "processor"); | |
| } | |
| private static Processor fieldProcessor(AsyncProcessor processor, String name) { | |
| try { | |
| Field field = processor.getClass().getDeclaredField(name); | |
| field.setAccessible(true); | |
| Processor x = (Processor) field.get(processor); | |
| System.err.println("- replaced by " + x); | |
| return unwrap(x); | |
| } catch (NoSuchFieldException e) { | |
| e.printStackTrace(); | |
| } catch (SecurityException e) { | |
| e.printStackTrace(); | |
| } catch (IllegalArgumentException e) { | |
| e.printStackTrace(); | |
| } catch (IllegalAccessException e) { | |
| e.printStackTrace(); | |
| } | |
| return processor; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment