View Javadoc
1   /*
2    * junixsocket
3    *
4    * Copyright 2009-2024 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  @SuppressWarnings("CatchAndPrintStackTrace" /* errorprone */)
37  public final class SimpleRMIClientActingAsServer {
38    public static void main(String[] args) throws IOException, NotBoundException {
39      AFUNIXNaming naming = AFUNIXNaming.getInstance();
40  
41      System.out.println("Locating registry...");
42      final Registry registry = naming.getRegistry();
43      System.out.println(registry);
44      System.out.println();
45      HelloWorld obj = (HelloWorld) registry.lookup("helloWorld");
46      System.out.println("HelloWorld instance:");
47      System.out.println("    " + obj);
48      System.out.println("    " + RemotePeerInfo.remotePeerCredentials(obj));
49      System.out.println();
50  
51      World world = new WorldImpl("everybody");
52      naming.exportAndRebind("world", world);
53      System.out.println("Exporting our own World instance:");
54      System.out.println("    " + RemoteObject.toStub(world));
55      System.out.println("    " + RemotePeerInfo.remotePeerCredentials(world));
56      System.out.println();
57  
58      System.out.println("Calling HelloWorld...");
59      System.out.println(obj.hello() + " " + obj.world() + "!");
60  
61      /*
62       * Uncommenting the line below keeps this instance running.
63       *
64       * Try it and run SimpleRMIClient to see the difference.
65       */
66      naming.unexportAndUnbind("world", world);
67  
68      /*
69       * Also try to remotely shut down the registry.
70       *
71       * This will not succeed if the server set {@code naming.setRemoteShutdownAllowed(false)}. See
72       * {@link SimpleRMIServer}
73       */
74      // naming.shutdownRegistry();
75    }
76  }