1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.log4j.lf5.viewer.configure;
18
19 import java.io.BufferedInputStream;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
28 import java.net.URL;
29 import java.util.Iterator;
30 import java.util.LinkedList;
31
32
33
34
35
36
37
38
39
40
41
42
43 public class MRUFileManager {
44
45
46
47 private static final String CONFIG_FILE_NAME = "mru_file_manager";
48 private static final int DEFAULT_MAX_SIZE = 3;
49
50
51
52
53
54
55
56
57 private int _maxSize = 0;
58 private LinkedList _mruFileList;
59
60
61
62
63 public MRUFileManager() {
64 load();
65 setMaxSize(DEFAULT_MAX_SIZE);
66 }
67
68 public MRUFileManager(int maxSize) {
69 load();
70 setMaxSize(maxSize);
71 }
72
73
74
75
76
77
78
79 public void save() {
80 File file = new File(getFilename());
81
82 try {
83 ObjectOutputStream oos = new ObjectOutputStream(new
84 FileOutputStream(file));
85 oos.writeObject(_mruFileList);
86 oos.flush();
87 oos.close();
88 } catch (Exception e) {
89
90 e.printStackTrace();
91 }
92 }
93
94
95
96
97 public int size() {
98 return _mruFileList.size();
99 }
100
101
102
103
104
105 public Object getFile(int index) {
106 if (index < size()) {
107 return _mruFileList.get(index);
108 }
109
110 return null;
111 }
112
113
114
115
116 public InputStream getInputStream(int index) throws IOException,
117 FileNotFoundException {
118 if (index < size()) {
119 Object o = getFile(index);
120 if (o instanceof File) {
121 return getInputStream((File) o);
122 } else {
123 return getInputStream((URL) o);
124 }
125 }
126 return null;
127 }
128
129
130
131
132 public void set(File file) {
133 setMRU(file);
134 }
135
136
137
138
139 public void set(URL url) {
140 setMRU(url);
141 }
142
143
144
145
146 public String[] getMRUFileList() {
147 if (size() == 0) {
148 return null;
149 }
150
151 String[] ss = new String[size()];
152
153 for (int i = 0; i < size(); i++) {
154 Object o = getFile(i);
155 if (o instanceof File) {
156 ss[i] = ((File) o).getAbsolutePath();
157 } else
158 {
159 ss[i] = o.toString();
160 }
161
162 }
163
164 return ss;
165 }
166
167
168
169
170
171
172 public void moveToTop(int index) {
173 _mruFileList.add(0, _mruFileList.remove(index));
174 }
175
176
177
178
179
180
181
182 public static void createConfigurationDirectory() {
183 String home = System.getProperty("user.home");
184 String sep = System.getProperty("file.separator");
185 File f = new File(home + sep + "lf5");
186 if (!f.exists()) {
187 try {
188 f.mkdir();
189 } catch (SecurityException e) {
190 e.printStackTrace();
191 }
192 }
193
194 }
195
196
197
198
199
200
201
202
203
204 protected InputStream getInputStream(File file) throws IOException,
205 FileNotFoundException {
206 BufferedInputStream reader =
207 new BufferedInputStream(new FileInputStream(file));
208
209 return reader;
210 }
211
212
213
214
215
216
217
218 protected InputStream getInputStream(URL url) throws IOException {
219 return url.openStream();
220 }
221
222
223
224
225 protected void setMRU(Object o) {
226 int index = _mruFileList.indexOf(o);
227
228 if (index == -1) {
229 _mruFileList.add(0, o);
230 setMaxSize(_maxSize);
231 } else {
232 moveToTop(index);
233 }
234 }
235
236
237
238
239
240 protected void load() {
241 createConfigurationDirectory();
242 File file = new File(getFilename());
243 if (file.exists()) {
244 try {
245 ObjectInputStream ois = new ObjectInputStream(
246 new FileInputStream(file));
247 _mruFileList = (LinkedList) ois.readObject();
248 ois.close();
249
250
251 Iterator it = _mruFileList.iterator();
252 while (it.hasNext()) {
253 Object o = it.next();
254 if (!(o instanceof File) && !(o instanceof URL)) {
255 it.remove();
256 }
257 }
258 } catch (Exception e) {
259 _mruFileList = new LinkedList();
260 }
261 } else {
262 _mruFileList = new LinkedList();
263 }
264
265 }
266
267 protected String getFilename() {
268 String home = System.getProperty("user.home");
269 String sep = System.getProperty("file.separator");
270
271 return home + sep + "lf5" + sep + CONFIG_FILE_NAME;
272 }
273
274
275
276
277 protected void setMaxSize(int maxSize) {
278 if (maxSize < _mruFileList.size()) {
279 for (int i = 0; i < _mruFileList.size() - maxSize; i++) {
280 _mruFileList.removeLast();
281 }
282 }
283
284 _maxSize = maxSize;
285 }
286
287
288
289
290
291
292
293 }