View Javadoc

1   /**
2    * Copyright 2005-2012 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 com.thoughtworks.xstream.XStream;
19  import com.thoughtworks.xstream.converters.MarshallingContext;
20  import com.thoughtworks.xstream.converters.SingleValueConverter;
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.joda.time.DateTime;
31  import org.joda.time.format.ISODateTimeFormat;
32  import org.kuali.rice.krad.service.KRADServiceLocator;
33  import org.kuali.rice.krad.service.PersistenceService;
34  import org.kuali.rice.krad.service.XmlObjectSerializerService;
35  import org.kuali.rice.krad.util.DateTimeConverter;
36  
37  import java.lang.reflect.Field;
38  import java.util.ArrayList;
39  import java.util.Iterator;
40  
41  
42  /**
43   * This class is the service implementation for the XmlObjectSerializer structure. This is the default implementation that gets
44   * delivered with Kuali. It utilizes the XStream open source libraries and framework.
45   * 
46   * 
47   */
48  public class XmlObjectSerializerServiceImpl implements XmlObjectSerializerService {
49  	private static final Log LOG = LogFactory.getLog(XmlObjectSerializerServiceImpl.class);
50  	
51  	private PersistenceService persistenceService;
52  	
53  	private XStream xstream;
54  	
55  	public XmlObjectSerializerServiceImpl() {
56  		xstream = new XStream(new ProxyAwareJavaReflectionProvider());
57  
58          // See http://xstream.codehaus.org/faq.html#Serialization_CGLIB
59          // To use a newer version of XStream we may need to do something like this:
60  //        xstream = new XStream() {
61  //
62  //            @Override
63  //            public ReflectionProvider getReflectionProvider() {
64  //                return new ProxyAwareJavaReflectionProvider();
65  //            }
66  //
67  //            protected MapperWrapper wrapMapper(MapperWrapper next) {
68  //                return new CGLIBMapper(next);
69  //            }
70  //        };
71  //        xstream.registerConverter(new CGLIBEnhancedConverter(xstream.getMapper(), xstream.getReflectionProvider()));
72  
73  		xstream.registerConverter(new ProxyConverter(xstream.getMapper(), xstream.getReflectionProvider() ));
74  		xstream.addDefaultImplementation(ArrayList.class, ListProxyDefaultImpl.class);
75          xstream.registerConverter(new DateTimeConverter());
76  	}
77  	
78      /**
79       * @see org.kuali.rice.krad.service.XmlObjectSerializer#toXml(java.lang.Object)
80       */
81      public String toXml(Object object) {
82      	if ( LOG.isDebugEnabled() ) {
83      		LOG.debug( "toXml(" + object + ") : \n" + xstream.toXML(object) );
84      	}
85          return xstream.toXML(object);
86      }
87  
88      /**
89       * @see org.kuali.rice.krad.service.XmlObjectSerializer#fromXml(java.lang.String)
90       */
91      public Object fromXml(String xml) {
92      	if ( LOG.isDebugEnabled() ) {
93      		LOG.debug( "fromXml() : \n" + xml );
94      	}
95      	if ( xml != null ) {
96      		xml = xml.replaceAll( "--EnhancerByCGLIB--[0-9a-f]{0,8}", "" );
97      	}
98          return xstream.fromXML(xml);
99      }
100 
101     /**
102      * This custom converter only handles proxies for BusinessObjects.  List-type proxies are handled by configuring XStream to treat
103      * ListProxyDefaultImpl as ArrayLists (see constructor for this service). 
104      */
105     public class ProxyConverter extends ReflectionConverter {
106         public ProxyConverter(Mapper mapper, ReflectionProvider reflectionProvider) {
107             super(mapper, reflectionProvider);
108         }
109         
110         @Override
111         // since the ReflectionConverter supertype defines canConvert without using a parameterized Class type, we must declare
112         // the overridden version the same way
113         @SuppressWarnings("unchecked")
114         public boolean canConvert(Class clazz) {
115             return clazz.getName().contains("CGLIB");
116         }
117 
118         @Override
119         public void marshal(Object obj, HierarchicalStreamWriter writer, MarshallingContext context) {
120             super.marshal(getPersistenceService().resolveProxy(obj), writer, context);
121         }
122         
123         // 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. 
124     }
125     
126     public class ProxyAwareJavaReflectionProvider extends PureJavaReflectionProvider {
127 
128     	public ProxyAwareJavaReflectionProvider() {
129     		super();
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                 Object value = null;
143                 try {
144                     value = field.get(object);
145                     if (value != null && getPersistenceService().isProxied(value)) {
146                         value = getPersistenceService().resolveProxy(value);
147                     }
148                 } catch (IllegalArgumentException e) {
149                     throw new ObjectAccessException("Could not get field " + field.getClass() + "." + field.getName(), e);
150                 } catch (IllegalAccessException e) {
151                     throw new ObjectAccessException("Could not get field " + field.getClass() + "." + field.getName(), e);
152                 }
153                 visitor.visit(field.getName(), field.getType(), field.getDeclaringClass(), value);
154             }
155         }
156         
157     }
158 
159 	public PersistenceService getPersistenceService() {
160 		if ( persistenceService == null ) {
161 			persistenceService = KRADServiceLocator.getPersistenceService();
162 		}
163 		return persistenceService;
164 	}
165 
166 }