001 /** 002 * Copyright 2005-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krad.service.impl; 017 018 import com.thoughtworks.xstream.XStream; 019 import com.thoughtworks.xstream.converters.MarshallingContext; 020 import com.thoughtworks.xstream.converters.SingleValueConverter; 021 import com.thoughtworks.xstream.converters.reflection.ObjectAccessException; 022 import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider; 023 import com.thoughtworks.xstream.converters.reflection.ReflectionConverter; 024 import com.thoughtworks.xstream.converters.reflection.ReflectionProvider; 025 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 026 import com.thoughtworks.xstream.mapper.Mapper; 027 import org.apache.commons.logging.Log; 028 import org.apache.commons.logging.LogFactory; 029 import org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl; 030 import org.joda.time.DateTime; 031 import org.joda.time.format.ISODateTimeFormat; 032 import org.kuali.rice.krad.service.KRADServiceLocator; 033 import org.kuali.rice.krad.service.PersistenceService; 034 import org.kuali.rice.krad.service.XmlObjectSerializerService; 035 import org.kuali.rice.krad.util.DateTimeConverter; 036 037 import java.lang.reflect.Field; 038 import java.util.ArrayList; 039 import java.util.Iterator; 040 041 042 /** 043 * This class is the service implementation for the XmlObjectSerializer structure. This is the default implementation that gets 044 * delivered with Kuali. It utilizes the XStream open source libraries and framework. 045 * 046 * 047 */ 048 public class XmlObjectSerializerServiceImpl implements XmlObjectSerializerService { 049 private static final Log LOG = LogFactory.getLog(XmlObjectSerializerServiceImpl.class); 050 051 private PersistenceService persistenceService; 052 053 private XStream xstream; 054 055 public XmlObjectSerializerServiceImpl() { 056 xstream = new XStream(new ProxyAwareJavaReflectionProvider()); 057 058 // See http://xstream.codehaus.org/faq.html#Serialization_CGLIB 059 // To use a newer version of XStream we may need to do something like this: 060 // xstream = new XStream() { 061 // 062 // @Override 063 // public ReflectionProvider getReflectionProvider() { 064 // return new ProxyAwareJavaReflectionProvider(); 065 // } 066 // 067 // protected MapperWrapper wrapMapper(MapperWrapper next) { 068 // return new CGLIBMapper(next); 069 // } 070 // }; 071 // xstream.registerConverter(new CGLIBEnhancedConverter(xstream.getMapper(), xstream.getReflectionProvider())); 072 073 xstream.registerConverter(new ProxyConverter(xstream.getMapper(), xstream.getReflectionProvider() )); 074 xstream.addDefaultImplementation(ArrayList.class, ListProxyDefaultImpl.class); 075 xstream.registerConverter(new DateTimeConverter()); 076 } 077 078 /** 079 * @see org.kuali.rice.krad.service.XmlObjectSerializer#toXml(java.lang.Object) 080 */ 081 public String toXml(Object object) { 082 if ( LOG.isDebugEnabled() ) { 083 LOG.debug( "toXml(" + object + ") : \n" + xstream.toXML(object) ); 084 } 085 return xstream.toXML(object); 086 } 087 088 /** 089 * @see org.kuali.rice.krad.service.XmlObjectSerializer#fromXml(java.lang.String) 090 */ 091 public Object fromXml(String xml) { 092 if ( LOG.isDebugEnabled() ) { 093 LOG.debug( "fromXml() : \n" + xml ); 094 } 095 if ( xml != null ) { 096 xml = xml.replaceAll( "--EnhancerByCGLIB--[0-9a-f]{0,8}", "" ); 097 } 098 return xstream.fromXML(xml); 099 } 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 }