1 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 ; 29 import java.util.Stack ; 30 import java.net.ServerSocket ; 31 import java.net.Socket ; 32 import java.net.InetSocketAddress ; 33 import java.io.File ; 34 import java.io.IOException ; 35 import java.io.FileNotFoundException ; 36 import java.io.Serializable ; 37 import java.io.OutputStream ; 38 import java.io.InputStream ; 39 import java.io.FileInputStream ; 40 41 42 47 public class FileStorageCollect 48 { 49 53 static public final int BLOCK_SIZE = 2048; 54 static protected Hashtable active_collects = new Hashtable (); 55 static protected Stack old_collects = new Stack (); 56 57 static public FileStorageCollect newCollect(File 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 ex) 69 { 70 throw new Error ("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 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 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 142 143 protected CollectKey key; 144 protected int fileIndex; 145 protected File [] 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 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 dir) 171 throws FileNotFoundException 172 { 173 files = dir.listFiles(); 174 if (files == null) 175 { 176 throw new FileNotFoundException ("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 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 221 class FileServer extends ServerSocket implements Runnable 222 { 223 public FileServer() 224 throws IOException 225 { 226 super(); 227 InetSocketAddress addr = new InetSocketAddress (Network.getInetAddress(), 0); 228 try 229 { 230 bind(addr); 231 } 232 catch (IOException ex) 233 { 234 IOException ex2 = new IOException ("Can't bind address " + addr); 235 ex2.setStackTrace(ex.getStackTrace()); 236 throw ex2; 237 } 238 new Thread (this).start(); 239 } 240 241 242 public void run() 243 { 244 Socket sock; 245 OutputStream out; 246 InputStream 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 (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 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 ex) 286 { 287 ex.printStackTrace(System.err); 288 } 289 } 290 } 291 } 292 } 293 } 294 | Popular Tags |