Debian Codegraph @

src/java/org/astrogrid/samp/xmlrpc/apache/ApacheServer.java

raw
1 package org.astrogrid.samp.xmlrpc.apache;
2
3 import java.io.IOException;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.Vector;
11 import org.apache.xmlrpc.WebServer;
12 import org.apache.xmlrpc.XmlRpcHandler;
13 import org.astrogrid.samp.SampUtils;
14 import org.astrogrid.samp.xmlrpc.SampXmlRpcServer;
15 import org.astrogrid.samp.xmlrpc.SampXmlRpcHandler;
16
17 /**
18 * SampXmlRpcServer implementation based on Apache XML-RPC library.
19 *
20 * @author Mark Taylor
21 * @since 22 Aug 2008
22 */
23 public class ApacheServer implements SampXmlRpcServer {
24
25 private final WebServer webServer_;
26 private final URL endpoint_;
27 private final List handlerList_;
28
29 /**
30 * Private constructor used by all other constructors.
31 * Uses the private LabelledServer class to aggregate the required
32 * information.
33 *
34 * @param server server with metadata
35 */
36 private ApacheServer( LabelledServer server ) {
37 webServer_ = server.webServer_;
38 endpoint_ = server.endpoint_;
39 handlerList_ = Collections.synchronizedList( new ArrayList() );
40 webServer_.addHandler( "$default", new XmlRpcHandler() {
41 public Object execute( String method, Vector params )
42 throws Exception {
43 return doExecute( method, params );
44 }
45 } );
46 }
47
48 /**
49 * Constructs a new server based on a given WebServer object.
50 * Responsibility for <code>start</code>ing the WebServer and performing
51 * any other required configuration lies with the caller.
52 *
53 * @param webServer apache xmlrpc webserver object
54 * @param port port number on which the server is running
55 */
56 public ApacheServer( WebServer webServer, int port ) {
57 this( new LabelledServer( webServer, getServerEndpoint( port ) ) );
58 }
59
60 /**
61 * Constructs a new server starting up a new WebServer object.
62 * The server runs in a daemon thread.
63 */
64 public ApacheServer() throws IOException {
65 this( createLabelledServer( true ) );
66 webServer_.start();
67 }
68
69 public URL getEndpoint() {
70 return endpoint_;
71 }
72
73 public void addHandler( SampXmlRpcHandler handler ) {
74 handlerList_.add( handler );
75 }
76
77 public void removeHandler( SampXmlRpcHandler handler ) {
78 handlerList_.remove( handler );
79 }
80
81 /**
82 * Does the work for executing an XML-RPC request.
83 *
84 * @param fqMethod fully qualified XML-RPC method name
85 * @param paramVec Apache-style list of method parameters
86 */
87 private Object doExecute( String fqMethod, Vector paramVec )
88 throws Exception {
89 SampXmlRpcHandler[] handlers =
90 (SampXmlRpcHandler[])
91 handlerList_.toArray( new SampXmlRpcHandler[ 0 ] );
92 for ( int ih = 0; ih < handlers.length; ih++ ) {
93 SampXmlRpcHandler handler = handlers[ ih ];
94 if ( handler.canHandleCall( fqMethod ) ) {
95 List paramList = (List) ApacheUtils.fromApache( paramVec );
96 Object result = handler.handleCall( fqMethod, paramList, null );
97 return ApacheUtils.toApache( result );
98 }
99 }
100 throw new UnsupportedOperationException( "No handler for method "
101 + fqMethod );
102 }
103
104 /**
105 * Constructs a new LabelledServer object suitable for use with this
106 * server.
107 *
108 * @param isDaemon whether the WebServer's main thread should run
109 * in daemon mode
110 */
111 private static LabelledServer createLabelledServer( final boolean isDaemon )
112 throws IOException {
113 int port = SampUtils.getUnusedPort( 2300 );
114 WebServer server = new WebServer( port ) {
115
116 // Same as superclass implementation except that the listener
117 // thread is marked as a daemon.
118 public void start() {
119 if ( this.listener == null ) {
120 this.listener =
121 new Thread( this, "XML-RPC Weblistener" );
122 this.listener.setDaemon( isDaemon );
123 this.listener.start();
124 }
125 }
126 };
127 return new LabelledServer( server, getServerEndpoint( port ) );
128 }
129
130 /**
131 * Returns the endpoint URL to use for an Apache server running on a
132 * given port.
133 *
134 * @param port port number
135 * @return URL
136 */
137 private static URL getServerEndpoint( int port ) {
138 String endpoint =
139 "http://" + SampUtils.getLocalhost() + ":" + port + "/";
140 try {
141 return new URL( endpoint );
142 }
143 catch ( MalformedURLException e ) {
144 throw (Error)
145 new AssertionError( "Bad protocol http?? " + endpoint )
146 .initCause( e );
147 }
148 }
149
150 /**
151 * Convenience class which aggregates a WebServer and an endpoint.
152 */
153 private static class LabelledServer {
154 private final WebServer webServer_;
155 private final URL endpoint_;
156
157 /**
158 * Constructor.
159 *
160 * @param webServer web server
161 * @param endpoint URL
162 */
163 LabelledServer( WebServer webServer, URL endpoint ) {
164 webServer_ = webServer;
165 endpoint_ = endpoint;
166 }
167 }
168 }