You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@ws.apache.org by Daniel Rall <dl...@finemaltcoding.com> on 2002/08/07 04:45:43 UTC

Re: Calling methods without parameters

The "Response format" section of the XML-RPC spec
<http://xml-rpc.com/spec> dicatates that there must be a return value:

  "The body of the response is a single XML structure, a
  <methodResponse>, which can contain a single <params> which contains
  a single <param> which contains a single <value>.

  The <methodResponse> could also contain a <fault> which contains a
  <value> which is a <struct> containing two elements, one named
  <faultCode>, an <int> and one named <faultString>, a <string>.

  A <methodResponse> can not contain both a <fault> and a <params>."

Since there is no concept of null in XML-RPC, we must return
_something_.  I've added error handling improvments to help people
avoid this problem in the future (shown below as unidiffs).


Index: XmlRpcServer.java
===================================================================
RCS file: /home/cvs/xml-rpc/src/java/org/apache/xmlrpc/XmlRpcServer.java,v
retrieving revision 1.24
diff -u -u -r1.24 XmlRpcServer.java
--- XmlRpcServer.java	20 Mar 2002 15:11:03 -0000	1.24
+++ XmlRpcServer.java	7 Aug 2002 02:38:00 -0000
@@ -547,6 +547,12 @@
             // It is some other exception
             throw new Exception(t.toString());
         }
+        if (returnValue == null && method.getReturnType() == Void.TYPE)
+        {
+            // Not supported by the spec.
+            throw new IllegalArgumentException
+                ("void return types for handler methods not supported");
+        }
         return returnValue;
     }
 }
Index: XmlRpc.java
===================================================================
RCS file: /home/cvs/xml-rpc/src/java/org/apache/xmlrpc/XmlRpc.java,v
retrieving revision 1.23
diff -u -u -r1.23 XmlRpc.java
--- XmlRpc.java	9 Jul 2002 18:29:24 -0000	1.23
+++ XmlRpc.java	7 Aug 2002 01:47:24 -0000
@@ -725,14 +725,18 @@
          * Writes the XML representation of a supported Java object type.
          *
          * @param obj The <code>Object</code> to write.
+         * @exception IOException Problem writing data.
+         * @throws IllegalArgumentException If a <code>null</code>
+         * parameter is passed to this method (not supported by the <a
+         * href="http://xml-rpc.com/spec">XML-RPC specification</a>).
          */
         public void writeObject(Object obj) throws IOException
         {
             startElement("value");
             if (obj == null)
             {
-                throw new RuntimeException(
-                        "null value not supported by XML-RPC");
+                throw new IllegalArgumentException
+                    ("null values not supported by XML-RPC");
             }
             else if (obj instanceof String)
             {


"Dejan Bosanac" <de...@datagate.co.uk> writes:

> Hi,
> yes that was a problem, I figured it out little bit later. Thank you for
> response.
> Why this case is not handled, I know that it maybe is not in standard but it
> can be very useful in the real life? This way I have to put my functions
> return
> some dummy boolean values.
> 
> Thanks  again.
> Regards,
> Dejan
> 
> ----- Original Message -----
> From: "Stan Berka" <sb...@poptal.com>
> To: <rp...@xml.apache.org>
> Sent: Friday, July 26, 2002 5:48 PM
> Subject: Re: Calling methods without parameters
> 
> 
> > Your problem is about the function return value rather then no
> > parameters.  Make the restart() function return something and you should
> be
> > fine be fine.
> >
> >
> > At 03:53 PM 7/26/2002 +0200, you wrote:
> > >Hi,
> > >I got a problem trying to call a Java method without parameters through
> > >XmlRpcClient class.
> > >
> > >Here is a little code example and exception I get.
> > >
> > >Code:
> > >
> > >XmlRpcClient xmlrpc = new XmlRpcClient("http://localhost:25252/RPC2");
> > >xmlrpc.execute("SnmpCollector.restart", new Vector());
> > >
> > >
> > >// this is a function definition
> > >public void restart() {
> > >     // do the work here
> > >}
> > >
> > >Exception:
> > >org.apache.xmlrpc.XmlRpcException: java.lang.RuntimeException: null value
> > >not supported by XML-RPC

Re: Calling methods without parameters

Posted by Dejan Bosanac <de...@datagate.co.uk>.
Yes, it will be definitely more descriptive.
Thanks.


> The "Response format" section of the XML-RPC spec
> <http://xml-rpc.com/spec> dicatates that there must be a return value:
>
>   "The body of the response is a single XML structure, a
>   <methodResponse>, which can contain a single <params> which contains
>   a single <param> which contains a single <value>.
>
>   The <methodResponse> could also contain a <fault> which contains a
>   <value> which is a <struct> containing two elements, one named
>   <faultCode>, an <int> and one named <faultString>, a <string>.
>
>   A <methodResponse> can not contain both a <fault> and a <params>."
>
> Since there is no concept of null in XML-RPC, we must return
> _something_.  I've added error handling improvments to help people
> avoid this problem in the future (shown below as unidiffs).
>
>
> Index: XmlRpcServer.java
> ===================================================================
> RCS file: /home/cvs/xml-rpc/src/java/org/apache/xmlrpc/XmlRpcServer.java,v
> retrieving revision 1.24
> diff -u -u -r1.24 XmlRpcServer.java
> --- XmlRpcServer.java 20 Mar 2002 15:11:03 -0000 1.24
> +++ XmlRpcServer.java 7 Aug 2002 02:38:00 -0000
> @@ -547,6 +547,12 @@
>              // It is some other exception
>              throw new Exception(t.toString());
>          }
> +        if (returnValue == null && method.getReturnType() == Void.TYPE)
> +        {
> +            // Not supported by the spec.
> +            throw new IllegalArgumentException
> +                ("void return types for handler methods not supported");
> +        }
>          return returnValue;
>      }
>  }
> Index: XmlRpc.java
> ===================================================================
> RCS file: /home/cvs/xml-rpc/src/java/org/apache/xmlrpc/XmlRpc.java,v
> retrieving revision 1.23
> diff -u -u -r1.23 XmlRpc.java
> --- XmlRpc.java 9 Jul 2002 18:29:24 -0000 1.23
> +++ XmlRpc.java 7 Aug 2002 01:47:24 -0000
> @@ -725,14 +725,18 @@
>           * Writes the XML representation of a supported Java object type.
>           *
>           * @param obj The <code>Object</code> to write.
> +         * @exception IOException Problem writing data.
> +         * @throws IllegalArgumentException If a <code>null</code>
> +         * parameter is passed to this method (not supported by the <a
> +         * href="http://xml-rpc.com/spec">XML-RPC specification</a>).
>           */
>          public void writeObject(Object obj) throws IOException
>          {
>              startElement("value");
>              if (obj == null)
>              {
> -                throw new RuntimeException(
> -                        "null value not supported by XML-RPC");
> +                throw new IllegalArgumentException
> +                    ("null values not supported by XML-RPC");
>              }
>              else if (obj instanceof String)
>              {
>
>
> "Dejan Bosanac" <de...@datagate.co.uk> writes:
>
> > Hi,
> > yes that was a problem, I figured it out little bit later. Thank you for
> > response.
> > Why this case is not handled, I know that it maybe is not in standard
but it
> > can be very useful in the real life? This way I have to put my functions
> > return
> > some dummy boolean values.
> >
> > Thanks  again.
> > Regards,
> > Dejan
> >
> > ----- Original Message -----
> > From: "Stan Berka" <sb...@poptal.com>
> > To: <rp...@xml.apache.org>
> > Sent: Friday, July 26, 2002 5:48 PM
> > Subject: Re: Calling methods without parameters
> >
> >
> > > Your problem is about the function return value rather then no
> > > parameters.  Make the restart() function return something and you
should
> > be
> > > fine be fine.
> > >
> > >
> > > At 03:53 PM 7/26/2002 +0200, you wrote:
> > > >Hi,
> > > >I got a problem trying to call a Java method without parameters
through
> > > >XmlRpcClient class.
> > > >
> > > >Here is a little code example and exception I get.
> > > >
> > > >Code:
> > > >
> > > >XmlRpcClient xmlrpc = new
XmlRpcClient("http://localhost:25252/RPC2");
> > > >xmlrpc.execute("SnmpCollector.restart", new Vector());
> > > >
> > > >
> > > >// this is a function definition
> > > >public void restart() {
> > > >     // do the work here
> > > >}
> > > >
> > > >Exception:
> > > >org.apache.xmlrpc.XmlRpcException: java.lang.RuntimeException: null
value
> > > >not supported by XML-RPC


Re: Calling methods without parameters

Posted by Dejan Bosanac <de...@datagate.co.uk>.
Yes, it will be definitely more descriptive.
Thanks.


> The "Response format" section of the XML-RPC spec
> <http://xml-rpc.com/spec> dicatates that there must be a return value:
>
>   "The body of the response is a single XML structure, a
>   <methodResponse>, which can contain a single <params> which contains
>   a single <param> which contains a single <value>.
>
>   The <methodResponse> could also contain a <fault> which contains a
>   <value> which is a <struct> containing two elements, one named
>   <faultCode>, an <int> and one named <faultString>, a <string>.
>
>   A <methodResponse> can not contain both a <fault> and a <params>."
>
> Since there is no concept of null in XML-RPC, we must return
> _something_.  I've added error handling improvments to help people
> avoid this problem in the future (shown below as unidiffs).
>
>
> Index: XmlRpcServer.java
> ===================================================================
> RCS file: /home/cvs/xml-rpc/src/java/org/apache/xmlrpc/XmlRpcServer.java,v
> retrieving revision 1.24
> diff -u -u -r1.24 XmlRpcServer.java
> --- XmlRpcServer.java 20 Mar 2002 15:11:03 -0000 1.24
> +++ XmlRpcServer.java 7 Aug 2002 02:38:00 -0000
> @@ -547,6 +547,12 @@
>              // It is some other exception
>              throw new Exception(t.toString());
>          }
> +        if (returnValue == null && method.getReturnType() == Void.TYPE)
> +        {
> +            // Not supported by the spec.
> +            throw new IllegalArgumentException
> +                ("void return types for handler methods not supported");
> +        }
>          return returnValue;
>      }
>  }
> Index: XmlRpc.java
> ===================================================================
> RCS file: /home/cvs/xml-rpc/src/java/org/apache/xmlrpc/XmlRpc.java,v
> retrieving revision 1.23
> diff -u -u -r1.23 XmlRpc.java
> --- XmlRpc.java 9 Jul 2002 18:29:24 -0000 1.23
> +++ XmlRpc.java 7 Aug 2002 01:47:24 -0000
> @@ -725,14 +725,18 @@
>           * Writes the XML representation of a supported Java object type.
>           *
>           * @param obj The <code>Object</code> to write.
> +         * @exception IOException Problem writing data.
> +         * @throws IllegalArgumentException If a <code>null</code>
> +         * parameter is passed to this method (not supported by the <a
> +         * href="http://xml-rpc.com/spec">XML-RPC specification</a>).
>           */
>          public void writeObject(Object obj) throws IOException
>          {
>              startElement("value");
>              if (obj == null)
>              {
> -                throw new RuntimeException(
> -                        "null value not supported by XML-RPC");
> +                throw new IllegalArgumentException
> +                    ("null values not supported by XML-RPC");
>              }
>              else if (obj instanceof String)
>              {
>
>
> "Dejan Bosanac" <de...@datagate.co.uk> writes:
>
> > Hi,
> > yes that was a problem, I figured it out little bit later. Thank you for
> > response.
> > Why this case is not handled, I know that it maybe is not in standard
but it
> > can be very useful in the real life? This way I have to put my functions
> > return
> > some dummy boolean values.
> >
> > Thanks  again.
> > Regards,
> > Dejan
> >
> > ----- Original Message -----
> > From: "Stan Berka" <sb...@poptal.com>
> > To: <rp...@xml.apache.org>
> > Sent: Friday, July 26, 2002 5:48 PM
> > Subject: Re: Calling methods without parameters
> >
> >
> > > Your problem is about the function return value rather then no
> > > parameters.  Make the restart() function return something and you
should
> > be
> > > fine be fine.
> > >
> > >
> > > At 03:53 PM 7/26/2002 +0200, you wrote:
> > > >Hi,
> > > >I got a problem trying to call a Java method without parameters
through
> > > >XmlRpcClient class.
> > > >
> > > >Here is a little code example and exception I get.
> > > >
> > > >Code:
> > > >
> > > >XmlRpcClient xmlrpc = new
XmlRpcClient("http://localhost:25252/RPC2");
> > > >xmlrpc.execute("SnmpCollector.restart", new Vector());
> > > >
> > > >
> > > >// this is a function definition
> > > >public void restart() {
> > > >     // do the work here
> > > >}
> > > >
> > > >Exception:
> > > >org.apache.xmlrpc.XmlRpcException: java.lang.RuntimeException: null
value
> > > >not supported by XML-RPC


Re: Calling methods without parameters

Posted by Daniel Rall <dl...@finemaltcoding.com>.
"Chris Bailey" <ch...@codeintensity.com> writes:

> I'd noticed this in my recent work with the code, but was wondering what
> folks typically do when they have a method that they'd normally define to
> return void?  I suppose it really doesn't matter what you return in that
> case, but are people typically just returning something like "new Object()",
> or I've been using "Boolean.TRUE" as a return value.  Is there a "best
> practice" for this?

Using Boolean.TRUE sounds like a very reasonable solution, as it
avoids the unnecessary object instantion caused by new Object().

Re: Calling methods without parameters

Posted by Daniel Rall <dl...@finemaltcoding.com>.
"Chris Bailey" <ch...@codeintensity.com> writes:

> I'd noticed this in my recent work with the code, but was wondering what
> folks typically do when they have a method that they'd normally define to
> return void?  I suppose it really doesn't matter what you return in that
> case, but are people typically just returning something like "new Object()",
> or I've been using "Boolean.TRUE" as a return value.  Is there a "best
> practice" for this?

Using Boolean.TRUE sounds like a very reasonable solution, as it
avoids the unnecessary object instantion caused by new Object().

RE: Calling methods without parameters

Posted by Chris Bailey <ch...@codeintensity.com>.
I'd noticed this in my recent work with the code, but was wondering what
folks typically do when they have a method that they'd normally define to
return void?  I suppose it really doesn't matter what you return in that
case, but are people typically just returning something like "new Object()",
or I've been using "Boolean.TRUE" as a return value.  Is there a "best
practice" for this?

-----Original Message-----
From: dlr@finemaltcoding.com [mailto:dlr@finemaltcoding.com]
Sent: Tuesday, August 06, 2002 7:46 PM
To: Dejan Bosanac; rpc-user@xml.apache.org
Subject: Re: Calling methods without parameters


The "Response format" section of the XML-RPC spec
<http://xml-rpc.com/spec> dicatates that there must be a return value:

  "The body of the response is a single XML structure, a
  <methodResponse>, which can contain a single <params> which contains
  a single <param> which contains a single <value>.

  The <methodResponse> could also contain a <fault> which contains a
  <value> which is a <struct> containing two elements, one named
  <faultCode>, an <int> and one named <faultString>, a <string>.

  A <methodResponse> can not contain both a <fault> and a <params>."

Since there is no concept of null in XML-RPC, we must return
_something_.  I've added error handling improvments to help people
avoid this problem in the future (shown below as unidiffs).


Index: XmlRpcServer.java
===================================================================
RCS file: /home/cvs/xml-rpc/src/java/org/apache/xmlrpc/XmlRpcServer.java,v
retrieving revision 1.24
diff -u -u -r1.24 XmlRpcServer.java
--- XmlRpcServer.java	20 Mar 2002 15:11:03 -0000	1.24
+++ XmlRpcServer.java	7 Aug 2002 02:38:00 -0000
@@ -547,6 +547,12 @@
             // It is some other exception
             throw new Exception(t.toString());
         }
+        if (returnValue == null && method.getReturnType() == Void.TYPE)
+        {
+            // Not supported by the spec.
+            throw new IllegalArgumentException
+                ("void return types for handler methods not supported");
+        }
         return returnValue;
     }
 }
