1 /*
2 Copyright (c) 2007 Health Market Science, Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package com.healthmarketscience.rmiio.exporter;
18
19 import java.rmi.RemoteException;
20 import java.rmi.server.UnicastRemoteObject;
21
22 import com.healthmarketscience.rmiio.RemoteStreamServer;
23
24
25 /**
26 * Default concrete implementation of RemoteStreamExporter which exports the
27 * object for use with with standard RMI, via {@link UnicastRemoteObject}.
28 * <p>
29 * The default constructor will use a port configured by the system property
30 * {@link #PORT_PROPERTY} if one is found, otherwise {@link #ANY_PORT} will be
31 * used.
32 *
33 * @author James Ahlborn
34 */
35 public class DefaultRemoteStreamExporter extends RemoteStreamExporter
36 {
37 /** constant indicating that export can use any port */
38 public static final int ANY_PORT = 0;
39
40 /** system property used to determine the port to use for the default
41 constructor. if not given, {@link #ANY_PORT} is used. */
42 public static final String PORT_PROPERTY =
43 "com.healthmarketscience.rmiio.exporter.port";
44
45 /** port number to use when exporting streams */
46 private final int _port;
47
48 public DefaultRemoteStreamExporter() {
49 this(getDefaultPort());
50 }
51
52 public DefaultRemoteStreamExporter(int port) {
53 _port = port;
54 }
55
56 public int getPort() {
57 return _port;
58 }
59
60 @Override
61 protected Object exportImpl(RemoteStreamServer<?,?> server)
62 throws RemoteException
63 {
64 return UnicastRemoteObject.exportObject(server, getPort());
65 }
66
67 @Override
68 protected void unexportImpl(RemoteStreamServer<?,?> server)
69 throws Exception
70 {
71 UnicastRemoteObject.unexportObject(server, true);
72 }
73
74 /**
75 * Determines the port to use for the default constructor. If the system
76 * property {@link #PORT_PROPERTY} has a valid integer it will be returned,
77 * otherwise {@link #ANY_PORT} will be returned.
78 * @return a port number
79 */
80 private static int getDefaultPort()
81 {
82 return Integer.getInteger(PORT_PROPERTY, ANY_PORT);
83 }
84
85 }