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 101-130
Link Here
|
101 |
} |
117 |
} |
102 |
|
118 |
|
103 |
/** |
119 |
/** |
104 |
* Creates an association between a filename and a File inputOutputObject, |
120 |
* Creates an association between a ObjectId 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 objectId - 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 objectId, String filename) { |
110 |
reserveFile(filename,null); |
126 |
reserveFile(objectId, filename, null, false); |
111 |
} |
127 |
} |
112 |
|
128 |
|
113 |
/** |
129 |
/** |
114 |
* Creates an association between a filename and a File inputOutputObject, |
130 |
* Creates an association between a ObjectId and a File inputOutputObject, |
115 |
* and stores it for later use - unless it is already stored. |
131 |
* and stores it for later use. If independent is true, a file is also |
|
|
132 |
* stored even if it is already exists in the HasMap. |
116 |
* |
133 |
* |
|
|
134 |
* @param objectId - use a given object as the key for the HashMap |
117 |
* @param filename - relative (to base) or absolute file name |
135 |
* @param filename - relative (to base) or absolute file name |
118 |
* @param charsetName - the character set encoding to use for the file |
136 |
* @param charsetName - the character set encoding to use for the file |
|
|
137 |
* @param independent - if true the given filename is stored even the same filename is already stored |
119 |
*/ |
138 |
*/ |
120 |
public synchronized void reserveFile(String filename, String charsetName) { |
139 |
public synchronized void reserveFile(Object objectId, String filename, String charsetName, boolean independent) { |
121 |
if (!files.containsKey(filename)) { |
140 |
Iterator it; |
122 |
File f = new File(filename); |
141 |
FileEntry existFileentry; |
123 |
FileEntry file = |
142 |
boolean b = false; |
124 |
new FileEntry(f.isAbsolute() ? f : new File(base, filename),null,charsetName); |
143 |
it = files.values().iterator(); |
125 |
log.info("Stored: "+filename); |
144 |
while (it.hasNext() && !b) { |
126 |
files.put(filename, file); |
145 |
existFileentry = (FileEntry) it.next(); |
127 |
} |
146 |
if (existFileentry.file.getName().equals(filename)){ |
|
|
147 |
b = true; |
148 |
} |
149 |
} |
150 |
|
151 |
if (!b || independent){ |
152 |
File f = new File(filename); |
153 |
FileEntry file = |
154 |
new FileEntry(f.isAbsolute() ? f : new File(base, filename), null, charsetName); |
155 |
|
156 |
log.info("Stored: "+ filename); |
157 |
files.put(objectId, file); |
158 |
} |
159 |
|
128 |
} |
160 |
} |
129 |
|
161 |
|
130 |
/** |
162 |
/** |
Lines 130-136
Link Here
|
130 |
/** |
162 |
/** |
131 |
* Get the next line of the named file, recycle by default. |
163 |
* Get the next line of the named file, recycle by default. |
132 |
* |
164 |
* |
133 |
* @param filename |
165 |
*@param filename - name of the file |
134 |
* @return String containing the next line in the file |
166 |
* @return String containing the next line in the file |
135 |
* @throws IOException |
167 |
* @throws IOException |
136 |
*/ |
168 |
*/ |
Lines 135-158
Link Here
|
135 |
* @throws IOException |
167 |
* @throws IOException |
136 |
*/ |
168 |
*/ |
137 |
public String readLine(String filename) throws IOException { |
169 |
public String readLine(String filename) throws IOException { |
138 |
return readLine(filename, true); |
170 |
return readLine(null, filename, true, false); |
139 |
} |
171 |
} |
140 |
|
172 |
|
141 |
/** |
173 |
/** |
142 |
* Get the next line of the named file. |
174 |
* Get the next line of a file that is associated with a given object. If independent |
|
|
175 |
* is true, the file will be read independent if it is associated with more than one object. |
143 |
* |
176 |
* |
144 |
* @param filename |
177 |
* @param objectId - use a given object as the key for the HashMap |
|
|
178 |
* @param filename - name of the file |
145 |
* @param recycle - should file be restarted at EOF? |
179 |
* @param recycle - should file be restarted at EOF? |
|
|
180 |
* @param independent - read lines from the same file independent from the calling objects |
146 |
* @return String containing the next line in the file (null if EOF reached and not recycle) |
181 |
* @return String containing the next line in the file (null if EOF reached and not recycle) |
147 |
* @throws IOException |
182 |
* @throws IOException |
148 |
*/ |
183 |
*/ |
149 |
public synchronized String readLine(String filename, boolean recycle) throws IOException { |
184 |
public synchronized String readLine(Object objectId, String filename, boolean recycle, boolean independent) throws IOException { |
150 |
FileEntry fileEntry = (FileEntry) files.get(filename); |
185 |
FileEntry fileEntry = null; |
|
|
186 |
|
187 |
if (independent){ |
188 |
// get FileEntry by key if the same file is more than once in the HashMap (independent = true) |
189 |
fileEntry = (FileEntry) files.get(objectId); |
190 |
} else { |
191 |
// get FileEntry by filename if a file is only once in the HashMap (independent = false) |
192 |
fileEntry = (FileEntry) getFileEntrybyFilename(files, filename); |
193 |
} |
194 |
|
151 |
if (fileEntry != null) { |
195 |
if (fileEntry != null) { |
152 |
if (fileEntry.inputOutputObject == null) { |
196 |
if (fileEntry.inputOutputObject == null) { |
153 |
fileEntry.inputOutputObject = createBufferedReader(fileEntry, filename); |
197 |
fileEntry.inputOutputObject = createBufferedReader(fileEntry); |
154 |
} else if (!(fileEntry.inputOutputObject instanceof Reader)) { |
198 |
} else if (!(fileEntry.inputOutputObject instanceof Reader)) { |
155 |
throw new IOException("File " + filename + " already in use"); |
199 |
throw new IOException("File " + fileEntry.file.getName() + " already in use"); |
156 |
} |
200 |
} |
157 |
BufferedReader reader = (BufferedReader) fileEntry.inputOutputObject; |
201 |
BufferedReader reader = (BufferedReader) fileEntry.inputOutputObject; |
158 |
String line = reader.readLine(); |
202 |
String line = reader.readLine(); |
Lines 158-164
Link Here
|
158 |
String line = reader.readLine(); |
202 |
String line = reader.readLine(); |
159 |
if (line == null && recycle) { |
203 |
if (line == null && recycle) { |
160 |
reader.close(); |
204 |
reader.close(); |
161 |
reader = createBufferedReader(fileEntry, filename); |
205 |
reader = createBufferedReader(fileEntry); |
162 |
fileEntry.inputOutputObject = reader; |
206 |
fileEntry.inputOutputObject = reader; |
163 |
line = reader.readLine(); |
207 |
line = reader.readLine(); |
164 |
} |
208 |
} |
Lines 165-174
Link Here
|
165 |
if (log.isDebugEnabled()) log.debug("Read:"+line); |
209 |
if (log.isDebugEnabled()) log.debug("Read:"+line); |
166 |
return line; |
210 |
return line; |
167 |
} |
211 |
} |
168 |
throw new IOException("File never reserved: "+filename); |
212 |
throw new IOException("File never reserved: "+ filename); |
169 |
} |
213 |
} |
170 |
|
214 |
|
171 |
private BufferedReader createBufferedReader(FileEntry fileEntry, String filename) throws IOException { |
215 |
private BufferedReader createBufferedReader(FileEntry fileEntry) throws IOException { |
172 |
FileInputStream fis = new FileInputStream(fileEntry.file); |
216 |
FileInputStream fis = new FileInputStream(fileEntry.file); |
173 |
InputStreamReader isr = null; |
217 |
InputStreamReader isr = null; |
174 |
// If file encoding is specified, read using that encoding, otherwise use default platform encoding |
218 |
// If file encoding is specified, read using that encoding, otherwise use default platform encoding |
Lines 182-191
Link Here
|
182 |
} |
226 |
} |
183 |
|
227 |
|
184 |
public synchronized void write(String filename, String value) throws IOException { |
228 |
public synchronized void write(String filename, String value) throws IOException { |
185 |
FileEntry fileEntry = (FileEntry) files.get(filename); |
229 |
FileEntry fileEntry = (FileEntry) getFileEntrybyFilename(files, filename); |
186 |
if (fileEntry != null) { |
230 |
if (fileEntry != null) { |
187 |
if (fileEntry.inputOutputObject == null) { |
231 |
if (fileEntry.inputOutputObject == null) { |
188 |
fileEntry.inputOutputObject = createBufferedWriter(fileEntry, filename); |
232 |
fileEntry.inputOutputObject = createBufferedWriter(fileEntry); |
189 |
} else if (!(fileEntry.inputOutputObject instanceof Writer)) { |
233 |
} else if (!(fileEntry.inputOutputObject instanceof Writer)) { |
190 |
throw new IOException("File " + filename + " already in use"); |
234 |
throw new IOException("File " + filename + " already in use"); |
191 |
} |
235 |
} |
Lines 197-203
Link Here
|
197 |
} |
241 |
} |
198 |
} |
242 |
} |
199 |
|
243 |
|
200 |
private BufferedWriter createBufferedWriter(FileEntry fileEntry, String filename) throws IOException { |
244 |
private BufferedWriter createBufferedWriter(FileEntry fileEntry) throws IOException { |
201 |
FileOutputStream fos = new FileOutputStream(fileEntry.file); |
245 |
FileOutputStream fos = new FileOutputStream(fileEntry.file); |
202 |
OutputStreamWriter osw = null; |
246 |
OutputStreamWriter osw = null; |
203 |
// If file encoding is specified, write using that encoding, otherwise use default platform encoding |
247 |
// If file encoding is specified, write using that encoding, otherwise use default platform encoding |
Lines 214-220
Link Here
|
214 |
Iterator iter = files.entrySet().iterator(); |
258 |
Iterator iter = files.entrySet().iterator(); |
215 |
while (iter.hasNext()) { |
259 |
while (iter.hasNext()) { |
216 |
Map.Entry me = (Map.Entry) iter.next(); |
260 |
Map.Entry me = (Map.Entry) iter.next(); |
217 |
closeFile((String)me.getKey(),(FileEntry)me.getValue() ); |
261 |
closeFile((FileEntry)me.getValue() ); |
218 |
} |
262 |
} |
219 |
files.clear(); |
263 |
files.clear(); |
220 |
} |
264 |
} |
Lines 223-236
Link Here
|
223 |
* @param name |
267 |
* @param name |
224 |
* @throws IOException |
268 |
* @throws IOException |
225 |
*/ |
269 |
*/ |
226 |
public synchronized void closeFile(String name) throws IOException { |
270 |
public synchronized void closeFile(Object objectId) throws IOException { |
227 |
FileEntry fileEntry = (FileEntry) files.get(name); |
271 |
FileEntry fileEntry = (FileEntry) files.get(objectId); |
228 |
closeFile(name, fileEntry); |
272 |
closeFile(fileEntry); |
229 |
} |
273 |
} |
230 |
|
274 |
|
231 |
private void closeFile(String name, FileEntry fileEntry) throws IOException { |
275 |
private void closeFile(FileEntry fileEntry) throws IOException { |
232 |
if (fileEntry != null && fileEntry.inputOutputObject != null) { |
276 |
if (fileEntry != null && fileEntry.inputOutputObject != null) { |
233 |
log.info("Close: "+name); |
277 |
log.info("Close: "+fileEntry.file.getName()); |
234 |
if (fileEntry.inputOutputObject instanceof Reader) { |
278 |
if (fileEntry.inputOutputObject instanceof Reader) { |
235 |
((Reader) fileEntry.inputOutputObject).close(); |
279 |
((Reader) fileEntry.inputOutputObject).close(); |
236 |
} else if (fileEntry.inputOutputObject instanceof Writer) { |
280 |
} else if (fileEntry.inputOutputObject instanceof Writer) { |