001 /** 002 * Copyright 2005-2014 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 java.lang.reflect.Field; 019 import java.util.ArrayList; 020 import java.util.Iterator; 021 022 import org.apache.commons.logging.Log; 023 import org.apache.commons.logging.LogFactory; 024 import org.kuali.rice.krad.service.LegacyDataAdapter; 025 import org.kuali.rice.krad.service.XmlObjectSerializerService; 026 import org.kuali.rice.krad.service.util.DateTimeConverter; 027 import org.springframework.beans.factory.annotation.Required; 028 029 import com.thoughtworks.xstream.XStream; 030 import com.thoughtworks.xstream.converters.MarshallingContext; 031 import com.thoughtworks.xstream.converters.reflection.ObjectAccessException; 032 import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider; 033 import com.thoughtworks.xstream.converters.reflection.ReflectionConverter; 034 import com.thoughtworks.xstream.converters.reflection.ReflectionProvider; 035 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 036 import com.thoughtworks.xstream.mapper.Mapper; 037 038 039 /** 040 * Service implementation for the XmlObjectSerializer structure. This is the default implementation that gets 041 * delivered with Kuali. It utilizes the XStream open source libraries and framework. 042 * 043 * @author Kuali Rice Team (rice.collab@kuali.org) 044 */ 045 public class XmlObjectSerializerServiceImpl implements XmlObjectSerializerService { 046 private static final Log LOG = LogFactory.getLog(XmlObjectSerializerServiceImpl.class); 047 048 protected LegacyDataAdapter lda; 049 050 protected XStream xstream; 051 052 public XmlObjectSerializerServiceImpl() { 053 xstream = new XStream(new ProxyAwareJavaReflectionProvider()); 054 055 // See http://xstream.codehaus.org/faq.html#Serialization_CGLIB 056 // To use a newer version of XStream we may need to do something like this: 057 // xstream = new XStream() { 058 // 059 // @Override 060 // public ReflectionProvider getReflectionProvider() { 061 // return new ProxyAwareJavaReflectionProvider(); 062 // } 063 // 064 // protected MapperWrapper wrapMapper(MapperWrapper next) { 065 // return new CGLIBMapper(next); 066 // } 067 // }; 068 // xstream.registerConverter(new CGLIBEnhancedConverter(xstream.getMapper(), xstream.getReflectionProvider())); 069 070 xstream.registerConverter(new ProxyConverter(xstream.getMapper(), xstream.getReflectionProvider() )); 071 try { 072 Class<?> objListProxyClass = Class.forName("org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl"); 073 xstream.addDefaultImplementation(ArrayList.class, objListProxyClass); 074 } catch ( Exception ex ) { 075 // Do nothing - this will blow if the OJB class does not exist, which it won't in some installs 076 } 077 xstream.registerConverter(new DateTimeConverter()); 078 } 079 080 @Required 081 public void setLegacyDataAdapter(LegacyDataAdapter lda) { 082 this.lda = lda; 083 } 084 085 @Override 086 public String toXml(Object object) { 087 if ( LOG.isDebugEnabled() ) { 088 LOG.debug( "toXml(" + object + ") : \n" + xstream.toXML(object) ); 089 } 090 return xstream.toXML(object); 091 } 092 093 @Override 094 public Object fromXml(String xml) { 095 if ( LOG.isDebugEnabled() ) { 096 LOG.debug( "fromXml() : \n" + xml ); 097 } 098 if ( xml != null ) { 099 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 }