1 20 package com.presumo.jms.persistence; 21 22 import com.presumo.jms.message.JmsMessage; 23 import com.presumo.jms.message.MessageEncoder; 24 import com.presumo.jms.resources.Resources; 25 import com.presumo.util.log.Logger; 26 import com.presumo.util.log.LoggerFactory; 27 28 import java.io.BufferedInputStream ; 29 import java.io.BufferedOutputStream ; 30 import java.io.DataInputStream ; 31 import java.io.DataOutputStream ; 32 import java.io.EOFException ; 33 import java.io.IOException ; 34 import java.io.File ; 35 import java.io.FileInputStream ; 36 import java.io.FileOutputStream ; 37 38 import java.util.HashSet ; 39 import java.util.Iterator ; 40 import java.util.LinkedList ; 41 42 43 46 class CheckPointFile 47 { 48 49 52 static void write(File the_file, 53 LinkedList mainQueue, 54 LinkedList pendingDelete) throws IOException 55 { 56 FileOutputStream fout = null; 57 BufferedOutputStream bout = null; 58 DataOutputStream dout = null; 59 try { 60 fout = new FileOutputStream (the_file); 61 bout = new BufferedOutputStream (fout,1024); 62 dout = new DataOutputStream (bout); 63 dout.writeInt(0); writeList(dout,mainQueue); 65 writeList(dout,pendingDelete); 66 dout.flush(); 67 fout.getFD().sync(); 68 } 69 finally { 70 if (dout != null) { 71 dout.close(); 72 } 73 if (bout != null) { 74 bout.close(); 75 } 76 if (fout != null) { 77 fout.close(); 78 } 79 } 80 81 } 82 83 86 static void read(File the_file, 87 LinkedList mainQueue, 88 LinkedList pendingDelete, 89 HashSet persistentDelete) throws IOException 90 { 91 92 mainQueue.clear(); 93 pendingDelete.clear(); 94 FileInputStream fin = null; 95 BufferedInputStream bin = null; 96 DataInputStream din = null; 97 try { 98 fin = new FileInputStream (the_file); 99 bin = new BufferedInputStream (fin,1024); 100 din = new DataInputStream (bin); 101 din.readInt(); readList(din, mainQueue); 103 readList(din, pendingDelete); 104 Iterator it = pendingDelete.iterator(); 105 while(it.hasNext()) { 106 JmsMessage msg = (JmsMessage)it.next(); 107 persistentDelete.add(msg.getJMSMessageID()); 108 } 109 } 110 finally { 111 if (din != null) { 112 din.close(); 113 } 114 if (bin != null) { 115 bin.close(); 116 } 117 if (fin != null) { 118 fin.close(); 119 } 120 } 121 } 122 123 124 125 128 static private void writeList(DataOutputStream dout, LinkedList list) 129 throws IOException 130 { 131 try { 132 logger.entry("writeList"); 133 134 dout.writeInt(PersistentQueue.getNumPersistent(list)); 135 Iterator it = list.iterator(); 136 while (it.hasNext()) { 137 JmsMessage msg = (JmsMessage) it.next(); 138 if (PersistentQueue.isMessagePersistent(msg)) { 139 if (logger.isDebugEnabled()) { 140 logger.debug("Writing Message: "+msg); 141 } 142 143 MessageEncoder.encode(msg, dout); 144 145 } 146 } 147 } 148 finally { 149 logger.exit("writeList"); 150 } 151 } 152 153 154 157 static private void readList(DataInputStream din, LinkedList list) 158 throws IOException , EOFException 159 { 160 try { 161 logger.entry("readList"); 162 163 int size = din.readInt(); 164 for (int i = 0 ; i < size; i++) { 165 JmsMessage msg = MessageEncoder.decode(din); 166 167 list.addLast(msg); 168 if (logger.isDebugEnabled()) { 169 logger.debug("Reading MEssage: " + msg); 170 } 171 } 172 } 173 finally { 174 logger.exit("readList"); 175 } 176 } 177 178 179 private static Logger logger = 181 LoggerFactory.getLogger(CheckPointFile.class, Resources.getBundle()); 182 184 } 185 | Popular Tags |