]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/util/PersistentObjectCache.java
Add whitelist for deserialization.
[Dictionary.git] / src / com / hughes / android / util / PersistentObjectCache.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package com.hughes.android.util;
16
17 import android.content.Context;
18 import android.os.Environment;
19 import android.util.Log;
20
21 import com.hughes.android.dictionary.DictionaryApplication;
22 import com.hughes.android.dictionary.DictionaryInfo;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.InvalidClassException;
30 import java.io.ObjectInputStream;
31 import java.io.ObjectOutputStream;
32 import java.io.ObjectStreamClass;
33 import java.io.Serializable;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.LinkedHashMap;
37 import java.util.Map;
38
39 public class PersistentObjectCache {
40
41     private final File dir;
42     private final Map<String, Object> objects = new LinkedHashMap<String, Object>();
43
44     class ConstrainedOIS extends ObjectInputStream {
45       public ConstrainedOIS(InputStream in) throws IOException {
46         super(in);
47       }
48
49       protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
50         String name = desc.getName();
51         // Note: try to avoid adding more classes.
52         // LinkedHashMap is already more than enough for a DoS
53         if (!name.equals(ArrayList.class.getName()) &&
54             !name.equals(HashMap.class.getName()) &&
55             !name.equals(LinkedHashMap.class.getName()) &&
56             !name.equals(String.class.getName()) &&
57             !name.equals(DictionaryApplication.DictionaryConfig.class.getName()) &&
58             !name.equals(DictionaryInfo.class.getName()) &&
59             !name.equals(DictionaryInfo.IndexInfo.class.getName()))
60         {
61           throw new InvalidClassException("Not allowed to deserialize class", name);
62         }
63         return super.resolveClass(desc);
64       }
65     }
66
67     public synchronized <T extends Serializable> T read(final String filename, final Class<T> resultClass) {
68         try {
69             Object object = (objects.get(filename));
70             if (object != null) {
71                 return resultClass.cast(object);
72             }
73             Log.d(getClass().getSimpleName(), "Cache miss.");
74             final File src = new File(dir, filename);
75             if (!src.canRead()) {
76                 Log.d(getClass().getSimpleName(), "File empty: " + src);
77                 return null;
78             }
79             try {
80                 final ObjectInputStream in = new ConstrainedOIS(new FileInputStream(src));
81                 object = in.readObject();
82                 in.close();
83             } catch (Exception e) {
84                 Log.e(getClass().getSimpleName(), "Deserialization failed: " + src, e);
85                 return null;
86             }
87             objects.put(filename, object);
88             return resultClass.cast(object);
89         } catch (ClassCastException e) {
90             return null;
91         }
92     }
93
94     public synchronized void write(final String filename, final Serializable object) {
95         objects.put(filename, object);
96         final File dest = new File(dir, filename);
97         try {
98             final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(dest));
99             out.writeObject(object);
100             out.close();
101         } catch (Exception e) {
102             Log.e(getClass().getSimpleName(), "Serialization failed: " + dest, e);
103         }
104     }
105
106     private PersistentObjectCache(final Context context) {
107         final File filesDir = context.getFilesDir();
108         dir = filesDir != null ? filesDir : Environment.getExternalStorageDirectory();
109         if (dir == null) {
110             throw new RuntimeException("context.getFilesDir() == " + context.getFilesDir()
111                     + ", Environment.getExternalStorageDirectory()="
112                     + Environment.getExternalStorageDirectory());
113         }
114     }
115
116     public static synchronized PersistentObjectCache getInstance() {
117         if (instance == null) {
118             throw new RuntimeException("getInstance called before init.");
119         }
120         return instance;
121     }
122
123     public static synchronized PersistentObjectCache init(final Context context) {
124         if (instance == null) {
125             instance = new PersistentObjectCache(context);
126         } else {
127             if (!instance.dir.equals(context.getFilesDir())) {
128                 throw new RuntimeException("File dir changed.  old=" + instance.dir + ", new="
129                         + context.getFilesDir());
130             }
131         }
132         return instance;
133     }
134
135     private static PersistentObjectCache instance = null;
136
137 }