KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ftpserver > Data


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package ftpserver;
16
17 import org.quickserver.net.*;
18 import org.quickserver.net.server.*;
19
20 import org.quickserver.util.pool.PoolableObject;
21 import org.apache.commons.pool.BasePoolableObjectFactory;
22 import org.apache.commons.pool.PoolableObjectFactory;
23
24 import java.io.*;
25 import java.net.*;
26
27 public class Data implements ClientData, PoolableObject {
28     boolean isTransferring = false;
29     boolean isRenameFrom = false;
30     boolean isStop = false;
31     boolean isPassive = false;
32
33     Socket socket = null;
34     ServerSocket server = null; //if passive
35
int socketPort = -1;
36     int serverPort = -1;
37     boolean closeDataServer = true;
38     String JavaDoc ip = null;
39
40     //pipelining
41
boolean wasQuit = false;
42     boolean isProcessing = false;
43     int noOfBytesTransferred = -1;
44     int startPosition = 0; //for REST
45

46     String JavaDoc root = null; //no ending slash \
47
String JavaDoc wDir = "/"; //WORKING DIRECTORY
48

49     String JavaDoc account = null; //ACCOUNT
50
String JavaDoc username = ""; //USERNAME
51
boolean binary = false;
52     char type = 'A'; //ASCII,IMAGE
53
char typeSub = 'N'; //Non-print,
54
String JavaDoc mode = "Stream"; //Stream
55
String JavaDoc structure = "File"; //File, Record
56

57     public void sendFile(String JavaDoc file) throws Exception JavaDoc {
58         isStop = false;
59         if(socket==null)
60             throw new IOException("No client connected");
61         String JavaDoc input = "";
62         int i=0;
63         FileInputStream fin = null;
64         BufferedOutputStream out = null;
65         try {
66             out = new BufferedOutputStream(socket.getOutputStream());
67             fin = new FileInputStream(file);
68             /*
69             if(data.type == 'A') {
70                 PrintWriter rout=new PrintWriter(out);
71                 BufferedReader br = new BufferedReader(new InputStreamReader(fin));
72                 while(true) {
73                     input = br.readLine();
74                     if(input==null)
75                         break;
76                     rout.
77                 }
78                 rout.flush();
79                 //rout.close();
80             } else {
81                 
82             }*/

83             while(true) {
84                 i = fin.read();
85                 if(i==-1 || isStop==true) //if aborted
86
break;
87                 out.write(i);
88             }
89             out.flush();
90         }catch (Exception JavaDoc e){
91             throw e;
92         } finally {
93             if(fin!=null)
94                 fin.close();
95         }
96     }
97
98     public void sendData(String JavaDoc sdata) throws Exception JavaDoc {
99         //System.out.print("Sending data on datacon ");
100
if(socket==null) {
101             Thread.currentThread().sleep(500);
102             if(socket==null) {
103                 throw new IOException("No client connected");
104             }
105         }
106         BufferedOutputStream out = null;
107         try {
108             out = new BufferedOutputStream(socket.getOutputStream());
109             out.write( sdata.getBytes(),0,sdata.length() );
110             out.flush();
111         }catch (Exception JavaDoc e){
112             throw e;
113         }
114     }
115
116     public void startDataServer(ServerSocket acceptSocket, ftpserver.Data data) {
117         new DataServer(acceptSocket, data);
118     }
119
120     //-- PoolableObject
121
public void clean() {
122         isTransferring = false;
123         isRenameFrom = false;
124         isStop = false;
125         isPassive = false;
126         socket = null;
127         server = null;
128         socketPort = -1;
129         serverPort = -1;
130         closeDataServer = true;
131         ip = null;
132         wasQuit = false;
133         isProcessing = false;
134         noOfBytesTransferred = -1;
135         startPosition = 0;
136         root = null;
137         wDir = "/";
138         account = null;
139         username = "";
140         binary = false;
141         type = 'A';
142         typeSub = 'N';
143         mode = "Stream";
144         structure = "File";
145     }
146
147     public boolean isPoolable() {
148         return true;
149     }
150
151     public PoolableObjectFactory getPoolableObjectFactory() {
152         return new BasePoolableObjectFactory() {
153             public Object JavaDoc makeObject() {
154                 return new Data();
155             }
156             public void passivateObject(Object JavaDoc obj) {
157                 Data ed = (Data)obj;
158                 ed.clean();
159             }
160             public void destroyObject(Object JavaDoc obj) {
161                 if(obj==null) return;
162                 passivateObject(obj);
163                 obj = null;
164             }
165             public boolean validateObject(Object JavaDoc obj) {
166                 if(obj==null)
167                     return false;
168                 else
169                     return true;
170             }
171         };
172     }
173 }
174
175 class DataServer extends Thread JavaDoc {
176     ServerSocket acceptSocket=null;
177     ftpserver.Data data = null;
178     public DataServer(ServerSocket acceptSocket, ftpserver.Data data) {
179         super("DataServer");
180         this.acceptSocket = acceptSocket;
181         this.data = data;
182         setDaemon(true);
183         this.data.isStop = false;
184         start();
185     }
186
187     public void run() {
188         try {
189             data.closeDataServer = false;
190             data.socket = acceptSocket.accept();
191             //System.out.println("Data con opened.");
192
while(data!=null && data.closeDataServer == false
193                 && data.isStop==false ) {
194                 Thread.sleep(300);
195             }
196             if(data.socket!=null) {
197                 data.socket.close();
198             }
199         } catch(IOException e) {
200             System.err.println("Error at dataServer :"+e);
201         } catch(InterruptedException JavaDoc e) {
202             System.err.println("Thread Error at dataServer :"+e);
203         } finally {
204             try {
205                 acceptSocket.close();
206             }catch(Exception JavaDoc re){
207                 System.err.println("BAD Error at dataServer :"+re);
208             }
209         }
210         data.socket = null;
211         data.server = null;
212     }
213
214     
215 }
216
217
218
219
Popular Tags