Here is my ClassLoader : 

package loader;
  | 
  | import java.io.FileInputStream;
  | import java.io.IOException;
  | import java.util.Properties;
  | 
  | import javassist.CannotCompileException;
  | import javassist.ClassPool;
  | import javassist.CtClass;
  | import javassist.NotFoundException;
  | 
  | /**
  |  * @author Batoussa Mougamadou
  |  */
  | public class MyClassLoader extends ClassLoader {
  | 
  |     private final ClassPool originalPool = ClassPool.getDefault();
  |     ////////////////
  | 
  |     public String getPackage() {
  |             try {
  |                     FileInputStream in = new FileInputStream("file.properties");
  |                     Properties prop = new Properties();
  | 
  |                     // load all
  |                     prop.load(in);
  | 
  |                     in.close();
  |                     return prop.getProperty("package");
  |             } catch (Exception e) {
  |                     e.printStackTrace();
  |                     return null;
  |             }
  |     }
  | 
  |     public Class loadClass(String className, boolean resolveIt)
  |             throws ClassNotFoundException {
  |             return super.loadClass(className, resolveIt);
  |     }
  | 
  |     private Class loadClass(byte[] b, String className) {
  |             //override classDefine (as it is protected) and define the class.
  |             Class clazz = null;
  |             try {
  |                     ClassLoader loader = ClassLoader.getSystemClassLoader();
  | 
  |                     Class cls = Class.forName("java.lang.ClassLoader");
  | 
  |                     // CALL FIND LOADED CLASS
  |                     java.lang.reflect.Method method =
  |                             cls.getDeclaredMethod("findLoadedClass", new Class[] { 
String.class });
  |                     method.setAccessible(true);
  |                     Object[] args = new Object[] { className }; 
  |                     clazz = (Class) method.invoke(loader, args);
  |                     method.setAccessible(false);
  |                     
  |                     if( clazz != null )
  |                             return clazz;
  |                     else System.out.println("CLASS NOT BEEN LOADED");
  |                     
  |                     // CALL DEFINE CLASS
  |                     method = cls.getDeclaredMethod( "defineClass", new Class[] { 
String.class, byte[].class, int.class, int.class });
  | 
  |                     // protected method invocaton
  |                     method.setAccessible(true);
  |                     args = new Object[] { className, b, new Integer(0), new 
Integer(b.length)}; 
  |                     clazz = (Class) method.invoke(loader, args);
  |                     method.setAccessible(false);
  |                     
  |             } catch (Exception e) {
  |                     e.printStackTrace();
  |                     System.exit(1);
  |             }
  |             return clazz;
  |     }
  | 
  |     private ClassPool pool;
  | 
  |     public MyClassLoader() throws NotFoundException {
  |             pool = new ClassPool();
  |             pool.insertClassPath("."); // MyApp.class must be there.
  |     }
  | 
  |     /* Finds a specified class.
  |      * The bytecode for that class can be modified.
  |      */
  |     public Class findClass(String name) throws ClassNotFoundException {
  |             try {
  |                     // Set the class path
  |                     pool.appendClassPath("C:/Program 
Files/eclipse/workspace/WebClassLoader/WEB-INF/lib/bat.jar");
  |                     CtClass cc = pool.get(getPackage()+"."+name);
  |                     // Change the name of the class
  |                     cc.setName("b." + name);
  |                     return loadClass(cc.toBytecode(), "b." + name);
  |             } catch (NotFoundException e) {
  |                     throw new ClassNotFoundException();
  |             } catch (IOException e) {
  |                     throw new ClassNotFoundException();
  |             } catch (CannotCompileException e) {
  |                     throw new ClassNotFoundException();
  |             }
  |     }
  | }
  | 

View the original post : 
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3843504#3843504

Reply to the post : 
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=3843504


-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop
FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=4721&alloc_id=10040&op=click
_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to