/*
*
* Copyright 2001-2004 The Apache Software Foundation.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package org.apache.beehive.wsm.axis.registration;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.rmi.Remote;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.jws.soap.SOAPBinding;
import javax.xml.namespace.QName;
import org.apache.axis.Constants;
import org.apache.axis.description.ElementDesc;
import org.apache.axis.description.FieldDesc;
import org.apache.axis.description.TypeDesc;
import org.apache.axis.encoding.DeserializerFactory;
import org.apache.axis.encoding.SerializerFactory;
import org.apache.axis.encoding.TypeMapping;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.ArrayDeserializerFactory;
import org.apache.axis.encoding.ser.ArraySerializerFactory;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.apache.axis.types.HexBinary;
import org.apache.axis.utils.BeanPropertyDescriptor;
import org.apache.beehive.wsm.axis.ant.AxisTypeGeneratorTask;
import org.apache.beehive.wsm.axis.databinding.AxisTypeLookup;
import org.apache.beehive.wsm.axis.util.encoding.XmlBeanDeserializerFactory;
import org.apache.beehive.wsm.axis.util.encoding.XmlBeanSerializerFactory;
import org.apache.beehive.wsm.databinding.BindingLookupService;
import org.apache.beehive.wsm.util.InvalidTypeMappingException;
import org.apache.beehive.wsm.registration.TypeRegistrar;
import org.apache.log4j.Logger;
import org.apache.xmlbeans.XmlObject;
public class AxisTypeRegistrar extends TypeRegistrar {
static Logger logger = Logger.getLogger(AxisTypeRegistrar.class);
public AxisTypeRegistrar(TypeMapping tm, BindingLookupService lookupService) {
super(tm, lookupService);
}
/**
* @param cls
* @return boolean
*/
protected boolean isBuiltInType(Class cls) {
return (AxisTypeMappingMetaData.isBuiltInType(cls));
}
/**
* @param cls
* @param q
*/
protected void registerClassAsWithDefaultSearialization(Class cls, QName q, SOAPBinding.Style style,
SOAPBinding.Use use) {
try {
logger.debug("Assigned Default Serialization to class: " + cls.getCanonicalName()
+ " qname:" + q);
TypeDesc td = TypeDesc.getTypeDescForClass(cls); // a class can
// provide its
// own
// descriptors
TypeDesc superTd = null;
BeanPropertyDescriptor[] superPd = null;
boolean shoudRegisterFields = false;
if (null == td) {
shoudRegisterFields = true;
td = new TypeDesc(cls);
Class supa = cls.getSuperclass();
if ((supa != null) && (supa != java.lang.Object.class)
&& (supa != java.lang.Exception.class)
&& (supa != java.lang.Throwable.class)
&& (supa != java.rmi.RemoteException.class)
&& (supa != org.apache.axis.AxisFault.class)) {
registerType(supa, style, use);
superTd = TypeDesc.getTypeDescForClass(supa);
if (superTd != null) {
superPd = superTd.getPropertyDescriptors();
}
}
td.setXmlType(q);
TypeDesc.registerTypeDescForClass(cls, td);
}
mTypeMapping.register(cls, q, new BeanSerializerFactory(cls, q),
/*
* NOTE jcolwell@bea.com 2004-Oct-11 -- should check that the type
* to deserialize has a default contructor but with this setup there
* is no way to know if it is used only in serialization.
*/
new BeanDeserializerFactory(cls, q));
Map serProps = BeanDeserializerFactory.getProperties(cls, null);
for (BeanPropertyDescriptor beanProps : (Collection<BeanPropertyDescriptor>) serProps
.values()) {
Class subType = beanProps.getType();
if (!(subType.isPrimitive()
|| subType.getName().startsWith("java.") || subType
.getName().startsWith("javax."))) {
registerType(subType, style, use);
}
if (shoudRegisterFields) {
String ns = q.getNamespaceURI();
if (superTd != null && superPd != null) {
for (int j = 0; j < superPd.length; j++) {
if (beanProps.getName()
.equals(superPd[j].getName())) {
ns = superTd.getXmlType().getNamespaceURI();
break;
}
}
}
FieldDesc fd = new ElementDesc();
fd.setJavaType(subType);
fd.setFieldName(beanProps.getName());
fd.setXmlName(new QName(ns, beanProps.getName()));
// NOTE jcolwell@bea.com 2004-Oct-28 -- might
// need
// to do more to ensure a useful type QName.
fd.setXmlType(getRegisteredQName(subType));
((ElementDesc) fd).setNillable(true);
// mmerz@apache.com 2005-Mar-09: required since Axis 1.2RC3
// to allow for null values in complex objects; note that
// there is no (JSR-181) annotation that allows configuring
// this value.
td.addFieldDesc(fd);
}
}
} catch (RuntimeException e) {
logger.error("Error in registering class: "
+ cls.getCanonicalName() + " qname:" + q);
e.printStackTrace();
throw e;
}
}
/**
* @param cls
* @param q
*/
protected void registerClassAsXMLBeans(Class cls, QName q) {
logger.debug("Assigned XMLBeans Serialization to class: " + cls.getCanonicalName()
+ " qname:" + q); mTypeMapping.register(cls, q, new XmlBeanSerializerFactory(cls, q),
new XmlBeanDeserializerFactory(cls, q));
}
/**
* @param cls
* @param q
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
protected void registerClassAsDataHandler(Class cls, QName q)
throws ClassNotFoundException, NoSuchMethodException,
InstantiationException, IllegalAccessException,
InvocationTargetException {
logger.debug("Assigned DataHandler to class: " + cls.getCanonicalName()
+ " qname:" + q); /*
* NOTE jcolwell@bea.com 2004-Oct-08 -- doing reflection here in case
* AXIS was built without attachment support.
*/
ClassLoader cl = getClass().getClassLoader();
Class<SerializerFactory> sfClass = (Class<SerializerFactory>) cl
.loadClass("org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory");
Class<DeserializerFactory> dsfClass = (Class<DeserializerFactory>) cl
.loadClass("org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory");
Constructor<SerializerFactory> sfCon = sfClass.getConstructor(
Class.class, QName.class);
Constructor<DeserializerFactory> dsfCon = dsfClass.getConstructor(
Class.class, QName.class);
SerializerFactory sf = sfCon.newInstance(cls, q);
DeserializerFactory dsf = dsfCon.newInstance(cls, q);
mTypeMapping.register(cls, q, sf, dsf);
}
/**
* @param cls
* @param q
*/
protected void registerClassAsSoapEncodedArray(Class cls, QName q) {
logger.debug("Assigned Array Serialization to class: " + cls.getCanonicalName()
+ " qname:" + q + " Array element qname: " + lookupService.class2qname(cls.getComponentType()));
mTypeMapping.register(cls, q, new ArraySerializerFactory(lookupService.class2qname(cls.getComponentType())),
new ArrayDeserializerFactory());
}
/**
* @param cls
* @param q
* @return boolean
*/
protected boolean classIsRegistered(Class cls, QName q) {
return mTypeMapping.isRegistered(cls, q);
}
/**
* @param cls
* @return QName
*/
protected QName getRegisteredQName(Class cls) {
QName currentQName = ((org.apache.axis.encoding.TypeMapping) mTypeMapping)
.getTypeQName(cls);
return currentQName;
}
/**
* @return QName
*/
public QName getVoidType() {
return XMLType.AXIS_VOID;
}
/* (non-Javadoc)
* @see org.apache.beehive.wsm.registration.TypeRegistrar#getBuiltInTypeQname(java.lang.Class)
*/
protected QName getBuiltInTypeQname(Class cls) {
return AxisTypeMappingMetaData.getBuiltInTypeQname(cls);
}
}