Java Class Loader getClassLoaderForDirectory(final File baseFolder)

Here you can find the source of getClassLoaderForDirectory(final File baseFolder)

Description

get Class Loader For Directory

License

Open Source License

Declaration

private static ClassLoader getClassLoaderForDirectory(final File baseFolder) throws MalformedURLException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Main {
    private static final ReadWriteLock LOCK = new ReentrantReadWriteLock();
    private static final Map<File, ClassLoader> CLASSLOADER_MAP = new ConcurrentHashMap<>(16);

    private static ClassLoader getClassLoaderForDirectory(final File baseFolder) throws MalformedURLException {
        final Lock readLock = LOCK.readLock();
        readLock.lock();//from   www  .  ja  va  2  s .  c om
        if (CLASSLOADER_MAP.containsKey(baseFolder)) {
            readLock.unlock();
            return CLASSLOADER_MAP.get(baseFolder);
        } else {
            final Lock writeLock = LOCK.writeLock();
            writeLock.lock();
            try {
                final URLClassLoader classLoader = URLClassLoader
                        .newInstance(new URL[] { baseFolder.toURI().toURL() });
                CLASSLOADER_MAP.put(baseFolder, classLoader);
                return classLoader;
            } finally {
                writeLock.unlock();
            }
        }
    }
}

Related

  1. getClassInputStream(String className, ClassLoader cl)
  2. getClassLoader()
  3. getClassLoader(File[] directories)
  4. getClassLoader(String path)
  5. getClassLoaderForClass(final Class clazz)
  6. getClassloaderRootDir(Class forClass)
  7. getClassLoaders(ClassLoader baseClassLoader)
  8. getClassLoaderStack(ClassLoader cl)
  9. getClassLocation(Class clazz)