| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
package org.apache.qpid.proton.systemtests; |
| 20 |
|
| 21 |
import java.lang.reflect.Constructor; |
| 22 |
import java.lang.reflect.Method; |
| 23 |
import java.util.logging.Logger; |
| 24 |
|
| 25 |
import org.apache.qpid.proton.engine.Transport; |
| 26 |
|
| 27 |
public class ProtocolTracerEnabler |
| 28 |
{ |
| 29 |
private static final Logger LOGGER = Logger.getLogger(ProtocolTracerEnabler.class.getName()); |
| 30 |
|
| 31 |
private static final String LOGGING_PROTOCOL_TRACER_CLASS_NAME = "org.apache.qpid.proton.logging.LoggingProtocolTracer"; |
| 32 |
|
| 33 |
/** |
| 34 |
* Attempts to set up a {@value #LOGGING_PROTOCOL_TRACER_CLASS_NAME} on the supplied transport. |
| 35 |
* Uses reflection so this code can be run without a compile-time dependency on proton-j-impl. |
| 36 |
*/ |
| 37 |
public static void setProtocolTracer(Transport transport, String prefix) |
| 38 |
{ |
| 39 |
try |
| 40 |
{ |
| 41 |
Class<?> loggingProtocolTracerClass = Class.forName(LOGGING_PROTOCOL_TRACER_CLASS_NAME); |
| 42 |
|
| 43 |
Constructor<?> loggingProtocolTracerConstructor = loggingProtocolTracerClass.getConstructor(String.class); |
| 44 |
Object newLoggingProtocolTracer = loggingProtocolTracerConstructor.newInstance(prefix); |
| 45 |
|
| 46 |
Class<?> protocolTracerClass = Class.forName("org.apache.qpid.proton.engine.impl.ProtocolTracer"); |
| 47 |
Method setPrococolTracerMethod = transport.getClass().getMethod("setProtocolTracer", protocolTracerClass); |
| 48 |
|
| 49 |
setPrococolTracerMethod.invoke(transport, newLoggingProtocolTracer); |
| 50 |
} |
| 51 |
catch(Exception e) |
| 52 |
{ |
| 53 |
if(e instanceof ClassNotFoundException || e instanceof NoSuchMethodException) |
| 54 |
{ |
| 55 |
LOGGER.fine("Protocol tracing disabled because unable to reflectively set a " |
| 56 |
+ LOGGING_PROTOCOL_TRACER_CLASS_NAME + " instance on the supplied transport which is a: " |
| 57 |
+ transport.getClass().getName()); |
| 58 |
} |
| 59 |
else |
| 60 |
{ |
| 61 |
throw new RuntimeException("Unable to set up protocol tracing", e); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |