ws-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Ws Wiki] Update of "XmlRpcExampleStringArray" by KenTanaka
Date Thu, 09 Jul 2009 23:23:05 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Ws Wiki" for change notification.

The following page has been changed by KenTanaka:
http://wiki.apache.org/ws/XmlRpcExampleStringArray

The comment on the change is:
Added App.java listing

------------------------------------------------------------------------------
  = myXmlRpcServer Code =
  The code provided below is for a Maven 2 project.
  
- == pom.xml Project File ==
+ == pom.xml Project File (myXmlRpcServer) ==
- The '''{{{pom.xml}}}''' file defines how the project is built for maven 2:
+ The '''{{{myXmlRpcServer/pom.xml}}}''' file defines how the project is built for maven 2:
  
  {{{
  <?xml version="1.0" encoding="UTF-8"?>
@@ -249, +249 @@

  = myXmlRpcClient Code =
  The code provided below is for a Maven 2 project.
  
- == pom.xml Project File ==
+ == pom.xml Project File (myXmlRpcClient) ==
- The '''{{{pom.xml}}}''' file defines how the project is built for maven 2:
+ The '''{{{myXmlRpcClient/pom.xml}}}''' file defines how the project is built for maven 2:
  
  {{{
  <?xml version="1.0" encoding="UTF-8"?>
@@ -306, +306 @@

  
  You can see that the '''maven-assembly-plugin''' is used here, and the name of the `<mainClass>`
"App" is configured here. If there is more than one class with an executable "main" method,
this is how it is specified. This creates an executable jar file, with all the dependent libraries
packaged in. This is not space efficient, but saves you having to get all the jar file dependencies
into the Java class path.
  
+ == App.java Listing ==
+ Contents of '''{{{src/main/java/gov/noaa/eds/myXmlRpcClient/App.java}}}''' (Maven expects
the file to be nested down in this directory hierarchy unless you configure it differently).
+ 
+ {{{
+ /*
+  * FILE: App.java
+  */
+ 
+ package gov.noaa.eds.myXmlRpcClient;
+ 
+ import java.lang.reflect.Array;
+ import java.net.MalformedURLException;
+ import java.net.URL;
+ import java.util.ArrayList;
+ import java.util.LinkedList;
+ import java.util.List;
+ import org.apache.xmlrpc.XmlRpcException;
+ import org.apache.xmlrpc.client.XmlRpcClient;
+ import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
+ 
+ 
+ /**
+  * This client will use the myXmlRpcServer.
+  *
+  */
+ public class App {
+ 
+    public static List decodeList(Object element) {
+       if (element == null) {
+          return null;
+       }
+       if (element instanceof List) {
+          return (List) element;
+       }
+       if (element.getClass().isArray()) {
+          int length = Array.getLength(element);
+          LinkedList result = new LinkedList();
+          for (int i = 0; i < length; i++) {
+             result.add (Array.get(element, i));
+          }
+          return result;
+       }
+       return null;
+    }
+ 
+     public static void main(String[] args) {
+         System.out.println("Starting myXmlRpcServer test client");
+ 
+         XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
+         try {
+             config.setServerURL(new URL("http://127.0.0.1:8080/myXmlRpcServer/xmlrpc"));
+         } catch (MalformedURLException ex) {
+             ex.printStackTrace();
+         }
+         XmlRpcClient client = new XmlRpcClient();
+         client.setConfig(config);
+         Object[] params = new Object[] {new String("testDir")};
+         try {
+             Integer fileCount =
+                     (Integer) client.execute("DirList.fileCount",
+                     params);
+             System.out.println("Client received fileCount=" + fileCount.toString());
+         } catch (XmlRpcException ex) {
+             ex.printStackTrace();
+         }
+ 
+         try {
+             /* OPTION A (next 5 lines):
+              * This works, but looks ugly. This is how to get an array without
+              * using a decodeList method.
+              */
+ //            Object[] result = (Object[]) client.execute("DirList.ls", params);
+ //            ArrayList<String> dirListing = new ArrayList<String>();
+ //            for (Object o : result) {
+ //                dirListing.add(o.toString());
+ //            }
+ 
+             /* OPTION B (next 2 lines):
+              * Using decodeList() is cleaner.
+              */
+             ArrayList<String> dirListing =
+                     new ArrayList<String>(decodeList(client.execute("DirList.ls",
params)));
+ 
+             System.out.println("Listing Length=" + dirListing.size());
+             System.out.println("  First 10:");
+             for (int i = 0; i < 10; i++) {
+                 System.out.println("    " + dirListing.get(i));
+             }
+         } catch (XmlRpcException ex) {
+             ex.printStackTrace();
+         }
+     }
+ }
+ }}}
+ 

Mime
View raw message