Debian Codegraph @

src/java/org/astrogrid/samp/xmlrpc/CallableClientServer.java

raw
1 package org.astrogrid.samp.xmlrpc;
2
3 import java.io.IOException;
4 import java.net.URL;
5 import java.util.Map;
6 import java.util.HashMap;
7 import org.astrogrid.samp.client.CallableClient;
8 import org.astrogrid.samp.client.HubConnection;
9
10 /**
11 * XML-RPC server which can host {@link CallableClient} instances.
12 * There should usually be only one instance of this class for each
13 * SampXmlRpcServer - see {@link #getInstance}.
14 *
15 * @author Mark Taylor
16 * @since 16 Jul 2008
17 */
18 class CallableClientServer {
19
20 private final URL url_;
21 private SampXmlRpcServer server_;
22 private ClientXmlRpcHandler clientHandler_;
23
24 private static final Map serverMap_ = new HashMap();
25
26 /**
27 * Constructor. Note that a {@link #getInstance} method exists as well.
28 *
29 * @param server XML-RPC server hosting this client server
30 */
31 public CallableClientServer( SampXmlRpcServer server ) throws IOException {
32 server_ = server;
33 clientHandler_ = new ClientXmlRpcHandler();
34 server_.addHandler( clientHandler_ );
35 url_ = server_.getEndpoint();
36 }
37
38 /**
39 * Returns the XML-RPC endpoint for this server.
40 *
41 * @return endpoint url
42 */
43 public URL getUrl() {
44 return url_;
45 }
46
47 /**
48 * Adds a CallableClient object to this server.
49 *
50 * @param connection hub connection for the registered client on behalf
51 * of which the client will operate
52 * @param callable callable client object
53 */
54 public void addClient( HubConnection connection, CallableClient callable ) {
55 if ( clientHandler_ == null ) {
56 throw new IllegalStateException( "Closed" );
57 }
58 clientHandler_.addClient( connection, callable );
59 }
60
61 /**
62 * Removes a CallableClient object from this server.
63 *
64 * @param privateKey hub connection for which this client was added
65 */
66 public void removeClient( HubConnection connection ) {
67 if ( clientHandler_ != null ) {
68 clientHandler_.removeClient( connection );
69 }
70 }
71
72 /**
73 * Tidies up resources. Following a call to this method, no further
74 * clients can be added.
75 */
76 public void close() {
77 if ( server_ != null ) {
78 server_.removeHandler( clientHandler_ );
79 }
80 server_ = null;
81 clientHandler_ = null;
82 }
83
84 /**
85 * Indicates whether this server currently has any clients.
86 *
87 * @return true iff there are clients
88 */
89 boolean hasClients() {
90 return clientHandler_ != null && clientHandler_.getClientCount() > 0;
91 }
92
93 /**
94 * Returns an instance of CallableClientServer for use with a given
95 * XML-RPC server. Because of the implementation, only one
96 * CallableClientServer is permitted per XML-RPC server, so if one
97 * has already been installed for the given <code>server</code>,
98 * that one will be returned. Otherwise a new one will be constructed,
99 * installed and returned.
100 *
101 * <p>To prevent memory leaks, once any clients added to the returned
102 * server have been removed (the client count drops to zero), the
103 * server will be closed and cannot be re-used.
104 *
105 * @param server XML-RPC server
106 * @return new or re-used CallableClientServer which is installed on
107 * <code>server</code>
108 */
109 public static synchronized CallableClientServer
110 getInstance( SampXmlRpcServerFactory serverFact )
111 throws IOException {
112 final SampXmlRpcServer server = serverFact.getServer();
113 if ( ! serverMap_.containsKey( server ) ) {
114 CallableClientServer clientServer =
115 new CallableClientServer( server ) {
116 public void removeClient( HubConnection connection ) {
117 super.removeClient( connection );
118 if ( ! hasClients() ) {
119 close();
120 synchronized ( CallableClientServer.class ) {
121 serverMap_.remove( server );
122 }
123 }
124 }
125 };
126 serverMap_.put( server, clientServer );
127 }
128 return (CallableClientServer) serverMap_.get( server );
129 }
130 }