001: package com.knowgate.dfs;
002:
003: /**
004: * <p>Direct piped copy from an FTP source to another FTP target.</p>
005: * <p>This is an alpha state testing module.</p>
006: * @author Sergio Montoro Ten
007: * @version 0.3alpha
008: */
009:
010: import java.io.IOException;
011: import java.io.FileWriter;
012: import java.io.PrintWriter;
013: import java.io.PipedInputStream;
014: import java.io.PipedOutputStream;
015:
016: import com.enterprisedt.net.ftp.FTPClient;
017: import com.enterprisedt.net.ftp.FTPException;
018: import com.enterprisedt.net.ftp.FTPTransferType;
019: import com.enterprisedt.net.ftp.FTPConnectMode;
020:
021: import com.knowgate.debug.DebugFile;
022:
023: public class FTPWorkerThread extends Thread {
024: private FTPClient oFTPC;
025: private boolean bLoged;
026: private PipedOutputStream oOutPipe;
027: private PipedInputStream oInPipe;
028: private FileWriter oFW;
029: private PrintWriter oPW;
030: private int iCmd;
031: private String sPar1;
032: private FileSystem oFileSys;
033:
034: private int GET_PIPE = 16;
035: private int PUT_PIPE = 32;
036: private int MOV_PIPE = 64;
037:
038: public FTPWorkerThread(String sHost, String sUser, String sPassword)
039: throws FTPException, IOException {
040: bLoged = false;
041: oFileSys = null;
042:
043: if (DebugFile.trace)
044: DebugFile.writeln("new FTPClient(" + sHost + ")");
045:
046: oFTPC = new FTPClient(sHost);
047: oFTPC.debugResponses(DebugFile.trace);
048:
049: if (DebugFile.trace) {
050: oFW = new FileWriter("/tmp/javatrc.txt", true);
051: oPW = new PrintWriter(oFW, true);
052: oFTPC.setLogStream(oPW);
053: DebugFile.writeln("FTPClient.login(" + sUser + ","
054: + sPassword + ")");
055: }
056:
057: oFTPC.login(sUser, sPassword);
058: bLoged = true;
059:
060: oFTPC.setConnectMode(FTPConnectMode.ACTIVE);
061: oFTPC.setType(FTPTransferType.BINARY);
062: } // FTPWorkerThread()
063:
064: //-----------------------------------------------------------
065:
066: public PipedInputStream getInputPipe() throws IOException {
067: return oInPipe;
068: } // getInputPipe()
069:
070: //-----------------------------------------------------------
071:
072: public PipedOutputStream getOutputPipe() throws IOException {
073: return oOutPipe;
074: } // getOutputPipe()
075:
076: //-----------------------------------------------------------
077:
078: public void get(String sFile) throws IOException {
079: oOutPipe = new PipedOutputStream();
080: sPar1 = sFile;
081: iCmd = GET_PIPE;
082: }
083:
084: //-----------------------------------------------------------
085:
086: public void put(String sFile) throws IOException {
087: oInPipe = new PipedInputStream();
088: sPar1 = sFile;
089: iCmd = PUT_PIPE;
090: } // put()
091:
092: //-----------------------------------------------------------
093:
094: public void move(String sFile) throws IOException {
095: oOutPipe = new PipedOutputStream();
096: sPar1 = sFile;
097: iCmd = MOV_PIPE;
098: }
099:
100: //-----------------------------------------------------------
101:
102: public void connect(PipedInputStream oStrm) throws IOException {
103: if (DebugFile.trace)
104: DebugFile
105: .writeln("FTPWorkerThread.connect([PipedInputStream])");
106: oOutPipe.connect(oStrm);
107: } // connect()
108:
109: //-----------------------------------------------------------
110:
111: public void connect(PipedOutputStream oStrm) throws IOException {
112: if (DebugFile.trace)
113: DebugFile
114: .writeln("FTPWorkerThread.connect([PipedOutputStream])");
115: oInPipe.connect(oStrm);
116: } // connect()
117:
118: //-----------------------------------------------------------
119:
120: public void chdir(String sPath) throws FTPException, IOException {
121: oFTPC.chdir(sPath);
122: } // chdir()
123:
124: //-----------------------------------------------------------
125:
126: public void run() {
127: try {
128: if (GET_PIPE == iCmd) {
129: if (DebugFile.trace)
130: DebugFile.writeln("oFTPC.get([PipedOutputStream],"
131: + sPar1 + ")");
132: oFTPC.get(oOutPipe, sPar1);
133: oFTPC.quit();
134: bLoged = false;
135: } else if (PUT_PIPE == iCmd) {
136: if (DebugFile.trace)
137: DebugFile.writeln("oFTPC.put([PipedInputStream],"
138: + sPar1 + ")");
139: oFTPC.put(oInPipe, sPar1);
140: oFTPC.quit();
141: bLoged = false;
142: } else if (MOV_PIPE == iCmd) {
143: if (DebugFile.trace)
144: DebugFile.writeln("oFTPC.get([PipedOutputStream],"
145: + sPar1 + ")");
146: oFTPC.get(oOutPipe, sPar1);
147: if (DebugFile.trace)
148: DebugFile
149: .writeln("oFTPC.delete([PipedOutputStream],"
150: + sPar1 + ")");
151: oFTPC.delete(sPar1);
152: oFTPC.quit();
153: bLoged = false;
154: }
155: } catch (FTPException ftpe) {
156: if (DebugFile.trace)
157: DebugFile.writeln("FTPException:" + ftpe.getMessage());
158: } catch (IOException ioe) {
159: if (DebugFile.trace)
160: DebugFile.writeln("IOException:" + ioe.getMessage());
161: }
162: } // run()
163: } // FTPWorkerThread
|