1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
<?xml version="1.0"?>
<document>
<properties>
<title>API Usage</title>
<author email="bob@werken.com">bob mcwhirter</author>
</properties>
<body>
<section name="API Usage">
<p>
The Java API can be used to create new realms and connect
realms together through importation of specific packages.
</p>
<p>
The core of the <code>classworlds</code> infrastructure is
the
<a href="apidocs/com/classworlds/classworlds/ClassWorld.html">ClassWorld</a>
class. An application must create a <code>ClassWorld</code> instance.
It is advisable to store the instance as a singleton or some other
handy location.
</p>
<source><![CDATA[
ClassWorld world = new ClassWorld();
]]></source>
<p>
Once a <code>ClassWorld</code> is created, realms within it
can be created. These realms effectively only allow loading
of the core JVM classes.
</p>
<source><![CDATA[
ClassWorld world = new ClassWorld();
ClassRealm containerRealm = world.newRealm( "container" );
ClassRealm logComponentRealm = world.newRealm( "logComponent" );
]]></source>
<p>
In order to make each <code>ClassRealm</code> useful, constituent
must be added to that each can provide certain classes.
</p>
<source><![CDATA[
containerRealm.addConstituent( containerJarUrl );
logComponentRealm.addConstituent( logComponentJarUrl );
]]></source>
<p>
Now, links between the various realms need to be created to allow
classes loaded from one to be available to classes loaded in another.
</p>
<source><![CDATA[
logComponentRealm.importFrom( "container",
"com.werken.projectz.component" );
]]></source>
<p>
The container implementation can then be loaded from it's realm
and used.
</p>
<source><![CDATA[
Class containerClass = containerRealm.loadClass( CONTAINER_CLASSNAME );
MyContainer container = (MyContainer) containerClass.newInstance();
Thread.currentThread().setContextClassLoader( containerRealm.getClassLoader() );
container.run();
]]></source>
<p>
Ideally, the container itself would be responsible for creating
a <code>ClassRealm</code> for each component that's loaded, and
importing the component contract interfaces into the component's
<code>ClassRealm</code> and using <code>loadClass(..)</code>
to gain entry into the sandboxed component realm.
</p>
</section>
</body>
</document>
|