📄 beantype.java
字号:
package org.codehaus.xfire.aegis.type.basic;import java.beans.PropertyDescriptor;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.lang.reflect.Proxy;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import javax.xml.namespace.QName;import org.codehaus.xfire.MessageContext;import org.codehaus.xfire.XFireRuntimeException;import org.codehaus.xfire.aegis.AegisBindingProvider;import org.codehaus.xfire.aegis.MessageReader;import org.codehaus.xfire.aegis.MessageWriter;import org.codehaus.xfire.aegis.type.Type;import org.codehaus.xfire.aegis.type.TypeMapping;import org.codehaus.xfire.fault.XFireFault;import org.codehaus.xfire.soap.SoapConstants;import org.codehaus.xfire.util.ClassLoaderUtils;import org.codehaus.xfire.util.NamespaceHelper;import org.jdom.Attribute;import org.jdom.Element;/** * Serializes JavaBeans. * * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a> * @author <a href="mailto:jack.xu.hong@gmail.com">Jack Hong</a> */public class BeanType extends Type{ private BeanTypeInfo _info; private boolean isInterface = false; private boolean isException = false; public BeanType() { } public BeanType(BeanTypeInfo info) { this._info = info; this.setTypeClass(info.getTypeClass()); } /* * (non-Javadoc) * * @see org.codehaus.xfire.aegis.type.Type#readObject(org.codehaus.xfire.aegis.MessageReader, * org.codehaus.xfire.MessageContext) */ public Object readObject(MessageReader reader, MessageContext context) throws XFireFault { BeanTypeInfo info = getTypeInfo(); try { Class clazz = getTypeClass(); Object object = null; InterfaceInvocationHandler delegate = null; boolean isProxy = false; if (isInterface) { String impl = null; if (context.getService() != null) { impl = (String) context.getService().getProperty(clazz.getName() + ".implementation"); } if (impl == null) { delegate = new InterfaceInvocationHandler(); object = Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] { clazz }, delegate); isProxy = true; } else { try { clazz = ClassLoaderUtils.loadClass(impl, getClass()); object = clazz.newInstance(); } catch (ClassNotFoundException e) { throw new XFireRuntimeException("Could not find implementation class " + impl + " for class " + clazz.getName()); } } } else if (isException) { object = createFromFault(context); } else { object = clazz.newInstance(); } // Read attributes while (reader.hasMoreAttributeReaders()) { MessageReader childReader = reader.getNextAttributeReader(); QName name = childReader.getName(); Type type = info.getType(name); if (type != null) { Object writeObj = type.readObject(childReader, context); if (isProxy) { delegate.writeProperty(name.getLocalPart(), writeObj); } else { writeProperty(name, object, writeObj, clazz, info); } } } // Read child elements while (reader.hasMoreElementReaders()) { MessageReader childReader = reader.getNextElementReader(); QName name = childReader.getName(); BeanType parent = getBeanTypeWithProperty(name); Type defaultType = null; if (parent != null) { info = parent.getTypeInfo(); defaultType = info.getType(name); } else { defaultType = null; } Type type = AegisBindingProvider.getReadType(childReader.getXMLStreamReader(), context, defaultType, getTypeMapping()); if (type != null) { if (!childReader.isXsiNil()) { Object writeObj = type.readObject(childReader, context); if (isProxy) { delegate.writeProperty(name.getLocalPart(), writeObj); } else { writeProperty(name, object, writeObj, clazz, info); } } else { if (!info.isNillable(name)) { throw new XFireFault( name.getLocalPart() + " is nil, but not nillable.", XFireFault.SENDER); } childReader.readToEnd(); } } else { childReader.readToEnd(); } } return object; } catch (IllegalAccessException e) { throw new XFireFault("Illegal access. " + e.getMessage(), e, XFireFault.RECEIVER); } catch (InstantiationException e) { throw new XFireFault("Couldn't instantiate class. " + e.getMessage(), e, XFireFault.SENDER); } catch (SecurityException e) { throw new XFireFault("Illegal access. " + e.getMessage(), e, XFireFault.RECEIVER); } catch (IllegalArgumentException e) { throw new XFireFault("Illegal argument. " + e.getMessage(), e, XFireFault.RECEIVER); } catch (InvocationTargetException e) { throw new XFireFault("Couldn't create class: " + e.getMessage(), e, XFireFault.RECEIVER); } } /** * If the class is an exception, this will try and instantiate it with * information from the XFireFault (if it exists). */ protected Object createFromFault(MessageContext context) throws SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Class clazz = getTypeClass(); Constructor ctr; Object o; Object body = context.getExchange().getFaultMessage().getBody(); if (!(body instanceof XFireFault)) return clazz.newInstance(); XFireFault fault = (XFireFault) body; try { ctr = clazz.getConstructor(new Class[] { String.class, Throwable.class }); o = ctr.newInstance(new Object[] { fault.getMessage(), fault }); } catch (NoSuchMethodException e) { try { ctr = clazz.getConstructor(new Class[] { String.class, Exception.class }); o = ctr.newInstance(new Object[] { fault.getMessage(), fault }); } catch (NoSuchMethodException e1) { try { ctr = clazz.getConstructor(new Class[] { String.class }); o = ctr.newInstance(new Object[] { fault.getMessage() }); } catch (NoSuchMethodException e2) { return clazz.newInstance(); } } } return o; } /** * Write the specified property to a field. */ protected void writeProperty(QName name, Object object, Object property, Class impl, BeanTypeInfo info) throws XFireFault { try { PropertyDescriptor desc = info.getPropertyDescriptorFromMappedName(name); Method m = desc.getWriteMethod(); if (m == null) { if (getTypeClass().isInterface()) m = getWriteMethodFromImplClass(impl, desc); if (m == null) throw new XFireFault("No write method for property " + name + " in " + object.getClass(), XFireFault.SENDER); } Class propertyType = desc.getPropertyType(); if ((property == null && !propertyType.isPrimitive()) || (property != null)) { m.invoke(object, new Object[] { property }); } } catch (Exception e) { if (e instanceof XFireFault) throw (XFireFault) e; throw new XFireFault("Couldn't set property " + name + " on " + object + ". " + e.getMessage(), e, XFireFault.SENDER); } } /** * This is a hack to get the write method from the implementation class for * an interface. */ private Method getWriteMethodFromImplClass(Class impl, PropertyDescriptor pd) throws Exception { String name = pd.getName(); name = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); return impl.getMethod(name, new Class[] { pd.getPropertyType() }); } /** * @see org.codehaus.xfire.aegis.type.Type#writeObject(Object, * org.codehaus.xfire.aegis.MessageWriter, * org.codehaus.xfire.MessageContext) */ public void writeObject(Object object, MessageWriter writer, MessageContext context) throws XFireFault { if (object == null) return; BeanTypeInfo info = getTypeInfo(); if (context.getService() != null) { Object writeXsiType = context.getService() .getProperty(AegisBindingProvider.WRITE_XSI_TYPE_KEY); if ((Boolean.TRUE.equals(writeXsiType) || "true".equals(writeXsiType)) && object.getClass() == getTypeClass()) { writer.writeXsiType(getSchemaType()); } } /* * TODO: Replace this method with one split into two pieces so that we * can front-load the attributes and traverse down the list of super * classes. */ for (Iterator itr = info.getAttributes(); itr.hasNext();) { QName name = (QName) itr.next(); Object value = readProperty(object, name); if (value != null) { Type type = getType(info, name); if (type == null) throw new XFireRuntimeException("Couldn't find type for " + value.getClass() + " for property " + name); MessageWriter cwriter = writer.getAttributeWriter(name); type.writeObject(value, cwriter, context); cwriter.close(); } } for (Iterator itr = info.getElements(); itr.hasNext();) { QName name = (QName) itr.next(); if (info.isExtension() && info.getPropertyDescriptorFromMappedName(name).getReadMethod() .getDeclaringClass() != info.getTypeClass()) { continue; } Object value = readProperty(object, name); Type type = getType(info, name); type = AegisBindingProvider.getWriteType(context, value, type); MessageWriter cwriter; // Write the value if it is not null. if (value != null) { cwriter = getWriter(writer, name, type); if (type == null) throw new XFireRuntimeException("Couldn't find type for " + value.getClass() + " for property " + name); type.writeObject(value, cwriter, context); cwriter.close(); } else if (info.isNillable(name)) { cwriter = getWriter(writer, name, type); // Write the xsi:nil if it is null. cwriter.writeXsiNil(); cwriter.close(); } } if (info.isExtension()) { Type t = getSuperType();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -