1 | /* |
---|
2 | * <p>$Id: QueuedFileServer.java,v 1.1 2001/01/15 15:38:47 pn Exp $</p><br> |
---|
3 | * |
---|
4 | * <p>The class AtaTest provides some facilities for writing application test programs.</p> |
---|
5 | * |
---|
6 | * @author Phillip Link |
---|
7 | * @since 1.2 |
---|
8 | * |
---|
9 | */ |
---|
10 | package fr.alcatel.ether.app.common; |
---|
11 | |
---|
12 | // JDK - utils |
---|
13 | import java.util.Collections; |
---|
14 | import java.util.Map; |
---|
15 | import java.util.HashMap; |
---|
16 | import java.util.Vector; |
---|
17 | import java.util.ArrayList; |
---|
18 | // JDK - rmi |
---|
19 | import java.rmi.RemoteException; |
---|
20 | // JDK - io |
---|
21 | import java.io.File; |
---|
22 | import java.io.FileInputStream; |
---|
23 | import java.io.FileOutputStream; |
---|
24 | import java.io.BufferedOutputStream; |
---|
25 | import java.io.IOException; |
---|
26 | import java.io.FileNotFoundException; |
---|
27 | // Ether Remote File Classes |
---|
28 | import fr.alcatel.ether.app.common.RemoteFile; |
---|
29 | import fr.alcatel.ether.app.common.RemoteFileImpl; |
---|
30 | import fr.alcatel.ether.app.common.RemoteFileManager; |
---|
31 | // flafi CopyFile |
---|
32 | import flafi.util.FileCopy2; |
---|
33 | |
---|
34 | import fr.alcatel.ether.tools.Trace; |
---|
35 | |
---|
36 | public class QueuedFileServer implements Runnable { |
---|
37 | |
---|
38 | private static final boolean DEBUG = true; |
---|
39 | |
---|
40 | /** RemoteFileManager that we work for - our boss */ |
---|
41 | RemoteFileManager manager = null; |
---|
42 | |
---|
43 | /** Event queue */ |
---|
44 | private Vector queue = new Vector(); |
---|
45 | |
---|
46 | /** Collection of file input streams, keyed on file name */ |
---|
47 | private final Map inputs = Collections.synchronizedMap( new HashMap() ); |
---|
48 | /** Collection of file input buffers, keyed on file name */ |
---|
49 | private final Map inputBuffers = Collections.synchronizedMap( new HashMap() ); |
---|
50 | |
---|
51 | /** Collection of currently open file input streams, keyed on file name */ |
---|
52 | private final Map open = Collections.synchronizedMap( new HashMap() ); |
---|
53 | |
---|
54 | /** Default buffer size used for copying */ |
---|
55 | public static final int DEFAULT_BUFFER_SIZE = 4096; |
---|
56 | /** Default buffer size used for copying */ |
---|
57 | public static final int MIN_BUFFER_SIZE = 128; |
---|
58 | |
---|
59 | /** Current buffer size used for copying */ |
---|
60 | private int bufferSize = DEFAULT_BUFFER_SIZE; |
---|
61 | |
---|
62 | /** |
---|
63 | * <p>Basic constructor.</p> |
---|
64 | * <p>Starts thread, which waits for operations to perform.</p> |
---|
65 | */ |
---|
66 | public QueuedFileServer( RemoteFileManager manager ) |
---|
67 | { |
---|
68 | this.manager = manager; |
---|
69 | |
---|
70 | Thread t = new Thread(this, "QueuedFileServer"); |
---|
71 | |
---|
72 | t.start(); |
---|
73 | } |
---|
74 | |
---|
75 | // ------------------------------------------------------------- |
---|
76 | // ------------------------------------------------------------- |
---|
77 | // Regular file operations |
---|
78 | // ------------------------------------------------------------- |
---|
79 | // ------------------------------------------------------------- |
---|
80 | |
---|
81 | /** |
---|
82 | * Check if a file is already open on this server. |
---|
83 | * |
---|
84 | * @param file: the file to check |
---|
85 | */ |
---|
86 | private synchronized boolean isOpen( File file ) { |
---|
87 | FileInputStream input = (FileInputStream)inputs.get( file ); |
---|
88 | if ( input != null ) { |
---|
89 | return true; |
---|
90 | } |
---|
91 | return false; |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * <p>Rapatrier un fichier distant sur ce serveur.</p> |
---|
96 | * <p>Le fichier créé est un fichier temporaire.</p> |
---|
97 | * |
---|
98 | * @param file: le fichier à raptrier |
---|
99 | */ |
---|
100 | public File opRetrieve( RemoteFile file ) { |
---|
101 | File retrieved = null; |
---|
102 | try { |
---|
103 | // ------------------------------------------- |
---|
104 | // Debugging |
---|
105 | // ------------------------------------------- |
---|
106 | if ( DEBUG ) { debug( "QueuedFileServer retrieving file " + file); } |
---|
107 | |
---|
108 | RemoteFileManager distant = file.getFileManager( ); |
---|
109 | // ----------------------------------------------- |
---|
110 | // This is our file |
---|
111 | // Create a temporary copy so that clients |
---|
112 | // can delete it without worrying about deleting |
---|
113 | // the original. |
---|
114 | // ----------------------------------------------- |
---|
115 | if ( manager.equals(distant) ) { |
---|
116 | File local = manager.getLocalFile( file ); |
---|
117 | retrieved = File.createTempFile( "eth", ".tmp" ); |
---|
118 | FileCopy2.copy( local, retrieved ); |
---|
119 | } |
---|
120 | // ----------------------------------------------- |
---|
121 | // Read bytes from remote file manager and |
---|
122 | // write to a local stream |
---|
123 | // ----------------------------------------------- |
---|
124 | else { |
---|
125 | retrieved = File.createTempFile( "eth", ".tmp" ); |
---|
126 | FileOutputStream os = new FileOutputStream( retrieved ); |
---|
127 | BufferedOutputStream out = new BufferedOutputStream( os ); |
---|
128 | distant.open( file ); |
---|
129 | try { |
---|
130 | while ( true ) { |
---|
131 | byte[] buffer = distant.read( file ); |
---|
132 | out.write( buffer, 0, buffer.length ); |
---|
133 | } |
---|
134 | } |
---|
135 | // Exception signals that there is nothing left to read |
---|
136 | catch ( IOException ignored ) { |
---|
137 | //if ( DEBUG ) { Trace.debug( this, "Finished reading file ", ignored ); } |
---|
138 | } |
---|
139 | out.close( ); |
---|
140 | distant.close( file ); |
---|
141 | } |
---|
142 | |
---|
143 | if ( DEBUG ) { debug( "QueuedFileServer ENDING retrieving file " + file); } |
---|
144 | } |
---|
145 | catch ( FileNotFoundException fileNotFound ) { |
---|
146 | if ( DEBUG ) { debug( "Could not find file '" + file + "' !", fileNotFound ); } |
---|
147 | } |
---|
148 | catch ( IOException ioProblem ) { |
---|
149 | if ( DEBUG ) { debug( "Had a problem retrieving '" + file + "' !", ioProblem ); } |
---|
150 | } |
---|
151 | catch ( Exception exception ) { |
---|
152 | if ( DEBUG ) { debug( "Had a problem retrieving '" + file + "' !", exception ); } |
---|
153 | } |
---|
154 | return retrieved; |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | /** |
---|
159 | * Copier un fichier distant. |
---|
160 | * |
---|
161 | * @param src: le fichier source |
---|
162 | * @param dest: le fichier destination |
---|
163 | */ |
---|
164 | public boolean opCopy( RemoteFile src, RemoteFile dest ) { |
---|
165 | boolean success = false; |
---|
166 | boolean deleteTemp = false; |
---|
167 | File source = null; |
---|
168 | try { |
---|
169 | // ------------------------------------------- |
---|
170 | // Debugging |
---|
171 | // ------------------------------------------- |
---|
172 | if ( DEBUG ) { debug( "QueuedFileServer copying file." ); } |
---|
173 | if ( DEBUG ) { debug( "Source file is " + src ); } |
---|
174 | if ( DEBUG ) { debug( "Destination file is " + dest ); } |
---|
175 | |
---|
176 | RemoteFileManager distant = dest.getFileManager( ); |
---|
177 | // ----------------------------------------------- |
---|
178 | // This is a job for us |
---|
179 | // ----------------------------------------------- |
---|
180 | if ( manager.equals(distant) ) { |
---|
181 | File destination = manager.getLocalFile( dest ); |
---|
182 | RemoteFileManager other = src.getFileManager( ); |
---|
183 | if ( manager.equals(other) ) { |
---|
184 | // ------------------------------------------- |
---|
185 | // Retrieve our local source file. |
---|
186 | // ------------------------------------------- |
---|
187 | if ( DEBUG ) { debug( "Retrieving local file " + src ); } |
---|
188 | source = manager.getLocalFile( src ); |
---|
189 | } |
---|
190 | else { |
---|
191 | // ------------------------------------------- |
---|
192 | // Ask the destination manager to retrieve the |
---|
193 | // source file for us. |
---|
194 | // ------------------------------------------- |
---|
195 | if ( DEBUG ) { debug( "Retrieving remote file " + src ); } |
---|
196 | deleteTemp = true; |
---|
197 | //source = distant.retrieve( src ); |
---|
198 | source = opRetrieve(src); |
---|
199 | } |
---|
200 | // --------------------------------------------- |
---|
201 | // This is a job for the remote manager |
---|
202 | // --------------------------------------------- |
---|
203 | if ( DEBUG ) { debug( "Copying source file " + source.getAbsolutePath() ); } |
---|
204 | FileCopy2.copy( source, destination ); |
---|
205 | success = true; |
---|
206 | } |
---|
207 | // ----------------------------------------------- |
---|
208 | // This is a job for the remote manager |
---|
209 | // ----------------------------------------------- |
---|
210 | else { |
---|
211 | if ( DEBUG ) { debug( "Warning trying to copy file '" + dest + "' on wrong QueuedFileServer !" ); } |
---|
212 | success = distant.copy( src, dest ); |
---|
213 | } |
---|
214 | } |
---|
215 | catch ( FileNotFoundException fileNotFound ) { |
---|
216 | if ( DEBUG ) { debug( "Could not find file '" + src + "' !", fileNotFound ); } |
---|
217 | } |
---|
218 | catch ( IOException ioProblem ) { |
---|
219 | if ( DEBUG ) { debug( "Had a problem copying '" + src + "' to '" + dest + "' !", ioProblem ); } |
---|
220 | } |
---|
221 | catch ( Exception exception ) { |
---|
222 | if ( DEBUG ) { debug( "Had a problem copying '" + src + "' to '" + dest + "' !", exception ); } |
---|
223 | } |
---|
224 | // ------------------------------------------- |
---|
225 | // Delete the temporary file. |
---|
226 | // ------------------------------------------- |
---|
227 | finally { |
---|
228 | if ( deleteTemp && source != null ) { |
---|
229 | source.delete( ); |
---|
230 | } |
---|
231 | } |
---|
232 | return success; |
---|
233 | } |
---|
234 | |
---|
235 | /** |
---|
236 | * Transférer un fichier distant. |
---|
237 | * |
---|
238 | * @param src: le fichier source |
---|
239 | * @param dest: le fichier destination |
---|
240 | */ |
---|
241 | public boolean opMove( RemoteFile src, RemoteFile dest ) { |
---|
242 | boolean success = false; |
---|
243 | boolean deleteTemp = false; |
---|
244 | File source = null; |
---|
245 | try { |
---|
246 | // ------------------------------------------- |
---|
247 | // Debugging |
---|
248 | // ------------------------------------------- |
---|
249 | if ( DEBUG ) { debug( "QueuedFileServer moving file." ); } |
---|
250 | if ( DEBUG ) { debug( "Source file is " + src ); } |
---|
251 | if ( DEBUG ) { debug( "Destination file is " + dest ); } |
---|
252 | success = opCopy( src, dest ); |
---|
253 | if ( success ) { |
---|
254 | success = opDelete( src ); |
---|
255 | } |
---|
256 | } |
---|
257 | catch ( Exception exception ) { |
---|
258 | if ( DEBUG ) { debug( "Had a problem moving '" + src + "' to '" + dest + "' !", exception ); } |
---|
259 | } |
---|
260 | return success; |
---|
261 | } |
---|
262 | |
---|
263 | /** |
---|
264 | * Supprimer un fichier distant. |
---|
265 | * |
---|
266 | * @param file: le fichier à supprimer |
---|
267 | */ |
---|
268 | public boolean opDelete( RemoteFile file ) { |
---|
269 | boolean success = false; |
---|
270 | try { |
---|
271 | RemoteFileManager distant = file.getFileManager( ); |
---|
272 | // ------------------------------------------- |
---|
273 | // Delete the file found on this local filesystem. |
---|
274 | // ------------------------------------------- |
---|
275 | if ( manager.equals(distant) ) { |
---|
276 | if ( DEBUG ) { debug( "QueuedFileServer deleting file '" + file + "'." ); } |
---|
277 | File toBeDeleted = manager.getLocalFile( file ); |
---|
278 | if ( toBeDeleted != null ) { |
---|
279 | toBeDeleted.delete( ); |
---|
280 | success = true; |
---|
281 | } |
---|
282 | } |
---|
283 | // ------------------------------------------- |
---|
284 | // Ask the destination manager to delete the |
---|
285 | // source file for us. |
---|
286 | // This case should not happen |
---|
287 | // ------------------------------------------- |
---|
288 | else { |
---|
289 | if ( DEBUG ) { debug( "Warning: trying to delete file '" + file + "' on wrong QueuedFileServer !" ); } |
---|
290 | success = manager.delete( file ); |
---|
291 | } |
---|
292 | } |
---|
293 | catch ( FileNotFoundException fileNotFound ) { |
---|
294 | if ( DEBUG ) { debug( "Could not find file '" + file + "' !", fileNotFound ); } |
---|
295 | } |
---|
296 | catch ( IOException ioProblem ) { |
---|
297 | if ( DEBUG ) { debug( "Had a problem deleting '" + file + "' !", ioProblem ); } |
---|
298 | } |
---|
299 | catch ( Exception exception ) { |
---|
300 | if ( DEBUG ) { debug( "Had a problem deleting '" + file + "' t !", exception ); } |
---|
301 | } |
---|
302 | return success; |
---|
303 | } |
---|
304 | |
---|
305 | /** |
---|
306 | * <p>Lister tous les fichiers dans un répertoires, ainsi que dans les sous-répertoires.</p> |
---|
307 | * |
---|
308 | * @param directory: le répertoire à lister |
---|
309 | * @return : la liste des fichiers se trouvant dans le répertoire |
---|
310 | */ |
---|
311 | public ArrayList opList( RemoteFile directory ) { |
---|
312 | final String ERROR = "Had a problem listing '" + directory + "' !"; |
---|
313 | ArrayList remoteFiles = null; |
---|
314 | try { |
---|
315 | RemoteFileManager distant = directory.getFileManager( ); |
---|
316 | // ------------------------------------------- |
---|
317 | // List the directory. |
---|
318 | // ------------------------------------------- |
---|
319 | if ( manager.equals(distant) ) { |
---|
320 | // ----------------------------------------- |
---|
321 | // Debugging |
---|
322 | // ----------------------------------------- |
---|
323 | if ( DEBUG ) { debug( "QueuedFileServer listing directory " + directory ); } |
---|
324 | remoteFiles = opList( directory, directory.getAbsolutePath() ); |
---|
325 | } |
---|
326 | // ------------------------------------------- |
---|
327 | // Ask the destination manager to retrieve the |
---|
328 | // source file for us. |
---|
329 | // ------------------------------------------- |
---|
330 | else { |
---|
331 | if ( DEBUG ) { debug( "Warning: trying to list directory '" + directory + "' on wrong QueuedFileServer !" ); } |
---|
332 | remoteFiles = distant.list( directory ); |
---|
333 | } |
---|
334 | } |
---|
335 | catch ( RemoteException remoteProblem ) { |
---|
336 | if ( DEBUG ) { debug( ERROR, remoteProblem ); } |
---|
337 | } |
---|
338 | catch ( IOException ioProblem ) { |
---|
339 | if ( DEBUG ) { debug( ERROR, ioProblem ); } |
---|
340 | } |
---|
341 | catch ( Exception exception ) { |
---|
342 | if ( DEBUG ) { debug( ERROR, exception ); } |
---|
343 | } |
---|
344 | return remoteFiles; |
---|
345 | |
---|
346 | } |
---|
347 | |
---|
348 | /** |
---|
349 | * <p>Lister les fichiers distant d'un hôte.</p> |
---|
350 | * <p>On considère que le répertoire de base des fichiers est le répertoire passé en argument.</p> |
---|
351 | * |
---|
352 | * @param directory: le répertoire distant |
---|
353 | * @param base: le répertoire distant de l'appel d'origine (traitement récursif) |
---|
354 | */ |
---|
355 | protected ArrayList opList( RemoteFile directory, String base ) throws RemoteException { |
---|
356 | final String ERROR = "Had a problem listing " + directory + "(" + base + ")"; |
---|
357 | ArrayList remoteFiles = null; |
---|
358 | try { |
---|
359 | // ------------------------------------------- |
---|
360 | // Debugging |
---|
361 | // ------------------------------------------- |
---|
362 | if ( DEBUG ) { debug( "Listing directory " + directory + " with base dir " + base ); } |
---|
363 | |
---|
364 | // ------------------------------------------- |
---|
365 | // Ask the destination manager to retrieve the |
---|
366 | // local file for us. |
---|
367 | // Let remote file server function fail |
---|
368 | // before allocating a new arraylist so that |
---|
369 | // return value is null if there is an error. |
---|
370 | // ------------------------------------------- |
---|
371 | File toList = directory.getFileManager().getLocalFile( directory ); |
---|
372 | remoteFiles = new ArrayList( ); |
---|
373 | File[] list = toList.listFiles( ); |
---|
374 | for (int i = 0 ; i < list.length; i++ ) |
---|
375 | { |
---|
376 | String absolute = list[i].getAbsolutePath( ); |
---|
377 | //if ( DEBUG ) { Trace.debug( this, "Absolute path found:" + absolute ); } |
---|
378 | if ( list[i].isDirectory() ) |
---|
379 | { |
---|
380 | RemoteFile file = new RemoteFileImpl( directory.getHost(), absolute ); |
---|
381 | remoteFiles.addAll( opList(file, base) ); |
---|
382 | } |
---|
383 | else |
---|
384 | { |
---|
385 | // Retrieve the substring after the base dir, minus the file separator |
---|
386 | // (relative paths should not begin with a file separator) |
---|
387 | int startRelative = absolute.lastIndexOf( base ); |
---|
388 | String relative = absolute.substring( startRelative + base.length()+1 ); |
---|
389 | //if ( DEBUG ) { Trace.debug( this, "Relative path found:" + relative ); } |
---|
390 | RemoteFile file = new RemoteFileImpl( directory.getHost(), relative ); |
---|
391 | file.setBaseDir( base ); |
---|
392 | remoteFiles.add( file ); |
---|
393 | } |
---|
394 | } |
---|
395 | } |
---|
396 | catch ( FileNotFoundException fileNotFound ) { |
---|
397 | if ( DEBUG ) { debug( ERROR, fileNotFound ); } |
---|
398 | } |
---|
399 | catch ( IOException ioProblem ) { |
---|
400 | if ( DEBUG ) { debug( ERROR, ioProblem ); } |
---|
401 | } |
---|
402 | catch ( Exception exception ) { |
---|
403 | if ( DEBUG ) { debug( ERROR, exception ); } |
---|
404 | } |
---|
405 | return remoteFiles; |
---|
406 | } |
---|
407 | |
---|
408 | /** |
---|
409 | * <p>Déterminer la taille, en octets, du fichier ou de tous les fichiers dans un répertoire.</p> |
---|
410 | * |
---|
411 | * @param directory: le fichier ou le répertoire |
---|
412 | * |
---|
413 | * @return la taille du fichier, ou la taille de tous les fichiers s'y trouvant (y compris dans les sous-répertoires) |
---|
414 | */ |
---|
415 | public long opSize( RemoteFile directory ) { |
---|
416 | long length = 0; |
---|
417 | final String ERROR = "Had a problem obtaining size of " + directory; |
---|
418 | try { |
---|
419 | // ------------------------------------------- |
---|
420 | // Ask the destination manager to retrieve the |
---|
421 | // source file for us. |
---|
422 | // ------------------------------------------- |
---|
423 | length = opSize( directory, directory.getAbsolutePath() ); |
---|
424 | } |
---|
425 | catch ( RemoteException remoteException ) { |
---|
426 | if ( DEBUG ) { debug( ERROR, remoteException ); } |
---|
427 | } |
---|
428 | catch ( IOException ioProblem ) { |
---|
429 | if ( DEBUG ) { debug( ERROR, ioProblem ); } |
---|
430 | } |
---|
431 | catch ( Exception exception ) { |
---|
432 | if ( DEBUG ) { debug( ERROR, exception ); } |
---|
433 | } |
---|
434 | if ( DEBUG ) { debug( "Size of files in " + directory + ": " + length + " bytes" ); } |
---|
435 | return length; |
---|
436 | } |
---|
437 | |
---|
438 | /* |
---|
439 | * <p>Determine the size, in bytes, of the file or of all the files in a directory.</p> |
---|
440 | * <p>Recursive call.</p> |
---|
441 | * |
---|
442 | * @see size(RemoteFile directory); |
---|
443 | */ |
---|
444 | protected long opSize( RemoteFile directory, String base ) throws RemoteException { |
---|
445 | long length = 0; |
---|
446 | final String ERROR = "Had a problem obtaining size of " + directory; |
---|
447 | try { |
---|
448 | // ------------------------------------------- |
---|
449 | // Debugging |
---|
450 | // ------------------------------------------- |
---|
451 | if ( DEBUG ) { debug( "Checking size of " + directory ); } |
---|
452 | // ------------------------------------------- |
---|
453 | // Ask the destination manager to retrieve the |
---|
454 | // source file for us. |
---|
455 | // ------------------------------------------- |
---|
456 | File toList = directory.getFileManager().getLocalFile( directory ); |
---|
457 | File[] list = toList.listFiles( ); |
---|
458 | // File is a directory |
---|
459 | if ( list != null ) { |
---|
460 | // Add length of directory |
---|
461 | length += toList.length( ); |
---|
462 | // Add length of all files in directory |
---|
463 | for (int i = 0 ; i < list.length; i++ ) |
---|
464 | { |
---|
465 | RemoteFile file = new RemoteFileImpl( directory.getHost(), list[i].getAbsolutePath() ); |
---|
466 | length += opSize( file, base ); |
---|
467 | } |
---|
468 | } |
---|
469 | // File is a regular file |
---|
470 | else { |
---|
471 | length = toList.length( ); |
---|
472 | } |
---|
473 | return length; |
---|
474 | } |
---|
475 | catch ( RemoteException remoteException ) { |
---|
476 | if ( DEBUG ) { debug( ERROR, remoteException ); } |
---|
477 | } |
---|
478 | catch ( FileNotFoundException fileNotFound ) { |
---|
479 | if ( DEBUG ) { debug( ERROR, fileNotFound ); } |
---|
480 | } |
---|
481 | catch ( IOException ioProblem ) { |
---|
482 | if ( DEBUG ) { debug( ERROR, ioProblem ); } |
---|
483 | } |
---|
484 | catch ( Exception exception ) { |
---|
485 | if ( DEBUG ) { debug( ERROR, exception ); } |
---|
486 | } |
---|
487 | return length; |
---|
488 | } |
---|
489 | |
---|
490 | /** |
---|
491 | * <p>Déterminer si un fichier distant existe.</p> |
---|
492 | * |
---|
493 | * @param file: fichier distant à vérifier |
---|
494 | * |
---|
495 | * @return true si le fichier distant existe |
---|
496 | */ |
---|
497 | public boolean opExist( RemoteFile file ) { |
---|
498 | final String ERROR = "Error checking existence of file " + file; |
---|
499 | boolean exists = false; |
---|
500 | try { |
---|
501 | RemoteFileManager distant = file.getFileManager( ); |
---|
502 | if ( distant.equals(manager) ) { |
---|
503 | File local = manager.getLocalFile( file ); |
---|
504 | exists = local.exists( ); |
---|
505 | } |
---|
506 | else { |
---|
507 | exists = manager.exists( file ); |
---|
508 | } |
---|
509 | } |
---|
510 | catch ( RemoteException remoteProblem ) { |
---|
511 | if ( DEBUG ) { debug( ERROR, remoteProblem ); } |
---|
512 | } |
---|
513 | catch ( Exception e ) { |
---|
514 | if ( DEBUG ) { debug( ERROR, e ); } |
---|
515 | } |
---|
516 | finally { |
---|
517 | return exists; |
---|
518 | } |
---|
519 | } |
---|
520 | |
---|
521 | /** |
---|
522 | * <p>Posts an event to the QueuedFileServer that does not have a result.</p>. |
---|
523 | * |
---|
524 | * @param evt: the event to post to the QueuedFileServer thread |
---|
525 | */ |
---|
526 | public synchronized void postEvent( RemoteFileServerEvent evt ) |
---|
527 | { |
---|
528 | queue.addElement( evt ); |
---|
529 | notify( ); |
---|
530 | } |
---|
531 | |
---|
532 | /** |
---|
533 | * <p>Posts an event to the QueuedFileServer that has a result.</p>. |
---|
534 | * |
---|
535 | * @param evt: the event to post to the QueuedFileServer thread |
---|
536 | */ |
---|
537 | public synchronized void postEventWithResult( RemoteFileServerEvent evt ) |
---|
538 | { |
---|
539 | evt.setHasResult( true ); |
---|
540 | queue.addElement( evt ); |
---|
541 | notify( ); |
---|
542 | try { |
---|
543 | wait( ); |
---|
544 | } |
---|
545 | catch ( InterruptedException ie ) { |
---|
546 | evt.setResult( null ); |
---|
547 | } |
---|
548 | } |
---|
549 | |
---|
550 | public synchronized void run() |
---|
551 | { |
---|
552 | debug( "QueuedFileServer running" ); |
---|
553 | while ( true ) { |
---|
554 | try { |
---|
555 | debug( "QueuedFileServer wait" ); |
---|
556 | wait( ); |
---|
557 | |
---|
558 | while ( queue.size() > 0 ) { |
---|
559 | debug( "QueuedFileServer getting next event" ); |
---|
560 | RemoteFileServerEvent evt = (RemoteFileServerEvent) queue.elementAt( 0 ); |
---|
561 | |
---|
562 | queue.removeElementAt( 0 ); |
---|
563 | debug( "QueuedFileServer handling event" ); |
---|
564 | handleEvent( evt ); |
---|
565 | } |
---|
566 | } |
---|
567 | catch ( InterruptedException ie ) { |
---|
568 | debug( "QueuedFileServer interrupted" ); |
---|
569 | } |
---|
570 | } // end while() |
---|
571 | } |
---|
572 | |
---|
573 | private synchronized void handleEvent( RemoteFileServerEvent evt ) |
---|
574 | { |
---|
575 | String op; |
---|
576 | boolean result; |
---|
577 | RemoteFile src = null; |
---|
578 | RemoteFile dest = null; |
---|
579 | switch( evt.getType() ) { |
---|
580 | case evt.OPEN: |
---|
581 | op = "OPEN"; |
---|
582 | break; |
---|
583 | case evt.CLOSE: |
---|
584 | op = "CLOSE"; |
---|
585 | break; |
---|
586 | case evt.READ: |
---|
587 | op = "READ"; |
---|
588 | break; |
---|
589 | case evt.WRITE: |
---|
590 | op = "WRITE"; |
---|
591 | break; |
---|
592 | case evt.COPY: |
---|
593 | op = "COPY"; |
---|
594 | src = (RemoteFile)evt.getArg( 0 ); |
---|
595 | dest = (RemoteFile)evt.getArg( 1 ); |
---|
596 | result = opCopy( src, dest ); |
---|
597 | evt.setResult( new Boolean(result) ); |
---|
598 | break; |
---|
599 | case evt.MOVE: |
---|
600 | op = "MOVE"; |
---|
601 | src = (RemoteFile)evt.getArg( 0 ); |
---|
602 | dest = (RemoteFile)evt.getArg( 1 ); |
---|
603 | result = opMove( src, dest ); |
---|
604 | evt.setResult( new Boolean(result) ); |
---|
605 | break; |
---|
606 | case evt.DELETE: |
---|
607 | op = "DELETE"; |
---|
608 | src = (RemoteFile)evt.getArg( 0 ); |
---|
609 | result = opDelete( src ); |
---|
610 | evt.setResult( new Boolean(result) ); |
---|
611 | break; |
---|
612 | case evt.LIST: |
---|
613 | op = "LIST"; |
---|
614 | src = (RemoteFile)evt.getArg( 0 ); |
---|
615 | ArrayList files = opList( src ); |
---|
616 | evt.setResult( files ); |
---|
617 | break; |
---|
618 | case evt.EXISTS: |
---|
619 | op = "EXISTS"; |
---|
620 | src = (RemoteFile)evt.getArg( 0 ); |
---|
621 | result = opExist( src ); |
---|
622 | evt.setResult( new Boolean(result) ); |
---|
623 | break; |
---|
624 | case evt.SIZE: |
---|
625 | op = "SIZE"; |
---|
626 | src = (RemoteFile)evt.getArg( 0 ); |
---|
627 | long size = opSize( src ); |
---|
628 | evt.setResult( new Long(size) ); |
---|
629 | break; |
---|
630 | case evt.RETRIEVE: |
---|
631 | op = "RETRIEVE"; |
---|
632 | src = (RemoteFile)evt.getArg( 0 ); |
---|
633 | File file = opRetrieve( src ); |
---|
634 | evt.setResult( file ); |
---|
635 | break; |
---|
636 | default: |
---|
637 | op = "DEFAULT"; |
---|
638 | break; |
---|
639 | } |
---|
640 | if ( evt.getHasResult() ) { |
---|
641 | notify(); |
---|
642 | } |
---|
643 | |
---|
644 | debug( "QueuedFileServer did " + op ); |
---|
645 | } |
---|
646 | |
---|
647 | private synchronized void debug( String debug ) { |
---|
648 | //System.out.println( debug ); |
---|
649 | Trace.debug(this,debug); |
---|
650 | } |
---|
651 | |
---|
652 | private synchronized void debug( String debug, Throwable t ) { |
---|
653 | // System.out.println( debug ); |
---|
654 | // t.printStackTrace( ); |
---|
655 | Trace.debug(this,debug,t); |
---|
656 | } |
---|
657 | |
---|
658 | |
---|
659 | } |
---|