KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > presumo > jms > persistence > CheckPointFile


1 /**
2  * This file is part of Presumo.
3  *
4  * Presumo is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Presumo is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Presumo; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Copyright (c) 2001 Rob Cauble
19  */

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 JavaDoc;
29 import java.io.BufferedOutputStream JavaDoc;
30 import java.io.DataInputStream JavaDoc;
31 import java.io.DataOutputStream JavaDoc;
32 import java.io.EOFException JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.File JavaDoc;
35 import java.io.FileInputStream JavaDoc;
36 import java.io.FileOutputStream JavaDoc;
37
38 import java.util.HashSet JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.LinkedList JavaDoc;
41
42
43 /**
44  * Encapsulates how a CheckPointFile is written and read from disk.
45  */

46 class CheckPointFile
47 {
48     
49   /**
50    * Mechanism by which the CheckPointFile is written.
51    */

52   static void write(File JavaDoc the_file,
53                     LinkedList JavaDoc mainQueue,
54                     LinkedList JavaDoc pendingDelete) throws IOException JavaDoc
55   {
56     FileOutputStream JavaDoc fout = null;
57     BufferedOutputStream JavaDoc bout = null;
58     DataOutputStream JavaDoc dout = null;
59     try {
60       fout = new FileOutputStream JavaDoc(the_file);
61       bout = new BufferedOutputStream JavaDoc(fout,1024);
62       dout = new DataOutputStream JavaDoc(bout);
63       dout.writeInt(0); //version
64
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   /**
84    * Mechanism by which the CheckPointFile is read from disk
85    */

86   static void read(File JavaDoc the_file,
87                    LinkedList JavaDoc mainQueue,
88                    LinkedList JavaDoc pendingDelete,
89                    HashSet JavaDoc persistentDelete) throws IOException JavaDoc
90   {
91
92     mainQueue.clear();
93     pendingDelete.clear();
94     FileInputStream JavaDoc fin = null;
95     BufferedInputStream JavaDoc bin = null;
96     DataInputStream JavaDoc din = null;
97     try {
98       fin = new FileInputStream JavaDoc(the_file);
99       bin = new BufferedInputStream JavaDoc(fin,1024);
100       din = new DataInputStream JavaDoc(bin);
101       din.readInt(); //version
102
readList(din, mainQueue);
103       readList(din, pendingDelete);
104       Iterator JavaDoc 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   /*
126    * Internal method to dump a list to the output stream.
127    */

128   static private void writeList(DataOutputStream JavaDoc dout, LinkedList JavaDoc list)
129           throws IOException JavaDoc
130   {
131     try {
132       logger.entry("writeList");
133       
134       dout.writeInt(PersistentQueue.getNumPersistent(list));
135       Iterator JavaDoc 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   /*
155    * Internal method to read a dumped list from the input stream.
156    */

157   static private void readList(DataInputStream JavaDoc din, LinkedList JavaDoc list)
158           throws IOException JavaDoc, EOFException JavaDoc
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   ////////////////////////////// Misc stuff ////////////////////////////////
180
private static Logger logger =
181     LoggerFactory.getLogger(CheckPointFile.class, Resources.getBundle());
182   ///////////////////////////////////////////////////////////////////////////
183

184 }
185
Popular Tags