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.rmi;
19  
20  import java.io.Closeable;
21  import java.io.IOException;
22  import java.rmi.Remote;
23  import java.rmi.RemoteException;
24  import java.util.stream.IntStream;
25  
26  /**
27   * The {@link AFRMIService} assigns and keeps track of anonymous ports, among other things.
28   *
29   * This feature is to be used by {@link AFRMISocketFactory} only.
30   *
31   * @author Christian Kohlschütter
32   */
33  public interface AFRMIService extends Remote {
34    /**
35     * Registers a new anonymous port and returns it. When the port is not required anymore, it must
36     * be returned via {@link #returnPort(int)}.
37     *
38     * @return The new port.
39     * @throws IOException if the operation fails.
40     */
41    int newPort() throws IOException;
42  
43    /**
44     * Returns a previously registered port. No error is thrown if the given port has not been
45     * assigned before.
46     *
47     * @param port The port.
48     * @throws IOException if the operation fails.
49     */
50    void returnPort(int port) throws IOException;
51  
52    /**
53     * Returns a stream of open ports.
54     *
55     * @return A sequence of open ports.
56     * @throws RemoteException if the operation fails.
57     */
58    IntStream openPorts() throws RemoteException;
59  
60    /**
61     * Indicates whether a remote-shutdown of the RMI registry is allowed.
62     *
63     * NOTE: A call to {@link #shutdown()} may or may not succeed regardless.
64     *
65     * @return Indication of whether a remote-shutdown of the RMI registry is allowed.
66     * @throws RemoteException if the operation fails.
67     */
68    boolean isShutdownAllowed() throws RemoteException;
69  
70    /**
71     * Asks that this RMI registry gets shut down.
72     *
73     * @throws RemoteException if the operation fails.
74     */
75    void shutdown() throws RemoteException;
76  
77    /**
78     * Adds the given {@link Closeable} to the list of instances to be closed upon shutdown of the RMI
79     * registry.
80     *
81     * @param closeable The instance.
82     * @throws RemoteException if the operation fails.
83     */
84    void registerForShutdown(Closeable closeable) throws RemoteException;
85  
86    /**
87     * Removes the given {@link Closeable} from the list of instances to be closed upon shutdown of
88     * the RMI registry.
89     *
90     * No error is returned if the given element was not registered before.
91     *
92     * @param closeable The instance.
93     * @throws RemoteException if the operation fails.
94     */
95    void unregisterForShutdown(Closeable closeable) throws RemoteException;
96  }