try {
// Install a Security manager, if the user didn't already install
// one with the -Djava.security.manager argument
if (System.getSecurityManager() == null) {
System.out.println("Establishing a security manager");
System.setSecurityManager(new SecurityManager());
}
// Create a Server object
Server server = new Server(null, 5);
// Create the ClassLoader that we'll use to load Service classes.
// The classes should be stored in the JAR file or the directory
// specified as a URL by the first command-line argument
URL serviceURL = new URL(args[0]);
ClassLoader loader =
new java.net.URLClassLoader(new URL[] {serviceURL});
// 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) {
// Dynamically load the Service class using the class loader
Class serviceClass = loader.loadClass(args[i++]);
// Dynamically instantiate the class.
Server.Service service =
(Server.Service)serviceClass.newInstance();
int port = Integer.parseInt(args[i++]); // Parse the 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.class.getName() +
" <url> <servicename> <port>\n" +
"\t[<servicename> <port> ... ]");
System.exit(1);
}