1 16 17 package org.apache.axis.encoding.ser; 18 19 import org.apache.axis.Constants; 20 import org.apache.axis.components.logger.LogFactory; 21 import org.apache.axis.encoding.Deserializer; 22 import org.apache.axis.encoding.DeserializerFactory; 23 import org.apache.axis.i18n.Messages; 24 import org.apache.commons.logging.Log; 25 26 import javax.xml.namespace.QName ; 27 import javax.xml.rpc.JAXRPCException ; 28 import java.lang.reflect.Constructor ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.lang.reflect.Method ; 31 import java.util.Iterator ; 32 import java.util.Vector ; 33 34 39 public abstract class BaseDeserializerFactory extends BaseFactory 40 implements DeserializerFactory { 41 42 protected static Log log = 43 LogFactory.getLog(BaseDeserializerFactory.class.getName()); 44 45 transient static Vector mechanisms = null; 46 47 protected Class deserClass = null; 48 protected QName xmlType = null; 49 protected Class javaType = null; 50 51 transient protected Constructor deserClassConstructor = null; 52 transient protected Method getDeserializer = null; 53 54 58 public BaseDeserializerFactory(Class deserClass) { 59 if (!Deserializer.class.isAssignableFrom(deserClass)) { 60 throw new ClassCastException ( 61 Messages.getMessage("BadImplementation00", 62 deserClass.getName(), 63 Deserializer.class.getName())); 64 } 65 this.deserClass = deserClass; 66 } 67 68 public BaseDeserializerFactory(Class deserClass, 69 QName xmlType, 70 Class javaType) { 71 this(deserClass); 72 this.xmlType = xmlType; 73 this.javaType = javaType; 74 } 75 76 public javax.xml.rpc.encoding.Deserializer 77 getDeserializerAs(String mechanismType) 78 throws JAXRPCException { 79 Deserializer deser = null; 80 81 83 deser = getSpecialized(mechanismType); 85 86 if (deser == null) { 89 deser = getGeneralPurpose(mechanismType); 90 } 91 92 try { 93 if (deser == null) { 95 deser = (Deserializer) deserClass.newInstance(); 96 } 97 } catch (Exception e) { 98 throw new JAXRPCException (e); 99 } 100 return deser; 101 } 102 103 107 protected Deserializer getGeneralPurpose(String mechanismType) { 108 if (javaType != null && xmlType != null) { 109 Constructor deserClassConstructor = getDeserClassConstructor(); 110 if (deserClassConstructor != null) { 111 try { 112 return (Deserializer) 113 deserClassConstructor.newInstance( 114 new Object [] {javaType, xmlType}); 115 } catch (InstantiationException e) { 116 if(log.isDebugEnabled()) { 117 log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e); 118 } 119 } catch (IllegalAccessException e) { 120 if(log.isDebugEnabled()) { 121 log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e); 122 } 123 } catch (InvocationTargetException e) { 124 if(log.isDebugEnabled()) { 125 log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e); 126 } 127 } 128 } 129 } 130 return null; 131 } 132 133 private static final Class [] CLASS_QNAME_CLASS = new Class [] {Class .class, QName .class}; 134 135 138 private Constructor getConstructor(Class clazz) { 139 try { 140 return clazz.getConstructor(CLASS_QNAME_CLASS); 141 } catch (NoSuchMethodException e) {} 142 return null; 143 } 144 145 149 protected Deserializer getSpecialized(String mechanismType) { 150 if (javaType != null && xmlType != null) { 151 Method getDeserializer = getGetDeserializer(); 152 if (getDeserializer != null) { 153 try { 154 return (Deserializer) 155 getDeserializer.invoke( 156 null, 157 new Object [] {mechanismType, 158 javaType, 159 xmlType}); 160 } catch (IllegalAccessException e) { 161 if(log.isDebugEnabled()) { 162 log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e); 163 } 164 } catch (InvocationTargetException e) { 165 if(log.isDebugEnabled()) { 166 log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e); 167 } 168 } 169 } 170 } 171 return null; 172 } 173 174 179 public Iterator getSupportedMechanismTypes() { 180 if (mechanisms == null) { 181 mechanisms = new Vector (1); 182 mechanisms.add(Constants.AXIS_SAX); 183 } 184 return mechanisms.iterator(); 185 } 186 187 197 public static DeserializerFactory createFactory(Class factory, 198 Class javaType, 199 QName xmlType) { 200 if (factory == null) { 201 return null; 202 } 203 204 try { 205 if (factory == BeanDeserializerFactory.class) { 206 return new BeanDeserializerFactory(javaType, xmlType); 207 } else if (factory == SimpleDeserializerFactory.class) { 208 return new SimpleDeserializerFactory(javaType, xmlType); 209 } else if (factory == EnumDeserializerFactory.class) { 210 return new EnumDeserializerFactory(javaType, xmlType); 211 } else if (factory == ElementDeserializerFactory.class) { 212 return new ElementDeserializerFactory(); 213 } else if (factory == SimpleListDeserializerFactory.class) { 214 return new SimpleListDeserializerFactory(javaType, xmlType); 215 } 216 } catch (Exception e) { 217 if (log.isDebugEnabled()) { 218 log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e); 219 } 220 return null; 221 } 222 223 DeserializerFactory df = null; 224 try { 225 Method method = 226 factory.getMethod("create", CLASS_QNAME_CLASS); 227 df = (DeserializerFactory) 228 method.invoke(null, 229 new Object [] {javaType, xmlType}); 230 } catch (NoSuchMethodException e) { 231 if(log.isDebugEnabled()) { 232 log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e); 233 } 234 } catch (IllegalAccessException e) { 235 if(log.isDebugEnabled()) { 236 log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e); 237 } 238 } catch (InvocationTargetException e) { 239 if(log.isDebugEnabled()) { 240 log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e); 241 } 242 } 243 244 if (df == null) { 245 try { 246 Constructor constructor = 247 factory.getConstructor(CLASS_QNAME_CLASS); 248 df = (DeserializerFactory) 249 constructor.newInstance( 250 new Object [] {javaType, xmlType}); 251 } catch (NoSuchMethodException e) { 252 if(log.isDebugEnabled()) { 253 log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e); 254 } 255 } catch (InstantiationException e) { 256 if(log.isDebugEnabled()) { 257 log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e); 258 } 259 } catch (IllegalAccessException e) { 260 if(log.isDebugEnabled()) { 261 log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e); 262 } 263 } catch (InvocationTargetException e) { 264 if(log.isDebugEnabled()) { 265 log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e); 266 } 267 } 268 } 269 270 if (df == null) { 271 try { 272 df = (DeserializerFactory) factory.newInstance(); 273 } catch (InstantiationException e) { 274 } catch (IllegalAccessException e) {} 275 } 276 return df; 277 } 278 282 protected Constructor getDeserClassConstructor() { 283 if (deserClassConstructor == null) { 284 deserClassConstructor = getConstructor(deserClass); 285 } 286 return deserClassConstructor; 287 } 288 289 293 protected Method getGetDeserializer() { 294 if (getDeserializer == null) { 295 getDeserializer = getMethod(javaType,"getDeserializer"); 296 } 297 return getDeserializer; 298 } 299 300 } 301 | Popular Tags |