Client Implementation
Listing 6.7 InfoClient.java
import javax.swing.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.awt.*;
//Client implementation
public class InfoClient
extends JFrame {
String message = "blank";
//Remote reference
InfoInterface obj = null;
//Constructor
public InfoClient() {
super("Dynamic Loading");
//Text area to show messages
JTextArea txtArea = new JTextArea(10,35);
//set the font of the component
txtArea.setFont(new Font("Serif",Font.BOLD,16));
//add the component to the content pane
getContentPane().add(txtArea);
try {
//To run this application, change the "servermachinename" to a
//valid machine name. To test without a network specify
//"localhost".
obj = (InfoInterface) Naming.lookup("rmi://servermachinename:1099/Server");
//call the remote method
message = obj.getInfo();
//Show the message returned
txtArea.setText(message);
} catch (Exception e) {
txtArea.setText("Error : "+e.getMessage());
}
//pack() causes the window to be sized to fit the
//preferred size and layouts of its subcomponents.
pack();
//Move the component to the specified location
setLocation(10,10);
show();
}
//main method
public static void main(String args[]){
if (System.getSecurityManager() == null) {
RMISecurityManager manager = new RMISecurityManager();
//set the security manager
System.setSecurityManager(manager);
}
new InfoClient();
}
}
Explanation
The most involved method of the InfoClient
class is the main method.
The main method installs a security
manager. A security manager protects access to system resources from
untrusted downloaded code running within the virtual machine. The security
manager determines whether downloaded code has access to the local file
system, or it can perform any other privileged operations. RMI(s) class
loader will not download any classes from remote locations, if no security
manager has been set. The following lines install a security manager.
if (System.getSecurityManager() == null) {
RMISecurityManager manager = new RMISecurityManager();
System.setSecurityManager(manager);
}
Finally, the main method creates an instance of the InfoClient
class.
|