View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.service.impl;
17  
18  import java.lang.reflect.Field;
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.kuali.rice.krad.service.LegacyDataAdapter;
25  import org.kuali.rice.krad.service.XmlObjectSerializerService;
26  import org.kuali.rice.krad.service.util.DateTimeConverter;
27  import org.springframework.beans.factory.annotation.Required;
28  
29  import com.thoughtworks.xstream.XStream;
30  import com.thoughtworks.xstream.converters.MarshallingContext;
31  import com.thoughtworks.xstream.converters.reflection.ObjectAccessException;
32  import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
33  import com.thoughtworks.xstream.converters.reflection.ReflectionConverter;
34  import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
35  import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
36  import com.thoughtworks.xstream.mapper.Mapper;
37  
38  
39  /**
40   * Service implementation for the XmlObjectSerializer structure. This is the default implementation that gets
41   * delivered with Kuali. It utilizes the XStream open source libraries and framework.
42   *
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  public class XmlObjectSerializerServiceImpl implements XmlObjectSerializerService {
46  	private static final Log LOG = LogFactory.getLog(XmlObjectSerializerServiceImpl.class);
47  
48  	protected LegacyDataAdapter lda;
49  
50  	protected XStream xstream;
51  
52  	public XmlObjectSerializerServiceImpl() {
53  		xstream = new XStream(new ProxyAwareJavaReflectionProvider());
54  
55          // See http://xstream.codehaus.org/faq.html#Serialization_CGLIB
56          // To use a newer version of XStream we may need to do something like this:
57  //        xstream = new XStream() {
58  //
59  //            @Override
60  //            public ReflectionProvider getReflectionProvider() {
61  //                return new ProxyAwareJavaReflectionProvider();
62  //            }
63  //
64  //            protected MapperWrapper wrapMapper(MapperWrapper next) {
65  //                return new CGLIBMapper(next);
66  //            }
67  //        };
68  //        xstream.registerConverter(new CGLIBEnhancedConverter(xstream.getMapper(), xstream.getReflectionProvider()));
69  
70  		xstream.registerConverter(new ProxyConverter(xstream.getMapper(), xstream.getReflectionProvider() ));
71          try {
72          	Class<?> objListProxyClass = Class.forName("org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl");
73              xstream.addDefaultImplementation(ArrayList.class, objListProxyClass);
74          } catch ( Exception ex ) {
75          	// Do nothing - this will blow if the OJB class does not exist, which it won't in some installs
76          }
77          xstream.registerConverter(new DateTimeConverter());
78  	}
79  
80      @Required
81      public void setLegacyDataAdapter(LegacyDataAdapter lda) {
82          this.lda = lda;
83      }
84  
85      @Override
86  	public String toXml(Object object) {
87      	if ( LOG.isDebugEnabled() ) {
88      		LOG.debug( "toXml(" + object + ") : \n" + xstream.toXML(object) );
89      	}
90          return xstream.toXML(object);
91      }
92  
93      @Override
94  	public Object fromXml(String xml) {
95      	if ( LOG.isDebugEnabled() ) {
96      		LOG.debug( "fromXml() : \n" + xml );
97      	}
98      	if ( xml != null ) {
99      		xml = xml.replaceAll( "--EnhancerByCGLIB--[0-9a-f]{0,8}", "" );
100     	}
101         return xstream.fromXML(xml);
102     }
103 
104     /**
105      * This custom converter only handles proxies for BusinessObjects.  List-type proxies are handled by configuring XStream to treat
106      * ListProxyDefaultImpl as ArrayLists (see constructor for this service).
107      */
108     public class ProxyConverter extends ReflectionConverter {
109         public ProxyConverter(Mapper mapper, ReflectionProvider reflectionProvider) {
110             super(mapper, reflectionProvider);
111         }
112 
113         @Override
114         // since the ReflectionConverter supertype defines canConvert without using a parameterized Class type, we must declare
115         // the overridden version the same way
116         @SuppressWarnings("unchecked")
117         public boolean canConvert(Class clazz) {
118             return clazz.getName().contains("CGLIB");
119         }
120 
121         @Override
122         public void marshal(Object obj, HierarchicalStreamWriter writer, MarshallingContext context) {
123             super.marshal(lda.resolveProxy(obj), writer, context);
124         }
125 
126         // we shouldn't need an unmarshal method because all proxy metadata is taken out of the XML, so we'll reserialize as a base BO.
127     }
128 
129     public class ProxyAwareJavaReflectionProvider extends PureJavaReflectionProvider {
130 
131         /**
132          * @see com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider#visitSerializableFields(java.lang.Object, com.thoughtworks.xstream.converters.reflection.ReflectionProvider.Visitor)
133          */
134         @Override
135         public void visitSerializableFields(Object object, Visitor visitor) {
136             for (Iterator iterator = fieldDictionary.serializableFieldsFor(object.getClass()); iterator.hasNext();) {
137                 Field field = (Field) iterator.next();
138                 if (!fieldModifiersSupported(field)) {
139                     continue;
140                 }
141                 validateFieldAccess(field);
142                 if (ignoreField(field)) {
143                     continue;
144                 }
145                 Object value = null;
146                 try {
147                     value = field.get(object);
148                     if (value != null && lda.isProxied(value)) {
149                         value = lda.resolveProxy(value);
150                     }
151                 } catch (Exception e) {
152                     throw new ObjectAccessException("Could not get field " + field.getClass() + "." + field.getName() + " on " + object, e);
153                 }
154                 visitor.visit(field.getName(), field.getType(), field.getDeclaringClass(), value);
155             }
156         }
157 
158         protected boolean ignoreField(Field field) {
159             return false;
160         }
161 
162     }
163 
164 }