KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > encoding > soapenc > BeanSerializer


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "SOAP" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2000, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.soap.encoding.soapenc;
59
60 import java.beans.*;
61 import java.io.*;
62 import java.util.*;
63 import java.lang.reflect.*;
64 import org.w3c.dom.*;
65 import org.apache.soap.util.*;
66 import org.apache.soap.util.xml.*;
67 import org.apache.soap.*;
68 import org.apache.soap.rpc.*;
69
70 /**
71  * A <code>BeanSerializer</code> can be used to serialize and deserialize
72  * <em>JavaBeans</em> using the <code>SOAP-ENC</code> encoding style. The
73  * public properties of the bean become named accessors.
74  *
75  * @author Matthew J. Duftler (duftler@us.ibm.com)
76  */

77 public class BeanSerializer implements Serializer, Deserializer
78 {
79   public void marshall(String JavaDoc inScopeEncStyle, Class JavaDoc javaType, Object JavaDoc src,
80                        Object JavaDoc context, Writer sink, NSStack nsStack,
81                        XMLJavaMappingRegistry xjmr, SOAPContext ctx)
82     throws IllegalArgumentException JavaDoc, IOException
83   {
84     nsStack.pushScope();
85
86     SoapEncUtils.generateStructureHeader(inScopeEncStyle,
87                                          javaType,
88                                          context,
89                                          sink,
90                                          nsStack,
91                                          xjmr);
92
93     sink.write(StringUtils.lineSeparator);
94
95     PropertyDescriptor[] properties = getPropertyDescriptors(javaType);
96
97     for (int i = 0; i < properties.length; i++)
98     {
99       String JavaDoc propName = properties[i].getName();
100       Class JavaDoc propType = properties[i].getPropertyType();
101
102       // Serialize every property except the "class" property.
103
if (!propType.equals(Class JavaDoc.class))
104       {
105         Method propReadMethod = properties[i].getReadMethod();
106
107         // Only serialize readable properties.
108
if (propReadMethod != null)
109         {
110           Object JavaDoc propValue = null;
111
112           // Get the property's value.
113
try
114           {
115             if (src != null)
116             {
117               propValue = propReadMethod.invoke(src, new Object JavaDoc[]{});
118             }
119           }
120           catch (Exception JavaDoc e)
121           {
122             throw new IllegalArgumentException JavaDoc("Unable to retrieve '" +
123                                                propName + "' property " +
124                                                "value: " + e.getMessage() +
125                                                '.');
126           }
127
128           // Serialize the property.
129
Parameter param = new Parameter(propName, propType, propValue, null);
130
131           xjmr.marshall(Constants.NS_URI_SOAP_ENC, Parameter.class, param,
132                         null, sink, nsStack, ctx);
133
134           sink.write(StringUtils.lineSeparator);
135         }
136       }
137     }
138
139     sink.write("</" + context + '>');
140
141     nsStack.popScope();
142   }
143
144   public Bean unmarshall(String JavaDoc inScopeEncStyle, QName elementType, Node src,
145                          XMLJavaMappingRegistry xjmr, SOAPContext ctx)
146     throws IllegalArgumentException JavaDoc
147   {
148     Element root = (Element)src;
149     Element tempEl = DOMUtils.getFirstChildElement(root);
150     Class JavaDoc javaType = xjmr.queryJavaType(elementType, inScopeEncStyle);
151     Object JavaDoc bean = instantiateBean(javaType);
152     PropertyDescriptor[] properties = getPropertyDescriptors(javaType);
153
154     while (tempEl != null)
155     {
156       Bean paramBean = xjmr.unmarshall(inScopeEncStyle,
157                                        RPCConstants.Q_ELEM_PARAMETER,
158                                        tempEl, ctx);
159       Parameter param = (Parameter)paramBean.value;
160       Method propWriteMethod = getWriteMethod(param.getName(),
161                                               properties,
162                                               javaType);
163
164       if (propWriteMethod != null)
165       {
166         // Set the property's value.
167
try
168         {
169           propWriteMethod.invoke(bean, new Object JavaDoc[]{param.getValue()});
170         }
171         catch (Exception JavaDoc e)
172         {
173           throw new IllegalArgumentException JavaDoc("Unable to set '" +
174                                              param.getName() +
175                                              "' property: " +
176                                              e.getMessage() + '.');
177         }
178       }
179
180       tempEl = DOMUtils.getNextSiblingElement(tempEl);
181     }
182
183     return new Bean(javaType, bean);
184   }
185
186   private PropertyDescriptor[] getPropertyDescriptors(Class JavaDoc javaType)
187     throws IllegalArgumentException JavaDoc
188   {
189     BeanInfo beanInfo = null;
190
191     try
192     {
193       beanInfo = Introspector.getBeanInfo(javaType);
194     }
195     catch (IntrospectionException e)
196     {
197     }
198
199     if (beanInfo != null)
200     {
201       PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
202
203       if (properties != null)
204       {
205         return properties;
206       }
207       else
208       {
209         throw new IllegalArgumentException JavaDoc("Unable to retrieve property " +
210                                            "descriptors for '" +
211                                            StringUtils.getClassName(javaType) +
212                                            "'.");
213       }
214     }
215     else
216     {
217       throw new IllegalArgumentException JavaDoc("Unable to retrieve BeanInfo " +
218                                          "for '" + StringUtils.getClassName(
219                                          javaType) + "'.");
220     }
221   }
222
223   protected Method getWriteMethod(String JavaDoc propertyName,
224                                        PropertyDescriptor[] pds,
225                                        Class JavaDoc javaType)
226   {
227     for (int i = 0; i < pds.length; i++)
228     {
229       if (propertyName.equals(pds[i].getName()))
230       {
231         return pds[i].getWriteMethod();
232       }
233     }
234
235     throw new IllegalArgumentException JavaDoc("Unable to retrieve " +
236                                        "PropertyDescriptor for property '" +
237                                        propertyName + "' of class '" +
238                                        javaType + "'.");
239   }
240
241   private Object JavaDoc instantiateBean(Class JavaDoc javaType)
242     throws IllegalArgumentException JavaDoc
243   {
244     try
245     {
246       return javaType.newInstance();
247     }
248     catch (Throwable JavaDoc t)
249     {
250       throw new IllegalArgumentException JavaDoc("Unable to instantiate '" +
251                                          StringUtils.getClassName(javaType) +
252                                          "': " + t.getMessage());
253     }
254   }
255 }
256
Popular Tags