Coverage Report - org.kuali.rice.krad.service.impl.XmlObjectSerializerServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlObjectSerializerServiceImpl
0%
0/17
0%
0/8
2.333
XmlObjectSerializerServiceImpl$ProxyAwareJavaReflectionProvider
0%
0/20
0%
0/8
2.333
XmlObjectSerializerServiceImpl$ProxyConverter
0%
0/6
N/A
2.333
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.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  
  * This class is the service implementation for the XmlObjectSerializer structure. This is the default implementation that gets
 40  
  * delivered with Kuali. It utilizes the XStream open source libraries and framework.
 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  
         // See http://xstream.codehaus.org/faq.html#Serialization_CGLIB
 55  
         // To use a newer version of XStream we may need to do something like this:
 56  
 //        xstream = new XStream() {
 57  
 //
 58  
 //            @Override
 59  
 //            public ReflectionProvider getReflectionProvider() {
 60  
 //                return new ProxyAwareJavaReflectionProvider();
 61  
 //            }
 62  
 //
 63  
 //            protected MapperWrapper wrapMapper(MapperWrapper next) {
 64  
 //                return new CGLIBMapper(next);
 65  
 //            }
 66  
 //        };
 67  
 //        xstream.registerConverter(new CGLIBEnhancedConverter(xstream.getMapper(), xstream.getReflectionProvider()));
 68  
 
 69  0
                 xstream.registerConverter(new ProxyConverter(xstream.getMapper(), xstream.getReflectionProvider() ));
 70  0
                 xstream.addDefaultImplementation(ArrayList.class, ListProxyDefaultImpl.class);
 71  0
         }
 72  
         
 73  
     /**
 74  
      * @see org.kuali.rice.krad.service.XmlObjectSerializer#toXml(java.lang.Object)
 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  
      * @see org.kuali.rice.krad.service.XmlObjectSerializer#fromXml(java.lang.String)
 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  
      * This custom converter only handles proxies for BusinessObjects.  List-type proxies are handled by configuring XStream to treat
 98  
      * ListProxyDefaultImpl as ArrayLists (see constructor for this service). 
 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  
         // since the ReflectionConverter supertype defines canConvert without using a parameterized Class type, we must declare
 107  
         // the overridden version the same way
 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  
         // 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. 
 119  
     }
 120  
     
 121  
     public class ProxyAwareJavaReflectionProvider extends PureJavaReflectionProvider {
 122  
 
 123  0
             public ProxyAwareJavaReflectionProvider() {
 124  0
                     super();
 125  0
             }
 126  
         /**
 127  
          * @see com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider#visitSerializableFields(java.lang.Object, com.thoughtworks.xstream.converters.reflection.ReflectionProvider.Visitor)
 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  
 }