1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
   |  
package sop.business;
 
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.jar.*;
 
 
public class CryptageClassLoader extends ClassLoader
{
 
    private String root;
 
    /**
     * CONSTRUCTEUR PERMETTANT DE PASSER LE REPERTOIRE OU SE TROUVE LA CLASSE
     */
//    public CryptageClassLoader (String rootDir)
//    {
//        if (rootDir == null)
//          throw new IllegalArgumentException ("Null root directory");
//        root = rootDir;
//    }
 
    protected synchronized Class loadClass(String name, boolean resolve)
    throws ClassNotFoundException
    {
        if (name.equals("sop.business.Cryptage"))
        {
            return findClass(name);
        }
        else
            return super.loadClass(name,resolve);
    }
 
    public Class findClass(String name)
    throws ClassNotFoundException
    {
        try
        {
//            String filename = name.replace ('.', File.separatorChar) + ".class";
            String filename = name.replace ('.', '/') + ".class";
            byte[] b = loadClassData(filename);
            return defineClass(name, b, 0, b.length);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            throw new ClassNotFoundException("IMPOSSIBLE DE TROUVER LA CLASSE DE CRYPTAGE");
        }
    }
 
//    private byte[] loadClassData (String filename)
//    throws IOException
//    {
//        // Create a file object relative to directory provided
//        File f = new File (root, filename);
//        // Get size of class file
//        int size = (int)f.length();
//        System.out.println(size);
//
//        // Reserve space to read
//        byte buff[] = new byte[size];
//
//        // Get stream to read from
//        FileInputStream fis = new FileInputStream(f);
//        DataInputStream dis = new DataInputStream (fis);
//
//        // Read in data
//        dis.readFully (buff);
//
//        // close stream
//        dis.close();
//
//        // return data
//        return buff;
//    }
 
    private byte[] loadClassData (String filename)
    throws IOException
    {
        JarFile archive = new JarFile("MonProjet.jar");
        JarEntry entry = archive.getJarEntry(filename);
        byte[] contents = new byte[(int)entry.getSize()];
        DataInputStream stream = new DataInputStream(archive.getInputStream(entry));
        stream.readFully(contents);
        return contents;
//        JarInputStream jis = new JarInputStream(new FileInputStream("MonProjet.jar"));
//        JarEntry je = null;
//
//        while ((je = jis.getNextJarEntry()) != null )
//        {
//            if (je.getName().equalsIgnoreCase(filename))
//            {
//               byte[] tab = new byte[(int)je.getSize()];
//               jis.read(tab,0,(int)je.getSize());
//               jis.close();
//               return tab;
//            }
//        }
//        throw new IOException();
 
//        while (bContinue)
//        {
//            je = jis.getNextJarEntry();
//            if ( je == null )
//                throw new IOException("Impossible de trouver la classe de cryptage");
//            else if (je.getName().equalsIgnoreCase(filename))
//            {
//               byte[] tab = new byte[(int)je.getSize()];
//               jis.read(tab,0,(int)je.getSize());
//               jis.close();
//               return tab;
//            }
//        }
//        return null;
    }
 
    protected void finalize()
    throws Throwable
    {
        System.out.println("finalize");
        super.finalize();
    }
} | 
Partager