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.net.MalformedURLException;
21  import java.rmi.AccessException;
22  import java.rmi.AlreadyBoundException;
23  import java.rmi.NotBoundException;
24  import java.rmi.Remote;
25  import java.rmi.RemoteException;
26  import java.util.concurrent.TimeUnit;
27  
28  abstract class AFRegistryAccess {
29    /**
30     * Returns a reference to the existing RMI registry.
31     *
32     * If there's no registry running at this port, an exception is thrown.
33     *
34     * @return The registry.
35     * @throws RemoteException If there was a problem.
36     */
37    public abstract AFRegistry getRegistry() throws RemoteException;
38  
39    /**
40     * Convenience method for {@code getRegistry().lookup}.
41     *
42     * @param name the name for the remote reference to look up
43     * @return The instance
44     * @throws NotBoundException upon error
45     * @throws MalformedURLException upon error
46     * @throws RemoteException upon error
47     * @see AFRegistry#lookup(String)
48     */
49    public Remote lookup(String name) throws NotBoundException, MalformedURLException,
50        RemoteException {
51      return getRegistry().lookup(name);
52    }
53  
54    /**
55     * Convenience method for {@code getRegistry().lookup}.
56     *
57     * @param name the name for the remote reference to look up
58     * @param timeout The timeout value.
59     * @param unit The timeout unit.
60     * @return The instance
61     * @throws NotBoundException upon error
62     * @throws MalformedURLException upon error
63     * @throws RemoteException upon error
64     * @see AFRegistry#lookup(String, long, TimeUnit)
65     */
66    public Remote lookup(String name, long timeout, TimeUnit unit) throws NotBoundException,
67        MalformedURLException, RemoteException {
68      return getRegistry().lookup(name, timeout, unit);
69    }
70  
71    /**
72     * Convenience method for {@code getRegistry().unbind}.
73     *
74     * @param name the name for the remote reference to unbind
75     * @throws RemoteException upon error
76     * @throws NotBoundException upon error
77     * @throws MalformedURLException upon error
78     * @see AFRegistry#unbind(String)
79     */
80    public void unbind(String name) throws RemoteException, NotBoundException, MalformedURLException {
81      getRegistry().unbind(name);
82    }
83  
84    /**
85     * Convenience method for {@code getRegistry().bind}.
86     *
87     * @param name the name for the remote reference to bind
88     * @param obj the remote reference to bind
89     * @throws RemoteException upon error
90     * @throws AlreadyBoundException upon error
91     * @throws MalformedURLException upon error
92     * @see AFRegistry#bind(String, Remote)
93     */
94    public void bind(String name, Remote obj) throws AlreadyBoundException, MalformedURLException,
95        RemoteException {
96      getRegistry().bind(name, obj);
97    }
98  
99    /**
100    * Convenience method for {@code getRegistry().rebind}.
101    *
102    * @param name the name for the remote reference to rebind
103    * @param obj the remote reference to rebind
104    * @throws RemoteException upon error
105    * @throws MalformedURLException upon error
106    * @see AFRegistry#rebind(String, Remote)
107    */
108   public void rebind(String name, Remote obj) throws MalformedURLException, RemoteException {
109     getRegistry().rebind(name, obj);
110   }
111 
112   /**
113    * Convenience method for {@code getRegistry().list}.
114    *
115    * Unlike {@link AFRegistry#list()}, in case the registry has been shut down already, an empty
116    * array is returned.
117    *
118    * @return an array of the names bound in this registry
119    * @throws RemoteException upon error
120    * @throws AccessException upon error
121    */
122   public String[] list() throws RemoteException, AccessException {
123     try {
124       return getRegistry().list();
125     } catch (ShutdownException e) {
126       return new String[0];
127     }
128   }
129 }