Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: Java | Size: 2.18 KB | Hits: 36 | Expires: Never
Copy text to clipboard
  1. package com.pie.jotta.util;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.InputStream;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7.  
  8. /*
  9.  *  This file is part of Jotta.
  10.  *
  11.  *  Jotta is free software: you can redistribute it and/or modify
  12.  *  it under the terms of the GNU General Public License as published by
  13.  *  the Free Software Foundation, either version 3 of the License, or
  14.  *  (at your option) any later version.
  15.  *
  16.  *  Jotta is distributed in the hope that it will be useful,
  17.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  *  GNU General Public License for more details.
  20.  *
  21.  *  You should have received a copy of the GNU General Public License
  22.  *  along with Jotta.  If not, see <http://www.gnu.org/licenses/>.
  23.  */
  24.  
  25. public class CustomClassLoader extends ClassLoader{
  26.        
  27.     public CustomClassLoader(ClassLoader parent) {
  28.         super(parent);
  29.     }
  30.  
  31.     public Class<?> loadClass(String name) throws ClassNotFoundException {
  32.         System.out.println(name);
  33.         if(!name.contains("com.pie.jotta.util.command"))
  34.                 return super.loadClass(name);
  35.  
  36.         try {
  37.                 String[] nameSplit = name.split("[\\.]");
  38.                 name = name.split("[\\.]")[nameSplit.length-1];
  39.                         String url = "file:" + System.getProperty("user.dir")
  40.                                         + "/bin/com/pie/jotta/util/command/"
  41.                                         + (name.endsWith(".class") ? name : name + ".class");
  42.             URL myUrl = new URL(url);
  43.             URLConnection connection = myUrl.openConnection();
  44.             InputStream input = connection.getInputStream();
  45.             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  46.             int data = input.read();
  47.  
  48.             while(data != -1){
  49.                 buffer.write(data);
  50.                 data = input.read();
  51.             }
  52.  
  53.             input.close();
  54.  
  55.             byte[] classData = buffer.toByteArray();
  56.  
  57.             return defineClass("com.pie.jotta.util.command."+name,
  58.                     classData, 0, classData.length);
  59.  
  60.         } catch (Exception e) {
  61.             System.err.println(e.getMessage());
  62.         }
  63.  
  64.         return null;
  65.     }
  66.  
  67. }