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.*;
20 import java.net.URL;
21 import java.util.Iterator;
22 import java.util.LinkedList;
23
24
25 /***
26 * <p>MRUFileManager handles the storage and retrival the most
27 * recently opened log files.
28 *
29 * @author Brad Marlborough
30 * @author Richard Hurst
31 */
32
33
34
35 public class MRUFileManager {
36
37
38
39 private static final String CONFIG_FILE_NAME = "mru_file_manager";
40 private static final int DEFAULT_MAX_SIZE = 3;
41
42
43
44
45
46
47
48
49 private int _maxSize = 0;
50 private LinkedList _mruFileList;
51
52
53
54
55 public MRUFileManager() {
56 load();
57 setMaxSize(DEFAULT_MAX_SIZE);
58 }
59
60 public MRUFileManager(int maxSize) {
61 load();
62 setMaxSize(maxSize);
63 }
64
65
66
67
68 /***
69 * Saves a list of MRU files out to a file.
70 */
71 public void save() {
72 File file = new File(getFilename());
73
74 try {
75 ObjectOutputStream oos = new ObjectOutputStream(new
76 FileOutputStream(file));
77 oos.writeObject(_mruFileList);
78 oos.flush();
79 oos.close();
80 } catch (Exception e) {
81
82 e.printStackTrace();
83 }
84 }
85
86 /***
87 * Gets the size of the MRU file list.
88 */
89 public int size() {
90 return _mruFileList.size();
91 }
92
93 /***
94 * Returns a particular file name stored in a MRU file
95 * list based on an index value.
96 */
97 public Object getFile(int index) {
98 if (index < size()) {
99 return _mruFileList.get(index);
100 }
101
102 return null;
103 }
104
105 /***
106 * Returns a input stream to the resource at the specified index
107 */
108 public InputStream getInputStream(int index) throws IOException,
109 FileNotFoundException {
110 if (index < size()) {
111 Object o = getFile(index);
112 if (o instanceof File) {
113 return getInputStream((File) o);
114 } else {
115 return getInputStream((URL) o);
116 }
117 }
118 return null;
119 }
120
121 /***
122 * Adds a file name to the MRU file list.
123 */
124 public void set(File file) {
125 setMRU(file);
126 }
127
128 /***
129 * Adds a url to the MRU file list.
130 */
131 public void set(URL url) {
132 setMRU(url);
133 }
134
135 /***
136 * Gets the list of files stored in the MRU file list.
137 */
138 public String[] getMRUFileList() {
139 if (size() == 0) {
140 return null;
141 }
142
143 String[] ss = new String[size()];
144
145 for (int i = 0; i < size(); i++) {
146 Object o = getFile(i);
147 if (o instanceof File) {
148 ss[i] = ((File) o).getAbsolutePath();
149 } else
150 {
151 ss[i] = o.toString();
152 }
153
154 }
155
156 return ss;
157 }
158
159 /***
160 * Moves the the index to the top of the MRU List
161 *
162 * @param index The index to be first in the mru list
163 */
164 public void moveToTop(int index) {
165 _mruFileList.add(0, _mruFileList.remove(index));
166 }
167
168 /***
169 * Creates the directory where the MRU file list will be written.
170 * The "lf5" directory is created in the Documents and Settings
171 * directory on Windows 2000 machines and where ever the user.home
172 * variable points on all other platforms.
173 */
174 public static void createConfigurationDirectory() {
175 String home = System.getProperty("user.home");
176 String sep = System.getProperty("file.separator");
177 File f = new File(home + sep + "lf5");
178 if (!f.exists()) {
179 try {
180 f.mkdir();
181 } catch (SecurityException e) {
182 e.printStackTrace();
183 }
184 }
185
186 }
187
188
189
190 /***
191 * Gets an input stream for the corresponding file.
192 *
193 * @param file The file to create the input stream from.
194 * @return InputStream
195 */
196 protected InputStream getInputStream(File file) throws IOException,
197 FileNotFoundException {
198 BufferedInputStream reader =
199 new BufferedInputStream(new FileInputStream(file));
200
201 return reader;
202 }
203
204 /***
205 * Gets an input stream for the corresponding URL.
206 *
207 * @param url The url to create the input stream from.
208 * @return InputStream
209 */
210 protected InputStream getInputStream(URL url) throws IOException {
211 return url.openStream();
212 }
213
214 /***
215 * Adds an object to the mru.
216 */
217 protected void setMRU(Object o) {
218 int index = _mruFileList.indexOf(o);
219
220 if (index == -1) {
221 _mruFileList.add(0, o);
222 setMaxSize(_maxSize);
223 } else {
224 moveToTop(index);
225 }
226 }
227
228 /***
229 * Loads the MRU file list in from a file and stores it in a LinkedList.
230 * If no file exists, a new LinkedList is created.
231 */
232 protected void load() {
233 createConfigurationDirectory();
234 File file = new File(getFilename());
235 if (file.exists()) {
236 try {
237 ObjectInputStream ois = new ObjectInputStream(
238 new FileInputStream(file));
239 _mruFileList = (LinkedList) ois.readObject();
240 ois.close();
241
242
243 Iterator it = _mruFileList.iterator();
244 while (it.hasNext()) {
245 Object o = it.next();
246 if (!(o instanceof File) && !(o instanceof URL)) {
247 it.remove();
248 }
249 }
250 } catch (Exception e) {
251 _mruFileList = new LinkedList();
252 }
253 } else {
254 _mruFileList = new LinkedList();
255 }
256
257 }
258
259 protected String getFilename() {
260 String home = System.getProperty("user.home");
261 String sep = System.getProperty("file.separator");
262
263 return home + sep + "lf5" + sep + CONFIG_FILE_NAME;
264 }
265
266 /***
267 * Ensures that the MRU list will have a MaxSize.
268 */
269 protected void setMaxSize(int maxSize) {
270 if (maxSize < _mruFileList.size()) {
271 for (int i = 0; i < _mruFileList.size() - maxSize; i++) {
272 _mruFileList.removeLast();
273 }
274 }
275
276 _maxSize = maxSize;
277 }
278
279
280
281
282
283
284
285 }