Coverage report

  %line %branch
org.apache.jcs.utils.serialization.SerializationConversionUtil
63% 
90% 

 1  
 package org.apache.jcs.utils.serialization;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.IOException;
 23  
 
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 import org.apache.jcs.engine.CacheElement;
 27  
 import org.apache.jcs.engine.CacheElementSerialized;
 28  
 import org.apache.jcs.engine.behavior.ICacheElement;
 29  
 import org.apache.jcs.engine.behavior.ICacheElementSerialized;
 30  
 import org.apache.jcs.engine.behavior.IElementSerializer;
 31  
 
 32  
 /**
 33  
  * This uses a supplied Serialer to convert to and from cache elements.
 34  
  * <p>
 35  
  * @author Aaron Smuts
 36  
  */
 37  1
 public class SerializationConversionUtil
 38  
 {
 39  57
     private final static Log log = LogFactory.getLog( SerializationConversionUtil.class );
 40  
 
 41  
     /**
 42  
      * This returns a wrapper that has a serialized version of the value instead
 43  
      * of the value.
 44  
      * <p>
 45  
      * @param element
 46  
      * @param elementSerializer
 47  
      *            the serializer to be used.
 48  
      * @return null for null;
 49  
      * @throws IOException
 50  
      */
 51  
     public static ICacheElementSerialized getSerializedCacheElement( ICacheElement element,
 52  
                                                                     IElementSerializer elementSerializer )
 53  
         throws IOException
 54  
     {
 55  751
         if ( element == null )
 56  
         {
 57  0
             return null;
 58  
         }
 59  
 
 60  751
         byte[] serialzedValue = null;
 61  
 
 62  
         // if it has already been serialized, don't do it again.
 63  751
         if ( element instanceof ICacheElementSerialized )
 64  
         {
 65  0
             serialzedValue = ( (ICacheElementSerialized) element ).getSerializedValue();
 66  0
         }
 67  
         else
 68  
         {
 69  751
             if ( elementSerializer != null )
 70  
             {
 71  
                 try
 72  
                 {
 73  743
                     serialzedValue = elementSerializer.serialize( element.getVal() );
 74  
                 }
 75  0
                 catch ( IOException e )
 76  
                 {
 77  0
                     log.error( "Problem serializing object.", e );
 78  0
                     throw e;
 79  742
                 }
 80  
             }
 81  
             else
 82  
             {
 83  
                 // we could just use the default.
 84  8
                 log.warn( "ElementSerializer is null.  Could not serialize object." );
 85  8
                 throw new IOException( "Could not serialize object.  The ElementSerializer is null." );
 86  
             }
 87  
         }
 88  744
         ICacheElementSerialized serialized = new CacheElementSerialized( element.getCacheName(), element.getKey(),
 89  1
                                                                          serialzedValue, element.getElementAttributes() );
 90  
 
 91  743
         return serialized;
 92  
     }
 93  
 
 94  
     /**
 95  
      * This returns a wrapper that has a de-serialized version of the value
 96  
      * instead of the serialized value.
 97  
      * <p>
 98  
      * @param serialized
 99  
      * @param elementSerializer
 100  
      *            the serializer to be used.
 101  
      * @return null for null;
 102  
      * @throws IOException
 103  
      * @throws ClassNotFoundException
 104  
      */
 105  
     public static ICacheElement getDeSerializedCacheElement( ICacheElementSerialized serialized,
 106  
                                                             IElementSerializer elementSerializer )
 107  
         throws IOException, ClassNotFoundException
 108  
     {
 109  43
         if ( serialized == null )
 110  
         {
 111  0
             return null;
 112  
         }
 113  
 
 114  43
         Object deSerialzedValue = null;
 115  
 
 116  43
         if ( elementSerializer != null )
 117  
         {
 118  
             try
 119  
             {
 120  
                 try
 121  
                 {
 122  43
                     deSerialzedValue = elementSerializer.deSerialize( serialized.getSerializedValue() );
 123  
                 }
 124  0
                 catch ( ClassNotFoundException e )
 125  
                 {
 126  0
                     log.error( "Problem de-serializing object.", e );
 127  0
                     throw e;
 128  42
                 }
 129  
             }
 130  0
             catch ( IOException e )
 131  
             {
 132  0
                 log.error( "Problem de-serializing object.", e );
 133  0
                 throw e;
 134  42
             }
 135  
         }
 136  
         else
 137  
         {
 138  
             // we could just use the default.
 139  0
             log.warn( "ElementSerializer is null.  Could not serialize object." );
 140  
         }
 141  43
         ICacheElement deSerialized = new CacheElement( serialized.getCacheName(), serialized.getKey(), deSerialzedValue );
 142  43
         deSerialized.setElementAttributes( serialized.getElementAttributes() );
 143  
 
 144  43
         return deSerialized;
 145  
     }
 146  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.