KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > jndi2 > soap > SoapObjectHelper


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2003 - ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Frederic Maistre (INRIA)
21  * Contributor(s): Nicolas Tachker (ScalAgent DT)
22  */

23 package fr.dyade.aaa.jndi2.soap;
24
25 import javax.naming.NamingException JavaDoc;
26
27 import java.util.Hashtable JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29
30 /**
31  * The <code>SoapObjectHelper</code> class provides static methods for
32  * coding and decoding objects for the SOAP protocol.
33  *
34  * @see SoapObjectItf
35  */

36 public class SoapObjectHelper
37 {
38   /**
39    * Codes an given object into a Hashtable transportable by the SOAP protocol.
40    *
41    * @exception NamingException If the object could not be coded.
42    */

43   public static Hashtable JavaDoc soapCode(Object JavaDoc obj) throws NamingException JavaDoc {
44     if (obj instanceof SoapObjectItf) {
45       Hashtable JavaDoc res = ((SoapObjectItf) obj).code();
46       res.put("className", obj.getClass().getName());
47       return res;
48     } else {
49       throw new NamingException JavaDoc("Object " + obj.getClass().getName()
50                                 + " not codable into a SOAP Hashtable.");
51     }
52   }
53
54   /**
55    * Decodes a given Hashtable into an object.
56    *
57    * @exception NamingException If the Hashtable could not be decoded.
58    */

59   public static Object JavaDoc soapDecode(Hashtable JavaDoc codedObject) throws NamingException JavaDoc
60   {
61     Object JavaDoc object = null;
62
63     try {
64       String JavaDoc className = (String JavaDoc) codedObject.get("className");
65       Class JavaDoc clazz = Class.forName(className);
66       object = clazz.newInstance();
67     } catch (Throwable JavaDoc exc) {
68       throw new NamingException JavaDoc("could not decode Hashtable [" + codedObject
69                                 + "] into an object: " + exc);
70     }
71
72     if (object instanceof SoapObjectItf) {
73       ((SoapObjectItf)object).decode(codedObject);
74       return object;
75     } else {
76       throw new NamingException JavaDoc("hashtable [" + codedObject
77                                 + "] decoded into an unexpected object ["
78                                 + object + "]");
79     }
80   }
81 }
82
Popular Tags