Repository: camel Updated Branches: refs/heads/master efd7d3750 -> 2dfef38db
CAMEL-11692: Set ClassLoader property on HBase configuration Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/2dfef38d Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/2dfef38d Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/2dfef38d Branch: refs/heads/master Commit: 2dfef38db579ec8f14f421b34de8053306c6420b Parents: efd7d37 Author: James Netherton <jamesnether...@gmail.com> Authored: Wed Aug 23 09:11:39 2017 +0100 Committer: Andrea Cosentino <anco...@gmail.com> Committed: Wed Aug 23 10:52:03 2017 +0200 ---------------------------------------------------------------------- .../camel/component/hbase/HBaseComponent.java | 8 +++ .../component/hbase/HBaseComponentTest.java | 76 ++++++++++++++++++++ 2 files changed, 84 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/2dfef38d/components/camel-hbase/src/main/java/org/apache/camel/component/hbase/HBaseComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-hbase/src/main/java/org/apache/camel/component/hbase/HBaseComponent.java b/components/camel-hbase/src/main/java/org/apache/camel/component/hbase/HBaseComponent.java index 82244a3..d690092 100644 --- a/components/camel-hbase/src/main/java/org/apache/camel/component/hbase/HBaseComponent.java +++ b/components/camel-hbase/src/main/java/org/apache/camel/component/hbase/HBaseComponent.java @@ -50,6 +50,14 @@ public class HBaseComponent extends UriEndpointComponent { configuration = HBaseConfiguration.create(); } + if (configuration.getClassLoader() == null) { + ClassLoader applicationContextClassLoader = getCamelContext().getApplicationContextClassLoader(); + if (applicationContextClassLoader != null) { + configuration.setClassLoader(applicationContextClassLoader); + HBaseConfiguration.addHbaseResources(configuration); + } + } + connection = ConnectionFactory.createConnection( configuration, Executors.newFixedThreadPool(poolMaxSize) http://git-wip-us.apache.org/repos/asf/camel/blob/2dfef38d/components/camel-hbase/src/test/java/org/apache/camel/component/hbase/HBaseComponentTest.java ---------------------------------------------------------------------- diff --git a/components/camel-hbase/src/test/java/org/apache/camel/component/hbase/HBaseComponentTest.java b/components/camel-hbase/src/test/java/org/apache/camel/component/hbase/HBaseComponentTest.java new file mode 100644 index 0000000..5cfed5e --- /dev/null +++ b/components/camel-hbase/src/test/java/org/apache/camel/component/hbase/HBaseComponentTest.java @@ -0,0 +1,76 @@ +/** + * 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.camel.component.hbase; + +import java.io.IOException; + +import org.apache.camel.CamelContext; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseConfiguration; +import org.junit.Assert; +import org.junit.Test; + +public class HBaseComponentTest { + + @Test + public void testHBaseConfigurationClassLoaderSetToAppContextClassLoader() throws Exception { + ClassLoader expectedClassLoader = HBaseComponentTest.class.getClassLoader(); + + CamelContext camelContext = new DefaultCamelContext(); + camelContext.setApplicationContextClassLoader(expectedClassLoader); + + HBaseComponent component = new HBaseComponent(); + component.doStart(); + component.doStop(); + + ClassLoader actualClassLoader = component.getConfiguration().getClassLoader(); + Assert.assertSame(expectedClassLoader, actualClassLoader); + } + + @Test + public void testHBaseConfigurationClassLoaderNotOverridden() throws Exception { + ClassLoader expectedClassLoader = HBaseComponentTest.class.getClassLoader().getParent(); + + Configuration configuration = HBaseConfiguration.create(); + configuration.setClassLoader(expectedClassLoader); + + HBaseComponent component = new HBaseComponent(); + component.setConfiguration(configuration); + try { + component.doStart(); + } catch (IOException e) { + // Expected because the ClassLoader we set is not the correct one, but it's safe to ignore here + } + component.doStop(); + + ClassLoader actualClassLoader = component.getConfiguration().getClassLoader(); + Assert.assertSame(expectedClassLoader, actualClassLoader); + } + + @Test + public void testHBaseConfigurationClassLoaderSetToDefault() throws Exception { + ClassLoader expectedClassLoader = HBaseConfiguration.class.getClassLoader(); + + HBaseComponent component = new HBaseComponent(); + component.doStart(); + component.doStop(); + + ClassLoader actualClassLoader = component.getConfiguration().getClassLoader(); + Assert.assertSame(expectedClassLoader, actualClassLoader); + } +}