Index: XmlRpc.java
===================================================================
RCS file: /home/cvs/xml-rpc/src/java/org/apache/xmlrpc/XmlRpc.java,v
retrieving revision 1.23
diff -u -u -r1.23 XmlRpc.java
--- XmlRpc.java	9 Jul 2002 18:29:24 -0000	1.23
+++ XmlRpc.java	7 Aug 2002 01:47:24 -0000
@@ -725,14 +725,18 @@
          * Writes the XML representation of a supported Java object type.
          *
          * @param obj The <code>Object</code> to write.
+         * @exception IOException Problem writing data.
+         * @throws IllegalArgumentException If a <code>null</code>
+         * parameter is passed to this method (not supported by the <a
+         * href="http://xml-rpc.com/spec">XML-RPC specification</a>).
          */
         public void writeObject(Object obj) throws IOException
         {
             startElement("value");
             if (obj == null)
             {
-                throw new RuntimeException(
-                        "null value not supported by XML-RPC");
+                throw new IllegalArgumentException
+                    ("null values not supported by XML-RPC");
             }
             else if (obj instanceof String)
             {


"Dejan Bosanac" <de...@datagate.co.uk> writes:

> Hi,
> yes that was a problem, I figured it out little bit later. Thank you for
> response.
> Why this case is not handled, I know that it maybe is not in standard but
it
> can be very useful in the real life? This way I have to put my functions
> return
> some dummy boolean values.
>
> Thanks  again.
> Regards,
> Dejan
>
> ----- Original Message -----
> From: "Stan Berka" <sb...@poptal.com>
> To: <rp...@xml.apache.org>
> Sent: Friday, July 26, 2002 5:48 PM
> Subject: Re: Calling methods without parameters
>
>
> > Your problem is about the function return value rather then no
> > parameters.  Make the restart() function return something and you should
> be
> > fine be fine.
> >
> >
> > At 03:53 PM 7/26/2002 +0200, you wrote:
> > >Hi,
> > >I got a problem trying to call a Java method without parameters through
> > >XmlRpcClient class.
> > >
> > >Here is a little code example and exception I get.
> > >
> > >Code:
> > >
> > >XmlRpcClient xmlrpc = new XmlRpcClient("http://localhost:25252/RPC2");
> > >xmlrpc.execute("SnmpCollector.restart", new Vector());
> > >
> > >
> > >// this is a function definition
> > >public void restart() {
> > >     // do the work here
> > >}
> > >
> > >Exception:
> > >org.apache.xmlrpc.XmlRpcException: java.lang.RuntimeException: null
value
> > >not supported by XML-RPC


RE: Calling methods without parameters

Posted by Chris Bailey <ch...@codeintensity.com>.
I'd noticed this in my recent work with the code, but was wondering what
folks typically do when they have a method that they'd normally define to
return void?  I suppose it really doesn't matter what you return in that
case, but are people typically just returning something like "new Object()",
or I've been using "Boolean.TRUE" as a return value.  Is there a "best
practice" for this?

-----Original Message-----
From: dlr@finemaltcoding.com [mailto:dlr@finemaltcoding.com]
Sent: Tuesday, August 06, 2002 7:46 PM
To: Dejan Bosanac; rpc-user@xml.apache.org
Subject: Re: Calling methods without parameters


The "Response format" section of the XML-RPC spec
<http://xml-rpc.com/spec> dicatates that there must be a return value:

  "The body of the response is a single XML structure, a
  <methodResponse>, which can contain a single <params> which contains
  a single <param> which contains a single <value>.

  The <methodResponse> could also contain a <fault> which contains a
  <value> which is a <struct> containing two elements, one named
  <faultCode>, an <int> and one named <faultString>, a <string>.

  A <methodResponse> can not contain both a <fault> and a <params>."

Since there is no concept of null in XML-RPC, we must return
_something_.  I've added error handling improvments to help people
avoid this problem in the future (shown below as unidiffs).


Index: XmlRpcServer.java
===================================================================
RCS file: /home/cvs/xml-rpc/src/java/org/apache/xmlrpc/XmlRpcServer.java,v
retrieving revision 1.24
diff -u -u -r1.24 XmlRpcServer.java
--- XmlRpcServer.java	20 Mar 2002 15:11:03 -0000	1.24
+++ XmlRpcServer.java	7 Aug 2002 02:38:00 -0000
@@ -547,6 +547,12 @@
             // It is some other exception
             throw new Exception(t.toString());
         }
+        if (returnValue == null && method.getReturnType() == Void.TYPE)
+        {
+            // Not supported by the spec.
+            throw new IllegalArgumentException
+                ("void return types for handler methods not supported");
+        }
         return returnValue;
     }
 }
