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 Fri, 10 Jul 2009 17:51:02 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 comments to App.java

------------------------------------------------------------------------------
  }}}
  
  == Running ==
- Apache tomcat is fairly easy to install if it's not already on your system, you can usually
install it in your home directory for personal use if you don't have root (administrator)
permissions. You can run the server in your IDE, like Net''''''Beans with Web Application
plugins installed, or in a standalone Tomcat. Note that the port number may vary depending
on which method you use, set the port in the client (App.java) to match. Manually starting
the service should be on port 8080, inside Net''''''Beans the port chosen tends to be 8084.
+ Apache tomcat is fairly easy to install if it's not already on your system, you can usually
install it in your home directory for personal use if you don't have root (administrator)
permissions. You can run the server in your IDE, like Net''''''Beans with Web Application
plugins installed, or in a standalone Tomcat. Note that the port number may vary depending
on which method you use, set the port in the client (App.java) to match. Manually starting
the service should be on port 8080, inside Net''''''Beans the port chosen tends to be 8084
so as not to conflict with any standalone tomcat that might running on the usual 8080.
  
  Use a command like this to start up tomcat manually if not already running:
  {{{
@@ -371, +371 @@

  /*
   * 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;
@@ -386, +384 @@

  
  
  /**
-  * This client will use the myXmlRpcServer.
+  * This client will use the myXmlRpcServer. *
-  *
   */
  public class App {
  
+     /**
+      * Utility method to give an ArrayList when the returned XML-RPC
+      * object is expected to be an array.
+      *
+      * @param element result object coming from a client.execute call.
+      * @return a List or ArrayList. null is returned if the object is
+      * of another type
+      */
-    public static List decodeList(Object element) {
+     public static List decodeList(Object element) {
-       if (element == null) {
+         if (element == null) {
-          return null;
+             return null;
-       }
+         }
-       if (element instanceof List) {
+         if (element instanceof List) {
-          return (List) element;
+             return (List) element;
-       }
+         }
-       if (element.getClass().isArray()) {
+         if (element.getClass().isArray()) {
-          int length = Array.getLength(element);
+             int length = Array.getLength(element);
-          LinkedList result = new LinkedList();
+             ArrayList result = new ArrayList();
-          for (int i = 0; i < length; i++) {
+             for (int i = 0; i < length; i++) {
-             result.add (Array.get(element, i));
+                 result.add(Array.get(element, i));
+             }
+             return result;
-          }
+         }
-          return result;
-       }
-       return null;
+         return null;
-    }
+     }
+ 
  
      public static void main(String[] args) {
+         int port = 8080;
-         System.out.println("Starting myXmlRpcServer test client");
+         System.out.println("Starting myXmlRpcClient");
+         String address = "http://127.0.0.1:" + Integer.toString(port) +
+                 "/myXmlRpcServer/xmlrpc";
+         System.out.println("connecting to " + address);
  
          XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
          try {
-             config.setServerURL(new URL("http://127.0.0.1:8080/myXmlRpcServer/xmlrpc"));
+             config.setServerURL(new URL(address));
          } catch (MalformedURLException ex) {
              ex.printStackTrace();
          }
@@ -425, +435 @@

              Integer fileCount =
                      (Integer) client.execute("DirList.fileCount",
                      params);
-             System.out.println("Client received fileCount=" + fileCount.toString());
+             System.out.println("Client received fileCount=" +
+                     fileCount.toString());
          } catch (XmlRpcException ex) {
              ex.printStackTrace();
          }
@@ -433, +444 @@

          try {
              /* OPTION A (next 5 lines):
               * This works, but looks ugly. This is how to get an array without
-              * using a decodeList method.
+              * using a method like decodeList.
               */
  //            Object[] result = (Object[]) client.execute("DirList.ls", params);
  //            ArrayList<String> dirListing = new ArrayList<String>();
@@ -442, +453 @@

  //            }
  
              /* OPTION B (next 2 lines):
-              * Using decodeList() is cleaner.
+              * This works using decodeList()!
               */
              ArrayList<String> dirListing =
+                     new ArrayList<String>(decodeList(
-                     new ArrayList<String>(decodeList(client.execute("DirList.ls",
params)));
+                     client.execute("DirList.ls", params)));
  
              System.out.println("Listing Length=" + dirListing.size());
              System.out.println("  First 10:");
@@ -457, +469 @@

          }
      }
  }
- }}}
  
+ }}}
+ 

Mime
View raw message