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