KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Date JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Vector JavaDoc;
23
24
25 /**
26  * An applet that can be accessed via LiveConnect from JavaScript. It provides
27  * methods for adding arguments and triggering method execution for XML-RPC
28  * requests. This works on recent Netscape 4.x browsers as well as Internet
29  * Explorer 4.0 on Windows 95/NT, but not on IE/Mac. <p>
30  *
31  * Results from XML-RPC calls are exposed to JavaScript as the are, i.e.
32  * &lt;structs>s are <a HREF=http://java.sun.com/products/jdk/1.1/docs/api/java.util.Hashtable.html>Hashtables</a>
33  * and &lt;array>s are <a HREF=http://java.sun.com/products/jdk/1.1/docs/api/java.util.Vector.html>Vectors</a>
34  * and can be accessed thru their public methods. It seems like Date objects are
35  * not converted properly between JavaScript and Java, so the dateArg methods
36  * take long values instead of Date objects as parameters (date.getTime()).
37  *
38  * @version $Id: JSXmlRpcApplet.java,v 1.4 2005/04/22 10:25:58 hgomez Exp $
39  */

40 public class JSXmlRpcApplet extends XmlRpcApplet
41 {
42     public Object JavaDoc loaded = null;
43
44     private String JavaDoc errorMessage;
45     private Vector JavaDoc arguments;
46
47     /**
48      *
49      */

50     public void init()
51     {
52         initClient();
53         arguments = new Vector JavaDoc();
54         loaded = Boolean.TRUE;
55         System.out.println("JSXmlRpcApplet initialized");
56     }
57
58     // add ints (primitve != object) to structs, vectors
59
public void addIntArg(int value)
60     {
61         arguments.addElement(new Integer JavaDoc(value));
62     }
63
64     public void addIntArgToStruct(Hashtable JavaDoc struct, String JavaDoc key, int value)
65     {
66         struct.put(key, new Integer JavaDoc(value));
67     }
68
69     public void addIntArgToArray(Vector JavaDoc ary, int value)
70     {
71         ary.addElement(new Integer JavaDoc(value));
72     }
73
74     // add floats/doubles to structs, vectors
75
public void addDoubleArg(float value)
76     {
77         arguments.addElement(new Double JavaDoc(value));
78     }
79
80     public void addDoubleArgToStruct(Hashtable JavaDoc struct, String JavaDoc key, float value)
81     {
82         struct.put(key, new Double JavaDoc(value));
83     }
84
85     public void addDoubleArgToArray(Vector JavaDoc ary, float value)
86     {
87         ary.addElement(new Double JavaDoc(value));
88     }
89
90     public void addDoubleArg(double value)
91     {
92         arguments.addElement(new Double JavaDoc(value));
93     }
94
95     public void addDoubleArgToStruct(Hashtable JavaDoc struct, String JavaDoc key, double value)
96     {
97         struct.put(key, new Double JavaDoc(value));
98     }
99
100     public void addDoubleArgToArray(Vector JavaDoc ary, double value)
101     {
102         ary.addElement(new Double JavaDoc(value));
103     }
104
105     // add bools to structs, vectors
106
public void addBooleanArg(boolean value)
107     {
108         arguments.addElement(new Boolean JavaDoc(value));
109     }
110
111     public void addBooleanArgToStruct(Hashtable JavaDoc struct, String JavaDoc key,
112             boolean value)
113     {
114         struct.put(key, new Boolean JavaDoc(value));
115     }
116
117     public void addBooleanArgToArray(Vector JavaDoc ary, boolean value)
118     {
119         ary.addElement(new Boolean JavaDoc(value));
120     }
121
122     // add Dates to structs, vectors Date argument in SystemTimeMillis (seems to be the way)
123
public void addDateArg(long dateNo)
124     {
125         arguments.addElement(new Date JavaDoc(dateNo));
126     }
127
128     public void addDateArgToStruct(Hashtable JavaDoc struct, String JavaDoc key, long dateNo)
129     {
130         struct.put(key, new Date JavaDoc(dateNo));
131     }
132
133     public void addDateArgToArray(Vector JavaDoc ary, long dateNo)
134     {
135         ary.addElement(new Date JavaDoc(dateNo));
136     }
137
138     // add String arguments
139
public void addStringArg(String JavaDoc str)
140     {
141         arguments.addElement(str);
142     }
143
144     public void addStringArgToStruct(Hashtable JavaDoc struct, String JavaDoc key, String JavaDoc str)
145     {
146         struct.put(key, str);
147     }
148
149     public void addStringArgToArray(Vector JavaDoc ary, String JavaDoc str)
150     {
151         ary.addElement (str);
152     }
153
154     // add Array arguments
155
public Vector JavaDoc addArrayArg()
156     {
157         Vector JavaDoc v = new Vector JavaDoc();
158         arguments.addElement(v);
159         return v;
160     }
161
162     public Vector JavaDoc addArrayArgToStruct(Hashtable JavaDoc struct, String JavaDoc key)
163     {
164         Vector JavaDoc v = new Vector JavaDoc();
165         struct.put(key, v);
166         return v;
167     }
168
169     public Vector JavaDoc addArrayArgToArray(Vector JavaDoc ary)
170     {
171         Vector JavaDoc v = new Vector JavaDoc();
172         ary.addElement(v);
173         return v;
174     }
175
176     // add Struct arguments
177
public Hashtable JavaDoc addStructArg()
178     {
179         Hashtable JavaDoc ht = new Hashtable JavaDoc();
180         arguments.addElement(ht);
181         return ht;
182     }
183
184     public Hashtable JavaDoc addStructArgToStruct(Hashtable JavaDoc struct, String JavaDoc key)
185     {
186         Hashtable JavaDoc ht = new Hashtable JavaDoc();
187         struct.put(key, ht);
188         return ht;
189     }
190
191     public Hashtable JavaDoc addStructArgToArray(Vector JavaDoc ary)
192     {
193         Hashtable JavaDoc ht = new Hashtable JavaDoc();
194         ary.addElement(ht);
195         return ht;
196     }
197
198     // get the errorMessage, null if none
199
public String JavaDoc getErrorMessage()
200     {
201         return errorMessage;
202     }
203
204     public void reset()
205     {
206         arguments = new Vector JavaDoc();
207     }
208
209     public Object JavaDoc execute(String JavaDoc methodName)
210     {
211         // XmlRpcSupport.setDebug (true);
212
errorMessage = null;
213         showStatus("Connecting to Server...");
214         Object JavaDoc returnValue = null;
215         try
216         {
217             returnValue = execute(methodName, arguments);
218         }
219         catch (Exception JavaDoc e)
220         {
221             errorMessage = e.getMessage();
222             if (errorMessage == null || errorMessage == "")
223             {
224                 errorMessage = e.toString();
225             }
226         }
227         // reset argument array for reuse
228
arguments = new Vector JavaDoc();
229
230         showStatus("");
231         return returnValue;
232     }
233 }
234
Popular Tags