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  
23  import org.newsclub.net.unix.AFUNIXSocketCredentials;
24  import org.newsclub.net.unix.demo.rmi.services.HelloWorld;
25  import org.newsclub.net.unix.demo.rmi.services.World;
26  import org.newsclub.net.unix.rmi.AFNaming;
27  
28  import com.kohlschutter.annotations.compiletime.SuppressFBWarnings;
29  
30  /**
31   * The implementation of the very simple {@link HelloWorld} service.
32   *
33   * @author Christian Kohlschütter
34   */
35  public class HelloWorldImpl implements HelloWorld {
36    private final AFNaming naming;
37  
38    /**
39     * Creates a new {@link HelloWorld} implementation.
40     *
41     * @param naming The naming instance to use.
42     */
43    @SuppressFBWarnings("EI_EXPOSE_REP")
44    public HelloWorldImpl(AFNaming naming) {
45      this.naming = naming;
46    }
47  
48    @Override
49    public String hello() throws IOException {
50      System.out.println("Received call to hello() from: " + AFUNIXSocketCredentials
51          .remotePeerCredentials());
52      return "Hello";
53    }
54  
55    @Override
56    public String world() throws IOException {
57      try {
58        return ((World) naming.lookup("world")).world();
59      } catch (NotBoundException e) {
60        return "world";
61      }
62    }
63  }