Package com.atolsystems.atolutilities

Source Code of com.atolsystems.atolutilities.PlugInLoader

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.atolsystems.atolutilities;

import code.from.internet.JarClassLoader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashSet;
import java.util.Set;

/**
*
* @author atolsystems
*/
public class PlugInLoader {

    final Class appClass;
    final String aResourceInTheSamePackage;

    public PlugInLoader(Class appClass, String aResourceInTheSamePackage) {
        this.appClass = appClass;
        this.aResourceInTheSamePackage = aResourceInTheSamePackage;
    }

    public void addJarFile(File jarFile) throws MalformedURLException, IOException{
        jarFile=jarFile.getCanonicalFile();
        String name=jarFile.getCanonicalPath();
        if(false==jarFile.exists())
            throw new FileNotFoundException("Could not find the following file:\n"+name);
        if(jarFiles.contains(name))
            return;//already have this one...
        jarFiles.add(name);
        if (null == pluginJarClassLoader) {
            pluginJarClassLoader = new JarClassLoader(jarFile.toURI().toURL());
        } else {
            pluginJarClassLoader.addJarURL(jarFile.toURI().toURL());
        }
    }

    public Class getPlugInClass(String className) throws MalformedURLException, ClassNotFoundException {
        Class plugInClass = null;
        try{
            if (null == className) {
                return null;
            }
            if (null == pluginJarClassLoader) {
                File appJarFile = new File(AFileUtilities.getPackagePath(appClass, aResourceInTheSamePackage/*"packageLocator"*/));
                pluginJarClassLoader = new JarClassLoader(appJarFile.toURI().toURL());
            }
            plugInClass = pluginJarClassLoader.loadClass(className);
        } catch(ClassNotFoundException ex){
            throw new ClassNotFoundException("Loaded jar files:\n"+dumpJarFiles(),ex);
        }
        return plugInClass;
    }

    protected String dumpJarFiles(){
        String out="";
        for(String jarFile:jarFiles){
            out+=jarFile+"\n";
        }
        return out;
    }
    JarClassLoader pluginJarClassLoader;
    Set<String> jarFiles = new HashSet<String>();
}
TOP

Related Classes of com.atolsystems.atolutilities.PlugInLoader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.