1 package org.apache.jcs.utils.serialization;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
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 }