| 1 |
/* |
| 2 |
* junixsocket |
| 3 |
* |
| 4 |
* Copyright 2009-2022 Christian Kohlschütter |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
package org.newsclub.net.unix.demo.rmi; |
| 19 |
|
| 20 |
import java.io.IOException; |
| 21 |
import java.rmi.NotBoundException; |
| 22 |
import java.rmi.registry.Registry; |
| 23 |
import java.rmi.server.RemoteObject; |
| 24 |
|
| 25 |
import org.newsclub.net.unix.demo.rmi.services.HelloWorld; |
| 26 |
import org.newsclub.net.unix.demo.rmi.services.World; |
| 27 |
import org.newsclub.net.unix.rmi.AFUNIXNaming; |
| 28 |
import org.newsclub.net.unix.rmi.RemotePeerInfo; |
| 29 |
|
| 30 |
/** |
| 31 |
* A simple RMI client. Locates the RMI registry via AF_UNIX sockets and calls |
| 32 |
* {@link HelloWorld#hello()}. |
| 33 |
* |
| 34 |
* @author Christian Kohlschütter |
| 35 |
*/ |
| 36 |
public final class SimpleRMIClientActingAsServer { |
| 37 |
public static void main(String[] args) throws IOException, NotBoundException { |
| 38 |
AFUNIXNaming naming = AFUNIXNaming.getInstance(); |
| 39 |
|
| 40 |
System.out.println("Locating registry..."); |
| 41 |
final Registry registry = naming.getRegistry(); |
| 42 |
System.out.println(registry); |
| 43 |
System.out.println(); |
| 44 |
HelloWorld obj = (HelloWorld) registry.lookup("helloWorld"); |
| 45 |
System.out.println("HelloWorld instance:"); |
| 46 |
System.out.println(" " + obj); |
| 47 |
System.out.println(" " + RemotePeerInfo.remotePeerCredentials(obj)); |
| 48 |
System.out.println(); |
| 49 |
|
| 50 |
World world = new WorldImpl("everybody"); |
| 51 |
naming.exportAndRebind("world", world); |
| 52 |
System.out.println("Exporting our own World instance:"); |
| 53 |
System.out.println(" " + RemoteObject.toStub(world)); |
| 54 |
System.out.println(" " + RemotePeerInfo.remotePeerCredentials(world)); |
| 55 |
System.out.println(); |
| 56 |
|
| 57 |
System.out.println("Calling HelloWorld..."); |
| 58 |
System.out.println(obj.hello() + " " + obj.world() + "!"); |
| 59 |
|
| 60 |
/** |
| 61 |
* Uncommenting the line below keeps this instance running. |
| 62 |
* |
| 63 |
* Try it and run SimpleRMIClient to see the difference. |
| 64 |
*/ |
| 65 |
naming.unexportAndUnbind("world", world); |
| 66 |
|
| 67 |
/** |
| 68 |
* Also try to remotely shut down the registry. |
| 69 |
* |
| 70 |
* This will not succeed if the server set {@code naming.setRemoteShutdownAllowed(false)}. See |
| 71 |
* {@link SimpleRMIServer} |
| 72 |
*/ |
| 73 |
// naming.shutdownRegistry(); |
| 74 |
} |
| 75 |
} |