Dynamic Linkage
Dynamic Linkage allows a program to load and use arbitrary classes
that implement a known interface.
This is the "known" interface.
In this case an abstract class.
This allows the environment to interact with the
loadable class without knowing its concrete type.
-----------------------------------------------------
public abstract class AbstractLoadableClass{
private IEnvironment iEnvironment;
public void setEnvironment(IEnvironment environment){
this.iEnvironment = environment;
}
public IEnvironment getEnvironment(){
return this.iEnvironment;
}
public abstract void start();
}
-----------------------------------------------------
This is the environment interface. It allows the
concrete loadable class to interact with its
environment without knowing its type.
-----------------------------------------------------
public interface IEnvironment{
public void function_1();
public void function_2();
public void function_3();
public void function_4();
public void function_5();
}
-----------------------------------------------------
This is the concrete class that the environment will
run.
-----------------------------------------------------
class ConcreteLoadableClass extends AbstractLoadableClass{
public void start(){
System.out.println("class: ConcreteLoadableClass, method: start");
if(this.getEnvironment() == null)
return;
this.getEnvironment().function_1();
this.getEnvironment().function_3();
this.getEnvironment().function_5();
}
}
-----------------------------------------------------
This is the environment class.
Note the main method.
This takes the name of the class to run as an argument,
dynamicaly links to it and runs it.
Note also the classpath points to the floppy drive.
If you attempt to run this example you must compile
the concreteLoadableClass and place the binary on a
floppy and in the drive. (unix/linux system)
-----------------------------------------------------
class Environment implements IEnvironment{
private static final URL[] classPath;
private AbstractLoadableClass program;
static{
try{
classPath = new URL[]{new URL("file:/mnt/floppy/")};
}catch (java.net.MalformedURLException e){
throw new ExceptionInInitializerError(e);
}
}
public static void main(String[] args){
System.out.println("class: Environment, method: main");
Environment environment = new Environment();
if(args.length <1){
System.out.println("like .. I need some class dude");
return;
}
environment.run(args[0]);
}
public void run(String programName){
System.out.println("class: Environment, method: run");
URLClassLoader classLoader = new URLClassLoader(classPath);
Class programClass;
try{
programClass = classLoader.loadClass(programName);
}catch(ClassNotFoundException e){System.out.println("class not found");return;}
try{
program = (AbstractLoadableClass)programClass.newInstance();
}catch(Exception e){return;}
program.setEnvironment(this);
program.start();
}
public void function_1(){
System.out.println("class: Environment, method: function_1");
}
public void function_2(){
System.out.println("class: Environment, method: function_2");
}
public void function_3(){
System.out.println("class: Environment, method: function_3");
}
public void function_4(){
System.out.println("class: Environment, method: function_4");
}
public void function_5(){
System.out.println("class: Environment, method: function_5");
}
}
-----------------------------------------------------
Here is a tar with the above java classes in it.
dynmicLinkage.tar