| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.kns.service.impl; |
| 18 | |
|
| 19 | |
import com.thoughtworks.xstream.XStream; |
| 20 | |
import com.thoughtworks.xstream.converters.MarshallingContext; |
| 21 | |
import com.thoughtworks.xstream.converters.reflection.ObjectAccessException; |
| 22 | |
import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider; |
| 23 | |
import com.thoughtworks.xstream.converters.reflection.ReflectionConverter; |
| 24 | |
import com.thoughtworks.xstream.converters.reflection.ReflectionProvider; |
| 25 | |
import com.thoughtworks.xstream.io.HierarchicalStreamWriter; |
| 26 | |
import com.thoughtworks.xstream.mapper.Mapper; |
| 27 | |
import org.apache.commons.logging.Log; |
| 28 | |
import org.apache.commons.logging.LogFactory; |
| 29 | |
import org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl; |
| 30 | |
import org.apache.ojb.broker.core.proxy.ProxyHelper; |
| 31 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
| 32 | |
import org.kuali.rice.kns.service.PersistenceService; |
| 33 | |
import org.kuali.rice.kns.service.XmlObjectSerializerService; |
| 34 | |
|
| 35 | |
import javax.xml.transform.*; |
| 36 | |
import javax.xml.transform.dom.DOMSource; |
| 37 | |
import javax.xml.transform.stream.StreamResult; |
| 38 | |
import java.io.StringWriter; |
| 39 | |
import java.lang.reflect.Field; |
| 40 | |
import java.util.ArrayList; |
| 41 | |
import java.util.Iterator; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public class XmlObjectSerializerServiceImpl implements XmlObjectSerializerService { |
| 51 | 0 | private static final Log LOG = LogFactory.getLog(XmlObjectSerializerServiceImpl.class); |
| 52 | |
|
| 53 | |
private PersistenceService persistenceService; |
| 54 | |
|
| 55 | |
private XStream xstream; |
| 56 | |
|
| 57 | 0 | public XmlObjectSerializerServiceImpl() { |
| 58 | 0 | xstream = new XStream(new ProxyAwareJavaReflectionProvider()); |
| 59 | 0 | xstream.registerConverter(new ProxyConverter(xstream.getMapper(), xstream.getReflectionProvider() )); |
| 60 | 0 | xstream.addDefaultImplementation(ArrayList.class, ListProxyDefaultImpl.class); |
| 61 | 0 | } |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public String toXml(Object object) { |
| 67 | 0 | if ( LOG.isDebugEnabled() ) { |
| 68 | 0 | LOG.debug( "toXml(" + object + ") : \n" + xstream.toXML(object) ); |
| 69 | |
} |
| 70 | 0 | return xstream.toXML(object); |
| 71 | |
} |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public Object fromXml(String xml) { |
| 77 | 0 | if ( LOG.isDebugEnabled() ) { |
| 78 | 0 | LOG.debug( "fromXml() : \n" + xml ); |
| 79 | |
} |
| 80 | 0 | if ( xml != null ) { |
| 81 | 0 | xml = xml.replaceAll( "--EnhancerByCGLIB--[0-9a-f]{0,8}", "" ); |
| 82 | |
} |
| 83 | 0 | return xstream.fromXML(xml); |
| 84 | |
} |
| 85 | |
|
| 86 | |
public String writeNode(org.w3c.dom.Node node, boolean indent) throws TransformerException { |
| 87 | 0 | Source source = new DOMSource(node); |
| 88 | 0 | StringWriter writer = new StringWriter(); |
| 89 | 0 | Result result = new StreamResult(writer); |
| 90 | 0 | Transformer transformer = TransformerFactory.newInstance().newTransformer(); |
| 91 | 0 | transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 92 | 0 | if (indent) { |
| 93 | 0 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
| 94 | |
} |
| 95 | 0 | transformer.transform(source, result); |
| 96 | 0 | return writer.toString(); |
| 97 | |
} |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public class ProxyConverter extends ReflectionConverter { |
| 105 | 0 | public ProxyConverter(Mapper mapper, ReflectionProvider reflectionProvider) { |
| 106 | 0 | super(mapper, reflectionProvider); |
| 107 | 0 | } |
| 108 | |
public boolean canConvert(Class clazz) { |
| 109 | 0 | return clazz.getName().contains("CGLIB"); |
| 110 | |
} |
| 111 | |
|
| 112 | |
public void marshal(Object obj, HierarchicalStreamWriter writer, MarshallingContext context) { |
| 113 | 0 | super.marshal(getPersistenceService().resolveProxy(obj), writer, context); |
| 114 | 0 | } |
| 115 | |
|
| 116 | |
|
| 117 | |
} |
| 118 | |
|
| 119 | |
public class ProxyAwareJavaReflectionProvider extends PureJavaReflectionProvider { |
| 120 | |
|
| 121 | 0 | public ProxyAwareJavaReflectionProvider() { |
| 122 | 0 | super(); |
| 123 | 0 | } |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
@Override |
| 128 | |
public void visitSerializableFields(Object object, Visitor visitor) { |
| 129 | 0 | for (Iterator iterator = fieldDictionary.serializableFieldsFor(object.getClass()); iterator.hasNext();) { |
| 130 | 0 | Field field = (Field) iterator.next(); |
| 131 | 0 | if (!fieldModifiersSupported(field)) { |
| 132 | 0 | continue; |
| 133 | |
} |
| 134 | 0 | validateFieldAccess(field); |
| 135 | 0 | Object value = null; |
| 136 | |
try { |
| 137 | 0 | value = field.get(object); |
| 138 | 0 | if (value != null && ProxyHelper.isProxy(value)) { |
| 139 | 0 | value = getPersistenceService().resolveProxy(value); |
| 140 | |
} |
| 141 | 0 | } catch (IllegalArgumentException e) { |
| 142 | 0 | throw new ObjectAccessException("Could not get field " + field.getClass() + "." + field.getName(), e); |
| 143 | 0 | } catch (IllegalAccessException e) { |
| 144 | 0 | throw new ObjectAccessException("Could not get field " + field.getClass() + "." + field.getName(), e); |
| 145 | 0 | } |
| 146 | 0 | visitor.visit(field.getName(), field.getType(), field.getDeclaringClass(), value); |
| 147 | 0 | } |
| 148 | 0 | } |
| 149 | |
|
| 150 | |
} |
| 151 | |
|
| 152 | |
public PersistenceService getPersistenceService() { |
| 153 | 0 | if ( persistenceService == null ) { |
| 154 | 0 | persistenceService = KNSServiceLocator.getPersistenceService(); |
| 155 | |
} |
| 156 | 0 | return persistenceService; |
| 157 | |
} |
| 158 | |
|
| 159 | |
} |