Java InstantCode. Developing Applications Using Java NIO
The FileServer.java file contains the main function of the file server. The file server binds the remote object to the RMI registry. So, any authorized client can access the remote objects.
Listing 3-4 shows the content of the FileServer.java file:
Listing 3-4: The FileServer.java
|
/* Imports java.io package classes. */ import java.io.*; /* Imports java.rmi package classes. */ import java.rmi.*; /* class FileServer - Creates the RMI server that registers all the remote objects. Method: main() - Starts the RMI server and registers the remote objects. */ public class FileServer { public static void main(String args[]) { /* Sets the RMI security manager. */ System.setSecurityManager(new RMISecurityManager()); try { /* Creates the remote object. */ FileRemote f = new FileRemoteImpl("FServer"); /* Binds the remote object to the RMI registry. */ Naming.rebind("FServer", f); System.out.println("Object is registered."); System.out.println("Now server is waiting for client request"); } catch(Exception e) { e.printStackTrace(); System.out.println("FileServer: " + e); } } }
|
Download this Listing .
In the above code:
-
The main() method creates an instance of the RMISecurityManager class. This method sets the security manager to the File Download application using the setSecurityManager() method.
-
The main() method also creates an object of the FileRemoteImpl class and binds the remote object to the RMI registry using the rebind() method.
Категории