Java ClassPath Add addAllFilesToExtClassLoaderClasspathWithReg(String sDir, String regexpFilter)

Here you can find the source of addAllFilesToExtClassLoaderClasspathWithReg(String sDir, String regexpFilter)

Description

Adds all files in a directory to the Ext classloader's classpath

License

Open Source License

Parameter

Parameter Description
sDir The directory path
regexpFilter a regular expression that is used to filter some files in a directory.

Declaration

public static void addAllFilesToExtClassLoaderClasspathWithReg(String sDir, String regexpFilter)
        throws IOException 

Method Source Code


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

import java.io.File;
import java.io.IOException;

import java.lang.reflect.Method;

import java.net.URL;
import java.net.URLClassLoader;

import java.util.Vector;

public class Main {
    private static final String S_ERROR_ADDING_TO_CLASSPATH = "Error adding the given URL to the classpath";
    private static final String S_ADD_URL_METHOD_NAME = "addURL";

    /**//w ww.  ja v a2  s  . co  m
     * Adds all files in a directory to the Ext classloader's classpath
     * @param sDir
     *            The directory path
     * @param regexpFilter
     *           a regular expression that is used to filter some files in a directory. 
     */
    public static void addAllFilesToExtClassLoaderClasspathWithReg(String sDir, String regexpFilter)
            throws IOException {
        String[] asFilteredFiles = getAllFilesMatchingThisPatternIgnoreCase(sDir, regexpFilter);

        for (int i = 0; i < asFilteredFiles.length; i++) {
            String filteredFilePath = new File(sDir, asFilteredFiles[i]).getAbsolutePath();
            addFileToExtClassLoaderClasspath(filteredFilePath);
        }
    }

    /**
     * Returns the set of all files in the given directory, matching the given
     * pattern (ignoring case)
     * <p>
     * 
     * @param sDirectoryPath
     *            The directory path
     * @param sPattern
     *            The pattern to match against
     *            <p>
     * @return The required list of files
     */
    private static String[] getAllFilesMatchingThisPatternIgnoreCase(String sDirectoryPath, String sPattern) {

        String[] asAllFiles = getAllFiles(sDirectoryPath);

        Vector vsFiltered = new Vector();
        for (int i = 0; i < asAllFiles.length; i++) {
            if (asAllFiles[i].toLowerCase().matches(sPattern.toLowerCase())) {
                vsFiltered.add(asAllFiles[i]);
            }
        }

        String[] asFilteredFiles = new String[vsFiltered.size()];
        for (int i = 0; i < vsFiltered.size(); i++) {
            asFilteredFiles[i] = vsFiltered.elementAt(i).toString();
        }

        return asFilteredFiles;
    }

    /**
     * Adds the given file to the Ext classloader's classpath
     * @param sFilePath
     *            The path to the file to add to the classpath
     */
    public static void addFileToExtClassLoaderClasspath(String sFilePath) throws IOException {
        addURLToExtClassLoaderClasspath(new File(sFilePath).toURL());
    }

    /**
     * Returns the set of all files in the given directory
     * <p>
     * 
     * @param sDirectoryPath
     *            The directory path
     *            <p>
     * @return The required list of files
     */
    private static String[] getAllFiles(String sDirectoryPath) {

        if (sDirectoryPath == null) {
            return new String[0];
        }

        File fileThisDirectory = new File(sDirectoryPath);

        if (fileThisDirectory.isDirectory() == false) {
            return new String[0];
        } else {
            String[] asFileList = fileThisDirectory.list();
            if (asFileList == null || asFileList.length == 0) {
                return asFileList;
            }
            return asFileList;
        }
    }

    /**
     * Adds the given URL to the ext classloader's classpath
     * @param urlToAddToClassPath
     *            The URL to add to the classpath
     */
    public static void addURLToExtClassLoaderClasspath(URL urlToAddToClassPath) throws IOException {

        try {
            URLClassLoader systemClassLoader = (URLClassLoader) getExtClassLoader();

            Method method = URLClassLoader.class.getDeclaredMethod(S_ADD_URL_METHOD_NAME,
                    new Class[] { URL.class });
            method.setAccessible(true);

            method.invoke(systemClassLoader, new Object[] { urlToAddToClassPath });
        } catch (Throwable t) {
            throw new IOException(S_ERROR_ADDING_TO_CLASSPATH);
        }
    }

    public static ClassLoader getExtClassLoader() {
        return URLClassLoader.class.getClassLoader().getSystemClassLoader().getParent();
    }
}

Related

  1. addBundleToClassPath(String bundleId)
  2. addClassPath(File filePath)
  3. addClasspath(String path)
  4. addClassPath2ClassLoader(ClassLoader cl, String path)