HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"URL ClassLoader"
Vol. 3, Issue 1, p. 44

	

Listing 1.
 
 // Creates a class from the byte codes 
  c = defineClass(className, array, 0, array.length); 
  if (resolve) { 
     // resolves all of the classes needed by the class 
     // links the class into the JVM process 
     resolveClass(c); 

     // caches the class into a Hashtable 
     classList.put(className, c); 
    } 

Listing 2.
 
try { 
     c = findSystemClass(name); 
     System.out.println("Resolved " + name + " locally"); 
    } 

 // If Class is Not Local, Attempt to Resolved it From Downloaded Classes 
catch (ClassNotFoundException e) { 
       System.out.println("Resolving Class: " + name); 
       classRequested = (Class) classList.get(name); 

       if (classRequested == null) { 
      System.out.println("Resolving " + name + " remotely"); 

      // If a Class has not been resolved previously then 
      // attempt to load it from the last Web site that was 
      // accessed. 
                   loadClassName( name ); 
      c = ourClass; 
   } 
             } 
             return c; 

Listing 3.
 
public void loadClassName(String name) { 
                 String URLlocation; 
      try { 
      URLlocation = new String("http://" + serv + dir + "/" + name + ".class"); 
      registerJavaClass(URLlocation); 
                } 
               catch (Exception e) { 
     e.printStackTrace(); 
                } 
             } 

Listing 4.
 
Object obj = c.newInstance(); 
 

 // set the parameters for the static main method 
 Class array1[] = {Class.forName("[Ljava.lang.String;")}; 

 // gets the static main method 
 m1 = c.getMethod("main", array1); 

 // invoke the static main method 
 m1.invoke(obj, object_array); 
  

Listing 5: urlclassloader.java
 
import java.net.*; 
import java.io.*; 
import java.util.*; 

class URLClassLoader extends ClassLoader { 

Class  ourClass=null; 
Hashtable classList=null; 
int  vector_counter; 
String  directory=null; 
String  server=null; 
String  className=null; 
String  UnresolvedURL=null; 
String  temporaryURL=null; 
boolean  flagClass; 
static int counter1; 

 public URLClassLoader() { 

   super(); 

   classList = new Hashtable(); 
 } 

 public void registerJavaClass(String URLlocation) throws IOException { 

    String  filename=null; 
    int  slash_index=0; 
    InputStream  input=null; 
    URL   u=null; 
    DataInputStream data=null; 

    try { 
  u = new URL(URLlocation); 
  temporaryURL = new String(URLlocation); 

  filename = new String(u.getFile()); 
  slash_index = filename.lastIndexOf("/"); 
  className =  
    new String(filename.substring(slash_index+1, 
                       filename.length() - 6)); 

  directory = 
   new String(filename.substring(0, 
                                 slash_index)); 

  server = new String(u.getHost()); 

  input = u.openStream(); 

  data = new DataInputStream(input); 

  byte classBytes[] = 
    downloadByteCodesFromURL(data); 

  data.close(); 
  input.close(); 
   
  input = null; 
  u = null; 
  System.gc(); 
   
  ourClass = 
    createClassFromByteCodes(classBytes, true); 

    } catch (Exception e) { 

            if (input != null) { 
                  input.close(); 
                  if (data != null) { 
                      data.close(); 
                   } 
            } 
         
            u = null; 
         
     UnresolvedURL = new String(URLlocation); 

     ourClass = null; 

     throw new IOException("Problems Defining " + 
              "Network Class +++++++++++++++>"); 
    } 
 } 

 public byte[] downloadByteCodesFromURL( 
                          DataInputStream in) { 

    ByteArrayOutputStream outStream = 
                    new ByteArrayOutputStream(); 
  
    while (true) { 
  try { 
   outStream.write(in.readByte()); 
  }  
  catch (IOException e) {  
   break;  
  } 
    } 
    return outStream.toByteArray(); 
 } 

