1 16 17 package org.apache.catalina.cluster.deploy; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.FileInputStream ; 22 import java.io.FileOutputStream ; 23 import java.io.FileNotFoundException ; 24 25 39 public class FileMessageFactory { 40 41 public static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory 42 .getLog(FileMessageFactory.class); 43 44 47 public static final int READ_SIZE = 1024 * 10; 49 52 protected File file = null; 53 54 58 protected boolean openForWrite; 59 60 63 protected boolean closed = false; 64 65 68 protected FileInputStream in; 69 70 73 protected FileOutputStream out; 74 75 78 protected int nrOfMessagesProcessed = 0; 79 80 83 protected long size = 0; 84 85 88 protected long totalNrOfMessages = 0; 89 90 93 protected byte[] data = new byte[READ_SIZE]; 94 95 113 private FileMessageFactory(File f, boolean openForWrite) 114 throws FileNotFoundException , IOException { 115 this.file = f; 116 this.openForWrite = openForWrite; 117 if (log.isDebugEnabled()) 118 log.debug("open file " + f + " write " + openForWrite); 119 if (openForWrite) { 120 if (!file.exists()) 121 file.createNewFile(); 122 out = new FileOutputStream (f); 123 } else { 124 size = file.length(); 125 totalNrOfMessages = (size / READ_SIZE) + 1; 126 in = new FileInputStream (f); 127 } 129 } 130 131 147 public static FileMessageFactory getInstance(File f, boolean openForWrite) 148 throws FileNotFoundException , IOException { 149 return new FileMessageFactory(f, openForWrite); 150 } 151 152 170 public FileMessage readMessage(FileMessage f) 171 throws IllegalArgumentException , IOException { 172 checkState(false); 173 int length = in.read(data); 174 if (length == -1) { 175 cleanup(); 176 return null; 177 } else { 178 f.setData(data, length); 179 f.setTotalLength(size); 180 f.setTotalNrOfMsgs(totalNrOfMessages); 181 f.setMessageNumber(++nrOfMessagesProcessed); 182 return f; 183 } } 185 186 199 public boolean writeMessage(FileMessage msg) 200 throws IllegalArgumentException , IOException { 201 if (!openForWrite) 202 throw new IllegalArgumentException ( 203 "Can't write message, this factory is reading."); 204 if (log.isDebugEnabled()) 205 log.debug("Message " + msg + " data " + msg.getData() 206 + " data length " + msg.getDataLength() + " out " + out); 207 if (out != null) { 208 out.write(msg.getData(), 0, msg.getDataLength()); 209 nrOfMessagesProcessed++; 210 out.flush(); 211 if (msg.getMessageNumber() == msg.getTotalNrOfMsgs()) { 212 out.close(); 213 cleanup(); 214 return true; 215 } } else { 217 if (log.isWarnEnabled()) 218 log.warn("Receive Message again -- Sender ActTimeout to short [ path: " 219 + msg.getContextPath() 220 + " war: " 221 + msg.getFileName() 222 + " data: " 223 + msg.getData() 224 + " data length: " + msg.getDataLength() + " ]"); 225 } 226 return false; 227 } 229 232 public void cleanup() { 233 if (in != null) 234 try { 235 in.close(); 236 } catch (Exception ignore) { 237 } 238 if (out != null) 239 try { 240 out.close(); 241 } catch (Exception ignore) { 242 } 243 in = null; 244 out = null; 245 size = 0; 246 closed = true; 247 data = null; 248 nrOfMessagesProcessed = 0; 249 totalNrOfMessages = 0; 250 } 251 252 261 protected void checkState(boolean openForWrite) 262 throws IllegalArgumentException { 263 if (this.openForWrite != openForWrite) { 264 cleanup(); 265 if (openForWrite) 266 throw new IllegalArgumentException ( 267 "Can't write message, this factory is reading."); 268 else 269 throw new IllegalArgumentException ( 270 "Can't read message, this factory is writing."); 271 } 272 if (this.closed) { 273 cleanup(); 274 throw new IllegalArgumentException ("Factory has been closed."); 275 } 276 } 277 278 286 public static void main(String [] args) throws Exception { 287 288 System.out 289 .println("Usage: FileMessageFactory fileToBeRead fileToBeWritten"); 290 System.out 291 .println("Usage: This will make a copy of the file on the local file system"); 292 FileMessageFactory read = getInstance(new File (args[0]), false); 293 FileMessageFactory write = getInstance(new File (args[1]), true); 294 FileMessage msg = new FileMessage(null, args[0], args[0]); 295 msg = read.readMessage(msg); 296 System.out.println("Expecting to write " + msg.getTotalNrOfMsgs() 297 + " messages."); 298 int cnt = 0; 299 while (msg != null) { 300 write.writeMessage(msg); 301 cnt++; 302 msg = read.readMessage(msg); 303 } System.out.println("Actually wrote " + cnt + " messages."); 305 } 307 public File getFile() { 308 return file; 309 } 310 311 } | Popular Tags |