All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.cdap.plugin.common.CombineClassLoader Maven / Gradle / Ivy

There is a newer version: 2.12.3
Show newest version
/*
 * Copyright © 2020 Cask Data, Inc.
 *
 * Licensed 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 io.cdap.plugin.common;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;

/**
 * A {@link ClassLoader} that load classes from list of other {@link ClassLoader}s. Note that
 * this ClassLoader just delegates to other ClassLoaders, but never define class, hence no Class
 * loaded by this class would have {@link Class#getClassLoader()}} returning this ClassLoader.
 */
public class CombineClassLoader extends URLClassLoader {

  private static final Logger LOG = LoggerFactory.getLogger(CombineClassLoader.class);
  private final List delegates;

  /**
   * Creates a CombineClassLoader with the given parent and a list of ClassLoaders for delegation.
   *
   * @param parent parent ClassLoader. If null, bootstrap ClassLoader will be the parent.
   * @param delegates list of ClassLoaders for delegation
   */
  public CombineClassLoader(@Nullable ClassLoader parent, ClassLoader...delegates) {
    this(parent, Arrays.asList(delegates));
  }

  /**
   * Creates a CombineClassLoader with the given parent and a list of ClassLoaders for delegation.
   *
   * @param parent parent ClassLoader. If null, bootstrap ClassLoader will be the parent.
   * @param delegates list of ClassLoaders for delegation
   */
  public CombineClassLoader(@Nullable ClassLoader parent, Iterable delegates) {
    super(new URL[0], parent);
    this.delegates = ImmutableList.copyOf(delegates);
  }

  @Override
  public URL[] getURLs() {
    List urls = new ArrayList<>();
    for (ClassLoader delegate : delegates) {
      if (delegate instanceof URLClassLoader) {
        Collections.addAll(urls, ((URLClassLoader) delegate).getURLs());
      }
    }
    return urls.toArray(new URL[0]);
  }

  /**
   * Returns the list of {@link ClassLoader}s that this class delegates to.
   */
  public List getDelegates() {
    return delegates;
  }

  @Override
  protected Class findClass(String name) throws ClassNotFoundException {
    for (ClassLoader classLoader : delegates) {
      try {
        return classLoader.loadClass(name);
      } catch (ClassNotFoundException e) {
        LOG.trace("Class {} not found in ClassLoader {}", name, classLoader);
      }
    }

    throw new ClassNotFoundException("Class not found in all delegated ClassLoaders: " + name);
  }

  @Override
  public URL findResource(String name) {
    for (ClassLoader classLoader : delegates) {
      URL url = classLoader.getResource(name);
      if (url != null) {
        return url;
      }
    }
    return null;
  }

  @Override
  public Enumeration findResources(String name) throws IOException {
    // Using LinkedHashSet to preserve the ordering
    Set urls = Sets.newLinkedHashSet();
    for (ClassLoader classLoader : delegates) {
      Iterators.addAll(urls, Iterators.forEnumeration(classLoader.getResources(name)));
    }
    return Iterators.asEnumeration(urls.iterator());
  }

  @Override
  public InputStream getResourceAsStream(String name) {
    for (ClassLoader classLoader : delegates) {
      InputStream is = classLoader.getResourceAsStream(name);
      if (is != null) {
        return is;
      }
    }
    return null;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy