Coverage Report - org.kuali.rice.kns.service.impl.XmlObjectSerializerServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlObjectSerializerServiceImpl
0%
0/26
0%
0/10
2.3
XmlObjectSerializerServiceImpl$ProxyAwareJavaReflectionProvider
0%
0/20
0%
0/8
2.3
XmlObjectSerializerServiceImpl$ProxyConverter
0%
0/6
N/A
2.3
 
 1  
 /*
 2  
  * Copyright 2005-2007 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  
 
 17  
 package org.kuali.rice.kns.service.impl;
 18  
 
 19  
 import com.thoughtworks.xstream.XStream;
 20  
 import com.thoughtworks.xstream.converters.MarshallingContext;
 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.apache.ojb.broker.core.proxy.ProxyHelper;
 31  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 32  
 import org.kuali.rice.kns.service.PersistenceService;
 33  
 import org.kuali.rice.kns.service.XmlObjectSerializerService;
 34  
 
 35  
 import javax.xml.transform.*;
 36  
 import javax.xml.transform.dom.DOMSource;
 37  
 import javax.xml.transform.stream.StreamResult;
 38  
 import java.io.StringWriter;
 39  
 import java.lang.reflect.Field;
 40  
 import java.util.ArrayList;
 41  
 import java.util.Iterator;
 42  
 
 43  
 
 44  
 /**
 45  
  * This class is the service implementation for the XmlObjectSerializer structure. This is the default implementation that gets
 46  
  * delivered with Kuali. It utilizes the XStream open source libraries and framework.
 47  
  * 
 48  
  * 
 49  
  */
 50  
 public class XmlObjectSerializerServiceImpl implements XmlObjectSerializerService {
 51  0
         private static final Log LOG = LogFactory.getLog(XmlObjectSerializerServiceImpl.class);
 52  
         
 53  
         private PersistenceService persistenceService;
 54  
         
 55  
         private XStream xstream;
 56  
         
 57  0
         public XmlObjectSerializerServiceImpl() {
 58  0
                 xstream = new XStream(new ProxyAwareJavaReflectionProvider());
 59  0
                 xstream.registerConverter(new ProxyConverter(xstream.getMapper(), xstream.getReflectionProvider() ));
 60  0
                 xstream.addDefaultImplementation(ArrayList.class, ListProxyDefaultImpl.class);
 61  0
         }
 62  
         
 63  
     /**
 64  
      * @see org.kuali.rice.kns.service.XmlObjectSerializer#toXml(java.lang.Object)
 65  
      */
 66  
     public String toXml(Object object) {
 67  0
             if ( LOG.isDebugEnabled() ) {
 68  0
                     LOG.debug( "toXml(" + object + ") : \n" + xstream.toXML(object) );
 69  
             }
 70  0
         return xstream.toXML(object);
 71  
     }
 72  
 
 73  
     /**
 74  
      * @see org.kuali.rice.kns.service.XmlObjectSerializer#fromXml(java.lang.String)
 75  
      */
 76  
     public Object fromXml(String xml) {
 77  0
             if ( LOG.isDebugEnabled() ) {
 78  0
                     LOG.debug( "fromXml() : \n" + xml );
 79  
             }
 80  0
             if ( xml != null ) {
 81  0
                     xml = xml.replaceAll( "--EnhancerByCGLIB--[0-9a-f]{0,8}", "" );
 82  
             }
 83  0
         return xstream.fromXML(xml);
 84  
     }
 85  
 
 86  
     public String writeNode(org.w3c.dom.Node node, boolean indent) throws TransformerException {
 87  0
         Source source = new DOMSource(node);
 88  0
         StringWriter writer = new StringWriter();
 89  0
         Result result = new StreamResult(writer);
 90  0
         Transformer transformer = TransformerFactory.newInstance().newTransformer();
 91  0
         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
 92  0
         if (indent) {
 93  0
             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
 94  
         }
 95  0
         transformer.transform(source, result);
 96  0
         return writer.toString();
 97  
     }
 98  
 
 99  
 
 100  
     /**
 101  
      * This custom converter only handles proxies for BusinessObjects.  List-type proxies are handled by configuring XStream to treat
 102  
      * ListProxyDefaultImpl as ArrayLists (see constructor for this service). 
 103  
      */
 104  
     public class ProxyConverter extends ReflectionConverter {
 105  0
         public ProxyConverter(Mapper mapper, ReflectionProvider reflectionProvider) {
 106  0
             super(mapper, reflectionProvider);
 107  0
         }
 108  
         public boolean canConvert(Class clazz) {
 109  0
             return clazz.getName().contains("CGLIB");
 110  
         }
 111  
 
 112  
         public void marshal(Object obj, HierarchicalStreamWriter writer, MarshallingContext context) {
 113  0
             super.marshal(getPersistenceService().resolveProxy(obj), writer, context);
 114  0
         }
 115  
         
 116  
         // 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. 
 117  
     }
 118  
     
 119  
     public class ProxyAwareJavaReflectionProvider extends PureJavaReflectionProvider {
 120  
 
 121  0
             public ProxyAwareJavaReflectionProvider() {
 122  0
                     super();
 123  0
             }
 124  
         /**
 125  
          * @see com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider#visitSerializableFields(java.lang.Object, com.thoughtworks.xstream.converters.reflection.ReflectionProvider.Visitor)
 126  
          */
 127  
         @Override
 128  
         public void visitSerializableFields(Object object, Visitor visitor) {
 129  0
             for (Iterator iterator = fieldDictionary.serializableFieldsFor(object.getClass()); iterator.hasNext();) {
 130  0
                 Field field = (Field) iterator.next();
 131  0
                 if (!fieldModifiersSupported(field)) {
 132  0
                     continue;
 133  
                 }
 134  0
                 validateFieldAccess(field);
 135  0
                 Object value = null;
 136  
                 try {
 137  0
                     value = field.get(object);
 138  0
                     if (value != null && ProxyHelper.isProxy(value)) {
 139  0
                         value = getPersistenceService().resolveProxy(value);
 140  
                     }
 141  0
                 } catch (IllegalArgumentException e) {
 142  0
                     throw new ObjectAccessException("Could not get field " + field.getClass() + "." + field.getName(), e);
 143  0
                 } catch (IllegalAccessException e) {
 144  0
                     throw new ObjectAccessException("Could not get field " + field.getClass() + "." + field.getName(), e);
 145  0
                 }
 146  0
                 visitor.visit(field.getName(), field.getType(), field.getDeclaringClass(), value);
 147  0
             }
 148  0
         }
 149  
         
 150  
     }
 151  
 
 152  
         public PersistenceService getPersistenceService() {
 153  0
                 if ( persistenceService == null ) {
 154  0
                         persistenceService = KNSServiceLocator.getPersistenceService();
 155  
                 }
 156  0
                 return persistenceService;
 157  
         }
 158  
     
 159  
 }