KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > dfs > FTPWorkerThread


1 package com.knowgate.dfs;
2
3 /**
4  * <p>Direct piped copy from an FTP source to another FTP target.</p>
5  * <p>This is an alpha state testing module.</p>
6  * @author Sergio Montoro Ten
7  * @version 0.3alpha
8  */

9
10 import java.io.IOException JavaDoc;
11 import java.io.FileWriter JavaDoc;
12 import java.io.PrintWriter JavaDoc;
13 import java.io.PipedInputStream JavaDoc;
14 import java.io.PipedOutputStream JavaDoc;
15
16 import com.enterprisedt.net.ftp.FTPClient;
17 import com.enterprisedt.net.ftp.FTPException;
18 import com.enterprisedt.net.ftp.FTPTransferType;
19 import com.enterprisedt.net.ftp.FTPConnectMode;
20
21 import com.knowgate.debug.DebugFile;
22
23 public class FTPWorkerThread extends Thread JavaDoc {
24   private FTPClient oFTPC;
25   private boolean bLoged;
26   private PipedOutputStream JavaDoc oOutPipe;
27   private PipedInputStream JavaDoc oInPipe;
28   private FileWriter JavaDoc oFW;
29   private PrintWriter JavaDoc oPW;
30   private int iCmd;
31   private String JavaDoc sPar1;
32   private FileSystem oFileSys;
33
34   private int GET_PIPE = 16;
35   private int PUT_PIPE = 32;
36   private int MOV_PIPE = 64;
37
38   public FTPWorkerThread(String JavaDoc sHost, String JavaDoc sUser, String JavaDoc sPassword) throws FTPException,IOException JavaDoc {
39     bLoged = false;
40     oFileSys = null;
41
42     if (DebugFile.trace) DebugFile.writeln("new FTPClient(" + sHost + ")");
43
44     oFTPC = new FTPClient(sHost);
45     oFTPC.debugResponses(DebugFile.trace);
46
47     if (DebugFile.trace) {
48       oFW = new FileWriter JavaDoc("/tmp/javatrc.txt", true);
49       oPW = new PrintWriter JavaDoc(oFW, true);
50       oFTPC.setLogStream(oPW);
51       DebugFile.writeln("FTPClient.login(" + sUser + "," + sPassword + ")");
52     }
53
54     oFTPC.login(sUser, sPassword);
55     bLoged = true;
56
57     oFTPC.setConnectMode(FTPConnectMode.ACTIVE);
58     oFTPC.setType(FTPTransferType.BINARY);
59   } // FTPWorkerThread()
60

61   //-----------------------------------------------------------
62

63   public PipedInputStream JavaDoc getInputPipe () throws IOException JavaDoc {
64     return oInPipe;
65   } // getInputPipe()
66

67   //-----------------------------------------------------------
68

69   public PipedOutputStream JavaDoc getOutputPipe () throws IOException JavaDoc {
70     return oOutPipe;
71   } // getOutputPipe()
72

73   //-----------------------------------------------------------
74

75   public void get (String JavaDoc sFile) throws IOException JavaDoc {
76     oOutPipe = new PipedOutputStream JavaDoc();
77     sPar1 = sFile;
78     iCmd = GET_PIPE;
79   }
80
81   //-----------------------------------------------------------
82

83   public void put (String JavaDoc sFile) throws IOException JavaDoc {
84     oInPipe = new PipedInputStream JavaDoc();
85     sPar1 = sFile;
86     iCmd = PUT_PIPE;
87   } // put()
88

89   //-----------------------------------------------------------
90

91   public void move (String JavaDoc sFile) throws IOException JavaDoc {
92     oOutPipe = new PipedOutputStream JavaDoc();
93     sPar1 = sFile;
94     iCmd = MOV_PIPE;
95   }
96
97   //-----------------------------------------------------------
98

99   public void connect (PipedInputStream JavaDoc oStrm) throws IOException JavaDoc {
100     if (DebugFile.trace) DebugFile.writeln("FTPWorkerThread.connect([PipedInputStream])");
101     oOutPipe.connect (oStrm);
102   } // connect()
103

104   //-----------------------------------------------------------
105

106   public void connect (PipedOutputStream JavaDoc oStrm) throws IOException JavaDoc {
107     if (DebugFile.trace) DebugFile.writeln("FTPWorkerThread.connect([PipedOutputStream])");
108     oInPipe.connect (oStrm);
109   } // connect()
110

111   //-----------------------------------------------------------
112

113   public void chdir (String JavaDoc sPath) throws FTPException,IOException JavaDoc {
114     oFTPC.chdir(sPath);
115   } // chdir()
116

117   //-----------------------------------------------------------
118

119   public void run() {
120     try {
121       if (GET_PIPE==iCmd) {
122         if (DebugFile.trace) DebugFile.writeln("oFTPC.get([PipedOutputStream]," + sPar1 + ")");
123         oFTPC.get(oOutPipe, sPar1);
124         oFTPC.quit();
125         bLoged=false;
126       }
127       else if (PUT_PIPE==iCmd) {
128         if (DebugFile.trace) DebugFile.writeln("oFTPC.put([PipedInputStream]," + sPar1 + ")");
129         oFTPC.put(oInPipe, sPar1);
130         oFTPC.quit();
131         bLoged=false;
132       }
133       else if (MOV_PIPE==iCmd) {
134         if (DebugFile.trace) DebugFile.writeln("oFTPC.get([PipedOutputStream]," + sPar1 + ")");
135         oFTPC.get(oOutPipe, sPar1);
136         if (DebugFile.trace) DebugFile.writeln("oFTPC.delete([PipedOutputStream]," + sPar1 + ")");
137         oFTPC.delete(sPar1);
138         oFTPC.quit();
139         bLoged=false;
140       }
141     }
142     catch (FTPException ftpe) {
143       if (DebugFile.trace) DebugFile.writeln("FTPException:" + ftpe.getMessage());
144     }
145     catch (IOException JavaDoc ioe) {
146       if (DebugFile.trace) DebugFile.writeln("IOException:" + ioe.getMessage());
147     }
148   } // run()
149
} // FTPWorkerThread
Popular Tags