001package org.apache.commons.jcs.utils.serialization;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.commons.jcs.engine.CacheElement;
023import org.apache.commons.jcs.engine.CacheElementSerialized;
024import org.apache.commons.jcs.engine.behavior.ICacheElement;
025import org.apache.commons.jcs.engine.behavior.ICacheElementSerialized;
026import org.apache.commons.jcs.engine.behavior.IElementSerializer;
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029
030import java.io.IOException;
031
032/**
033 * This uses a supplied Serializer to convert to and from cache elements.
034 * <p>
035 * @author Aaron Smuts
036 */
037public class SerializationConversionUtil
038{
039    /** The logger */
040    private static final Log log = LogFactory.getLog( SerializationConversionUtil.class );
041
042    /**
043     * This returns a wrapper that has a serialized version of the value instead
044     * of the value.
045     * <p>
046     * @param element
047     * @param elementSerializer
048     *            the serializer to be used.
049     * @return null for null;
050     * @throws IOException
051     */
052    public static <K, V> ICacheElementSerialized<K, V> getSerializedCacheElement( ICacheElement<K, V> element,
053                                                                    IElementSerializer elementSerializer )
054        throws IOException
055    {
056        if ( element == null )
057        {
058            return null;
059        }
060
061        byte[] serializedValue = null;
062
063        // if it has already been serialized, don't do it again.
064        if ( element instanceof ICacheElementSerialized )
065        {
066            serializedValue = ( (ICacheElementSerialized<K, V>) element ).getSerializedValue();
067        }
068        else
069        {
070            if ( elementSerializer != null )
071            {
072                try
073                {
074                    serializedValue = elementSerializer.serialize( element.getVal() );
075                }
076                catch ( IOException e )
077                {
078                    log.error( "Problem serializing object.", e );
079                    throw e;
080                }
081            }
082            else
083            {
084                // we could just use the default.
085                log.warn( "ElementSerializer is null.  Could not serialize object." );
086                throw new IOException( "Could not serialize object.  The ElementSerializer is null." );
087            }
088        }
089        ICacheElementSerialized<K, V> serialized = new CacheElementSerialized<K, V>(
090                element.getCacheName(), element.getKey(), serializedValue, element.getElementAttributes() );
091
092        return serialized;
093    }
094
095    /**
096     * This returns a wrapper that has a de-serialized version of the value
097     * instead of the serialized value.
098     * <p>
099     * @param serialized
100     * @param elementSerializer
101     *            the serializer to be used.
102     * @return null for null;
103     * @throws IOException
104     * @throws ClassNotFoundException
105     */
106    public static <K, V> ICacheElement<K, V> getDeSerializedCacheElement( ICacheElementSerialized<K, V> serialized,
107                                                            IElementSerializer elementSerializer )
108        throws IOException, ClassNotFoundException
109    {
110        if ( serialized == null )
111        {
112            return null;
113        }
114
115        V deSerializedValue = null;
116
117        if ( elementSerializer != null )
118        {
119            try
120            {
121                try
122                {
123                    deSerializedValue = elementSerializer.deSerialize( serialized.getSerializedValue(), null );
124                }
125                catch ( ClassNotFoundException e )
126                {
127                    log.error( "Problem de-serializing object.", e );
128                    throw e;
129                }
130            }
131            catch ( IOException e )
132            {
133                log.error( "Problem de-serializing object.", e );
134                throw e;
135            }
136        }
137        else
138        {
139            // we could just use the default.
140            log.warn( "ElementSerializer is null.  Could not serialize object." );
141        }
142        ICacheElement<K, V> deSerialized = new CacheElement<K, V>( serialized.getCacheName(), serialized.getKey(), deSerializedValue );
143        deSerialized.setElementAttributes( serialized.getElementAttributes() );
144
145        return deSerialized;
146    }
147}