KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > lf5 > viewer > configure > MRUFileManager


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
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.log4j.lf5.viewer.configure;
17
18 import java.io.*;
19 import java.net.URL JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.LinkedList JavaDoc;
22
23
24 /**
25  * <p>MRUFileManager handles the storage and retrival the most
26  * recently opened log files.
27  *
28  * @author Brad Marlborough
29  * @author Richard Hurst
30  */

31
32 // Contributed by ThoughtWorks Inc.
33

34 public class MRUFileManager {
35   //--------------------------------------------------------------------------
36
// Constants:
37
//--------------------------------------------------------------------------
38
private static final String JavaDoc CONFIG_FILE_NAME = "mru_file_manager";
39   private static final int DEFAULT_MAX_SIZE = 3;
40
41   //--------------------------------------------------------------------------
42
// Protected Variables:
43
//--------------------------------------------------------------------------
44

45   //--------------------------------------------------------------------------
46
// Private Variables:
47
//--------------------------------------------------------------------------
48
private int _maxSize = 0;
49   private LinkedList JavaDoc _mruFileList;
50
51   //--------------------------------------------------------------------------
52
// Constructors:
53
//--------------------------------------------------------------------------
54
public MRUFileManager() {
55     load();
56     setMaxSize(DEFAULT_MAX_SIZE);
57   }
58
59   public MRUFileManager(int maxSize) {
60     load();
61     setMaxSize(maxSize);
62   }
63   //--------------------------------------------------------------------------
64
// Public Methods:
65
//--------------------------------------------------------------------------
66

67   /**
68    * Saves a list of MRU files out to a file.
69    */

70   public void save() {
71     File file = new File(getFilename());
72
73     try {
74       ObjectOutputStream oos = new ObjectOutputStream(new
75           FileOutputStream(file));
76       oos.writeObject(_mruFileList);
77       oos.flush();
78       oos.close();
79     } catch (Exception JavaDoc e) {
80       // do nothing
81
e.printStackTrace();
82     }
83   }
84
85   /**
86    * Gets the size of the MRU file list.
87    */

88   public int size() {
89     return _mruFileList.size();
90   }
91
92   /**
93    * Returns a particular file name stored in a MRU file
94    * list based on an index value.
95    */

96   public Object JavaDoc getFile(int index) {
97     if (index < size()) {
98       return _mruFileList.get(index);
99     }
100
101     return null;
102   }
103
104   /**
105    * Returns a input stream to the resource at the specified index
106    */

107   public InputStream getInputStream(int index) throws IOException,
108       FileNotFoundException {
109     if (index < size()) {
110       Object JavaDoc o = getFile(index);
111       if (o instanceof File) {
112         return getInputStream((File) o);
113       } else {
114         return getInputStream((URL JavaDoc) o);
115       }
116     }
117     return null;
118   }
119
120   /**
121    * Adds a file name to the MRU file list.
122    */

123   public void set(File file) {
124     setMRU(file);
125   }
126
127   /**
128    * Adds a url to the MRU file list.
129    */

130   public void set(URL JavaDoc url) {
131     setMRU(url);
132   }
133
134   /**
135    * Gets the list of files stored in the MRU file list.
136    */

137   public String JavaDoc[] getMRUFileList() {
138     if (size() == 0) {
139       return null;
140     }
141
142     String JavaDoc[] ss = new String JavaDoc[size()];
143
144     for (int i = 0; i < size(); i++) {
145       Object JavaDoc o = getFile(i);
146       if (o instanceof File) {
147         ss[i] = ((File) o).getAbsolutePath();
148       } else // must be a url
149
{
150         ss[i] = o.toString();
151       }
152
153     }
154
155     return ss;
156   }
157
158   /**
159    * Moves the the index to the top of the MRU List
160    *
161    * @param index The index to be first in the mru list
162    */

163   public void moveToTop(int index) {
164     _mruFileList.add(0, _mruFileList.remove(index));
165   }
166
167   /**
168    * Creates the directory where the MRU file list will be written.
169    * The "lf5" directory is created in the Documents and Settings
170    * directory on Windows 2000 machines and where ever the user.home
171    * variable points on all other platforms.
172    */

173   public static void createConfigurationDirectory() {
174     String JavaDoc home = System.getProperty("user.home");
175     String JavaDoc sep = System.getProperty("file.separator");
176     File f = new File(home + sep + "lf5");
177     if (!f.exists()) {
178       try {
179         f.mkdir();
180       } catch (SecurityException JavaDoc e) {
181         e.printStackTrace();
182       }
183     }
184
185   }
186   //--------------------------------------------------------------------------
187
// Protected Methods:
188
//--------------------------------------------------------------------------
189
/**
190    * Gets an input stream for the corresponding file.
191    *
192    * @param file The file to create the input stream from.
193    * @return InputStream
194    */

195   protected InputStream getInputStream(File file) throws IOException,
196       FileNotFoundException {
197     BufferedInputStream reader =
198         new BufferedInputStream(new FileInputStream(file));
199
200     return reader;
201   }
202
203   /**
204    * Gets an input stream for the corresponding URL.
205    *
206    * @param url The url to create the input stream from.
207    * @return InputStream
208    */

209   protected InputStream getInputStream(URL JavaDoc url) throws IOException {
210     return url.openStream();
211   }
212
213   /**
214    * Adds an object to the mru.
215    */

216   protected void setMRU(Object JavaDoc o) {
217     int index = _mruFileList.indexOf(o);
218
219     if (index == -1) {
220       _mruFileList.add(0, o);
221       setMaxSize(_maxSize);
222     } else {
223       moveToTop(index);
224     }
225   }
226
227   /**
228    * Loads the MRU file list in from a file and stores it in a LinkedList.
229    * If no file exists, a new LinkedList is created.
230    */

231   protected void load() {
232     createConfigurationDirectory();
233     File file = new File(getFilename());
234     if (file.exists()) {
235       try {
236         ObjectInputStream ois = new ObjectInputStream(
237             new FileInputStream(file));
238         _mruFileList = (LinkedList JavaDoc) ois.readObject();
239         ois.close();
240
241         // check that only files and url are in linked list
242
Iterator JavaDoc it = _mruFileList.iterator();
243         while (it.hasNext()) {
244           Object JavaDoc o = it.next();
245           if (!(o instanceof File) && !(o instanceof URL JavaDoc)) {
246             it.remove();
247           }
248         }
249       } catch (Exception JavaDoc e) {
250         _mruFileList = new LinkedList JavaDoc();
251       }
252     } else {
253       _mruFileList = new LinkedList JavaDoc();
254     }
255
256   }
257
258   protected String JavaDoc getFilename() {
259     String JavaDoc home = System.getProperty("user.home");
260     String JavaDoc sep = System.getProperty("file.separator");
261
262     return home + sep + "lf5" + sep + CONFIG_FILE_NAME;
263   }
264
265   /**
266    * Ensures that the MRU list will have a MaxSize.
267    */

268   protected void setMaxSize(int maxSize) {
269     if (maxSize < _mruFileList.size()) {
270       for (int i = 0; i < _mruFileList.size() - maxSize; i++) {
271         _mruFileList.removeLast();
272       }
273     }
274
275     _maxSize = maxSize;
276   }
277   //--------------------------------------------------------------------------
278
// Private Methods:
279
//--------------------------------------------------------------------------
280

281   //--------------------------------------------------------------------------
282
// Nested Top-Level Classes or Interfaces
283
//--------------------------------------------------------------------------
284
}
Popular Tags