View Javadoc
1   /**
2    *    Copyright 2009-2015 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.cache.decorators;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.ByteArrayOutputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.ObjectInputStream;
23  import java.io.ObjectOutputStream;
24  import java.io.ObjectStreamClass;
25  import java.io.Serializable;
26  import java.util.concurrent.locks.ReadWriteLock;
27  
28  import org.apache.ibatis.cache.Cache;
29  import org.apache.ibatis.cache.CacheException;
30  import org.apache.ibatis.io.Resources;
31  
32  /**
33   * @author Clinton Begin
34   */
35  public class SerializedCache implements Cache {
36  
37    private Cache delegate;
38  
39    public SerializedCache(Cache delegate) {
40      this.delegate = delegate;
41    }
42  
43    @Override
44    public String getId() {
45      return delegate.getId();
46    }
47  
48    @Override
49    public int getSize() {
50      return delegate.getSize();
51    }
52  
53    @Override
54    public void putObject(Object key, Object object) {
55      if (object == null || object instanceof Serializable) {
56        delegate.putObject(key, serialize((Serializable) object));
57      } else {
58        throw new CacheException("SharedCache failed to make a copy of a non-serializable object: " + object);
59      }
60    }
61  
62    @Override
63    public Object getObject(Object key) {
64      Object object = delegate.getObject(key);
65      return object == null ? null : deserialize((byte[]) object);
66    }
67  
68    @Override
69    public Object removeObject(Object key) {
70      return delegate.removeObject(key);
71    }
72  
73    @Override
74    public void clear() {
75      delegate.clear();
76    }
77  
78    @Override
79    public ReadWriteLock getReadWriteLock() {
80      return null;
81    }
82  
83    @Override
84    public int hashCode() {
85      return delegate.hashCode();
86    }
87  
88    @Override
89    public boolean equals(Object obj) {
90      return delegate.equals(obj);
91    }
92  
93    private byte[] serialize(Serializable value) {
94      try {
95        ByteArrayOutputStream bos = new ByteArrayOutputStream();
96        ObjectOutputStream oos = new ObjectOutputStream(bos);
97        oos.writeObject(value);
98        oos.flush();
99        oos.close();
100       return bos.toByteArray();
101     } catch (Exception e) {
102       throw new CacheException("Error serializing object.  Cause: " + e, e);
103     }
104   }
105 
106   private Serializable deserialize(byte[] value) {
107     Serializable result;
108     try {
109       ByteArrayInputStream bis = new ByteArrayInputStream(value);
110       ObjectInputStream ois = new CustomObjectInputStream(bis);
111       result = (Serializable) ois.readObject();
112       ois.close();
113     } catch (Exception e) {
114       throw new CacheException("Error deserializing object.  Cause: " + e, e);
115     }
116     return result;
117   }
118 
119   public static class CustomObjectInputStream extends ObjectInputStream {
120 
121     public CustomObjectInputStream(InputStream in) throws IOException {
122       super(in);
123     }
124 
125     @Override
126     protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
127       return Resources.classForName(desc.getName());
128     }
129     
130   }
131 
132 }