Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: Java | Size: 2.33 KB | Hits: 22 | 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.         private final String DIR_SEP = System.getProperty("file.separator");
  28.        
  29.     public CustomClassLoader(ClassLoader parent) {
  30.         super(parent);
  31.     }
  32.  
  33.     public Class<?> loadClass(String name) throws ClassNotFoundException {
  34.         System.out.println(name);
  35.         if(!name.contains("com.pie.jotta.util.command"))
  36.                 return super.loadClass(name);
  37.  
  38.         try {
  39.                 String[] nameSplit = name.split("[\\.]");
  40.                 name = name.split("[\\.]")[nameSplit.length-1];
  41.             String url = "file:"+System.getProperty("user.dir")+DIR_SEP+"bin"+
  42.                 DIR_SEP+"com"+DIR_SEP+"pie"+DIR_SEP+"jotta"+DIR_SEP+"util"+DIR_SEP+
  43.                 "command"+DIR_SEP+(name.endsWith(".class") ? name : name+".class");
  44.             URL myUrl = new URL(url);
  45.             URLConnection connection = myUrl.openConnection();
  46.             InputStream input = connection.getInputStream();
  47.             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  48.             int data = input.read();
  49.  
  50.             while(data != -1){
  51.                 buffer.write(data);
  52.                 data = input.read();
  53.             }
  54.  
  55.             input.close();
  56.  
  57.             byte[] classData = buffer.toByteArray();
  58.  
  59.             return defineClass("com.pie.jotta.util.command.Command",
  60.                     classData, 0, classData.length);
  61.  
  62.         } catch (Exception e) {
  63.             System.err.println(e.getMessage());
  64.         }
  65.  
  66.         return null;
  67.     }
  68.  
  69. }