Last active
April 3, 2022 16:00
-
-
Save fmbenhassine/6729553053bbff14b9b0 to your computer and use it in GitHub Desktop.
RMI hello world #lab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.benas.labs.javase.rmi; | |
import java.rmi.registry.LocateRegistry; | |
import java.rmi.registry.Registry; | |
public class Client { | |
public static void main(String[] args) throws Exception{ | |
Registry r = LocateRegistry.getRegistry("localhost", 2000); | |
IFoo foo = (IFoo) r.lookup("remoteFoo"); | |
System.out.println(foo.getMessage()); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.benas.labs.javase.rmi; | |
import java.rmi.RemoteException; | |
import java.rmi.server.UnicastRemoteObject; | |
public class Foo extends UnicastRemoteObject implements IFoo{ | |
private static final long serialVersionUID = -5655647669552931408L; | |
protected Foo() throws RemoteException { | |
super(); | |
} | |
public String getMessage() throws RemoteException { | |
return " Hi from remote Foo!"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.benas.labs.javase.rmi; | |
import java.rmi.Remote; | |
import java.rmi.RemoteException; | |
public interface IFoo extends Remote { | |
public String getMessage() throws RemoteException; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.benas.labs.javase.rmi; | |
import java.rmi.registry.LocateRegistry; | |
import java.rmi.registry.Registry; | |
public class Main { | |
public static void main(String[] args) { | |
// run with -Djava.security.manager -Djava.security.policy=/path/to/security.policy | |
try { | |
Registry registry = LocateRegistry.createRegistry(2000); | |
System.setSecurityManager(new java.rmi.RMISecurityManager()); | |
IFoo foo = new Foo(); | |
registry.bind("remoteFoo", foo); | |
System.out.println("RMI registry started."); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grant { | |
permission java.net.SocketPermission "*:2000-2100", "accept, connect"; | |
permission java.net.SocketPermission "*:80", "connect"; | |
permission java.security.AllPermission "", ""; | |
//From Sun : http://java.sun.com/j2se/1.5.0/docs/guide/jndi/jndi-rmi.html | |
//The application using JNDI and the RMI registry provider must be granted the following permissions: | |
//permission java.net.SocketPermission "host[:port]", "connect"; | |
// For each host/port identified in the java.naming.provider.url property and in URL string names supplied to context methods. | |
//permission java.net.SocketPermission "host[:port]", "connect,accept"; | |
//For each host/port identified in the URL strings in javax.naming.References. | |
//permission java.lang.RuntimePermission "setSecurityManager"; | |
// If using the java.naming.rmi.security.manager environment property, which asks the RMI registry provider to install the RMISecurityManager. | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment