View Javadoc

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  public class SerializationConversionUtil
38  {
39      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          if ( element == null )
56          {
57              return null;
58          }
59  
60          byte[] serialzedValue = null;
61  
62          // if it has already been serialized, don't do it again.
63          if ( element instanceof ICacheElementSerialized )
64          {
65              serialzedValue = ( (ICacheElementSerialized) element ).getSerializedValue();
66          }
67          else
68          {
69              if ( elementSerializer != null )
70              {
71                  try
72                  {
73                      serialzedValue = elementSerializer.serialize( element.getVal() );
74                  }
75                  catch ( IOException e )
76                  {
77                      log.error( "Problem serializing object.", e );
78                      throw e;
79                  }
80              }
81              else
82              {
83                  // we could just use the default.
84                  log.warn( "ElementSerializer is null.  Could not serialize object." );
85                  throw new IOException( "Could not serialize object.  The ElementSerializer is null." );
86              }
87          }
88          ICacheElementSerialized serialized = new CacheElementSerialized( element.getCacheName(), element.getKey(),
89                                                                           serialzedValue, element.getElementAttributes() );
90  
91          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         if ( serialized == null )
110         {
111             return null;
112         }
113 
114         Object deSerialzedValue = null;
115 
116         if ( elementSerializer != null )
117         {
118             try
119             {
120                 try
121                 {
122                     deSerialzedValue = elementSerializer.deSerialize( serialized.getSerializedValue() );
123                 }
124                 catch ( ClassNotFoundException e )
125                 {
126                     log.error( "Problem de-serializing object.", e );
127                     throw e;
128                 }
129             }
130             catch ( IOException e )
131             {
132                 log.error( "Problem de-serializing object.", e );
133                 throw e;
134             }
135         }
136         else
137         {
138             // we could just use the default.
139             log.warn( "ElementSerializer is null.  Could not serialize object." );
140         }
141         ICacheElement deSerialized = new CacheElement( serialized.getCacheName(), serialized.getKey(), deSerialzedValue );
142         deSerialized.setElementAttributes( serialized.getElementAttributes() );
143 
144         return deSerialized;
145     }
146 }