Changeset 2278


Ignore:
Timestamp:
08/02/19 10:58:48 (5 years ago)
Author:
pharms
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/autoquest-plugin-core/src/main/java/de/ugoe/cs/autoquest/plugin/PluginLoader.java

    r2260 r2278  
    1616 
    1717import java.io.File; 
     18import java.io.FileInputStream; 
     19import java.io.FileNotFoundException; 
    1820import java.io.FilenameFilter; 
     21import java.io.IOException; 
    1922import java.lang.reflect.InvocationTargetException; 
    2023import java.net.MalformedURLException; 
     
    2528import java.util.LinkedList; 
    2629import java.util.List; 
     30import java.util.jar.JarInputStream; 
     31import java.util.jar.Manifest; 
    2732 
    2833/** 
     
    96101        if (jarFiles != null) { 
    97102            for (File jarFile : jarFiles) { 
    98                 updateClassLoader(jarFile); 
     103                ClassLoader loader = getClassLoader(jarFile); 
    99104 
    100105                String pluginName = jarFile.getName().split("-")[2]; 
     
    104109                Class<?> pluginClass = null; 
    105110                try { 
    106                     pluginClass = Class.forName(pluginClassName); 
     111                    pluginClass = loader.loadClass(pluginClassName); 
    107112                } 
    108113                catch (ClassNotFoundException e) { 
     
    150155     * </p> 
    151156     */ 
    152     /* 
    153      * protected String[] getClassPathFromJar(File jarFile) { String[] classPath; 
    154      *  
    155      * JarInputStream jarInputStream = null; Manifest manifest = null; try { FileInputStream 
    156      * fileStream = new FileInputStream(jarFile); try { jarInputStream = new 
    157      * JarInputStream(fileStream); manifest = jarInputStream.getManifest(); } finally { 
    158      * jarInputStream.close(); fileStream.close(); } } catch (FileNotFoundException e) { throw new 
    159      * AssertionError( "FileNotFoundException should be impossible!"); } catch (IOException e) { 
    160      * throw new PluginLoaderException(e); } 
    161      *  
    162      * String jarClassPath = manifest.getMainAttributes().getValue( "Class-Path"); 
    163      *  
    164      * if (jarClassPath != null) { String[] jarClassPathElements = jarClassPath.split(" "); 
    165      * classPath = new String[jarClassPathElements.length]; for (int i = 0; i < 
    166      * jarClassPathElements.length; i++) { classPath[i] = "file:" + 
    167      * jarFile.getParentFile().getAbsolutePath() + "/" + jarClassPathElements[i]; } try { 
    168      * jarInputStream.close(); } catch (IOException e) { throw new PluginLoaderException(e); } } 
    169      * else { classPath = new String[] {}; } return classPath; } 
    170      */ 
     157     
     158     protected String[] getClassPathFromJar(File jarFile) { 
     159        String[] classPath; 
     160        JarInputStream jarInputStream = null; 
     161        Manifest manifest = null; 
     162        try { 
     163            FileInputStream fileStream = new FileInputStream(jarFile); 
     164            try { 
     165                jarInputStream = new JarInputStream(fileStream); 
     166                manifest = jarInputStream.getManifest(); 
     167            } 
     168            finally { 
     169                jarInputStream.close(); 
     170                fileStream.close(); 
     171            } 
     172        } 
     173        catch (FileNotFoundException e) { 
     174            throw new AssertionError("FileNotFoundException should be impossible!"); 
     175        } 
     176        catch (IOException e) { 
     177            throw new PluginLoaderException(e); 
     178        } 
     179 
     180        String jarClassPath = manifest.getMainAttributes().getValue("Class-Path"); 
     181 
     182        if (jarClassPath != null) { 
     183            String[] jarClassPathElements = jarClassPath.split(" "); 
     184            classPath = new String[jarClassPathElements.length]; 
     185            for (int i = 0; i < jarClassPathElements.length; i++) { 
     186                classPath[i] = "file:" + jarFile.getParentFile().getAbsolutePath() + "/" + 
     187                    jarClassPathElements[i]; 
     188            } 
     189            try { 
     190                jarInputStream.close(); 
     191            } 
     192            catch (IOException e) { 
     193                throw new PluginLoaderException(e); 
     194            } 
     195        } 
     196        else { 
     197            classPath = new String[] { }; 
     198        } 
     199        return classPath; 
     200    }      
    171201 
    172202    /** 
     
    182212     *             thrown if there is a problem updating the class loader or loading the plug-in jar 
    183213     */ 
    184     private void updateClassLoader(File jarFile) throws PluginLoaderException { 
    185         // String[] classPath = getClassPathFromJar(jarFile); 
     214    private ClassLoader getClassLoader(File jarFile) throws PluginLoaderException { 
     215        String[] otherJars = getClassPathFromJar(jarFile); 
    186216 
    187217        try { 
     218            URL[] urls = new URL[otherJars.length + 1]; 
     219            urls[0] = new URL("jar:file:" + jarFile.getAbsoluteFile() + "!/"); 
     220             
     221            int index = 1; 
     222            for (String jar : otherJars) { 
     223                urls[index++] = new URL("jar:file:" + new File(jar).getAbsoluteFile() + "!/"); 
     224            } 
     225             
    188226            URLClassLoader loader = new URLClassLoader(new URL[] 
    189                 { new URL("file:" + jarFile.getAbsoluteFile()) }, 
     227                { new URL("jar:file:" + jarFile.getAbsoluteFile() + "!/") }, 
    190228                                                    ClassLoader.getSystemClassLoader()); 
    191229             
    192230            instantiatedClassLoaders.add(loader); 
     231             
     232            return loader; 
    193233        } 
    194234        catch (MalformedURLException e) { 
Note: See TracChangeset for help on using the changeset viewer.