1 55 56 package org.jboss.axis.encoding.ser; 57 58 import org.jboss.axis.Constants; 59 import org.jboss.axis.encoding.Deserializer; 60 import org.jboss.axis.encoding.DeserializerFactory; 61 62 import javax.xml.namespace.QName ; 63 import javax.xml.rpc.JAXRPCException ; 64 import java.lang.reflect.Constructor ; 65 import java.lang.reflect.InvocationTargetException ; 66 import java.lang.reflect.Method ; 67 import java.util.Iterator ; 68 import java.util.Vector ; 69 70 75 public abstract class BaseDeserializerFactory extends BaseFactory 76 implements DeserializerFactory 77 { 78 79 transient static Vector mechanisms = null; 80 81 protected Class deserClass = null; 82 protected QName xmlType = null; 83 protected Class javaType = null; 84 85 transient protected Constructor deserClassConstructor = null; 86 transient protected Method getDeserializer = null; 87 88 93 public BaseDeserializerFactory(Class deserClass) 94 { 95 this.deserClass = deserClass; 96 this.mechanisms = new Vector (); 97 this.mechanisms.add(Constants.AXIS_SAX); 98 } 99 100 public BaseDeserializerFactory(Class deserClass, 101 QName xmlType, 102 Class javaType) 103 { 104 this(deserClass); 105 this.xmlType = xmlType; 106 this.javaType = javaType; 107 } 108 109 public javax.xml.rpc.encoding.Deserializer 110 getDeserializerAs(String mechanismType) 111 throws JAXRPCException 112 { 113 Deserializer deser = null; 114 115 117 deser = getSpecialized(mechanismType); 119 120 if (deser == null) 123 { 124 deser = getGeneralPurpose(mechanismType); 125 } 126 127 try 128 { 129 if (deser == null) 131 { 132 deser = (Deserializer)deserClass.newInstance(); 133 } 134 } 135 catch (Exception e) 136 { 137 throw new JAXRPCException (e); 138 } 139 return deser; 140 } 141 142 146 protected Deserializer getGeneralPurpose(String mechanismType) 147 { 148 if (javaType != null && xmlType != null) 149 { 150 Constructor deserClassConstructor = getDeserClassConstructor(); 151 if (deserClassConstructor != null) 152 { 153 try 154 { 155 return (Deserializer) 156 deserClassConstructor.newInstance(new Object []{javaType, xmlType}); 157 } 158 catch (InstantiationException e) 159 { 160 } 161 catch (IllegalAccessException e) 162 { 163 } 164 catch (InvocationTargetException e) 165 { 166 } 167 } 168 } 169 return null; 170 } 171 172 175 private Constructor getConstructor(Class clazz) 176 { 177 try 178 { 179 return clazz.getConstructor(new Class []{Class .class, QName .class}); 180 } 181 catch (NoSuchMethodException e) 182 { 183 } 184 return null; 185 } 186 187 191 protected Deserializer getSpecialized(String mechanismType) 192 { 193 if (javaType != null && xmlType != null) 194 { 195 Method getDeserializer = getGetDeserializer(); 196 if (getDeserializer != null) 197 { 198 try 199 { 200 return (Deserializer) 201 getDeserializer.invoke(null, 202 new Object []{mechanismType, 203 javaType, 204 xmlType}); 205 } 206 catch (IllegalAccessException e) 207 { 208 } 209 catch (InvocationTargetException e) 210 { 211 } 212 } 213 } 214 return null; 215 } 216 217 222 public Iterator getSupportedMechanismTypes() 223 { 224 return mechanisms.iterator(); 225 } 226 227 238 public static DeserializerFactory createFactory(Class factory, 239 Class javaType, 240 QName xmlType) 241 { 242 243 DeserializerFactory df = null; 244 try 245 { 246 Method method = 247 factory.getMethod("create", 248 new Class []{Class .class, QName .class}); 249 df = (DeserializerFactory) 250 method.invoke(null, 251 new Object []{javaType, xmlType}); 252 } 253 catch (NoSuchMethodException e) 254 { 255 } 256 catch (IllegalAccessException e) 257 { 258 } 259 catch (InvocationTargetException e) 260 { 261 } 262 263 if (df == null) 264 { 265 try 266 { 267 Constructor constructor = 268 factory.getConstructor(new Class []{Class .class, QName .class}); 269 df = (DeserializerFactory) 270 constructor.newInstance(new Object []{javaType, xmlType}); 271 } 272 catch (NoSuchMethodException e) 273 { 274 } 275 catch (InstantiationException e) 276 { 277 } 278 catch (IllegalAccessException e) 279 { 280 } 281 catch (InvocationTargetException e) 282 { 283 } 284 } 285 286 if (df == null) 287 { 288 try 289 { 290 df = (DeserializerFactory)factory.newInstance(); 291 } 292 catch (InstantiationException e) 293 { 294 } 295 catch (IllegalAccessException e) 296 { 297 } 298 } 299 return df; 300 } 301 302 307 protected Constructor getDeserClassConstructor() 308 { 309 if (deserClassConstructor == null) 310 { 311 deserClassConstructor = getConstructor(deserClass); 312 } 313 return deserClassConstructor; 314 } 315 316 321 protected Method getGetDeserializer() 322 { 323 if (getDeserializer == null) 324 { 325 getDeserializer = getMethod(javaType, "getDeserializer"); 326 } 327 return getDeserializer; 328 } 329 330 } 331 | Popular Tags |