Writing an RMI Server Overview import iava.rmi.*: import java.rmi.server.*; erver extends UnicastRemoteobject public static void main String [args throws RemoteException,java.net.MalformedURLException, RMISecurityException //1.Set Security Manager /2.Create an object instance //3.Register object into the name space In addition to implementing the interface,we also need to write the server's main program.RMI currently does not support server programs as applets,so the main program needs to be a standalone Java application.You can either code a separate Java class for main,or you can just code a main method in the implementation class as we did here. Note also that we coded the main function to throw any RMI-related exceptions to the command line.That's OK for a small sample program like this,but in a real program you will probably instead bracket the steps that follow in separate try-catch blocks so you can perform better error handling. The steps that the server main typically does are: 1.Install a security manager class that lets the server program accept stub classes from other machines 2.Create an instance of the server object 3.Register the server object into the RMI naming registry so that client programs can find it Let's now take a closer look at each of these steps
Writing an RMI Server Overview import java.rmi.*; import java.rmi.server.*; public class MeetingServer extends UnicastRemoteObject implements Meeting { . . . public static void main ( String [] args ) throws RemoteException, java.net.MalformedURLException, RMISecurityException { // 1. Set Security Manager // 2. Create an object instance // 3. Register object into the name space } } In addition to implementing the interface, we also need to write the server's main program. RMI currently does not support server programs as applets, so the main program needs to be a standalone Java application. You can either code a separate Java class for main, or you can just code a main method in the implementation class as we did here. Note also that we coded the main function to throw any RMI-related exceptions to the command line. That's OK for a small sample program like this, but in a real program you will probably instead bracket the steps that follow in separate try-catch blocks so you can perform better error handling. The steps that the server main typically does are: 1. Install a security manager class that lets the server program accept stub classes from other machines 2. Create an instance of the server object 3. Register the server object into the RMI naming registry so that client programs can find it Let's now take a closer look at each of these steps
Setting the Security Manager import java.rmi.*; import java.rmi.server.*; publitati void main stringags java.net.Malforme RMISecurityException System.setsecurityManager new RMISecurityManager()); MeetingServer ms new MeetingServer(); Naming.rebind rmi://myhost.com/Meeting",ms ) The first step is to install the RMI security manager.While this is not strictly required,it does allow the server virtual machine to download class files.For example,suppose the client calls a method in this server that accepts a reference to an application-defined object type,for example a BankAccount.By setting the security manager,we allow the RMI runtime to copy the BankAccount class file to the server dynamically -that eases the configuration on the server. The downside to letting RMI dynamically download such classes is that it's a security risk.In other words,we are essentially letting the server execute code from another machine.While we hope that these class files are not going to harm the server,if you want to avoid the risk,your RMI server should not install a security manager.You must then ensure that all class files are installed locally in the server's classpath And now just an aside before we move on:passing object argument types is actually a pretty involved topic,since there are two ways to do it.One way is to pass only a reference across the communication wire;the other is to serialize the object and create a new object remotely.We will not discuss these any further since we don't have enough time,so you should read the RMI documentation in the JDK for more details
Setting the Security Manager import java.rmi.*; import java.rmi.server.*; public static void main ( String [] args ) throws RemoteException, java.net.MalformedURLException, RMISecurityException { System.setSecurityManager ( new RMISecurityManager() ); MeetingServer ms = new MeetingServer(); Naming.rebind ( "rmi://myhost.com/Meeting", ms ); } The first step is to install the RMI security manager. While this is not strictly required, it does allow the server virtual machine to download class files. For example, suppose the client calls a method in this server that accepts a reference to an application-defined object type, for example a BankAccount. By setting the security manager, we allow the RMI runtime to copy the BankAccount class file to the server dynamically - that eases the configuration on the server. The downside to letting RMI dynamically download such classes is that it's a security risk. In other words, we are essentially letting the server execute code from another machine. While we hope that these class files are not going to harm the server, if you want to avoid the risk, your RMI server should not install a security manager. You must then ensure that all class files are installed locally in the server's classpath. And now just an aside before we move on: passing object argument types is actually a pretty involved topic, since there are two ways to do it. One way is to pass only a reference across the communication wire; the other is to serialize the object and create a new object remotely. We will not discuss these any further since we don't have enough time, so you should read the RMI documentation in the JDK for more details
Naming the Remote Object import iava.rmi,*: import iava.rmi.server.*; public static void main,(String[args)throws Rm86ry82pe18n.aeta1tomaRiacoptiom new RMISecurityManager()); MeetingServer ms new MeetingServer(); Naming.rebind "rmi://myhost.com/Meeting",ms ) The next server job is to create an initial instance of the server object and then write a name for the object into the RMI name registry.The RMI name registry lets you assign URL names to objects so that clients can look them up To register the name,call the static rebind method,which is defined on the Naming class.This method accepts the URL name for the object and the object reference. The name string is the interesting part.It contains the rmi:/prefix,the hostname of the computer where the RMI object's server runs,and the object's name itself,which is pretty much whatever you want.Note that instead of hard-coding the hostname as we did here,you can call the getLocalHost method defined by the java.net.InetAddress class
Naming the Remote Object import java.rmi.*; import java.rmi.server.*; public static void main ( String [] args ) throws RemoteException, java.net.MalformedURLException, RMISecurityException { System.setSecurityManager ( new RMISecurityManager() ); MeetingServer ms = new MeetingServer(); Naming.rebind ( "rmi://myhost.com/Meeting", ms ); } The next server job is to create an initial instance of the server object and then write a name for the object into the RMI name registry. The RMI name registry lets you assign URL names to objects so that clients can look them up To register the name, call the static rebind method, which is defined on the Naming class. This method accepts the URL name for the object and the object reference. The name string is the interesting part. It contains the rmi:// prefix, the hostname of the computer where the RMI object's server runs, and the object's name itself, which is pretty much whatever you want. Note that instead of hard-coding the hostname as we did here, you can call the getLocalHost method defined by the java.net.InetAddress class
Generating the Stubs and Skeletons MeetingServer_Stub.class MeetingServer.class rmic MeetingServer_Skel.class After writing and compiling your server implementation,you're ready to create the stubs and skeleton classes.And that's easy to do:just run the JDK's rmic command,specifying the name of your implementation class file (without the extension).RMIC will create a class file each for the stub and skeleton.You will then need to deploy these files properly,which we will cover after we cover writing the client-side code
Generating the Stubs and Skeletons MeetingServer.class rmic MeetingServer_Stub.class MeetingServer_Skel.class After writing and compiling your server implementation, you're ready to create the stubs and skeleton classes. And that's easy to do: just run the JDK's rmic command, specifying the name of your implementation class file (without the extension). RMIC will create a class file each for the stub and skeleton. You will then need to deploy these files properly, which we will cover after we cover writing the client-side code