diff --git a/htrace-core4/src/main/java/org/apache/htrace/core/SpanReceiver.java b/htrace-core4/src/main/java/org/apache/htrace/core/SpanReceiver.java index a955ddf..8093e77 100644 --- a/htrace-core4/src/main/java/org/apache/htrace/core/SpanReceiver.java +++ b/htrace-core4/src/main/java/org/apache/htrace/core/SpanReceiver.java @@ -18,6 +18,7 @@ package org.apache.htrace.core; import java.io.Closeable; import java.lang.reflect.Constructor; +import java.util.ArrayList; import java.util.concurrent.atomic.AtomicLong; import org.apache.commons.logging.Log; @@ -39,7 +40,8 @@ public abstract class SpanReceiver implements Closeable { public static class Builder { private static final Log LOG = LogFactory.getLog(Builder.class); - private final static String DEFAULT_PACKAGE = "org.apache.htrace.core"; + private final static String[] DEFAULT_PACKAGES = + {"org.apache.htrace.impl", "org.apache.htrace.core"}; private final HTraceConfiguration conf; private boolean logErrors; private String className; @@ -107,33 +109,43 @@ public abstract class SpanReceiver implements Closeable { if ((className == null) || className.isEmpty()) { throwError("No span receiver class specified."); } - String str = className; - if (!str.contains(".")) { - str = DEFAULT_PACKAGE + "." + str; + ArrayList classNames = new ArrayList(); + if (!className.contains(".")) { + for (String pkg : DEFAULT_PACKAGES) { + classNames.add(pkg + "." + className); + } + } else { + classNames.add(className); } Class cls = null; - try { - cls = classLoader.loadClass(str); - } catch (ClassNotFoundException e) { - throwError("Cannot find SpanReceiver class " + str); + for (String name : classNames) { + try { + cls = classLoader.loadClass(name); + break; + } catch (ClassNotFoundException e) { + LOG.debug("Cannot find SpanReceiver class " + name); + } + } + if (cls == null) { + throwError("Cannot find SpanReceiver class " + className); } Constructor ctor = null; try { ctor = cls.getConstructor(HTraceConfiguration.class); } catch (NoSuchMethodException e) { throwError("Cannot find a constructor for class " + - str + "which takes an HTraceConfiguration."); + cls.getName() + "which takes an HTraceConfiguration."); } SpanReceiver receiver = null; try { - LOG.debug("Creating new instance of " + str + "..."); + LOG.debug("Creating new instance of " + cls.getName() + "..."); receiver = ctor.newInstance(conf); } catch (ReflectiveOperationException e) { throwError("Reflection error when constructing " + - str + ".", e); + cls.getName() + ".", e); } catch (Throwable t) { throwError("NewInstance error when constructing " + - str + ".", t); + cls.getName() + ".", t); } return receiver; } diff --git a/htrace-core4/src/test/java/org/apache/htrace/core/TestSpanReceiverBuilder.java b/htrace-core4/src/test/java/org/apache/htrace/core/TestSpanReceiverBuilder.java index b97d624..ece6b32 100644 --- a/htrace-core4/src/test/java/org/apache/htrace/core/TestSpanReceiverBuilder.java +++ b/htrace-core4/src/test/java/org/apache/htrace/core/TestSpanReceiverBuilder.java @@ -55,6 +55,9 @@ public class TestSpanReceiverBuilder { receivers = createSpanReceivers("POJOSpanReceiver"); Assert.assertTrue(receivers.get(0).getClass().getName(). equals("org.apache.htrace.core.POJOSpanReceiver")); + receivers = createSpanReceivers("ImplSpanReceiver"); + Assert.assertTrue(receivers.get(0).getClass().getName(). + equals("org.apache.htrace.impl.ImplSpanReceiver")); receivers = createSpanReceivers( "org.apache.htrace.core.StandardOutSpanReceiver"); Assert.assertTrue(receivers.get(0).getClass().getName(). diff --git a/htrace-core4/src/test/java/org/apache/htrace/impl/ImplSpanReceiver.java b/htrace-core4/src/test/java/org/apache/htrace/impl/ImplSpanReceiver.java new file mode 100644 index 0000000..c5f3171 --- /dev/null +++ b/htrace-core4/src/test/java/org/apache/htrace/impl/ImplSpanReceiver.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 org.apache.htrace.impl; + +import java.io.IOException; +import org.apache.htrace.core.HTraceConfiguration; +import org.apache.htrace.core.Span; +import org.apache.htrace.core.SpanReceiver; + +/** + * SpanReceiver for testing only. + * Builder should be able to load this class by simple name. + */ +public class ImplSpanReceiver extends SpanReceiver { + public ImplSpanReceiver(HTraceConfiguration conf) { + } + + @Override + public void close() throws IOException { + } + + @Override + public void receiveSpan(Span span) { + } +}