Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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 com.facebook.presto.server;
 
 
 import java.net.URL;
 import java.util.List;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
         extends URLClassLoader
 {
     private final List<StringhiddenClasses;
     private final List<StringparentFirstClasses;
     private final List<StringhiddenResources;
     private final List<StringparentFirstResources;
 
     public PluginClassLoader(List<URLurls,
             ClassLoader parent,
             Iterable<StringhiddenClasses,
             Iterable<StringparentFirstClasses)
     {
         this(urls,
                 parent,
                 hiddenClasses,
                 parentFirstClasses,
                 Iterables.transform(hiddenClasses, PluginClassLoader::classNameToResource),
                 Iterables.transform(parentFirstClasses, PluginClassLoader::classNameToResource));
     }
 
     public PluginClassLoader(List<URLurls,
             ClassLoader parent,
             Iterable<StringhiddenClasses,
             Iterable<StringparentFirstClasses,
             Iterable<StringhiddenResources,
             Iterable<StringparentFirstResources)
     {
         // child first requires a parent class loader
         super(urls.toArray(new URL[urls.size()]), checkNotNull(parent"parent is null"));
         this. = ImmutableList.copyOf(hiddenClasses);
         this. = ImmutableList.copyOf(parentFirstClasses);
         this. = ImmutableList.copyOf(hiddenResources);
         this. = ImmutableList.copyOf(parentFirstResources);
     }
 
     @Override
     protected Class<?> loadClass(String nameboolean resolve)
             throws ClassNotFoundException
     {
         // grab the magic lock
         synchronized (getClassLoadingLock(name)) {
             // Check if class is in the loaded classes cache
             Class<?> cachedClass = findLoadedClass(name);
             if (cachedClass != null) {
                 return resolveClass(cachedClassresolve);
             }
 
             // If this is not a parent first class, look for the class locally
             if (!isParentFirstClass(name)) {
                 try {
                     Class<?> clazz = findClass(name);
                     return resolveClass(clazzresolve);
                 }
                 catch (ClassNotFoundException ignored) {
                     // not a local class
                 }
             }
 
             // Check parent class loaders, unless this is a hidden class
             if (!isHiddenClass(name)) {
                 try {
                     Class<?> clazz = getParent().loadClass(name);
                     return resolveClass(clazzresolve);
                 }
                 catch (ClassNotFoundException ignored) {
                     // this parent didn't have the class
                 }
             }
 
            // If this is a parent first class, now look for the class locally
            if (isParentFirstClass(name)) {
                Class<?> clazz = findClass(name);
                return resolveClass(clazzresolve);
            }
            throw new ClassNotFoundException(name);
        }
    }
    private Class<?> resolveClass(Class<?> clazzboolean resolve)
    {
        if (resolve) {
            resolveClass(clazz);
        }
        return clazz;
    }
    private boolean isParentFirstClass(String name)
    {
        for (String nonOverridableClass : ) {
            // todo maybe make this more precise and only match base package
            if (name.startsWith(nonOverridableClass)) {
                return true;
            }
        }
        return false;
    }
    private boolean isHiddenClass(String name)
    {
        for (String hiddenClass : ) {
            // todo maybe make this more precise and only match base package
            if (name.startsWith(hiddenClass)) {
                return true;
            }
        }
        return false;
    }
    @Override
    public URL getResource(String name)
    {
        // If this is not a parent first resource, check local resources first
        if (!isParentFirstResource(name)) {
            URL url = findResource(name);
            if (url != null) {
                return url;
            }
        }
        // Check parent class loaders
        if (!isHiddenResource(name)) {
            URL url = getParent().getResource(name);
            if (url != null) {
                return url;
            }
        }
        // If this is a parent first resource, now check local resources
        if (isParentFirstResource(name)) {
            URL url = findResource(name);
            if (url != null) {
                return url;
            }
        }
        return null;
    }
    @Override
    public Enumeration<URLgetResources(String name)
            throws IOException
    {
        List<Iterator<URL>> resources = new ArrayList<>();
        // If this is not a parent first resource, add resources from local urls first
        if (!isParentFirstResource(name)) {
            Iterator<URLmyResources = Iterators.forEnumeration(findResources(name));
            resources.add(myResources);
        }
        // Add parent resources
        if (!isHiddenResource(name)) {
            Iterator<URLparentResources = Iterators.forEnumeration(getParent().getResources(name));
            resources.add(parentResources);
        }
        // If this is a parent first resource, now add resources from local urls
        if (isParentFirstResource(name)) {
            Iterator<URLmyResources = Iterators.forEnumeration(findResources(name));
            resources.add(myResources);
        }
        return Iterators.asEnumeration(Iterators.concat(resources.iterator()));
    }
    private boolean isParentFirstResource(String name)
    {
        for (String nonOverridableResource : ) {
            if (name.startsWith(nonOverridableResource)) {
                return true;
            }
        }
        return false;
    }
    private boolean isHiddenResource(String name)
    {
        for (String hiddenResource : ) {
            if (name.startsWith(hiddenResource)) {
                return true;
            }
        }
        return false;
    }
    private static String classNameToResource(String className)
    {
        return className.replace('.''/');
    }
New to GrepCode? Check out our FAQ X