Lines 63-69
Link Here
|
63 |
|
63 |
|
64 |
//TODO - make "files" and "random" static as the class is a singleton? |
64 |
//TODO - make "files" and "random" static as the class is a singleton? |
65 |
|
65 |
|
66 |
private final Map files = new HashMap(); |
66 |
private final Map keys = new HashMap(); |
67 |
|
67 |
|
68 |
private static final FileServer server = new FileServer(); |
68 |
private static final FileServer server = new FileServer(); |
69 |
|
69 |
|
Lines 73-78
Link Here
|
73 |
base = new File(DEFAULT_BASE); |
73 |
base = new File(DEFAULT_BASE); |
74 |
log.info("Default base="+DEFAULT_BASE); |
74 |
log.info("Default base="+DEFAULT_BASE); |
75 |
} |
75 |
} |
|
|
76 |
|
77 |
private FileEntry getFileEntrybyFilename(Map map, String filename){ |
78 |
FileEntry fileEntry = null; |
79 |
Iterator it; |
80 |
boolean found = false; |
81 |
if (map != null){ |
82 |
it = map.values().iterator(); |
83 |
while (it.hasNext() && !found) { |
84 |
fileEntry = (FileEntry) it.next(); |
85 |
if (fileEntry.file.getName().equals(filename)){ |
86 |
found = true; |
87 |
} |
88 |
} |
89 |
} |
90 |
return fileEntry; |
91 |
} |
76 |
|
92 |
|
77 |
public static FileServer getFileServer() { |
93 |
public static FileServer getFileServer() { |
78 |
return server; |
94 |
return server; |
Lines 86-92
Link Here
|
86 |
if (filesOpen()) { |
102 |
if (filesOpen()) { |
87 |
throw new IOException("Files are still open, cannot change base directory"); |
103 |
throw new IOException("Files are still open, cannot change base directory"); |
88 |
} |
104 |
} |
89 |
files.clear(); |
105 |
keys.clear(); |
90 |
if (basedir != null) { |
106 |
if (basedir != null) { |
91 |
base = new File(basedir); |
107 |
base = new File(basedir); |
92 |
if (!base.isDirectory()) { |
108 |
if (!base.isDirectory()) { |
Lines 103-113
Link Here
|
103 |
/** |
119 |
/** |
104 |
* Creates an association between a filename and a File inputOutputObject, |
120 |
* Creates an association between a filename and a File inputOutputObject, |
105 |
* and stores it for later use - unless it is already stored. |
121 |
* and stores it for later use - unless it is already stored. |
106 |
* |
122 |
* @param key - use a given object as the key for the HashMap |
107 |
* @param filename - relative (to base) or absolute file name |
123 |
* @param filename - relative (to base) or absolute file name |
108 |
*/ |
124 |
*/ |
109 |
public synchronized void reserveFile(String filename) { |
125 |
public synchronized void reserveFile(Object key, String filename) { |
110 |
reserveFile(filename,null); |
126 |
reserveFile(key, filename, null, false); |
111 |
} |
127 |
} |
112 |
|
128 |
|
113 |
/** |
129 |
/** |
Lines 114-130
Link Here
|
114 |
* Creates an association between a filename and a File inputOutputObject, |
130 |
* Creates an association between a filename and a File inputOutputObject, |
115 |
* and stores it for later use - unless it is already stored. |
131 |
* and stores it for later use - unless it is already stored. |
116 |
* |
132 |
* |
|
|
133 |
* @param key - use a given object as the key for the HashMap |
117 |
* @param filename - relative (to base) or absolute file name |
134 |
* @param filename - relative (to base) or absolute file name |
118 |
* @param charsetName - the character set encoding to use for the file |
135 |
* @param charsetName - the character set encoding to use for the file |
119 |
*/ |
136 |
*/ |
120 |
public synchronized void reserveFile(String filename, String charsetName) { |
137 |
public synchronized void reserveFile(Object key, String filename, String charsetName, boolean independent) { |
121 |
if (!files.containsKey(filename)) { |
138 |
Iterator it; |
122 |
File f = new File(filename); |
139 |
FileEntry existFileentry; |
123 |
FileEntry file = |
140 |
boolean b = false; |
124 |
new FileEntry(f.isAbsolute() ? f : new File(base, filename),null,charsetName); |
141 |
it = keys.values().iterator(); |
125 |
log.info("Stored: "+filename); |
142 |
while (it.hasNext() && !b) { |
126 |
files.put(filename, file); |
143 |
existFileentry = (FileEntry) it.next(); |
127 |
} |
144 |
if (existFileentry.file.getName().equals(filename)){ |
|
|
145 |
b = true; |
146 |
} |
147 |
} |
148 |
|
149 |
if (!b || independent){ |
150 |
File f = new File(filename); |
151 |
FileEntry file = |
152 |
new FileEntry(f.isAbsolute() ? f : new File(base, filename), null, charsetName); |
153 |
|
154 |
log.info("Stored: "+ filename); |
155 |
keys.put(key, file); |
156 |
} |
157 |
|
128 |
} |
158 |
} |
129 |
|
159 |
|
130 |
/** |
160 |
/** |
Lines 130-136
Link Here
|
130 |
/** |
160 |
/** |
131 |
* Get the next line of the named file, recycle by default. |
161 |
* Get the next line of the named file, recycle by default. |
132 |
* |
162 |
* |
133 |
* @param filename |
163 |
*@param filename - name of the file |
134 |
* @return String containing the next line in the file |
164 |
* @return String containing the next line in the file |
135 |
* @throws IOException |
165 |
* @throws IOException |
136 |
*/ |
166 |
*/ |
Lines 135-141
Link Here
|
135 |
* @throws IOException |
165 |
* @throws IOException |
136 |
*/ |
166 |
*/ |
137 |
public String readLine(String filename) throws IOException { |
167 |
public String readLine(String filename) throws IOException { |
138 |
return readLine(filename, true); |
168 |
return readLine(null, filename, true, false); |
139 |
} |
169 |
} |
140 |
|
170 |
|
141 |
/** |
171 |
/** |
Lines 141-147
Link Here
|
141 |
/** |
171 |
/** |
142 |
* Get the next line of the named file. |
172 |
* Get the next line of the named file. |
143 |
* |
173 |
* |
144 |
* @param filename |
174 |
* @param key - use a given object as the key for the HashMap |
|
|
175 |
* @param filename - name of the file |
145 |
* @param recycle - should file be restarted at EOF? |
176 |
* @param recycle - should file be restarted at EOF? |
146 |
* @return String containing the next line in the file (null if EOF reached and not recycle) |
177 |
* @return String containing the next line in the file (null if EOF reached and not recycle) |
147 |
* @throws IOException |
178 |
* @throws IOException |
Lines 146-158
Link Here
|
146 |
* @return String containing the next line in the file (null if EOF reached and not recycle) |
177 |
* @return String containing the next line in the file (null if EOF reached and not recycle) |
147 |
* @throws IOException |
178 |
* @throws IOException |
148 |
*/ |
179 |
*/ |
149 |
public synchronized String readLine(String filename, boolean recycle) throws IOException { |
180 |
public synchronized String readLine(Object key, String filename, boolean recycle, boolean independent) throws IOException { |
150 |
FileEntry fileEntry = (FileEntry) files.get(filename); |
181 |
FileEntry fileEntry = null; |
|
|
182 |
|
183 |
if (independent){ |
184 |
// get FileEntry by key if the same file is more than once in the HashMap (independent = true) |
185 |
fileEntry = (FileEntry) keys.get(key); |
186 |
} else { |
187 |
// get FileEntry by filename if a file is only once in the HashMap (independent = false) |
188 |
fileEntry = (FileEntry) getFileEntrybyFilename(keys, filename); |
189 |
} |
190 |
|
151 |
if (fileEntry != null) { |
191 |
if (fileEntry != null) { |
152 |
if (fileEntry.inputOutputObject == null) { |
192 |
if (fileEntry.inputOutputObject == null) { |
153 |
fileEntry.inputOutputObject = createBufferedReader(fileEntry, filename); |
193 |
fileEntry.inputOutputObject = createBufferedReader(fileEntry); |
154 |
} else if (!(fileEntry.inputOutputObject instanceof Reader)) { |
194 |
} else if (!(fileEntry.inputOutputObject instanceof Reader)) { |
155 |
throw new IOException("File " + filename + " already in use"); |
195 |
throw new IOException("File " + fileEntry.file.getName() + " already in use"); |
156 |
} |
196 |
} |
157 |
BufferedReader reader = (BufferedReader) fileEntry.inputOutputObject; |
197 |
BufferedReader reader = (BufferedReader) fileEntry.inputOutputObject; |
158 |
String line = reader.readLine(); |
198 |
String line = reader.readLine(); |
Lines 158-164
Link Here
|
158 |
String line = reader.readLine(); |
198 |
String line = reader.readLine(); |
159 |
if (line == null && recycle) { |
199 |
if (line == null && recycle) { |
160 |
reader.close(); |
200 |
reader.close(); |
161 |
reader = createBufferedReader(fileEntry, filename); |
201 |
reader = createBufferedReader(fileEntry); |
162 |
fileEntry.inputOutputObject = reader; |
202 |
fileEntry.inputOutputObject = reader; |
163 |
line = reader.readLine(); |
203 |
line = reader.readLine(); |
164 |
} |
204 |
} |
Lines 165-174
Link Here
|
165 |
if (log.isDebugEnabled()) log.debug("Read:"+line); |
205 |
if (log.isDebugEnabled()) log.debug("Read:"+line); |
166 |
return line; |
206 |
return line; |
167 |
} |
207 |
} |
168 |
throw new IOException("File never reserved: "+filename); |
208 |
throw new IOException("File never reserved: "+ filename); |
169 |
} |
209 |
} |
170 |
|
210 |
|
171 |
private BufferedReader createBufferedReader(FileEntry fileEntry, String filename) throws IOException { |
211 |
private BufferedReader createBufferedReader(FileEntry fileEntry) throws IOException { |
172 |
FileInputStream fis = new FileInputStream(fileEntry.file); |
212 |
FileInputStream fis = new FileInputStream(fileEntry.file); |
173 |
InputStreamReader isr = null; |
213 |
InputStreamReader isr = null; |
174 |
// If file encoding is specified, read using that encoding, otherwise use default platform encoding |
214 |
// If file encoding is specified, read using that encoding, otherwise use default platform encoding |
Lines 182-191
Link Here
|
182 |
} |
222 |
} |
183 |
|
223 |
|
184 |
public synchronized void write(String filename, String value) throws IOException { |
224 |
public synchronized void write(String filename, String value) throws IOException { |
185 |
FileEntry fileEntry = (FileEntry) files.get(filename); |
225 |
FileEntry fileEntry = (FileEntry) getFileEntrybyFilename(keys, filename); |
186 |
if (fileEntry != null) { |
226 |
if (fileEntry != null) { |
187 |
if (fileEntry.inputOutputObject == null) { |
227 |
if (fileEntry.inputOutputObject == null) { |
188 |
fileEntry.inputOutputObject = createBufferedWriter(fileEntry, filename); |
228 |
fileEntry.inputOutputObject = createBufferedWriter(fileEntry); |
189 |
} else if (!(fileEntry.inputOutputObject instanceof Writer)) { |
229 |
} else if (!(fileEntry.inputOutputObject instanceof Writer)) { |
190 |
throw new IOException("File " + filename + " already in use"); |
230 |
throw new IOException("File " + filename + " already in use"); |
191 |
} |
231 |
} |
Lines 197-203
Link Here
|
197 |
} |
237 |
} |
198 |
} |
238 |
} |
199 |
|
239 |
|
200 |
private BufferedWriter createBufferedWriter(FileEntry fileEntry, String filename) throws IOException { |
240 |
private BufferedWriter createBufferedWriter(FileEntry fileEntry) throws IOException { |
201 |
FileOutputStream fos = new FileOutputStream(fileEntry.file); |
241 |
FileOutputStream fos = new FileOutputStream(fileEntry.file); |
202 |
OutputStreamWriter osw = null; |
242 |
OutputStreamWriter osw = null; |
203 |
// If file encoding is specified, write using that encoding, otherwise use default platform encoding |
243 |
// If file encoding is specified, write using that encoding, otherwise use default platform encoding |
Lines 211-222
Link Here
|
211 |
} |
251 |
} |
212 |
|
252 |
|
213 |
public void closeFiles() throws IOException { |
253 |
public void closeFiles() throws IOException { |
214 |
Iterator iter = files.entrySet().iterator(); |
254 |
Iterator iter = keys.entrySet().iterator(); |
215 |
while (iter.hasNext()) { |
255 |
while (iter.hasNext()) { |
216 |
Map.Entry me = (Map.Entry) iter.next(); |
256 |
Map.Entry me = (Map.Entry) iter.next(); |
217 |
closeFile((String)me.getKey(),(FileEntry)me.getValue() ); |
257 |
closeFile((Object)me.getKey(),(FileEntry)me.getValue() ); |
218 |
} |
258 |
} |
219 |
files.clear(); |
259 |
keys.clear(); |
220 |
} |
260 |
} |
221 |
|
261 |
|
222 |
/** |
262 |
/** |
Lines 223-236
Link Here
|
223 |
* @param name |
263 |
* @param name |
224 |
* @throws IOException |
264 |
* @throws IOException |
225 |
*/ |
265 |
*/ |
226 |
public synchronized void closeFile(String name) throws IOException { |
266 |
public synchronized void closeFile(Object key) throws IOException { |
227 |
FileEntry fileEntry = (FileEntry) files.get(name); |
267 |
FileEntry fileEntry = (FileEntry) keys.get(key); |
228 |
closeFile(name, fileEntry); |
268 |
closeFile(keys.get(key), fileEntry); |
229 |
} |
269 |
} |
230 |
|
270 |
|
231 |
private void closeFile(String name, FileEntry fileEntry) throws IOException { |
271 |
private void closeFile(Object key, FileEntry fileEntry) throws IOException { |
232 |
if (fileEntry != null && fileEntry.inputOutputObject != null) { |
272 |
if (fileEntry != null && fileEntry.inputOutputObject != null) { |
233 |
log.info("Close: "+name); |
273 |
log.info("Close: "+fileEntry.file.getName()); |
234 |
if (fileEntry.inputOutputObject instanceof Reader) { |
274 |
if (fileEntry.inputOutputObject instanceof Reader) { |
235 |
((Reader) fileEntry.inputOutputObject).close(); |
275 |
((Reader) fileEntry.inputOutputObject).close(); |
236 |
} else if (fileEntry.inputOutputObject instanceof Writer) { |
276 |
} else if (fileEntry.inputOutputObject instanceof Writer) { |
Lines 243-249
Link Here
|
243 |
} |
283 |
} |
244 |
|
284 |
|
245 |
protected boolean filesOpen() { |
285 |
protected boolean filesOpen() { |
246 |
Iterator iter = files.values().iterator(); |
286 |
Iterator iter = keys.values().iterator(); |
247 |
while (iter.hasNext()) { |
287 |
while (iter.hasNext()) { |
248 |
FileEntry fileEntry = (FileEntry) iter.next(); |
288 |
FileEntry fileEntry = (FileEntry) iter.next(); |
249 |
if (fileEntry.inputOutputObject != null) { |
289 |
if (fileEntry.inputOutputObject != null) { |