KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > net > FilesystemConnection


1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package net.sf.jftp.net;
17
18 import java.io.BufferedInputStream JavaDoc;
19 import java.io.BufferedOutputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.StreamTokenizer JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import net.sf.jftp.config.Settings;
30 import net.sf.jftp.system.StringUtils;
31 import net.sf.jftp.system.logging.Log;
32
33
34 public class FilesystemConnection implements BasicConnection
35 {
36     public static int filesystemBuffer = 128000;
37     private String JavaDoc path = "";
38     private String JavaDoc pwd = "";
39     private Vector JavaDoc listeners = new Vector JavaDoc();
40     private String JavaDoc[] files;
41     private String JavaDoc[] size = new String JavaDoc[0];
42     private int[] perms = null;
43     private String JavaDoc baseFile;
44     private int fileCount;
45     private boolean shortProgress = false;
46     public Vector JavaDoc dateVector = new Vector JavaDoc();
47
48     public FilesystemConnection()
49     {
50     }
51
52     public FilesystemConnection(String JavaDoc path, ConnectionListener l)
53     {
54         listeners.add(l);
55         chdir(path);
56     }
57
58     public int removeFileOrDir(String JavaDoc file)
59     {
60         try
61         {
62             if((file == null) || file.equals(""))
63             {
64                 return -1;
65             }
66
67             String JavaDoc tmp = file;
68
69             if(StringUtils.isRelative(file))
70             {
71                 tmp = getPWD() + file;
72             }
73
74             File JavaDoc f = new File JavaDoc(tmp);
75
76             if(!f.getAbsolutePath().equals(f.getCanonicalPath()))
77             {
78                 //Log.debug("WARNING: Symlink removed");//Skipping symlink, remove failed.");
79
//Log.debug("This is necessary to prevent possible data loss when removing those symlinks.");
80
//return -1;
81
if(!f.delete())
82                 {
83                     return -1;
84                 }
85             }
86
87             if(f.exists() && f.isDirectory())
88             {
89                 cleanLocalDir(tmp);
90             }
91
92             //System.out.println(tmp);
93
if(!f.delete())
94             {
95                 Log.debug("Removal failed.");
96
97                 return -1;
98             }
99         }
100         catch(IOException JavaDoc ex)
101         {
102             Log.debug("Error: " + ex.toString());
103             ex.printStackTrace();
104         }
105
106         return -1;
107     }
108
109     private void cleanLocalDir(String JavaDoc dir)
110     {
111         try
112         {
113             dir = dir.replace('\\', '/');
114
115             if(!dir.endsWith("/"))
116             {
117                 dir = dir + "/";
118             }
119
120             //String remoteDir = StringUtils.removeStart(dir,path);
121
//System.out.println(">>> " + dir);
122
File JavaDoc f2 = new File JavaDoc(dir);
123             String JavaDoc[] tmp = f2.list();
124
125             if(tmp == null)
126             {
127                 return;
128             }
129
130             for(int i = 0; i < tmp.length; i++)
131             {
132                 File JavaDoc f3 = new File JavaDoc(dir + tmp[i]);
133
134                 if(!f3.getAbsolutePath().equals(f3.getCanonicalPath()))
135                 {
136                     //Log.debug("WARNING: Symlink remove");//Skipping symlink, remove may fail.");
137
f3.delete();
138
139                     //Log.debug("This is necessary to prevent possible data loss when removing those symlinks.");
140
//continue;
141
}
142
143                 if(f3.isDirectory())
144                 {
145                     //System.out.println(dir);
146
cleanLocalDir(dir + tmp[i]);
147                     f3.delete();
148                 }
149                 else
150                 {
151                     //System.out.println(dir+tmp[i]);
152
f3.delete();
153                 }
154             }
155         }
156         catch(IOException JavaDoc ex)
157         {
158             Log.debug("Error: " + ex.toString());
159             ex.printStackTrace();
160         }
161     }
162
163     public void sendRawCommand(String JavaDoc cmd)
164     {
165     }
166
167     public void disconnect()
168     {
169     }
170
171     public boolean isConnected()
172     {
173         return true;
174     }
175
176     public String JavaDoc getPWD()
177     {
178         return pwd;
179     }
180
181     public boolean cdup()
182     {
183         return chdir(pwd.substring(0, pwd.lastIndexOf("/") + 1));
184     }
185
186     public boolean mkdir(String JavaDoc dirName)
187     {
188         if(StringUtils.isRelative(dirName))
189         {
190             dirName = getPWD() + dirName;
191         }
192
193         File JavaDoc f = new File JavaDoc(dirName);
194
195         boolean x = f.mkdir();
196         fireDirectoryUpdate();
197
198         return x;
199     }
200
201     public void list() throws IOException JavaDoc
202     {
203     }
204
205     public boolean chdir(String JavaDoc p)
206     {
207         String JavaDoc p2 = processPath(p);
208
209         if(p2 == null)
210         {
211             return false;
212         }
213
214         File JavaDoc f = new File JavaDoc(p2);
215
216         if(!f.exists() || !f.isDirectory() || !f.canRead())
217         {
218             Log.debug("Access denied.");
219
220             return false;
221         }
222
223         pwd = p2;
224
225         fireDirectoryUpdate();
226
227         return true;
228     }
229
230     public boolean chdirNoRefresh(String JavaDoc p)
231     {
232         String JavaDoc p2 = processPath(p);
233
234         if(p2 == null)
235         {
236             return false;
237         }
238
239         //System.out.println(p2);
240
pwd = p2;
241
242         return true;
243     }
244
245     public String JavaDoc getLocalPath()
246     {
247         //System.out.println("local: " + path);
248
return path;
249     }
250
251     public String JavaDoc processPath(String JavaDoc p)
252     {
253         p = p.replace('\\', '/');
254
255         //System.out.print("processPath 1: "+p);
256
if(StringUtils.isRelative(p))
257         {
258             p = pwd + p;
259         }
260
261         p = p.replace('\\', '/');
262
263         //System.out.println(", processPath 2: "+p);
264
File JavaDoc f = new File JavaDoc(p);
265         String JavaDoc p2;
266
267         if(f.exists())
268         {
269             try
270             {
271                 p2 = f.getCanonicalPath();
272                 p2 = p2.replace('\\', '/');
273
274                 if(!p2.endsWith("/"))
275                 {
276                     p2 = p2 + "/";
277                 }
278
279                 return p2;
280             }
281             catch(IOException JavaDoc ex)
282             {
283                 Log.debug("Error: can not get pathname (processPath)!");
284
285                 return null;
286             }
287         }
288         else
289         {
290             Log.debug("(processpPath) No such path: \"" + p + "\"");
291
292             return null;
293         }
294     }
295
296     public boolean setLocalPath(String JavaDoc p)
297     {
298         p = p.replace('\\', '/');
299
300         //System.out.print("local 1:" + p);
301
if(StringUtils.isRelative(p))
302         {
303             p = path + p;
304         }
305
306         p = p.replace('\\', '/');
307
308         //System.out.println(", local 2:" + p);
309
File JavaDoc f = new File JavaDoc(p);
310
311         if(f.exists())
312         {
313             try
314             {
315                 path = f.getCanonicalPath();
316                 path = path.replace('\\', '/');
317
318                 if(!path.endsWith("/"))
319                 {
320                     path = path + "/";
321                 }
322
323                 //System.out.println("localPath: "+path);
324
}
325             catch(IOException JavaDoc ex)
326             {
327                 Log.debug("Error: can not get pathname (local)!");
328
329                 return false;
330             }
331         }
332         else
333         {
334             Log.out("(local) obsolete setLocalDir: \"" + p + "\"");
335
336             return false;
337         }
338
339         return true;
340     }
341
342     public String JavaDoc[] sortLs()
343     {
344         dateVector = new Vector JavaDoc();
345
346         File JavaDoc f = new File JavaDoc(pwd);
347         files = f.list();
348
349         if(files == null)
350         {
351             return new String JavaDoc[0];
352         }
353
354         size = new String JavaDoc[files.length];
355         perms = new int[files.length];
356
357         int accessible = 0;
358
359         for(int i = 0; i < files.length; i++)
360         {
361             File JavaDoc f2 = new File JavaDoc(pwd + files[i]);
362
363             if(f2.isDirectory() && !files[i].endsWith("/"))
364             {
365                 files[i] = files[i] + "/";
366             }
367
368             size[i] = "" + new File JavaDoc(pwd + files[i]).length();
369
370             if(f2.canWrite())
371             {
372                 accessible = FtpConnection.W;
373             }
374             else if(f2.canRead())
375             {
376                 accessible = FtpConnection.R;
377             }
378             else
379             {
380                 accessible = FtpConnection.DENIED;
381             }
382
383             perms[i] = accessible;
384
385             //System.out.println(pwd+files[i] +" : " +accessible + " : " + size[i]);
386
Date JavaDoc d = new Date JavaDoc(f2.lastModified());
387
388             //System.out.println(d.toString());
389
dateVector.add(d);
390         }
391
392         return files;
393     }
394
395     public String JavaDoc[] sortSize()
396     {
397         return size;
398     }
399
400     public int[] getPermissions()
401     {
402         return perms;
403     }
404
405     public int handleDownload(String JavaDoc file)
406     {
407         transfer(file);
408
409         return 0;
410     }
411
412     public int handleUpload(String JavaDoc file)
413     {
414         transfer(file);
415
416         return 0;
417     }
418
419     public int download(String JavaDoc file)
420     {
421         transfer(file);
422
423         return 0;
424     }
425
426     public int upload(String JavaDoc file)
427     {
428         transfer(file);
429
430         return 0;
431     }
432
433     private void transferDir(String JavaDoc dir, String JavaDoc out)
434     {
435         fileCount = 0;
436         shortProgress = true;
437         baseFile = StringUtils.getDir(dir);
438
439         File JavaDoc f2 = new File JavaDoc(dir);
440         String JavaDoc[] tmp = f2.list();
441
442         if(tmp == null)
443         {
444             return;
445         }
446
447         File JavaDoc fx = new File JavaDoc(out);
448
449         if(!fx.mkdir())
450         {
451             Log.debug("Can not create directory: " + out +
452                       " - already exist or permission denied?");
453         }
454
455         for(int i = 0; i < tmp.length; i++)
456         {
457             tmp[i] = tmp[i].replace('\\', '/');
458
459             //System.out.println("1: " + dir+tmp[i] + ", " + out +tmp[i]);
460
File JavaDoc f3 = new File JavaDoc(dir + tmp[i]);
461
462             if(f3.isDirectory())
463             {
464                 if(!tmp[i].endsWith("/"))
465                 {
466                     tmp[i] = tmp[i] + "/";
467                 }
468
469                 transferDir(dir + tmp[i], out + tmp[i]);
470             }
471             else
472             {
473                 fireProgressUpdate(baseFile,
474                                    DataConnection.GETDIR + ":" + fileCount, -1);
475                 work(dir + tmp[i], out + tmp[i]);
476             }
477         }
478
479         fireProgressUpdate(baseFile,
480                            DataConnection.DFINISHED + ":" + fileCount, -1);
481         shortProgress = false;
482     }
483
484     private void transfer(String JavaDoc file)
485     {
486         String JavaDoc out = StringUtils.getDir(file);
487
488         if(StringUtils.isRelative(file))
489         {
490             file = getPWD() + file;
491         }
492
493         file = file.replace('\\', '/');
494         out = out.replace('\\', '/');
495
496         String JavaDoc outfile = StringUtils.getFile(file);
497
498         if(file.endsWith("/"))
499         {
500             transferDir(file, getLocalPath() + out);
501
502             return;
503         }
504         else
505         {
506             work(file, getLocalPath() + outfile);
507         }
508     }
509
510     private void work(String JavaDoc file, String JavaDoc outfile)
511     {
512         try
513         {
514             BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(outfile));
515             BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
516             byte[] buf = new byte[filesystemBuffer];
517             int len = 0;
518             int reallen = 0;
519
520             //System.out.println(file+":"+getLocalPath()+outfile);
521
while(true)
522             {
523                 len = in.read(buf);
524
525                 //System.out.print(".");
526
if(len == StreamTokenizer.TT_EOF)
527                 {
528                     break;
529                 }
530
531                 out.write(buf, 0, len);
532
533                 reallen += len;
534                 fireProgressUpdate(StringUtils.getFile(file),
535                                    DataConnection.GET, reallen);
536             }
537
538             out.flush();
539             out.close();
540             in.close();
541             fireProgressUpdate(file, DataConnection.FINISHED, -1);
542         }
543         catch(IOException JavaDoc ex)
544         {
545             Log.debug("Error with file IO (" + ex + ")!");
546             fireProgressUpdate(file, DataConnection.FAILED, -1);
547         }
548     }
549
550     public int upload(String JavaDoc file, InputStream JavaDoc in)
551     {
552         if(StringUtils.isRelative(file))
553         {
554             file = getPWD() + file;
555         }
556
557         file = file.replace('\\', '/');
558
559         try
560         {
561             work(new BufferedInputStream JavaDoc(in), file, file);
562
563             return 0;
564         }
565         catch(Exception JavaDoc ex)
566         {
567             Log.debug("Error: " + ex.toString());
568             ex.printStackTrace();
569
570             return -1;
571         }
572     }
573
574     private void work(BufferedInputStream JavaDoc in, String JavaDoc outfile, String JavaDoc file)
575     {
576         try
577         {
578             BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(outfile));
579             byte[] buf = new byte[filesystemBuffer];
580             int len = 0;
581             int reallen = 0;
582
583             while(true)
584             {
585                 len = in.read(buf);
586                 System.out.print(".");
587
588                 if(len == StreamTokenizer.TT_EOF)
589                 {
590                     break;
591                 }
592
593                 out.write(buf, 0, len);
594
595                 reallen += len;
596                 fireProgressUpdate(StringUtils.getFile(file),
597                                    DataConnection.GET, reallen);
598             }
599
600             out.flush();
601             out.close();
602             in.close();
603             fireProgressUpdate(file, DataConnection.FINISHED, -1);
604         }
605         catch(IOException JavaDoc ex)
606         {
607             Log.debug("Error with file IO (" + ex + ")!");
608             fireProgressUpdate(file, DataConnection.FAILED, -1);
609         }
610     }
611
612     public InputStream JavaDoc getDownloadInputStream(String JavaDoc file)
613     {
614         if(StringUtils.isRelative(file))
615         {
616             file = getPWD() + file;
617         }
618
619         file = file.replace('\\', '/');
620
621         try
622         {
623             return new FileInputStream JavaDoc(file);
624         }
625         catch(Exception JavaDoc ex)
626         {
627             ex.printStackTrace();
628             Log.debug(ex.toString() +
629                       " @Filesystemconnection::getDownloadInputStream");
630
631             return null;
632         }
633     }
634
635     public void addConnectionListener(ConnectionListener l)
636     {
637         listeners.add(l);
638         fireDirectoryUpdate();
639     }
640
641     public void setConnectionListeners(Vector JavaDoc l)
642     {
643         listeners = l;
644         fireDirectoryUpdate();
645     }
646
647     /** remote directory has changed */
648     public void fireDirectoryUpdate()
649     {
650         if(listeners == null)
651         {
652             return;
653         }
654         else
655         {
656             for(int i = 0; i < listeners.size(); i++)
657             {
658                 ((ConnectionListener) listeners.elementAt(i)).updateRemoteDirectory(this);
659             }
660         }
661     }
662
663     public boolean login(String JavaDoc user, String JavaDoc pass)
664     {
665         return true;
666     }
667
668     public void fireProgressUpdate(String JavaDoc file, String JavaDoc type, int bytes)
669     {
670         if(listeners == null)
671         {
672             return;
673         }
674         else
675         {
676             for(int i = 0; i < listeners.size(); i++)
677             {
678                 ConnectionListener listener = (ConnectionListener) listeners.elementAt(i);
679
680                 if(shortProgress && Settings.shortProgress)
681                 {
682                     if(type.startsWith(DataConnection.DFINISHED))
683                     {
684                         listener.updateProgress(baseFile,
685                                                 DataConnection.DFINISHED + ":" +
686                                                 fileCount, bytes);
687                     }
688
689                     listener.updateProgress(baseFile,
690                                             DataConnection.GETDIR + ":" +
691                                             fileCount, bytes);
692                 }
693                 else
694                 {
695                     listener.updateProgress(file, type, bytes);
696                 }
697             }
698         }
699     }
700
701     public Date JavaDoc[] sortDates()
702     {
703         if(dateVector.size() > 0)
704         {
705             return (Date JavaDoc[]) dateVector.toArray();
706         }
707         else
708         {
709             return null;
710         }
711     }
712
713     public boolean rename(String JavaDoc file, String JavaDoc to)
714     {
715         if(StringUtils.isRelative(file))
716         {
717             file = getPWD() + file;
718         }
719
720         file = file.replace('\\', '/');
721
722         File JavaDoc f = new File JavaDoc(file);
723
724         if(StringUtils.isRelative(to))
725         {
726             to = getPWD() + to;
727         }
728
729         to = to.replace('\\', '/');
730
731         return f.renameTo(new File JavaDoc(to));
732     }
733 }
734
Popular Tags