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
 

"A Multiple Application Launcher"
Vol. 7, Issue 4, p. 54

	


Listing 1

package com.jpt.mal.application;

import java.lang.reflect.*;
import java.net.*;

public class Application implements Runnable {

  protected URLClassLoader classLoader;
  protected URL[] classPath;
  protected String className;
  protected Object[] args;
  protected Exception applicationException;
  protected boolean running;


  public Application(String aClass, URL[] aClassPath) {
    className = aClass;
    classPath = classPath;
    running = false;
    classLoader = new URLClassLoader(
        classPath,
        Thread.currentThread() .getContextClassLoader());
    args = new Object[1];
  }

  public Exception getApplicationException() {
    return applicationException;
  }

  public void launch(String[] args) {
    if (isRunning()) return;
    this.args[0] = args;
    applicationException = null;
    Thread thread = new Thread( this,
                           "Running " +
                           className);
    thread.setContextClassLoader(this.classLoader);
    thread.start();
    Thread.yield();
  }

  // Runnable targets
	public void run() {

    setRunning(true);
		try {
      this.resolveMain().invoke(null, args);
    } catch (Exception ex) {
      applicationException = ex;
    } finally {
      setRunning(false);
    }
  }

  private Method resolveMain()
    throws ClassNotFoundException,
    NoSuchMethodException {
    Class targetClass = 										this.classLoader.loadClass(className);
    Class[] arg_types = { String[].class };
    Method main = targetClass.getMethod("main", arg_types);
    int mask = Modifier.STATIC ^ Modifier.PUBLIC;
    if ((main.getModifiers() & mask) != mask)
      throw new NoSuchMethodException(
      "Cannot find method public static void main(String[]) in class "+ className);
      return main;
    }

  private synchronized void setRunning(boolean value) {
    this.running = value;
    notifyAll();
  }

  public synchronized boolean isRunning() { return running; }

 public synchronized void waitFor() {
    while (isRunning())
      try {
        wait();
      } catch (InterruptedException ie) {}
  }
}



Listing 2

package com.jpt.testcomp;

public class TestComponent {

  public TestComponent() {}

  public void runJava2Style(String implementation, String arg)
    throws ClassNotFoundException,
    InstantiationException,
    IllegalAccessException {

    Class clazz = Class.forName( implementation, true,
                Thread.currentThread().getContextClassLoader());
    TestComponentInterface tci = (TestComponentInterface)
                             clazz.newInstance();
    tci.run(arg);
  }

  public void runJDK11Style(String implementation, String arg)
    throws ClassNotFoundException,
          InstantiationException,
          IllegalAccessException {
    Class clazz = Class.forName( implementation);
    TestComponentInterface tci = (TestComponentInterface)
                             clazz.newInstance();
    tci.run(arg);
  }
}



Listing 3

public class MultiApplicationSecurityManager extends SecurityManager 
  throws SecurityException {

public void checkExit(int status) {

  if ( Thread.currentThread().getContextClassLoader() !=
     ClassLoader.getSystemClassLoader())
    throw new SecurityException();

  super.checkExit(status);

}



Listing 4

java.security.AccessControlException: access denied (java.lang.Runtime
			Permission createClassLoader)
	at java.security.AccessControlContext.checkPermission(AccessControlContext.
			java:272)
	at java.security.AccessController.checkPermission(AccessController.
			java:399)
	at java.lang.SecurityManager.checkPermission(SecurityManager.java:545)
	at java.lang.SecurityManager.checkCreateClassLoader(SecurityManager.
			java:610)
	at java.lang.ClassLoader.<init>(ClassLoader.java:203)
	at java.security.SecureClassLoader.<init>(SecureClassLoader.java:56)
	at java.net.URLClassLoader.<init>(URLClassLoader.java:82)
	at com.jpt.application.Application.<init>(./src/com/jpt/application
			/Application.java:20)
	at com.jpt.ui.JShell.launch(./src/com/jpt/ui/JShell.java:60)
	at com.jpt.ui.JShell.main(./src/com/jpt/ui/JShell.java:72)
Exception in thread "main" Process terminated with exit code 1



Listing 5

/* AUTOMATICALLY GENERATED ON Sun Feb 03 15:43:44 EST 2002*/
/* DO NOT EDIT */

grant {
 permission java.security.AllPermission;
};


Listing 6

Running JDK 1.1 Style on com.jpt.testapp.TestApplication1
Expected Exception:com.jpt.testapp.TestApplication1java.lang.ClassNotFoundException: com.jpt.testapp.TestApplication1
	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:297)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:253)Running JDK 1.1 Style on com.jpt.testapp.TestApplication2
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:313)
	at java.lang.Class.forName0(Native Method)
	at java.lang.
Expected Exception:com.jpt.testapp.TestApplication2
Class.forName(Class.java:120)
	at com.jpt.testcomp.TestComponent.runJDK11Style(Unknown Source)
	at com.jpt.testapp.TestApplication1.start(Unknown Source)Running Java2 Style oncom.jpt.testapp.TestApplication1
	at com.jpt.testapp.TestApplication1.main(Unknown Source)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.jpt.mal
com.jpt.testapp.TestApplication1:0 sleeping 
.component.Application.run(Unknown Source)
	at java.lang.Thread.run(Thread.java:484)
java.lang.ClassNotFoundException: com.jpt.testapp.TestApplication2Running JDK 1.1 Style on com.jpt.testapp.TestApplication3
	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
	at java.security.AccessController.doPrivileged(Native Method)
	a
Expected Exception:com.jpt.testapp.TestApplication3
t java.net.URLClassLoader.findClass(URLClassLoader.java:188)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:297)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:313)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:120)
	at com.jpt.testcomp.TestComponent.runJDK11Style(Unknown Source)
	at com.jpt.testapp.TestApplicaRunning Java2 Style oncom.jpt.testapp.TestApplication2
com.jpt.testapp.TestApplication2:0 sleeping 
tion2.start(Unknown Source)
	at com.jpt.testapp.TestApplication2.main(Unknown Source)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.jpt.mal.component.Application.run(Unknown Source)
	at java.lang.Thread.run(Thread.java:484)
java.lang.ClassNotFoundException: com.jpt.testapp.TestApplication3
	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:297)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:313)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:120)
	at com.jpt.testcomp.TestComponent.runJDK11Style(Unknown Source)
	at com.jpt.testapp.TestApplicaRunning Java2 Style oncom.jpt.testapp.TestApplication3
com.jpt.testapp.TestApplication3:0 sleeping 
tion3.start(Unknown Source)
	at com.jpt.testapp.TestApplication3.main(Unknown Source)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.jpt.mal.component.Application.run(Unknown Source)
	at java.lang.Thread.run(Thread.java:484)
com.jpt.testapp.TestApplication1:1 sleeping 
com.jpt.testapp.TestApplication2:1 sleeping 
com.jpt.testapp.TestApplication3:1 sleeping 
com.jpt.testapp.TestApplication1:2 sleeping 
com.jpt.testapp.TestApplication2:2 sleeping 
com.jpt.testapp.TestApplication3:2 sleeping 
com.jpt.testapp.TestApplication1 done!
com.jpt.testapp.TestApplication2 done!
com.jpt.testapp.TestApplication3 done!

 
 

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.