KickJava   Java API By Example, From Geeks To Geeks.

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


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.DataInput JavaDoc;
29 import java.io.DataOutput JavaDoc;
30 import java.io.EOFException JavaDoc;
31 import java.io.IOException JavaDoc;
32
33 import java.util.HashSet JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.LinkedList JavaDoc;
36 import java.util.ListIterator JavaDoc;
37
38 /**
39  *
40  */

41 class LogFileEntryGetNext extends LogFileEntry
42 {
43   private int num_entries;
44   
45
46     /////////////////////////////////////////////////////////////////////////
47
// Constructors //
48
/////////////////////////////////////////////////////////////////////////
49
LogFileEntryGetNext()
50   {
51     this(0);
52   }
53   
54   
55   LogFileEntryGetNext(int num_entries)
56   {
57     this.num_entries = num_entries;
58   }
59   
60
61     /////////////////////////////////////////////////////////////////////////
62
// Package Methods //
63
/////////////////////////////////////////////////////////////////////////
64

65   boolean writeAndProcess(LinkedList JavaDoc mainQueue,
66                           LinkedList JavaDoc pendingDelete,
67                           HashSet JavaDoc persistentDelete,
68                           DataOutput JavaDoc out) throws IOException JavaDoc
69   {
70     try {
71       logger.entry("write");
72       int num_to_move = 0;
73       int num_persistent = 0;
74       Iterator JavaDoc it = mainQueue.iterator();
75       
76       for (int i = 0; i < num_entries && it.hasNext(); i++) {
77         num_to_move++;
78         JmsMessage msg = (JmsMessage)it.next();
79         if (PersistentQueue.isMessagePersistent(msg)) {
80           num_persistent++;
81         }
82       }
83       
84       if (num_persistent == 0) {
85         process(mainQueue, pendingDelete, persistentDelete, num_to_move);
86         return false;
87       }
88       out.writeInt(GET_NEXT);
89       out.writeInt(num_persistent);
90       process(mainQueue, pendingDelete, persistentDelete, num_to_move);
91       return true;
92     }
93     finally {
94       logger.exit("write");
95     }
96   }
97   
98   
99   void read(DataInput JavaDoc in) throws IOException JavaDoc, EOFException JavaDoc
100   {
101     try {
102       logger.entry("read");
103       num_entries = in.readInt();
104     }
105     finally {
106       logger.exit("read");
107     }
108   }
109   
110   
111   private void process(LinkedList JavaDoc mainQueue,
112                        LinkedList JavaDoc pendingDelete,
113                        HashSet JavaDoc persistentDelete,
114                        int num)
115   {
116     for (int i = 0; i < num; i++) {
117       JmsMessage msg = (JmsMessage) mainQueue.removeFirst();
118       if (PersistentQueue.isMessagePersistent(msg)) {
119         persistentDelete.add(msg.getJMSMessageID());
120         // DTG
121
pendingDelete.addLast(msg);
122       }
123
124     }
125   }
126   
127   
128   void restore(LinkedList JavaDoc mainQueue,
129                LinkedList JavaDoc pendingDelete,
130                HashSet JavaDoc persistentDelete)
131   {
132     process(mainQueue,pendingDelete,persistentDelete,num_entries);
133   }
134   
135   
136   ////////////////////////////// Misc stuff ////////////////////////////////
137
private static Logger logger =
138     LoggerFactory.getLogger(LogFileEntryGetNext.class, Resources.getBundle());
139   ///////////////////////////////////////////////////////////////////////////
140
}
141
Popular Tags