Repository: camel Updated Branches: refs/heads/fix/CAMEL-8689-camel-dozer-classloading-issue 87eebc6c0 -> bf7e94fcb
CAMEL-8689 Camel Dozer classloading issue: minor adjustments for code quality/clarity. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/bf7e94fc Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/bf7e94fc Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/bf7e94fc Branch: refs/heads/fix/CAMEL-8689-camel-dozer-classloading-issue Commit: bf7e94fcb189760fd3c2d33376dce66c6f9273ee Parents: 87eebc6 Author: Raul Kripalani <ra...@apache.org> Authored: Thu Apr 23 15:40:41 2015 +0100 Committer: Raul Kripalani <ra...@apache.org> Committed: Thu Apr 23 15:40:41 2015 +0100 ---------------------------------------------------------------------- .../dozer/DozerThreadContextClassLoader.java | 83 ++++++++++++++++++++ .../dozer/DozerTypeConverterLoader.java | 8 +- .../dozer/ThreadContextClassLoader.java | 83 -------------------- 3 files changed, 87 insertions(+), 87 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/bf7e94fc/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/DozerThreadContextClassLoader.java ---------------------------------------------------------------------- diff --git a/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/DozerThreadContextClassLoader.java b/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/DozerThreadContextClassLoader.java new file mode 100644 index 0000000..e5d89e1 --- /dev/null +++ b/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/DozerThreadContextClassLoader.java @@ -0,0 +1,83 @@ +/** + * 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.converter.dozer; + +import java.net.MalformedURLException; +import java.net.URL; + +import org.apache.commons.lang3.ClassUtils; +import org.apache.commons.lang3.StringUtils; +import org.dozer.util.DozerClassLoader; +import org.dozer.util.MappingUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DozerThreadContextClassLoader implements DozerClassLoader { + + private static final Logger LOG = LoggerFactory.getLogger(DozerThreadContextClassLoader.class); + + private final DozerClassLoader delegate; + + public DozerThreadContextClassLoader(DozerClassLoader delegate) { + this.delegate = delegate; + } + + @Override + public Class<?> loadClass(String className) { + LOG.debug("Loading class from classloader: {}.", Thread.currentThread().getContextClassLoader()); + Class<?> result = null; + try { + // try to resolve the class from the thread context classloader + result = ClassUtils.getClass(Thread.currentThread().getContextClassLoader(), className); + } catch (ClassNotFoundException e) { + // if unresolvable, ask the delegate + result = delegate.loadClass(className); + if (result == null) { + MappingUtils.throwMappingException(e); + } + } + return result; + } + + @Override + public URL loadResource(String uri) { + LOG.debug("Loading resource from classloader: {}.", Thread.currentThread().getContextClassLoader()); + URL answer = Thread.currentThread().getContextClassLoader().getResource(uri); + + // try loading it from the delegate + if (answer == null) { + answer = delegate.loadResource(uri); + } + + // try treating it as a system resource + if (answer == null) { + answer = ClassLoader.getSystemResource(uri); + } + + // one more time in case it's a normal URI + if (answer == null && StringUtils.contains(uri, ":")) { + try { + answer = new URL(uri); + } catch (MalformedURLException e) { + MappingUtils.throwMappingException(e); + } + } + + return answer; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/bf7e94fc/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/DozerTypeConverterLoader.java ---------------------------------------------------------------------- diff --git a/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/DozerTypeConverterLoader.java b/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/DozerTypeConverterLoader.java index fb40c3b..744f117 100644 --- a/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/DozerTypeConverterLoader.java +++ b/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/DozerTypeConverterLoader.java @@ -105,9 +105,9 @@ public class DozerTypeConverterLoader extends ServiceSupport implements CamelCon public DozerTypeConverterLoader(CamelContext camelContext, DozerBeanMapperConfiguration configuration) { GlobalSettings settings = GlobalSettings.getInstance(); try { - log.info("Configuring GlobalSettings to use Camel classloader: {}", ThreadContextClassLoader.class.getName()); + log.info("Configuring GlobalSettings to use Camel classloader: {}", DozerThreadContextClassLoader.class.getName()); Field field = settings.getClass().getDeclaredField("classLoaderBeanName"); - ReflectionHelper.setField(field, settings, ThreadContextClassLoader.class.getName()); + ReflectionHelper.setField(field, settings, DozerThreadContextClassLoader.class.getName()); } catch (Exception e) { throw new IllegalStateException("Cannot configure Dozer GlobalSettings to use CamelToDozerClassResolverAdapter as classloader due " + e.getMessage(), e); } @@ -366,8 +366,8 @@ public class DozerTypeConverterLoader extends ServiceSupport implements CamelCon // if the classloader we're replacing is not a ThreadContextClassLoader, pass it on as delegate target // otherwise, don't do anything as we have - if (!(oldCl instanceof ThreadContextClassLoader)) { - ThreadContextClassLoader newCl = new ThreadContextClassLoader(oldCl); + if (!(oldCl instanceof DozerThreadContextClassLoader)) { + DozerThreadContextClassLoader newCl = new DozerThreadContextClassLoader(oldCl); BeanContainer.getInstance().setClassLoader(newCl); log.info("Switched Dozer container-wide classloader from: {} to {} (where the latter delegates to former).", oldCl, newCl); } else { http://git-wip-us.apache.org/repos/asf/camel/blob/bf7e94fc/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/ThreadContextClassLoader.java ---------------------------------------------------------------------- diff --git a/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/ThreadContextClassLoader.java b/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/ThreadContextClassLoader.java deleted file mode 100644 index 44cf055..0000000 --- a/components/camel-dozer/src/main/java/org/apache/camel/converter/dozer/ThreadContextClassLoader.java +++ /dev/null @@ -1,83 +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.camel.converter.dozer; - -import java.net.MalformedURLException; -import java.net.URL; - -import org.apache.commons.lang3.ClassUtils; -import org.apache.commons.lang3.StringUtils; -import org.dozer.util.DozerClassLoader; -import org.dozer.util.MappingUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ThreadContextClassLoader implements DozerClassLoader { - - private static final Logger LOG = LoggerFactory.getLogger(ThreadContextClassLoader.class); - - private DozerClassLoader delegate; - - public ThreadContextClassLoader(DozerClassLoader delegate) { - this.delegate = delegate; - } - - @Override - public Class<?> loadClass(String className) { - LOG.debug("Loading class from classloader: {}.", Thread.currentThread().getContextClassLoader()); - Class<?> result = null; - try { - // try to resolve the class from the thread context classloader - result = ClassUtils.getClass(Thread.currentThread().getContextClassLoader(), className); - } catch (ClassNotFoundException e) { - // if unresolvable, ask the delegate - result = delegate.loadClass(className); - if (result == null) { - MappingUtils.throwMappingException(e); - } - } - return result; - } - - @Override - public URL loadResource(String uri) { - LOG.debug("Loading resource from classloader: {}.", Thread.currentThread().getContextClassLoader()); - URL answer = Thread.currentThread().getContextClassLoader().getResource(uri); - - // try loading it from the delegate - if (answer == null) { - answer = delegate.loadResource(uri); - } - - // try treating it as a system resource - if (answer == null) { - answer = ClassLoader.getSystemResource(uri); - } - - // one more time in case it's a normal URI - if (answer == null && StringUtils.contains(uri, ":")) { - try { - answer = new URL(uri); - } catch (MalformedURLException e) { - MappingUtils.throwMappingException(e); - } - } - - return answer; - } - -}