 public synchronized Class 
 createClassFromByteCodes(byte[] array, 
                          boolean resolve) { 

    Class c = null; 

           try {  
  c = defineClass(className, array, 
                  0, array.length); 
     if (resolve) { 
           resolveClass(c); 
            classList.put(className, c); 
                } 
    } 
    catch (ClassFormatError e) { 
  e.printStackTrace(); 
    } 

    return (c); 
 } 

 public synchronized Class loadClass(String name, 
                               boolean resolve) 
  throws ClassNotFoundException { 
    Class c = null; 
    Class classRequested; 

    // Attempt to Load the Class Locally First 
    try { 
     c = findSystemClass(name); 
     System.out.println("Resolved " + name +  
                        " locally"); 
    } 
    // If Class is Not Local, Attempt to  
    // Resolve it From Downloaded Classes 
           catch (ClassNotFoundException e) { 
     System.out.println("Resolving Class: " + 
                        name); 

     classRequested =  
             (Class) classList.get(name); 

     if (classRequested == null) { 
     System.out.println("Resolving " + name +  
                        " remotely"); 

     // If a Class has not been resolved 
     // previously then attempt to load it 
     // from the last web site that was accessed. 
     loadClassName( name ); 
     c = ourClass; 
  } 
    } 

    return c; 
 } 

 public void loadClassName(String name) { 
    String   URLlocation; 

    try { 
  URLlocation = new String("http://" + server 
           + directory + "/" + name + ".class"); 
  registerJavaClass(URLlocation); 
    } catch (Exception e) { 
  e.printStackTrace(); 
    } 
 } 

 public Class getTheClass() { 
    return (ourClass); 
 } 
} 

Listing 6: trigger.java
 
import java.awt.*; 
import java.net.*; 
import java.io.*; 
import java.lang.reflect.*; 
import java.awt.event.*; 
import java.util.*; 
import URLClassLoader; 

public class trigger extends Frame implements ActionListener{ 

 URLClassLoader  loader=null; 
 TextField  tf = null; 
 Button   bt = null; 

 public trigger() { 
  super("Trigger Application"); 

  setLayout(new BorderLayout()); 
  tf = new TextField(); 
  bt = new Button("Execute Java Application"); 
  bt.addActionListener(this); 

  add("North", 
    new Label("Enter URL for loading Java application")); 
  add("Center", tf); 
  add("South", bt); 

  loader = new URLClassLoader(); 

  setSize(300, 100); 
 } 

 public void actionPerformed(ActionEvent evt) { 
  String URLLocation = tf.getText(); 

  if (URLLocation != null) { 
     executeApplication(URLLocation); 
  } 
 } 

   public void executeApplication( 
                         String URLlocation) { 

  try { 
     loader.registerJavaClass(URLlocation); 

         Class c = loader.getTheClass(); 

     startJavaApplication(c, "New Application"); 
  } 
  catch (IOException e) { 
    System.out.println("Error loading URL Class"); 
  } 
   } 

   public void startJavaApplication( 
                       Class c, String label) { 

                try { 

    Object obj = c.newInstance(); 

           String[] array = new String[2]; 
           array[0] = new String(label); 
           Object object_array[] = {array}; 

           Method m1; 

          // set the parameters for main method 
           Class array1[] = 
           {Class.forName("[Ljava.lang.String;")}; 

    // get the static main method 
    m1 = c.getMethod("main", array1); 

    // invoke the static main method 
    m1.invoke(obj, object_array); 

                } catch (NoSuchMethodException e) { 
          e.printStackTrace(); 
      } catch (ClassNotFoundException e) { 
         e.printStackTrace(); 
      } catch (InvocationTargetException e) { 
         e.printStackTrace(); 
      } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
         } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
         } catch (InstantiationException e) { 
     e.printStackTrace(); 
  } catch (NullPointerException e) { 
     e.printStackTrace(); 
  } 
   } 

 public static void main(String args[]) { 
  trigger application = new trigger(); 
  application.show(); 
 } 
}


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.