ComputeEngineImpl
Home ] Up ] [ ComputeEngineImpl ] Security Policies ] ComputePi ] ComputeEngineClient ] Running the Example ]

 

 

Here's the ComputeEngineImpl class.

There's nothing much different here than from earlier examples, except for one thing, which we'll examine in the next web page.

package rmiServer;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import rmi.ComputeEngine;
import rmi.Task;

/**
 * An implementation of an RMI remote compute engine.
 *
 * @author Bryan Higgs
 * @version 1.0
 */
public class ComputeEngineImpl
    extends UnicastRemoteObject
    implements ComputeEngine
{
  /**
   * Creates a new instance of ComputeEngineImpl
   */
  public ComputeEngineImpl() throws RemoteException
  {
  }

  /**
   * Computes the specified task
   * @param task the task to be performed.
   */
  public <T> T compute(Task<T> task) throws RemoteException
  {
    return task.execute();
  }
  
  /**
   * Main entry point
   * @param args the command line arguments
   *             args[0] contains the port number for the registry
   */
  public static void main(String[] args)
  {
    System.out.println("Setting java.security.policy ...");   
    System.setProperty("java.security.policy", "policies/server.policy");
    // Use a security manager
    System.err.println("Setting SecurityManager ...");
    System.setSecurityManager( new SecurityManager() );
    
    // Determine the port number
    int port = 1099;  // the default port number for the registry
    if (args.length > 0)
    {
      try
      {
        port = Integer.parseInt(args[0]);
      }
      catch (NumberFormatException nfe)
      {
        System.err.println("Port number " + args[1] + " invalid");
        return;
      }
    }
    
    System.err.println("Using port " + port);

    try
    {
      System.err.println("Creating ComputeEngineImpl instance...");
      ComputeEngineImpl engine = new ComputeEngineImpl();
      
      System.err.println("[Re]binding ComputeEngineImpl instance in registry...");
      String url = "rmi://localhost:" + port + "/ComputeEngine";
      System.err.println("Using url: " + url);
      
      Context namingContext = new InitialContext();
      namingContext.rebind("rmi://localhost:" + port + "/ComputeEngine", engine);
      
      System.err.println("Ready to compute...");
    }
    catch (NamingException ne)
    {
      ne.printStackTrace();
    }
    catch (RemoteException re)
    {
      re.printStackTrace();
    }
  }
}
 
The page was last updated February 19, 2008