KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xmlrpc > applet > XmlRpcApplet


1 /*
2  * Copyright 1999,2005 The Apache Software Foundation.
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
18 package org.apache.xmlrpc.applet;
19
20 import java.applet.Applet JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Vector JavaDoc;
25
26
27 /**
28  * An applet that provides basic XML-RPC client functionality.
29  *
30  * @version $Id: XmlRpcApplet.java,v 1.3 2005/04/22 10:25:58 hgomez Exp $
31  */

32 public class XmlRpcApplet extends Applet JavaDoc {
33
34     SimpleXmlRpcClient client;
35
36
37     /**
38      * Initialize the XML-RPC client, trying to get the port number from the
39      * applet parameter tags. The default for port is 80. The client connects to
40      * the server this applet came from.
41      */

42     public void initClient()
43     {
44         int port = 80;
45         String JavaDoc p = getParameter("PORT");
46         if (p != null)
47         {
48             try
49             {
50                 port = Integer.parseInt(p);
51             }
52             catch (NumberFormatException JavaDoc nfx)
53             {
54                 System.out.println("Error parsing port: " + nfx);
55             }
56         }
57         initClient(port);
58     }
59
60     /**
61      * Initialize the XML-RPC client with the specified port and the server this
62      * applet came from.
63      */

64     public void initClient(int port)
65     {
66         String JavaDoc uri = getParameter("URI");
67         if (uri == null)
68         {
69             uri = "/RPC2";
70         }
71         else if (!uri.startsWith("/"))
72         {
73             uri = "/" + uri;
74         }
75         initClient(port, uri);
76     }
77
78     /**
79      * Initialize the XML-RPC client with the specified port and request path
80      * and the server this applet came from.
81      */

82     public void initClient(int port, String JavaDoc uri)
83     {
84         String JavaDoc host = getCodeBase().getHost();
85         try
86         {
87             URL JavaDoc url = new URL JavaDoc("http://" + host + ":" + port + uri);
88             System.out.println("XML-RPC URL: " + url);
89             client = new SimpleXmlRpcClient(url);
90         }
91         catch (MalformedURLException JavaDoc unlikely)
92         {
93             System.out.println("Error constructing XML-RPC client for "
94                     + host + ":" + port + ": " + unlikely);
95         }
96     }
97
98     /**
99      * Calls the XML-RPC server with the specified methodname and argument list.
100      */

101     public Object JavaDoc execute(String JavaDoc methodName, Vector JavaDoc arguments)
102             throws XmlRpcException, IOException JavaDoc
103     {
104         if (client == null)
105         {
106             initClient ();
107         }
108         Object JavaDoc returnValue = null;
109         return returnValue = client.execute(methodName, arguments);
110     }
111 }
112
Popular Tags