KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.sun.xfile.XFile;
34 import com.sun.xfile.XFileInputStream;
35 import com.sun.xfile.XFileOutputStream;
36
37
38 public class NfsConnection implements BasicConnection
39 {
40     public static int buffer = 128000;
41     private String JavaDoc url = "";
42     private String JavaDoc host = "";
43     private String JavaDoc path = "";
44     private String JavaDoc pwd = "";
45     private Vector JavaDoc listeners = new Vector JavaDoc();
46     private String JavaDoc[] files;
47     private String JavaDoc[] size = new String JavaDoc[0];
48     private int[] perms = null;
49
50     //private NfsHandler handler = new NfsHandler();
51
private String JavaDoc baseFile;
52     private int fileCount;
53     private boolean isDirUpload = false;
54     private boolean shortProgress = false;
55     private boolean dummy = false;
56
57     public NfsConnection(String JavaDoc url)
58     {
59         this.url = url;
60
61         host = url.substring(6);
62
63         int x = host.indexOf("/");
64
65         if(x >= 0)
66         {
67             host = host.substring(0, x);
68         }
69
70         Log.out("nfs host is: " + host);
71     }
72
73     public boolean login(String JavaDoc user, String JavaDoc pass)
74     {
75         Log.out("nfs login called: " + url);
76
77         try
78         {
79             XFile xf = new XFile(url);
80
81             if(xf.exists())
82             {
83                 Log.out("nfs url ok");
84             }
85             else
86             {
87                 Log.out("WARNING: nfs url not found, cennection will fail!");
88             }
89
90             com.sun.nfs.XFileExtensionAccessor nfsx = (com.sun.nfs.XFileExtensionAccessor) xf.getExtensionAccessor();
91
92             //Log.out("nfs extension accessor: " + nfsx);
93
if(!nfsx.loginPCNFSD(host, user, pass))
94             {
95                 Log.out("login failed!");
96
97                 return false;
98             }
99             else
100             {
101                 Log.debug("Login successful...");
102             }
103         }
104         catch(Exception JavaDoc e)
105         {
106             e.printStackTrace();
107         }
108
109         return true;
110     }
111
112     public String JavaDoc[] getExports() throws Exception JavaDoc
113     {
114         XFile xf = new XFile(url);
115         com.sun.nfs.XFileExtensionAccessor nfsx = (com.sun.nfs.XFileExtensionAccessor) xf.getExtensionAccessor();
116
117         String JavaDoc[] tmp = nfsx.getExports();
118
119         if(tmp == null)
120         {
121             return new String JavaDoc[0];
122         }
123
124         for(int i = 0; i < tmp.length; i++)
125         {
126             Log.out("nfs export found: " + tmp[i]);
127         }
128
129         return tmp;
130     }
131
132     public int removeFileOrDir(String JavaDoc file)
133     {
134         try
135         {
136             String JavaDoc tmp = toNFS(file);
137
138             XFile f = new XFile(tmp);
139
140             if(!f.getAbsolutePath().equals(f.getCanonicalPath()))
141             {
142                 Log.debug("WARNING: Skipping symlink, remove failed.");
143                 Log.debug("This is necessary to prevent possible data loss when removing those symlinks.");
144
145                 return -1;
146             }
147
148             if(f.exists() && f.isDirectory())
149             {
150                 cleanLocalDir(tmp);
151             }
152
153             //System.out.println(tmp);
154
if(!f.delete())
155             {
156                 return -1;
157             }
158             else
159             {
160                 return 1;
161             }
162         }
163         catch(IOException JavaDoc ex)
164         {
165             Log.debug("Error: " + ex.toString());
166             ex.printStackTrace();
167         }
168
169         return -1;
170     }
171
172     private void cleanLocalDir(String JavaDoc dir)
173     {
174         dir = toNFS(dir);
175
176         if(dir.endsWith("\\"))
177         {
178             Log.out("need to fix \\-problem!!!");
179         }
180
181         if(!dir.endsWith("/"))
182         {
183             dir = dir + "/";
184         }
185
186         //String remoteDir = StringUtils.removeStart(dir,path);
187
//System.out.println(">>> " + dir);
188
XFile f2 = new XFile(dir);
189         String JavaDoc[] tmp = f2.list();
190
191         if(tmp == null)
192         {
193             return;
194         }
195
196         for(int i = 0; i < tmp.length; i++)
197         {
198             XFile f3 = new XFile(dir + tmp[i]);
199
200             if(f3.isDirectory())
201             {
202                 //System.out.println(dir);
203
cleanLocalDir(dir + tmp[i]);
204                 f3.delete();
205             }
206             else
207             {
208                 //System.out.println(dir+tmp[i]);
209
f3.delete();
210             }
211         }
212     }
213
214     public void sendRawCommand(String JavaDoc cmd)
215     {
216     }
217
218     public void disconnect()
219     {
220     }
221
222     public boolean isConnected()
223     {
224         return true;
225     }
226
227     public String JavaDoc getPWD()
228     {
229         String JavaDoc tmp = toNFS(pwd);
230
231         if(!tmp.endsWith("/"))
232         {
233             tmp = tmp + "/";
234         }
235
236         return tmp;
237     }
238
239     public boolean cdup()
240     {
241         String JavaDoc tmp = pwd;
242
243         if(pwd.endsWith("/") && !pwd.equals("nfs://"))
244         {
245             tmp = pwd.substring(0, pwd.lastIndexOf("/"));
246         }
247
248         return chdir(tmp.substring(0, tmp.lastIndexOf("/") + 1));
249     }
250
251     public boolean mkdir(String JavaDoc dirName)
252     {
253         if(!dirName.endsWith("/"))
254         {
255             dirName = dirName + "/";
256         }
257
258         dirName = toNFS(dirName);
259
260         File JavaDoc f = new File JavaDoc(dirName);
261
262         boolean x = f.mkdir();
263         fireDirectoryUpdate();
264
265         return x;
266     }
267
268     public void list() throws IOException JavaDoc
269     {
270     }
271
272     public boolean chdir(String JavaDoc p)
273     {
274         return chdir(p, true);
275     }
276
277     public boolean chdir(String JavaDoc p, boolean refresh)
278     {
279         if(p.endsWith(".."))
280         {
281             return cdup();
282         }
283
284         String JavaDoc tmp = toNFS(p);
285
286         if(!tmp.endsWith("/"))
287         {
288             tmp = tmp + "/";
289         }
290
291         if(check(tmp) < 3)
292         {
293             return false;
294         }
295
296         pwd = tmp;
297
298         if(refresh)
299         {
300             fireDirectoryUpdate();
301         }
302
303         return true;
304     }
305
306     private int check(String JavaDoc url)
307     {
308         int x = 0;
309
310         for(int j = 0; j < url.length(); j++)
311         {
312             if(url.charAt(j) == '/')
313             {
314                 x++;
315             }
316         }
317
318         return x;
319     }
320
321     public boolean chdirNoRefresh(String JavaDoc p)
322     {
323         /*
324                 String p2 = toNFS(p);
325             if(p2 == null) return false;
326
327             pwd = p2;
328
329             return true;
330         */

331         return chdir(p, false);
332     }
333
334     public String JavaDoc getLocalPath()
335     {
336         //System.out.println("local: " + path);
337
return path;
338     }
339
340     private String JavaDoc toNFS(String JavaDoc f)
341     {
342         String JavaDoc file;
343
344         if(f.lastIndexOf("nfs://") > 0)
345         {
346             f = f.substring(f.lastIndexOf("nfs://"));
347         }
348
349         if(f.startsWith("nfs://"))
350         {
351             file = f;
352         }
353         else
354         {
355             file = getPWD() + f;
356         }
357
358         file = file.replace('\\', '/');
359
360         Log.out("nfs url: " + file);
361
362         return file;
363     }
364
365     public boolean setLocalPath(String JavaDoc p)
366     {
367         if(!p.startsWith("/") && !p.startsWith(":", 1))
368         {
369             p = path + p;
370         }
371
372         File JavaDoc f = new File JavaDoc(p);
373
374         if(f.exists())
375         {
376             try
377             {
378                 path = f.getCanonicalPath();
379                 path = path.replace('\\', '/');
380
381                 if(!path.endsWith("/"))
382                 {
383                     path = path + "/";
384                 }
385
386                 //System.out.println("2:"+path+":"+getPWD());
387
}
388             catch(IOException JavaDoc ex)
389             {
390                 Log.debug("Error: can not get pathname (local)!");
391
392                 return false;
393             }
394         }
395         else
396         {
397             Log.debug("(local) No such path: \"" + p + "\"");
398
399             return false;
400         }
401
402         return true;
403     }
404
405     public String JavaDoc[] sortLs()
406     {
407         String JavaDoc dir = getPWD();
408
409         if(check(toNFS(dir)) == 3)
410         {
411             try
412             {
413                 files = getExports();
414             }
415             catch(Exception JavaDoc ex)
416             {
417                 Log.debug("Can not list exports:" + ex.toString());
418                 ex.printStackTrace();
419             }
420         }
421         else
422         {
423             XFile f = new XFile(dir);
424             files = f.list();
425         }
426
427         if(files == null)
428         {
429             return new String JavaDoc[0];
430         }
431
432         size = new String JavaDoc[files.length];
433         perms = new int[files.length];
434
435         int accessible = 0;
436
437         for(int i = 0; i < files.length; i++)
438         {
439             XFile f2 = new XFile(dir + files[i]);
440
441             if(f2.isDirectory() && !files[i].endsWith("/"))
442             {
443                 files[i] = files[i] + "/";
444             }
445
446             size[i] = "" + f2.length();
447
448             if(f2.canWrite())
449             {
450                 accessible = FtpConnection.W;
451             }
452             else if(f2.canRead())
453             {
454                 accessible = FtpConnection.R;
455             }
456             else
457             {
458                 accessible = FtpConnection.DENIED;
459             }
460
461             perms[i] = accessible;
462
463             //System.out.println(pwd+files[i] +" : " +accessible + " : " + size[i]);
464
}
465
466         return files;
467     }
468
469     public String JavaDoc[] sortSize()
470     {
471         return size;
472     }
473
474     public int[] getPermissions()
475     {
476         return perms;
477     }
478
479     public int handleUpload(String JavaDoc f)
480     {
481         upload(f);
482
483         return 0;
484     }
485
486     public int handleDownload(String JavaDoc f)
487     {
488         download(f);
489
490         return 0;
491     }
492
493     public int upload(String JavaDoc f)
494     {
495         String JavaDoc file = toNFS(f);
496
497         if(file.endsWith("/"))
498         {
499             String JavaDoc out = StringUtils.getDir(file);
500             uploadDir(file, getLocalPath() + out);
501             fireActionFinished(this);
502         }
503         else
504         {
505             String JavaDoc outfile = StringUtils.getFile(file);
506
507             //System.out.println("transfer: " + file + ", " + getLocalPath() + outfile);
508
work(getLocalPath() + outfile, file);
509             fireActionFinished(this);
510         }
511
512         return 0;
513     }
514
515     public int download(String JavaDoc f)
516     {
517         String JavaDoc file = toNFS(f);
518
519         if(file.endsWith("/"))
520         {
521             String JavaDoc out = StringUtils.getDir(file);
522             downloadDir(file, getLocalPath() + out);
523             fireActionFinished(this);
524         }
525         else
526         {
527             String JavaDoc outfile = StringUtils.getFile(file);
528
529             //System.out.println("transfer: " + file + ", " + getLocalPath() + outfile);
530
work(file, getLocalPath() + outfile);
531             fireActionFinished(this);
532         }
533
534         return 0;
535     }
536
537     private void downloadDir(String JavaDoc dir, String JavaDoc out)
538     {
539         try
540         {
541             //System.out.println("downloadDir: " + dir + "," + out);
542
fileCount = 0;
543             shortProgress = true;
544             baseFile = StringUtils.getDir(dir);
545
546             XFile f2 = new XFile(dir);
547             String JavaDoc[] tmp = f2.list();
548
549             if(tmp == null)
550             {
551                 return;
552             }
553
554             File JavaDoc fx = new File JavaDoc(out);
555             fx.mkdir();
556
557             for(int i = 0; i < tmp.length; i++)
558             {
559                 tmp[i] = tmp[i].replace('\\', '/');
560
561                 //System.out.println("1: " + dir+tmp[i] + ", " + out +tmp[i]);
562
XFile f3 = new XFile(dir + tmp[i]);
563
564                 if(f3.isDirectory())
565                 {
566                     if(!tmp[i].endsWith("/"))
567                     {
568                         tmp[i] = tmp[i] + "/";
569                     }
570
571                     downloadDir(dir + tmp[i], out + tmp[i]);
572                 }
573                 else
574                 {
575                     fileCount++;
576                     fireProgressUpdate(baseFile,
577                                        DataConnection.GETDIR + ":" + fileCount,
578                                        -1);
579                     work(dir + tmp[i], out + tmp[i]);
580                 }
581             }
582
583             fireProgressUpdate(baseFile,
584                                DataConnection.DFINISHED + ":" + fileCount, -1);
585         }
586         catch(Exception JavaDoc ex)
587         {
588             ex.printStackTrace();
589
590             //System.out.println(dir + ", " + out);
591
Log.debug("Transfer error: " + ex);
592             fireProgressUpdate(baseFile,
593                                DataConnection.FAILED + ":" + fileCount, -1);
594         }
595
596         shortProgress = false;
597     }
598
599     private void uploadDir(String JavaDoc dir, String JavaDoc out)
600     {
601         try
602         {
603             //System.out.println("uploadDir: " + dir + "," + out);
604
isDirUpload = true;
605             fileCount = 0;
606             shortProgress = true;
607             baseFile = StringUtils.getDir(dir);
608
609             File JavaDoc f2 = new File JavaDoc(out);
610             String JavaDoc[] tmp = f2.list();
611
612             if(tmp == null)
613             {
614                 return;
615             }
616
617             XFile fx = new XFile(dir);
618             fx.mkdir();
619
620             for(int i = 0; i < tmp.length; i++)
621             {
622                 tmp[i] = tmp[i].replace('\\', '/');
623
624                 //System.out.println("1: " + dir+tmp[i] + ", " + out +tmp[i]);
625
File JavaDoc f3 = new File JavaDoc(out + tmp[i]);
626
627                 if(f3.isDirectory())
628                 {
629                     if(!tmp[i].endsWith("/"))
630                     {
631                         tmp[i] = tmp[i] + "/";
632                     }
633
634                     uploadDir(dir + tmp[i], out + tmp[i]);
635                 }
636                 else
637                 {
638                     fileCount++;
639                     fireProgressUpdate(baseFile,
640                                        DataConnection.PUTDIR + ":" + fileCount,
641                                        -1);
642                     work(out + tmp[i], dir + tmp[i]);
643                 }
644             }
645
646             fireProgressUpdate(baseFile,
647                                DataConnection.DFINISHED + ":" + fileCount, -1);
648         }
649         catch(Exception JavaDoc ex)
650         {
651             ex.printStackTrace();
652
653             //System.out.println(dir + ", " + out);
654
Log.debug("Transfer error: " + ex);
655             fireProgressUpdate(baseFile,
656                                DataConnection.FAILED + ":" + fileCount, -1);
657         }
658
659         isDirUpload = false;
660         shortProgress = true;
661     }
662
663     private void work(String JavaDoc file, String JavaDoc outfile)
664     {
665         BufferedOutputStream JavaDoc out = null;
666         BufferedInputStream JavaDoc in = null;
667
668         try
669         {
670             boolean outflag = false;
671
672             if(outfile.startsWith("nfs://"))
673             {
674                 outflag = true;
675                 out = new BufferedOutputStream JavaDoc(new XFileOutputStream(outfile));
676             }
677             else
678             {
679                 out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(outfile));
680             }
681
682             //System.out.println("out: " + outfile + ", in: " + file);
683
if(file.startsWith("nfs://"))
684             {
685                 in = new BufferedInputStream JavaDoc(new XFileInputStream(file));
686             }
687             else
688             {
689                 in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
690             }
691
692             byte[] buf = new byte[buffer];
693             int len = 0;
694             int reallen = 0;
695
696             //System.out.println(file+":"+getLocalPath()+outfile);
697
while(true)
698             {
699                 len = in.read(buf);
700
701                 //System.out.print(".");
702
if(len == StreamTokenizer.TT_EOF)
703                 {
704                     break;
705                 }
706
707                 out.write(buf, 0, len);
708                 reallen += len;
709
710                 //System.out.println(file + ":" + StringUtils.getFile(file));
711
if(outflag)
712                 {
713                     fireProgressUpdate(StringUtils.getFile(outfile),
714                                        DataConnection.PUT, reallen);
715                 }
716                 else
717                 {
718                     fireProgressUpdate(StringUtils.getFile(file),
719                                        DataConnection.GET, reallen);
720                 }
721             }
722
723             fireProgressUpdate(file, DataConnection.FINISHED, -1);
724         }
725         catch(IOException JavaDoc ex)
726         {
727             Log.debug("Error with file IO (" + ex + ")!");
728             fireProgressUpdate(file, DataConnection.FAILED, -1);
729         }
730         finally
731         {
732             try
733             {
734                 out.flush();
735                 out.close();
736                 in.close();
737             }
738             catch(Exception JavaDoc ex)
739             {
740                 ex.printStackTrace();
741             }
742         }
743     }
744
745     private void update(String JavaDoc file, String JavaDoc type, int bytes)
746     {
747         if(listeners == null)
748         {
749             return;
750         }
751         else
752         {
753             for(int i = 0; i < listeners.size(); i++)
754             {
755                 ConnectionListener listener = (ConnectionListener) listeners.elementAt(i);
756                 listener.updateProgress(file, type, bytes);
757             }
758         }
759     }
760
761     public void addConnectionListener(ConnectionListener l)
762     {
763         listeners.add(l);
764     }
765
766     public void setConnectionListeners(Vector JavaDoc l)
767     {
768         listeners = l;
769     }
770
771     /** remote directory has changed */
772     public void fireDirectoryUpdate()
773     {
774         if(listeners == null)
775         {
776             return;
777         }
778         else
779         {
780             for(int i = 0; i < listeners.size(); i++)
781             {
782                 ((ConnectionListener) listeners.elementAt(i)).updateRemoteDirectory(this);
783             }
784         }
785     }
786
787     /** progress update */
788     public void fireProgressUpdate(String JavaDoc file, String JavaDoc type, int bytes)
789     {
790         //System.out.println(listener);
791
if(listeners == null)
792         {
793             return;
794         }
795         else
796         {
797             for(int i = 0; i < listeners.size(); i++)
798             {
799                 ConnectionListener listener = (ConnectionListener) listeners.elementAt(i);
800
801                 if(shortProgress && Settings.shortProgress)
802                 {
803                     if(type.startsWith(DataConnection.DFINISHED))
804                     {
805                         listener.updateProgress(baseFile,
806                                                 DataConnection.DFINISHED + ":" +
807                                                 fileCount, bytes);
808                     }
809                     else if(isDirUpload)
810                     {
811                         listener.updateProgress(baseFile,
812                                                 DataConnection.PUTDIR + ":" +
813                                                 fileCount, bytes);
814                     }
815                     else
816                     {
817                         listener.updateProgress(baseFile,
818                                                 DataConnection.GETDIR + ":" +
819                                                 fileCount, bytes);
820                     }
821                 }
822                 else
823                 {
824                     listener.updateProgress(file, type, bytes);
825                 }
826             }
827         }
828     }
829
830     public void fireActionFinished(NfsConnection con)
831     {
832         if(listeners == null)
833         {
834             return;
835         }
836         else
837         {
838             for(int i = 0; i < listeners.size(); i++)
839             {
840                 ((ConnectionListener) listeners.elementAt(i)).actionFinished(con);
841             }
842         }
843     }
844
845     public int upload(String JavaDoc file, InputStream JavaDoc i)
846     {
847         BufferedInputStream JavaDoc in = null;
848         BufferedOutputStream JavaDoc out = null;
849
850         try
851         {
852             file = toNFS(file);
853
854             out = new BufferedOutputStream JavaDoc(new XFileOutputStream(file));
855             in = new BufferedInputStream JavaDoc(i);
856
857             byte[] buf = new byte[buffer];
858             int len = 0;
859             int reallen = 0;
860
861             while(true)
862             {
863                 len = in.read(buf);
864
865                 if(len == StreamTokenizer.TT_EOF)
866                 {
867                     break;
868                 }
869
870                 out.write(buf, 0, len);
871                 reallen += len;
872
873                 fireProgressUpdate(StringUtils.getFile(file),
874                                    DataConnection.PUT, reallen);
875             }
876
877             fireProgressUpdate(file, DataConnection.FINISHED, -1);
878         }
879         catch(IOException JavaDoc ex)
880         {
881             Log.debug("Error with file IO (" + ex + ")!");
882             fireProgressUpdate(file, DataConnection.FAILED, -1);
883
884             return -1;
885         }
886         finally
887         {
888             try
889             {
890                 out.flush();
891                 out.close();
892                 in.close();
893             }
894             catch(Exception JavaDoc ex)
895             {
896                 ex.printStackTrace();
897             }
898         }
899
900         return 0;
901     }
902
903     public InputStream JavaDoc getDownloadInputStream(String JavaDoc file)
904     {
905         file = toNFS(file);
906         Log.debug(file);
907
908         try
909         {
910             return new BufferedInputStream JavaDoc(new XFileInputStream(file));
911         }
912         catch(Exception JavaDoc ex)
913         {
914             ex.printStackTrace();
915             Log.debug(ex.toString() +
916                       " @NfsConnection::getDownloadInputStream");
917
918             return null;
919         }
920     }
921
922     public Date JavaDoc[] sortDates()
923     {
924         return null;
925     }
926
927     public boolean rename(String JavaDoc from, String JavaDoc to)
928     {
929         Log.debug("Not implemented!");
930
931         return false;
932     }
933 }
934
Popular Tags