Index: XmlRpc.java
===================================================================
RCS file: /home/cvs/xml-rpc/src/java/org/apache/xmlrpc/XmlRpc.java,v
retrieving revision 1.23
diff -u -u -r1.23 XmlRpc.java
--- XmlRpc.java	9 Jul 2002 18:29:24 -0000	1.23
+++ XmlRpc.java	7 Aug 2002 01:47:24 -0000
@@ -725,14 +725,18 @@
          * Writes the XML representation of a supported Java object type.
          *
          * @param obj The <code>Object</code> to write.
+         * @exception IOException Problem writing data.
+         * @throws IllegalArgumentException If a <code>null</code>
+         * parameter is passed to this method (not supported by the <a
+         * href="http://xml-rpc.com/spec">XML-RPC specification</a>).
          */
         public void writeObject(Object obj) throws IOException
         {
             startElement("value");
             if (obj == null)
             {
-                throw new RuntimeException(
-                        "null value not supported by XML-RPC");
+                throw new IllegalArgumentException
+                    ("null values not supported by XML-RPC");
             }
             else if (obj instanceof String)
             {


"Dejan Bosanac" <de...@datagate.co.uk> writes:

> Hi,
> yes that was a problem, I figured it out little bit later. Thank you for
> response.
> Why this case is not handled, I know that it maybe is not in standard but
it
> can be very useful in the real life? This way I have to put my functions
> return
> some dummy boolean values.
>
> Thanks  again.
> Regards,
> Dejan
>
> ----- Original Message -----
> From: "Stan Berka" <sb...@poptal.com>
> To: <rp...@xml.apache.org>
> Sent: Friday, July 26, 2002 5:48 PM
> Subject: Re: Calling methods without parameters
>
>
> > Your problem is about the function return value rather then no
> > parameters.  Make the restart() function return something and you should
> be
> > fine be fine.
> >
> >
> > At 03:53 PM 7/26/2002 +0200, you wrote:
> > >Hi,
> > >I got a problem trying to call a Java method without parameters through
> > >XmlRpcClient class.
> > >
> > >Here is a little code example and exception I get.
> > >
> > >Code:
> > >
> > >XmlRpcClient xmlrpc = new XmlRpcClient("http://localhost:25252/RPC2");
> > >xmlrpc.execute("SnmpCollector.restart", new Vector());
> > >
> > >
> > >// this is a function definition
> > >public void restart() {
> > >     // do the work here
> > >}
> > >
> > >Exception:
> > >org.apache.xmlrpc.XmlRpcException: java.lang.RuntimeException: null
value
> > >not supported by XML-RPC