KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > storage > lib > filestorage > FileStorageCollect


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2003,2004 France Telecom R&D
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * CLIF $Name: $
20 *
21 * Contact: clif@objectweb.org
22 */

23
24 package org.objectweb.clif.storage.lib.filestorage;
25
26 import org.objectweb.clif.storage.api.CollectKey;
27 import org.objectweb.clif.util.Network;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Stack JavaDoc;
30 import java.net.ServerSocket JavaDoc;
31 import java.net.Socket JavaDoc;
32 import java.net.InetSocketAddress JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.FileNotFoundException JavaDoc;
36 import java.io.Serializable JavaDoc;
37 import java.io.OutputStream JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.io.FileInputStream JavaDoc;
40
41
42 /**
43  * Each instance of this class represents and manages the collect of test data files located in
44  * a given directory. Each file is transfered by socket, within a dedicated "collect step".
45  * @author Bruno Dillenseger
46  */

47 public class FileStorageCollect
48 {
49     ///////////////////////////////////
50
// static attributes and methods //
51
///////////////////////////////////
52

53     static public final int BLOCK_SIZE = 2048;
54     static protected Hashtable JavaDoc active_collects = new Hashtable JavaDoc();
55     static protected Stack JavaDoc old_collects = new Stack JavaDoc();
56
57     static public FileStorageCollect newCollect(File JavaDoc dir)
58     {
59         synchronized (active_collects)
60         {
61             FileStorageCollect collect = null;
62             if (old_collects.isEmpty())
63             {
64                 try
65                 {
66                     collect = new FileStorageCollect();
67                 }
68                 catch (IOException JavaDoc ex)
69                 {
70                     throw new Error JavaDoc("Can't initiate FileStorage data collect", ex);
71                 }
72             }
73             else
74             {
75                 collect = (FileStorageCollect) old_collects.pop();
76             }
77             CollectKey key = new FileStorageCollectKey();
78             try
79             {
80                 collect.setDirectory(dir);
81                 collect.setKey(key);
82                 active_collects.put(key, collect);
83             }
84             catch (FileNotFoundException JavaDoc ex)
85             {
86                 old_collects.push(collect);
87                 collect = null;
88             }
89             return collect;
90         }
91     }
92
93
94     static public FileStorageCollect getCollect(CollectKey key)
95     {
96         return (FileStorageCollect)active_collects.get(key);
97     }
98
99
100     static public Serializable JavaDoc collect(CollectKey key)
101     {
102         FileStorageCollect collect = getCollect(key);
103         if (collect != null)
104         {
105             return collect.collect();
106         }
107         else
108         {
109             return null;
110         }
111     }
112
113
114     static public void close(CollectKey key)
115     {
116         FileStorageCollect collect = getCollect(key);
117         if (collect != null)
118         {
119             collect.close();
120         }
121     }
122
123
124     public static long getSize(CollectKey key)
125     {
126         FileStorageCollect collect = getCollect(key);
127         if (collect != null)
128         {
129             return collect.getSize();
130         }
131         else
132         {
133             return -1;
134         }
135     }
136
137
138     ///////////////////////////////
139
// end of static definitions //
140
///////////////////////////////
141

142
143     protected CollectKey key;
144     protected int fileIndex;
145     protected File JavaDoc[] files;
146     protected FileServer fileServer;
147     protected long size;
148     protected byte[] buffer = new byte[BLOCK_SIZE];
149
150
151     private FileStorageCollect()
152         throws IOException JavaDoc
153     {
154         fileServer = new FileServer();
155     }
156
157
158     public CollectKey getKey()
159     {
160         return key;
161     }
162
163
164     private void setKey(CollectKey key)
165     {
166         this.key = key;
167     }
168
169
170     private void setDirectory(File JavaDoc dir)
171         throws FileNotFoundException JavaDoc
172     {
173         files = dir.listFiles();
174         if (files == null)
175         {
176             throw new FileNotFoundException JavaDoc("directory " + dir + " does not exist");
177         }
178         fileIndex = -1;
179         size = 0;
180         for (int i=0 ; i<files.length ; size += files[i++].length());
181     }
182
183
184     private Serializable JavaDoc collect()
185     {
186         if (++fileIndex < files.length)
187         {
188             return new FileStorageCollectStep(
189                 files[fileIndex].getName(),
190                 fileServer.getLocalSocketAddress());
191         }
192         else
193         {
194             close();
195             return null;
196         }
197     }
198
199
200     private void close()
201     {
202         synchronized (active_collects)
203         {
204             if (active_collects.containsKey(key))
205             {
206                 old_collects.push(active_collects.remove(key));
207             }
208         }
209     }
210
211
212     private long getSize()
213     {
214         return size;
215     }
216
217
218     /**
219      * Inner class for files download through sockets
220      */

221     class FileServer extends ServerSocket JavaDoc implements Runnable JavaDoc
222     {
223         public FileServer()
224             throws IOException JavaDoc
225         {
226             super();
227             InetSocketAddress JavaDoc addr = new InetSocketAddress JavaDoc(Network.getInetAddress(), 0);
228             try
229             {
230                 bind(addr);
231             }
232             catch (IOException JavaDoc ex)
233             {
234                 IOException JavaDoc ex2 = new IOException JavaDoc("Can't bind address " + addr);
235                 ex2.setStackTrace(ex.getStackTrace());
236                 throw ex2;
237             }
238             new Thread JavaDoc(this).start();
239         }
240
241
242         public void run()
243         {
244             Socket JavaDoc sock;
245             OutputStream JavaDoc out;
246             InputStream JavaDoc in;
247             while (true)
248             {
249                 sock = null;
250                 in = null;
251                 out = null;
252                 try
253                 {
254                     sock = accept();
255                     out = sock.getOutputStream();
256                     in = new FileInputStream JavaDoc(files[fileIndex]);
257                     int n = in.read(buffer);
258                     while (n != -1)
259                     {
260                         out.write(buffer, 0, n);
261                         n = in.read(buffer);
262                     }
263                 }
264                 catch (IOException JavaDoc ex)
265                 {
266                     ex.printStackTrace(System.err);
267                 }
268                 finally
269                 {
270                     try
271                     {
272                         if (sock != null)
273                         {
274                             if (out != null)
275                             {
276                                 if (in != null)
277                                 {
278                                     in.close();
279                                 }
280                                 out.close();
281                             }
282                             sock.close();
283                         }
284                     }
285                     catch (IOException JavaDoc ex)
286                     {
287                         ex.printStackTrace(System.err);
288                     }
289                 }
290             }
291         }
292     }
293 }
294
Popular Tags