FileDocCategorySizeDatePackage
SafeServer.javaAPI DocExample13437Sat Jun 02 02:43:08 BST 2001None

SafeServer

public class SafeServer extends Object
This class is a program that uses the Server class defined in Chapter 9. Server would load arbitrary "Service" classes to provide services. This class is an alternative program to start up a Server in a similar way. The difference is that this one uses a SecurityManager and a ClassLoader to prevent the Service classes from doing anything damaging or malicious on the local system. This allows us to safely run Service classes that come from untrusted sources.

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

    try {
      // Install the Security manager
      System.setSecurityManager(new ServiceSecurityManager());

      // Create the ClassLoader that we'll use to load Service classes.
      // The classes should be stored in (or beneath) the directory specified
      // as the first command-line argument
      LocalClassLoader loader = new LocalClassLoader(args[0]);

      // Create a Server object that does no logging and
      // has a limit of five concurrent connections at once.
      Server server = new Server(null, 5);

      // Parse the argument list, which should contain Service name/port pairs.
      // For each pair, load the named Service using the class loader, then
      // instantiate it with newInstance(), then tell the server to start
      // running it.
      int i = 1;
      while(i < args.length) {
        Class serviceClass = loader.loadClass(args[i++]);      // dynamic load
        Service service = (Service)serviceClass.newInstance(); // instantiate
        int port = Integer.parseInt(args[i++]);                // get port
        server.addService(service, port);                      // run service
      }
    }
    catch (Exception e) { // Display a message if anything goes wrong
      System.err.println(e);
      System.err.println("Usage: java SafeServer "  +
                         "<servicedir> <servicename> <port>\n" +
                         "\t\t[<servicename> <port> ... ]");
      System.exit(1);
    }