package org.jboss.axis.encoding.ser;
import org.jboss.axis.Constants;
import org.jboss.axis.encoding.Serializer;
import org.jboss.axis.encoding.SerializerFactory;
import org.jboss.axis.utils.Messages;
import javax.xml.namespace.QName;
import javax.xml.rpc.JAXRPCException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Vector;
public abstract class BaseSerializerFactory extends BaseFactory
implements SerializerFactory
{
transient static Vector mechanisms = null;
protected Class serClass = null;
protected QName xmlType = null;
protected Class javaType = null;
transient protected Serializer ser = null;
transient protected Constructor serClassConstructor = null;
transient protected Method getSerializer = null;
public BaseSerializerFactory(Class serClass)
{
this.serClass = serClass;
}
public BaseSerializerFactory(Class serClass,
QName xmlType, Class javaType)
{
this(serClass);
this.xmlType = xmlType;
this.javaType = javaType;
}
public javax.xml.rpc.encoding.Serializer
getSerializerAs(String mechanismType)
throws JAXRPCException
{
synchronized (this)
{
if (ser == null)
{
ser = getSerializerAsInternal(mechanismType);
}
return ser;
}
}
protected Serializer getSerializerAsInternal(String mechanismType)
throws JAXRPCException
{
Serializer serializer = getSpecialized(mechanismType);
if (serializer == null)
{
serializer = getGeneralPurpose(mechanismType);
}
try
{
if (serializer == null)
{
serializer = (Serializer)serClass.newInstance();
}
}
catch (Exception e)
{
throw new JAXRPCException(Messages.getMessage("CantGetSerializer",
serClass.getName()),
e);
}
return serializer;
}
protected Serializer getGeneralPurpose(String mechanismType)
{
if (javaType != null && xmlType != null)
{
Constructor serClassConstructor = getSerClassConstructor();
if (serClassConstructor != null)
{
try
{
return (Serializer)
serClassConstructor.newInstance(new Object[]{javaType, xmlType});
}
catch (InstantiationException e)
{
}
catch (IllegalAccessException e)
{
}
catch (InvocationTargetException e)
{
}
}
}
return null;
}
private Constructor getConstructor(Class clazz)
{
try
{
return clazz.getConstructor(new Class[]{Class.class, QName.class});
}
catch (NoSuchMethodException e)
{
}
return null;
}
protected Serializer getSpecialized(String mechanismType)
{
if (javaType != null && xmlType != null)
{
if (getSerializer != null)
{
try
{
return (Serializer)
getSerializer.invoke(null,
new Object[]{mechanismType,
javaType,
xmlType});
}
catch (IllegalAccessException e)
{
}
catch (InvocationTargetException e)
{
}
}
}
return null;
}
public Iterator getSupportedMechanismTypes()
{
if (mechanisms == null)
{
mechanisms = new Vector();
mechanisms.add(Constants.AXIS_SAX);
}
return mechanisms.iterator();
}
public QName getXMLType()
{
return xmlType;
}
public Class getJavaType()
{
return javaType;
}
public static SerializerFactory createFactory(Class factory,
Class javaType,
QName xmlType)
{
SerializerFactory sf = null;
try
{
Method method =
factory.getMethod("create",
new Class[]{Class.class, QName.class});
sf = (SerializerFactory)
method.invoke(null,
new Object[]{javaType, xmlType});
}
catch (NoSuchMethodException e)
{
}
catch (IllegalAccessException e)
{
}
catch (InvocationTargetException e)
{
}
if (sf == null)
{
try
{
Constructor constructor =
factory.getConstructor(new Class[]{Class.class, QName.class});
sf = (SerializerFactory)
constructor.newInstance(new Object[]{javaType, xmlType});
}
catch (NoSuchMethodException e)
{
}
catch (InstantiationException e)
{
}
catch (IllegalAccessException e)
{
}
catch (InvocationTargetException e)
{
}
}
if (sf == null)
{
try
{
sf = (SerializerFactory)factory.newInstance();
}
catch (InstantiationException e)
{
}
catch (IllegalAccessException e)
{
}
}
return sf;
}
protected Method getGetSerializer()
{
if (getSerializer == null)
{
getSerializer = getMethod(javaType, "getSerializer");
}
return getSerializer;
}
protected Constructor getSerClassConstructor()
{
if (serClassConstructor == null)
{
serClassConstructor = getConstructor(serClass);
}
return serClassConstructor;
}
}