First, here's a simple non-Jtrix application:
package org.jtrix.project.documentation.embeddedapp; import java.net.*; import java.io.*; import java.util.Date; /** * Class that uses ordinary system resources, so we can try to * embed it into Jtrix. */ public class ResourceUser { private static String _WEB_PAGE = "http://www.jtrix.org/index.htm"; public static void main(String[] args) { try { InputStream is = (new URL(_WEB_PAGE)).openStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); FileWriter file = new FileWriter("/myhome/output." +(new Date()).getTime()+".html"); String line; while ((line = br.readLine()) != null) { file.write(line+"\n"); } file.close(); } catch (Exception e) { e.printStackTrace(); } } // main(String[]) } // ResourceUser
It is designed deliberately to use networking and disk space. Note that it also expects to have a directory called /myhome. It could expect anything. We just need to make sure that when we map some disk space onto something called /myhome for our application.
Here is the netlet which embeds the ResourceUser:
package org.jtrix.project.documentation.embeddedapp; import org.jtrix.base.*; import org.jtrix.facets1.node.ITrampolineFacet; import org.jtrix.facets1.node.ISocketFactory; import org.jtrix.project.nodality.facet.INodeResourceFacet; import org.jtrix.facets1.util.io.IFileSystem; /** * A netlet which demonstrates use of an external application. * Note this is specific to Nodality. Ordinarily an application * would expect to have resources provided by its hosting service. */ public class EmbeddedAppNetlet implements INetlet { /** Initialise and execute the program. First we get a socket * factory and a filesystem which the program is going to use. * Then we can just execute the program. */ public byte[] initialise (INode node, Object bean, byte[] untrusted) throws InitialiseException { try { String tf_name = ITrampolineFacet.class.getName(); String fs_name = IFileSystem.class.getName(); String rf_name = INodeResourceFacet.class.getName(); // Use sockets ITrampolineFacet tf = (ITrampolineFacet) node.bindFacet(tf_name); ISocketFactory sf = tf.getDefaultSocketFactory(); tf.setSocketFactory(sf); // Use files. Read-only = false INodeResourceFacet rf = (INodeResourceFacet) node.bindFacet(rf_name); INodeResourceFacet.IAccountedFileSystem tfs = rf.createFileSystem("/tmp", false); tf.mountFileSystem("/myhome",tfs.getFileSystem()); // Run external program ResourceUser.main(new String[]{}); tf.unmountFileSystem("/myhome"); return null; } catch (Throwable e) { e.printStackTrace(); throw new InitialiseException("Couldn't run netlet"); } } // initialise /** Terminate; no actions required. We're 100% done as soon as we enter! */ public void terminate(long expiry, INetlet.IShutdownProgress progress) { progress.performed(100); } /** We offer any service connections. */ public IService bindService(Warrant w, IService s) throws ServiceBindException { throw new ServiceBindException("No services supported"); } /** We offer no facets. */ public String[] getFacets() { return new String[]{ }; } public IRemote bindFacet(String facet_name) throws FacetBindException { throw new FacetBindException("Don't support facet "+facet_name); } } // EmbeddedAppNetlet
It simply assigns some resources, making sure the disk space maps to /myhome.
Nik Silver 2002-03-09