View Javadoc
1   /*
2    *    Copyright 2009-2021 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  
27  import org.apache.ibatis.cache.Cache;
28  import org.apache.ibatis.cache.CacheException;
29  import org.apache.ibatis.io.Resources;
30  import org.apache.ibatis.io.SerialFilterChecker;
31  
32  /**
33   * @author Clinton Begin
34   */
35  public class SerializedCache implements Cache {
36  
37    private final 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 int hashCode() {
80      return delegate.hashCode();
81    }
82  
83    @Override
84    public boolean equals(Object obj) {
85      return delegate.equals(obj);
86    }
87  
88    private byte[] serialize(Serializable value) {
89      try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
90          ObjectOutputStream oos = new ObjectOutputStream(bos)) {
91        oos.writeObject(value);
92        oos.flush();
93        return bos.toByteArray();
94      } catch (Exception e) {
95        throw new CacheException("Error serializing object.  Cause: " + e, e);
96      }
97    }
98  
99    private Serializable deserialize(byte[] value) {
100     SerialFilterChecker.check();
101     Serializable result;
102     try (ByteArrayInputStream bis = new ByteArrayInputStream(value);
103         ObjectInputStream ois = new CustomObjectInputStream(bis)) {
104       result = (Serializable) ois.readObject();
105     } catch (Exception e) {
106       throw new CacheException("Error deserializing object.  Cause: " + e, e);
107     }
108     return result;
109   }
110 
111   public static class CustomObjectInputStream extends ObjectInputStream {
112 
113     public CustomObjectInputStream(InputStream in) throws IOException {
114       super(in);
115     }
116 
117     @Override
118     protected Class<?> resolveClass(ObjectStreamClass desc) throws ClassNotFoundException {
119       return Resources.classForName(desc.getName());
120     }
121 
122   }
123 
124 }