Repository: incubator-htrace Updated Branches: refs/heads/master ad47b1a39 -> 59f4ca949
HTRACE-15: Rename SamplerFactory to SamplerBuilder (cmccabe) Project: http://git-wip-us.apache.org/repos/asf/incubator-htrace/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-htrace/commit/59f4ca94 Tree: http://git-wip-us.apache.org/repos/asf/incubator-htrace/tree/59f4ca94 Diff: http://git-wip-us.apache.org/repos/asf/incubator-htrace/diff/59f4ca94 Branch: refs/heads/master Commit: 59f4ca949bae048e9336f2bf4306b55258e6e875 Parents: ad47b1a Author: Colin P. Mccabe <[email protected]> Authored: Thu Dec 11 13:57:37 2014 -0800 Committer: Colin P. Mccabe <[email protected]> Committed: Thu Dec 11 15:53:31 2014 -0800 ---------------------------------------------------------------------- .../java/org/apache/htrace/SamplerBuilder.java | 73 ++++++++++++++++++++ .../java/org/apache/htrace/SamplerFactory.java | 73 -------------------- .../java/org/apache/htrace/TestSampler.java | 8 +-- 3 files changed, 77 insertions(+), 77 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/59f4ca94/htrace-core/src/main/java/org/apache/htrace/SamplerBuilder.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/apache/htrace/SamplerBuilder.java b/htrace-core/src/main/java/org/apache/htrace/SamplerBuilder.java new file mode 100644 index 0000000..614a190 --- /dev/null +++ b/htrace-core/src/main/java/org/apache/htrace/SamplerBuilder.java @@ -0,0 +1,73 @@ +/* + * 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; + +import java.lang.reflect.Constructor; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.htrace.impl.AlwaysSampler; +import org.apache.htrace.impl.NeverSampler; + +public class SamplerBuilder { + private final static String SAMPLER_CONF_KEY = "sampler"; + private final static ClassLoader classLoader = + SamplerBuilder.class.getClassLoader(); + private final HTraceConfiguration conf; + private static final Log LOG = LogFactory.getLog(SamplerBuilder.class); + + public SamplerBuilder(HTraceConfiguration conf) { + this.conf = conf; + } + + public Sampler build() { + String str = conf.get(SAMPLER_CONF_KEY); + if (str.isEmpty()) { + return NeverSampler.INSTANCE; + } + if (!str.contains(".")) { + str = "org.apache.htrace.impl." + str; + } + Class cls = null; + try { + cls = classLoader.loadClass(str); + } catch (ClassNotFoundException e) { + LOG.error("SamplerBuilder cannot find sampler class " + str + + ": falling back on NeverSampler."); + return NeverSampler.INSTANCE; + } + Constructor<Sampler> ctor = null; + try { + ctor = cls.getConstructor(HTraceConfiguration.class); + } catch (NoSuchMethodException e) { + LOG.error("SamplerBuilder cannot find a constructor for class " + str + + "which takes an HTraceConfiguration. Falling back on " + + "NeverSampler."); + return NeverSampler.INSTANCE; + } + try { + return ctor.newInstance(conf); + } catch (ReflectiveOperationException e) { + LOG.error("SamplerBuilder reflection error when constructing " + str + + ". Falling back on NeverSampler.", e); + return NeverSampler.INSTANCE; + } catch (Throwable e) { + LOG.error("SamplerBuilder constructor error when constructing " + str + + ". Falling back on NeverSampler.", e); + return NeverSampler.INSTANCE; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/59f4ca94/htrace-core/src/main/java/org/apache/htrace/SamplerFactory.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/apache/htrace/SamplerFactory.java b/htrace-core/src/main/java/org/apache/htrace/SamplerFactory.java deleted file mode 100644 index 84b52f1..0000000 --- a/htrace-core/src/main/java/org/apache/htrace/SamplerFactory.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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; - -import java.lang.reflect.Constructor; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.htrace.impl.AlwaysSampler; -import org.apache.htrace.impl.NeverSampler; - -public class SamplerFactory { - private final static String SAMPLER_CONF_KEY = "sampler"; - private final static ClassLoader classLoader = - SamplerFactory.class.getClassLoader(); - private final HTraceConfiguration conf; - private static final Log LOG = LogFactory.getLog(SamplerFactory.class); - - public SamplerFactory(HTraceConfiguration conf) { - this.conf = conf; - } - - public Sampler build() { - String str = conf.get(SAMPLER_CONF_KEY); - if (str.isEmpty()) { - return NeverSampler.INSTANCE; - } - if (!str.contains(".")) { - str = "org.apache.htrace.impl." + str; - } - Class cls = null; - try { - cls = classLoader.loadClass(str); - } catch (ClassNotFoundException e) { - LOG.error("SamplerFactory cannot find sampler class " + str + - ": falling back on NeverSampler."); - return NeverSampler.INSTANCE; - } - Constructor<Sampler> ctor = null; - try { - ctor = cls.getConstructor(HTraceConfiguration.class); - } catch (NoSuchMethodException e) { - LOG.error("SamplerFactory cannot find a constructor for class " + str + - "which takes an HTraceConfiguration. Falling back on " + - "NeverSampler."); - return NeverSampler.INSTANCE; - } - try { - return ctor.newInstance(conf); - } catch (ReflectiveOperationException e) { - LOG.error("SamplerFactory reflection error when constructing " + str + - ". Falling back on NeverSampler.", e); - return NeverSampler.INSTANCE; - } catch (Throwable e) { - LOG.error("SamplerFactory constructor error when constructing " + str + - ". Falling back on NeverSampler.", e); - return NeverSampler.INSTANCE; - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/59f4ca94/htrace-core/src/test/java/org/apache/htrace/TestSampler.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/test/java/org/apache/htrace/TestSampler.java b/htrace-core/src/test/java/org/apache/htrace/TestSampler.java index 265d603..31060d4 100644 --- a/htrace-core/src/test/java/org/apache/htrace/TestSampler.java +++ b/htrace-core/src/test/java/org/apache/htrace/TestSampler.java @@ -29,18 +29,18 @@ import org.junit.Test; public class TestSampler { @Test - public void testSamplerFactory() { - Sampler alwaysSampler = new SamplerFactory( + public void testSamplerBuilder() { + Sampler alwaysSampler = new SamplerBuilder( HTraceConfiguration.fromKeyValuePairs("sampler", "AlwaysSampler")). build(); Assert.assertEquals(AlwaysSampler.class, alwaysSampler.getClass()); - Sampler neverSampler = new SamplerFactory( + Sampler neverSampler = new SamplerBuilder( HTraceConfiguration.fromKeyValuePairs("sampler", "NeverSampler")). build(); Assert.assertEquals(NeverSampler.class, neverSampler.getClass()); - Sampler neverSampler2 = new SamplerFactory(HTraceConfiguration. + Sampler neverSampler2 = new SamplerBuilder(HTraceConfiguration. fromKeyValuePairs("sampler", "NonExistentSampler")). build(); Assert.assertEquals(NeverSampler.class, neverSampler2.getClass());
