KickJava   Java API By Example, From Geeks To Geeks.

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


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.LinkedList JavaDoc;
35 import java.util.ListIterator JavaDoc;
36
37
38 class LogFileEntryInsert extends LogFileEntry
39 {
40   private JmsMessage [] msgs;
41
42
43     /////////////////////////////////////////////////////////////////////////
44
// Constructors //
45
/////////////////////////////////////////////////////////////////////////
46

47   LogFileEntryInsert()
48   {
49     this(null);
50   }
51
52   LogFileEntryInsert(JmsMessage [] msgs)
53   {
54     this.msgs = msgs;
55   }
56     
57     /////////////////////////////////////////////////////////////////////////
58
// Package Methods //
59
/////////////////////////////////////////////////////////////////////////
60

61     
62   boolean writeAndProcess(LinkedList JavaDoc mainQueue,
63                           LinkedList JavaDoc pendingDelete,
64                           HashSet JavaDoc persistentDelete,
65                           DataOutput JavaDoc out) throws IOException JavaDoc
66   {
67     try {
68       logger.entry("writeAndProcess");
69       
70       int num_per = PersistentQueue.getNumPersistent(msgs);
71       if (num_per == 0) {
72          restore(mainQueue, pendingDelete, persistentDelete);
73          return false;
74       }
75       out.writeInt(INSERT);
76       out.writeInt(num_per);
77       
78       for (int i = 0; i < msgs.length; i++) {
79         JmsMessage message = msgs[i];
80         if (PersistentQueue.isMessagePersistent(message)) {
81           if (logger.isDebugEnabled()) {
82             logger.debug("Writing Message to Log:"+message);
83           }
84           MessageEncoder.encode(message, out);
85         }
86       }
87       
88       restore(mainQueue, pendingDelete, persistentDelete);
89       return true;
90     }
91     finally {
92       logger.exit("write");
93     }
94   }
95     
96     
97   void read(DataInput JavaDoc in) throws IOException JavaDoc, EOFException JavaDoc
98   {
99     try {
100       logger.entry("read");
101       JmsMessage [] temp = new JmsMessage[in.readInt()];
102       
103       for (int i = 0; i < temp.length; i++) {
104         temp[i] = MessageEncoder.decode(in);
105         if (logger.isDebugEnabled()) {
106            logger.debug("Reading Message:"+temp[i]);
107         }
108       }
109       msgs = temp;
110     }
111     finally {
112       logger.exit("read");
113     }
114   }
115
116   void restore(LinkedList JavaDoc mainQueue,
117                LinkedList JavaDoc pendingDelete,
118                HashSet JavaDoc persistentDelete)
119   {
120     for (int i = 0; i < msgs.length; i++) {
121       mainQueue.addLast(msgs[i]);
122     }
123   }
124
125
126   ////////////////////////////// Misc stuff ////////////////////////////////
127
private static Logger logger =
128     LoggerFactory.getLogger(LogFileEntryInsert.class, Resources.getBundle());
129   ///////////////////////////////////////////////////////////////////////////
130
}
131
Popular Tags