View Javadoc
1   package org.apache.commons.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 org.apache.commons.jcs.engine.CacheElement;
23  import org.apache.commons.jcs.engine.CacheElementSerialized;
24  import org.apache.commons.jcs.engine.behavior.ICacheElement;
25  import org.apache.commons.jcs.engine.behavior.ICacheElementSerialized;
26  import org.apache.commons.jcs.engine.behavior.IElementSerializer;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  import java.io.IOException;
31  
32  /**
33   * This uses a supplied Serializer to convert to and from cache elements.
34   * <p>
35   * @author Aaron Smuts
36   */
37  public class SerializationConversionUtil
38  {
39      /** The logger */
40      private static final Log log = LogFactory.getLog( SerializationConversionUtil.class );
41  
42      /**
43       * This returns a wrapper that has a serialized version of the value instead
44       * of the value.
45       * <p>
46       * @param element
47       * @param elementSerializer
48       *            the serializer to be used.
49       * @return null for null;
50       * @throws IOException
51       */
52      public static <K, V> ICacheElementSerialized<K, V> getSerializedCacheElement( ICacheElement<K, V> element,
53                                                                      IElementSerializer elementSerializer )
54          throws IOException
55      {
56          if ( element == null )
57          {
58              return null;
59          }
60  
61          byte[] serializedValue = null;
62  
63          // if it has already been serialized, don't do it again.
64          if ( element instanceof ICacheElementSerialized )
65          {
66              serializedValue = ( (ICacheElementSerialized<K, V>) element ).getSerializedValue();
67          }
68          else
69          {
70              if ( elementSerializer != null )
71              {
72                  try
73                  {
74                      serializedValue = elementSerializer.serialize( element.getVal() );
75                  }
76                  catch ( IOException e )
77                  {
78                      log.error( "Problem serializing object.", e );
79                      throw e;
80                  }
81              }
82              else
83              {
84                  // we could just use the default.
85                  log.warn( "ElementSerializer is null.  Could not serialize object." );
86                  throw new IOException( "Could not serialize object.  The ElementSerializer is null." );
87              }
88          }
89          ICacheElementSerialized<K, V> serialized = new CacheElementSerialized<K, V>(
90                  element.getCacheName(), element.getKey(), serializedValue, element.getElementAttributes() );
91  
92          return serialized;
93      }
94  
95      /**
96       * This returns a wrapper that has a de-serialized version of the value
97       * instead of the serialized value.
98       * <p>
99       * @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 }