Java RMI HelloWorld Program and related security manager knowledge

Source: Internet
Author: User

Java RMI refers to remote method invocation (invocation). It is a mechanism that allows an object on a Java virtual machine to invoke a method on an object in a Java virtual machine. Any object that can be called with this method must implement the remote interface.
Java RMI is not a new technology (in the era of Java1.1), but it is a very important underlying technology.


The famous EJBs are built on RMI, and now other open source remote calling components, the underlying technology is RMI.
In the era of vigorously advocating Web service, SOA, is not every application should choose clumsy Web service components to achieve, through the comparison test, RMI is the simplest, in some small applications is the most appropriate.
The following example is a simple helloworld to illustrate the principle and application of RMI through a simple sample. However, the core application and development model of RMI has been covered.

The Java classes on the server are as follows:

Remote interface: Helloservice.java

Import java.rmi.remote;import java.rmi.remoteexception;/** * Remote interface * @author www.wjrong.com * */public interface Helloserv Ice extends Remote {public String showmessage (String s) throws remoteexception;}

Remote interface implementation class: Helloserviceimpl. java

Import java.rmi.remoteexception;import java.rmi.server.unicastremoteobject;/** * Remote Interface Implementation class * @author www.wjrong.com * */ public class Helloserviceimpl extends UnicastRemoteObject implements HelloService {private static final long Serialversio nuID = 1L; Public Helloserviceimpl () throws RemoteException    {     super (),    } public String ShowMessage (String s) throws remoteexception {System.out.println ("There is a customer call this method!");  Return "Hello" +s;}}

Remote server-side startup class: Simpleserver. java

Import java.rmi.remote;import java.rmi.remoteexception;/** * Remote interface * @author www.wjrong.com * */public interface Helloserv Ice extends Remote {<span style= "White-space:pre" ></span>public string ShowMessage (string s) throws RemoteException;}

Here is the Java class that the client executes:

Remote interface: Helloservice.java

Import java.rmi.remote;import java.rmi.remoteexception;/** * Remote interface * @author www.wjrong.com * */public interface Helloserv Ice extends Remote {public String showmessage (String s) throws remoteexception;}

Client startup class: SimpleClient. java

Import Java.rmi.remoteexception;import Javax.naming.context;import Javax.naming.initialcontext;import javax.naming.namingexception;/** * Client Startup class * @author www.wjrong.com * */public class SimpleClient {public static final I NT Rmiport=8080;public static void Main (string[] args) {try {Context context=new initialcontext (); HelloService helloservice1= (HelloService) context.lookup ("rmi://192.168.1.180:" +rmiport+ "/HelloService"); HelloService helloservice2= (HelloService) context.lookup ("Rmi://localhost:" +rmiport+ "/HelloService");   try {    System.out.println (helloservice1.showmessage ("world!"));    System.out.println (HELLOSERVICE1==HELLOSERVICE2);//The 2 proxy objects of a remote object are not the same as the 2 stub addresses of the client    //system.out.println ( Helloservice1.equals (HELLOSERVICE2));//The 2 proxy objects of a remote object are the same as the 2 stub contents of the client. Both refer to the same remote object} catch (RemoteException e) {e.printstacktrace ();}} catch (Namingexception e) {e.printstacktrace ();}}}


When testing, start the Simpleserver first, and then start the SimpleClient
Server-Side output:
There is a customer call this method!
Client output:
Hello world!
Simple RMI Test Success!

Where the details need attention:
1. Security Manager

When no write policy file overwrites the Java.policy in C:\Program files\java\jre1.6.0_05\lib\security, the call

if (System.getsecuritymanager () ==null)

System.setsecuritymanager (New Rmisecuritymanager ());

The system throws an exception java.security.AccessControlException.
Cause: Every Java application can have its own security manager, which is the primary security guard against malicious attacks.

The security Manager performs an execution phase check and access authorization. To implement the security policies required for the application. Thus protecting resources from malicious operations. In effect, the security manager determines which set of permissions to grant to the class based on the Java security policy file.

However. When untrusted classes and third-party applications use the JVM, the Java Security Manager uses a JVM-related security policy to identify malicious actions. In very many cases, the threat model does not include malicious code that executes in the JVM. Java Security Manager is not required at this time.

When the security Manager detects an action that violates a security policy, the JVM throws Accesscontrolexception or SecurityException.
In a Java application, the security Manager is set by the method Setsecuritymanager in the System class.

To obtain the current security manager. able to use getSecurityManager. The Java.lang.SecurityManager class includes very many checkxxxx methods. such as the Checkread (String file) method used to infer access to a file.

These check methods call the Securitymanager.checkpermission method. The latter infers whether the calling app has the requested operation permission to run according to the security policy file.  Assuming no, SecurityException will be thrown. Suppose you want your app to use Security Manager and security policy, you can set the-djava.security.manager option when you start the JVM. You can also specify a security policy file at the same time. Assume that Java Security Manager is enabled in the app. Without specifying a security policy file, the JAVA security Manager will use the default security policy, which is defined by the Java.policy located in the folder $java_home/jre/lib/security.

Class Loaders help them determine the policy object. What permissions should be given to importing a piece of code into a virtual machine. Whenever, each application has only one policy object.
2. Doubts about Rmic Rmic is to generate the associated stub and skeleton (actually a proxy class) for the client and server side. However, we can directly invoke the remote method directly in the Javac Java process. There seems to be no rmic call.


Cause: In the jdk5.0 version number. It is necessary to use the rmic command to generate static proxy classes (including stubs and skeleton classes) for remote objects, while in jdk5.0 the RMI framework takes its own initiative to generate dynamic proxy classes (including stubs and skeleton classes) for remote objects during execution. The implementation details of the RMI framework are thus more thoroughly encapsulated. Simplifies how the RMI framework is used.
3.registry Local Register
Why not start the Rmiregistry.exe to be able to complete the local. Register pinch? In fact Locateregistry.createregistry (Rmiport) is the creation of a local reference to a remote or local register. Create and Export registry instances.

Java RMI HelloWorld Program and related security manager knowledge

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.