1 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 77 public class BeanSerializer implements Serializer, Deserializer 78 { 79 public void marshall(String inScopeEncStyle, Class javaType, Object src, 80 Object context, Writer sink, NSStack nsStack, 81 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 82 throws IllegalArgumentException , 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 propName = properties[i].getName(); 100 Class propType = properties[i].getPropertyType(); 101 102 if (!propType.equals(Class .class)) 104 { 105 Method propReadMethod = properties[i].getReadMethod(); 106 107 if (propReadMethod != null) 109 { 110 Object propValue = null; 111 112 try 114 { 115 if (src != null) 116 { 117 propValue = propReadMethod.invoke(src, new Object []{}); 118 } 119 } 120 catch (Exception e) 121 { 122 throw new IllegalArgumentException ("Unable to retrieve '" + 123 propName + "' property " + 124 "value: " + e.getMessage() + 125 '.'); 126 } 127 128 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 inScopeEncStyle, QName elementType, Node src, 145 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 146 throws IllegalArgumentException 147 { 148 Element root = (Element)src; 149 Element tempEl = DOMUtils.getFirstChildElement(root); 150 Class javaType = xjmr.queryJavaType(elementType, inScopeEncStyle); 151 Object 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 try 168 { 169 propWriteMethod.invoke(bean, new Object []{param.getValue()}); 170 } 171 catch (Exception e) 172 { 173 throw new IllegalArgumentException ("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 javaType) 187 throws IllegalArgumentException 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 ("Unable to retrieve property " + 210 "descriptors for '" + 211 StringUtils.getClassName(javaType) + 212 "'."); 213 } 214 } 215 else 216 { 217 throw new IllegalArgumentException ("Unable to retrieve BeanInfo " + 218 "for '" + StringUtils.getClassName( 219 javaType) + "'."); 220 } 221 } 222 223 protected Method getWriteMethod(String propertyName, 224 PropertyDescriptor[] pds, 225 Class 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 ("Unable to retrieve " + 236 "PropertyDescriptor for property '" + 237 propertyName + "' of class '" + 238 javaType + "'."); 239 } 240 241 private Object instantiateBean(Class javaType) 242 throws IllegalArgumentException 243 { 244 try 245 { 246 return javaType.newInstance(); 247 } 248 catch (Throwable t) 249 { 250 throw new IllegalArgumentException ("Unable to instantiate '" + 251 StringUtils.getClassName(javaType) + 252 "': " + t.getMessage()); 253 } 254 } 255 } 256 | Popular Tags |