1 55 56 package org.jboss.axis.encoding.ser; 57 58 import org.jboss.axis.Constants; 59 import org.jboss.axis.encoding.Serializer; 60 import org.jboss.axis.encoding.SerializerFactory; 61 import org.jboss.axis.utils.Messages; 62 63 import javax.xml.namespace.QName ; 64 import javax.xml.rpc.JAXRPCException ; 65 import java.lang.reflect.Constructor ; 66 import java.lang.reflect.InvocationTargetException ; 67 import java.lang.reflect.Method ; 68 import java.util.Iterator ; 69 import java.util.Vector ; 70 71 76 public abstract class BaseSerializerFactory extends BaseFactory 77 implements SerializerFactory 78 { 79 80 transient static Vector mechanisms = null; 81 82 protected Class serClass = null; 83 protected QName xmlType = null; 84 protected Class javaType = null; 85 86 transient protected Serializer ser = null; 87 transient protected Constructor serClassConstructor = null; 88 transient protected Method getSerializer = null; 89 90 96 public BaseSerializerFactory(Class serClass) 97 { 98 this.serClass = serClass; 99 } 100 101 public BaseSerializerFactory(Class serClass, 102 QName xmlType, Class javaType) 103 { 104 this(serClass); 105 this.xmlType = xmlType; 106 this.javaType = javaType; 107 } 108 109 public javax.xml.rpc.encoding.Serializer 110 getSerializerAs(String mechanismType) 111 throws JAXRPCException 112 { 113 synchronized (this) 114 { 115 if (ser == null) 116 { 117 ser = getSerializerAsInternal(mechanismType); 118 } 119 return ser; 120 } 121 } 122 123 protected Serializer getSerializerAsInternal(String mechanismType) 124 throws JAXRPCException 125 { 126 Serializer serializer = getSpecialized(mechanismType); 128 129 if (serializer == null) 132 { 133 serializer = getGeneralPurpose(mechanismType); 134 } 135 136 try 137 { 138 if (serializer == null) 140 { 141 serializer = (Serializer)serClass.newInstance(); 142 } 143 } 144 catch (Exception e) 145 { 146 throw new JAXRPCException (Messages.getMessage("CantGetSerializer", 147 serClass.getName()), 148 e); 149 } 150 return serializer; 151 } 152 153 157 protected Serializer getGeneralPurpose(String mechanismType) 158 { 159 if (javaType != null && xmlType != null) 160 { 161 Constructor serClassConstructor = getSerClassConstructor(); 162 if (serClassConstructor != null) 163 { 164 try 165 { 166 return (Serializer) 167 serClassConstructor.newInstance(new Object []{javaType, xmlType}); 168 } 169 catch (InstantiationException e) 170 { 171 } 172 catch (IllegalAccessException e) 173 { 174 } 175 catch (InvocationTargetException e) 176 { 177 } 178 } 179 } 180 return null; 181 } 182 183 186 private Constructor getConstructor(Class clazz) 187 { 188 try 189 { 190 return clazz.getConstructor(new Class []{Class .class, QName .class}); 191 } 192 catch (NoSuchMethodException e) 193 { 194 } 195 return null; 196 } 197 198 202 protected Serializer getSpecialized(String mechanismType) 203 { 204 if (javaType != null && xmlType != null) 205 { 206 if (getSerializer != null) 207 { 208 try 209 { 210 return (Serializer) 211 getSerializer.invoke(null, 212 new Object []{mechanismType, 213 javaType, 214 xmlType}); 215 } 216 catch (IllegalAccessException e) 217 { 218 } 219 catch (InvocationTargetException e) 220 { 221 } 222 } 223 } 224 return null; 225 } 226 227 234 public Iterator getSupportedMechanismTypes() 235 { 236 if (mechanisms == null) 237 { 238 mechanisms = new Vector (); 239 mechanisms.add(Constants.AXIS_SAX); 240 } 241 return mechanisms.iterator(); 242 } 243 244 249 public QName getXMLType() 250 { 251 return xmlType; 252 } 253 254 259 public Class getJavaType() 260 { 261 return javaType; 262 } 263 264 275 public static SerializerFactory createFactory(Class factory, 276 Class javaType, 277 QName xmlType) 278 { 279 280 SerializerFactory sf = null; 281 try 282 { 283 Method method = 284 factory.getMethod("create", 285 new Class []{Class .class, QName .class}); 286 sf = (SerializerFactory) 287 method.invoke(null, 288 new Object []{javaType, xmlType}); 289 } 290 catch (NoSuchMethodException e) 291 { 292 } 293 catch (IllegalAccessException e) 294 { 295 } 296 catch (InvocationTargetException e) 297 { 298 } 299 300 if (sf == null) 301 { 302 try 303 { 304 Constructor constructor = 305 factory.getConstructor(new Class []{Class .class, QName .class}); 306 sf = (SerializerFactory) 307 constructor.newInstance(new Object []{javaType, xmlType}); 308 } 309 catch (NoSuchMethodException e) 310 { 311 } 312 catch (InstantiationException e) 313 { 314 } 315 catch (IllegalAccessException e) 316 { 317 } 318 catch (InvocationTargetException e) 319 { 320 } 321 } 322 323 if (sf == null) 324 { 325 try 326 { 327 sf = (SerializerFactory)factory.newInstance(); 328 } 329 catch (InstantiationException e) 330 { 331 } 332 catch (IllegalAccessException e) 333 { 334 } 335 } 336 return sf; 337 } 338 339 344 protected Method getGetSerializer() 345 { 346 if (getSerializer == null) 347 { 348 getSerializer = getMethod(javaType, "getSerializer"); 349 } 350 return getSerializer; 351 } 352 353 358 protected Constructor getSerClassConstructor() 359 { 360 if (serClassConstructor == null) 361 { 362 serClassConstructor = getConstructor(serClass); 363 } 364 return serClassConstructor; 365 } 366 367 } 368 | Popular Tags |