Re: Need help about code mobility
Gregg Wonderly <[email protected]>
| Newsgroups | gmane.comp.java.sun.javaspaces |
|---|---|
| Message-ID | <[email protected]> |
Andrea Angeloni wrote:
>> I'm not familiar with the example but I would imagine the producer
>> process has a direct dependency on PrimeJob in that it uses it's
>> constructor and fills in the fields.
>
>
> yes, this is the case under the producer:
>
> PrimeJob job = new PrimeJob(nextNumberToCalculate);
As Dan said, this code, has to executing within the confines of a ClassLoader
hierarchy that can resolve this class. This is a Java thing! Anytime that a
class is resolved, the classes that it refers to need to be resolvable as well.
Thus, the class that the above code is within, must be resolved in a context
where these classes are available.
Thus, if you want your Producer to use downloaded code, you need to create an
instance of your Producer using a ClassLoader that can provide all of the needed
classes either directly, or indirectly, through its parent classloaders.
As a short example, one could do something like the following to create a
service provided by a Java application which allows use of a Javaspace, without
explicit access to the Javaspace.
// Define an interface that has to be everywhere
// to specify the service contract.
public interface WorkDescription {
public URL[] getClassLoaderURLs();
public String getProducerClass();
}
// Define a semi generic interface for things that
// produce Entry objects.
public interface EntryProducer {
public Callable<Entry> getProducer();
}
// Define the main service entrypoint. You can
// provide some mechanism for an external entity
// to call this method, perhaps via a servlet with
// a URL such as:
//
// http://omino:8085/submitwork.cgi?loaders=...&class=...
//
// The servlet then might put together a WorkDescription
// instance, and call this method.
//
public void processItem( WorkDescription work ) {
// Create a classloader that can load from the network.
// We need create class loader permission to do this.
URLClassLoader cl = new URLClassLoader(
work.getClassLoaderURLs(),
Thread.getContextClassLoader() == null ?
getClass().getClassLoader() :
Thread.getContextClassLoader() );
// Get the Producer using a well know class name.
// This class name could be a parameter that you
// receive from a remote machine that is submitting
// work items.
EntryProducer prc = (EntryProducer )Class.forName(
work.getProducerClass(), true, cl );
// Get the current context class loader
ClassLoader ctxcl = Thread.currentThread().getContextClassLoader();
try {
// Set the current context class loader for this thread
// so that any and all attempts at class loading will
// include our class loader.
Thread.currentThread().setContextClassLoader( cl );
processItem( EntryProducer prc );
} finally {
Thread.currentThread().setContextClassLoader( ctxcl );
}
}
// This is the main work point for any EntryProducer that
// your system might provide services for. Note that an
// inbound JERI/RMI call would have a context class loader
// set automatically. So, this method might be the JERI/RMI
// interface to submitting work into your javaspace.
//
public void processItem( EntryProducer prc ) {
// Get the Entry producer.
Callable<Entry> cl = prc.getProducer();
// Loop until no more entries are produced.
boolean done = false;
while( !done ) {
// We'll call directly here, but we
// could use an Executor implementation to
// manage a Thread pool for all this work.
Entry e = cl.call();
if( e != null ) {
... now put the entry into the space ...
}
done = e == null;
}
}
Hopefully this will give you some classes to go look at the javadocs for, and
you should be able to see some of the ways to utilize downloaded code using a
different mechanism than that provided by JERI/RMI.
Gregg Wonderly
===========================================================================
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff JAVASPACES-USERS". For general help, send email to
[email protected] and include in the body of the message "help".
To view past JAVASPACES-USERS postings, please see:
http://archives.java.sun.com/archives/javaspaces-users.html