KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > mom > util > MessagePersistenceModule


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2005 - ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Nicolas Tachker (ScalAgent)
21  * Contributor(s):
22  */

23 package org.objectweb.joram.mom.util;
24
25 import fr.dyade.aaa.agent.AgentServer;
26 import fr.dyade.aaa.agent.TransactionError;
27 import fr.dyade.aaa.util.Transaction;
28
29 import org.objectweb.joram.shared.messages.Message;
30 import org.objectweb.joram.shared.messages.MessageBody;
31 import org.objectweb.joram.shared.messages.MessagePersistent;
32 import org.objectweb.joram.mom.MomTracing;
33 import org.objectweb.util.monolog.api.BasicLevel;
34
35 import java.util.Vector JavaDoc;
36 import java.io.IOException JavaDoc;
37
38
39 /**
40  * The <code>MessagePersistenceModule</code> class is a utility class used
41  * by queues and proxies for persisting, retrieving and deleting messages.
42  */

43 public class MessagePersistenceModule {
44  
45   static public String JavaDoc getSaveName(String JavaDoc agentId,
46                                    MessagePersistent message) {
47     String JavaDoc id = message.getIdentifier();
48     return "msg" + agentId + id.substring(3);
49   }
50
51   /**
52    * save the message (header and body).
53    *
54    * @param agentId id of agent.
55    * @param message Message to save.
56    */

57   static public String JavaDoc save(String JavaDoc agentId,
58                             MessagePersistent message) {
59     if (MomTracing.dbgDestination.isLoggable(BasicLevel.DEBUG))
60       MomTracing.dbgDestination.log(BasicLevel.DEBUG,
61                                     "MessagePersistenceModule.save(" + agentId +
62                                     "," + message + ')');
63     if (MomTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
64       MomTracing.dbgProxy.log(BasicLevel.DEBUG,
65                               "MessagePersistenceModule.save(" + agentId +
66                               "," + message + ')');
67
68     String JavaDoc id = message.getIdentifier();
69     String JavaDoc name = "msg" + agentId + id.substring(3);
70     Transaction tx = AgentServer.getTransaction();
71
72     if (!tx.isPersistent())
73       return null;
74     
75     try {
76       // save header part of message.
77
tx.save(message,name);
78
79       // save body part of message.
80
MessageBody body = message.getMessageBody();
81       if (! body.saved) {
82         body.saved = true;
83         tx.save(body,name+".body");
84       }
85     } catch (Exception JavaDoc exc) {
86       throw new TransactionError(exc.toString());
87     }
88
89     if (MomTracing.dbgDestination.isLoggable(BasicLevel.DEBUG))
90       MomTracing.dbgDestination.log(BasicLevel.DEBUG,
91                                     "msg " + id + " header save to " + name +
92                                     " body save to " + name + ".body");
93     if (MomTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
94       MomTracing.dbgProxy.log(BasicLevel.DEBUG,
95                               "msg " + id + " header save to " + name +
96                               " body save to " + name + ".body");
97     return name;
98   }
99
100   /**
101    * save the message header.
102    *
103    * @param agentId id of agent.
104    * @param message Message to save header.
105    */

106   static public String JavaDoc saveHeader(String JavaDoc agentId,
107                                   MessagePersistent message) {
108     if (MomTracing.dbgDestination.isLoggable(BasicLevel.DEBUG))
109       MomTracing.dbgDestination.log(BasicLevel.DEBUG,
110                                     "MessagePersistenceModule.saveHeader(" + agentId +
111                                     "," + message + ')');
112     if (MomTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
113       MomTracing.dbgProxy.log(BasicLevel.DEBUG,
114                               "MessagePersistenceModule.saveHeader(" + agentId +
115                               "," + message + ')');
116
117     String JavaDoc id = message.getIdentifier();
118     String JavaDoc name = "msg" + agentId + id.substring(3);
119     Transaction tx = AgentServer.getTransaction();
120
121     if (!tx.isPersistent())
122       return null;
123     
124     try {
125       // save header part of message.
126
tx.save(message,name);
127     } catch (Exception JavaDoc exc) {
128       throw new TransactionError(exc.toString());
129     }
130
131     if (MomTracing.dbgDestination.isLoggable(BasicLevel.DEBUG))
132       MomTracing.dbgDestination.log(BasicLevel.DEBUG,
133                                     "msg " + id + " header save to " + name);
134     if (MomTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
135       MomTracing.dbgProxy.log(BasicLevel.DEBUG,
136                               "msg " + id + " header save to " + name);
137     return name;
138   }
139
140   /**
141    * save the message body.
142    *
143    * @param agentId id of agent.
144    * @param message Message to save body.
145    */

146   static public String JavaDoc saveBody(String JavaDoc agentId,
147                                 MessagePersistent message) {
148     if (MomTracing.dbgDestination.isLoggable(BasicLevel.DEBUG))
149       MomTracing.dbgDestination.log(BasicLevel.DEBUG,
150                                     "MessagePersistenceModule.saveBody(" + agentId +
151                                     "," + message + ')');
152     if (MomTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
153       MomTracing.dbgProxy.log(BasicLevel.DEBUG,
154                               "MessagePersistenceModule.saveBody(" + agentId +
155                               "," + message + ')');
156
157     String JavaDoc id = message.getIdentifier();
158     String JavaDoc name = "msg" + agentId + id.substring(3);
159     Transaction tx = AgentServer.getTransaction();
160     
161     if (!tx.isPersistent())
162       return null;
163
164     try {
165       // save body part of message.
166
MessageBody body = message.getMessageBody();
167       if (! body.saved) {
168         body.saved = true;
169         tx.save(body,name+".body");
170       }
171     } catch (Exception JavaDoc exc) {
172       throw new TransactionError(exc.toString());
173     }
174
175     if (MomTracing.dbgDestination.isLoggable(BasicLevel.DEBUG))
176       MomTracing.dbgDestination.log(BasicLevel.DEBUG,
177                                     "msg " + id +
178                                     " body save to " + name + ".body");
179     if (MomTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
180       MomTracing.dbgProxy.log(BasicLevel.DEBUG,
181                               "msg " + id +
182                               " body save to " + name + ".body");
183     return name+".body";
184   }
185
186   /** Load persisted message header. */
187   static public Message loadHeader(String JavaDoc name)
188     throws ClassNotFoundException JavaDoc {
189     if (MomTracing.dbgDestination.isLoggable(BasicLevel.DEBUG))
190       MomTracing.dbgDestination.log(BasicLevel.DEBUG,
191                                     "MessagePersistenceModule.loadHeader(" + name + ')');
192     if (MomTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
193       MomTracing.dbgProxy.log(BasicLevel.DEBUG,
194                               "MessagePersistenceModule.loadHeader(" + name + ')');
195
196     Transaction tx = AgentServer.getTransaction();
197     try {
198       return ((MessagePersistent) tx.load(name)).getMessage();
199     } catch (IOException JavaDoc exc) {
200       throw new TransactionError(exc.toString());
201     }
202   }
203
204   /** Load persisted message body. */
205   static public MessageBody loadBody(String JavaDoc name)
206     throws ClassNotFoundException JavaDoc {
207     if (MomTracing.dbgDestination.isLoggable(BasicLevel.DEBUG))
208       MomTracing.dbgDestination.log(BasicLevel.DEBUG,
209                                     "MessagePersistenceModule.loadBody(" + name + ')');
210     if (MomTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
211       MomTracing.dbgProxy.log(BasicLevel.DEBUG,
212                               "MessagePersistenceModule.loadBody(" + name + ')');
213
214     Transaction tx = AgentServer.getTransaction();
215     try {
216       return (MessageBody) tx.load(name+".body");
217     } catch (IOException JavaDoc exc) {
218       throw new TransactionError(exc.toString());
219     }
220   }
221
222   /**
223    * delete the message.
224    *
225    * @param message Message to delete.
226    */

227   static public void delete(MessagePersistent message) {
228     if (MomTracing.dbgDestination.isLoggable(BasicLevel.DEBUG))
229       MomTracing.dbgDestination.log(BasicLevel.DEBUG,
230                                     "MessagePersistenceModule.delete(" + message + ')');
231     if (MomTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
232       MomTracing.dbgProxy.log(BasicLevel.DEBUG,
233                               "MessagePersistenceModule.delete(" + message + ')');
234
235     String JavaDoc id = message.getIdentifier();
236     String JavaDoc name = message.getSaveName();
237     Transaction tx = AgentServer.getTransaction();
238
239     if (!tx.isPersistent()) return;
240
241     if (name != null) {
242       tx.delete(name);
243       tx.delete(name+".body");
244
245       if (MomTracing.dbgDestination.isLoggable(BasicLevel.DEBUG))
246         MomTracing.dbgDestination.log(BasicLevel.DEBUG,
247                                       "msg " + id + " delete " + name +
248                                       " and " + name + ".body");
249       if (MomTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
250         MomTracing.dbgProxy.log(BasicLevel.DEBUG,
251                                 "msg " + id + " delete " + name +
252                                 " and " + name + ".body");
253     }
254   }
255
256   /** Loads all persisted messages. */
257   static public Vector JavaDoc loadAll(String JavaDoc agentId) {
258     if (MomTracing.dbgDestination.isLoggable(BasicLevel.DEBUG))
259       MomTracing.dbgDestination.log(BasicLevel.DEBUG,
260                                     "MessagePersistenceModule.loadAll(" + agentId + ')');
261     if (MomTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
262       MomTracing.dbgProxy.log(BasicLevel.DEBUG,
263                               "MessagePersistenceModule.loadAll(" + agentId + ')');
264
265     Vector JavaDoc messages = new Vector JavaDoc();
266
267     // Retrieving the names of the persistence message previously saved.
268
Transaction tx = AgentServer.getTransaction();
269     String JavaDoc[] messageNames = tx.getList("msg" + agentId);
270
271     // Retrieving the messages individually persisted.
272
for (int i = 0; i < messageNames.length; i++) {
273       if (messageNames[i].endsWith("body")) continue;
274       try {
275         MessagePersistent mp = (MessagePersistent) tx.load(messageNames[i]);
276         Message msg = mp.getMessage();
277         msg.setPin(mp.getPin());
278         if (msg.isPin() &&
279             ! msg.noBody) {
280           msg.setMessageBody((MessageBody) tx.load(messageNames[i]+".body"));
281         }
282
283         if (MomTracing.dbgDestination.isLoggable(BasicLevel.DEBUG))
284           MomTracing.dbgDestination.log(BasicLevel.DEBUG,
285                                         "loadAll: messageNames[" + i +
286                                         "] msg = " + msg);
287         if (MomTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
288           MomTracing.dbgProxy.log(BasicLevel.DEBUG,
289                                   "loadAll: messageNames[" + i +
290                                   "] msg = " + msg);
291         messages.add(msg);
292       } catch (Exception JavaDoc exc) {
293         MomTracing.dbgDestination.log(BasicLevel.ERROR,
294                                       "Message named ["
295                                       + messageNames[i]
296                                       + "] could not be loaded for queue ["
297                                       + agentId
298                                       + "]",
299                                       exc);
300         MomTracing.dbgProxy.log(BasicLevel.ERROR,
301                                 "Message named ["
302                                 + messageNames[i]
303                                 + "] could not be loaded for queue ["
304                                 + agentId
305                                 + "]",
306                                 exc);
307       }
308     }
309     return messages;
310   }
311
312   /** Deletes all persisted objects. */
313   static public void deleteAll(String JavaDoc agentId) {
314     if (MomTracing.dbgDestination.isLoggable(BasicLevel.DEBUG))
315       MomTracing.dbgDestination.log(BasicLevel.DEBUG,
316                                     "MessagePersistenceModule.deleteAll(" + agentId + ')');
317     if (MomTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
318       MomTracing.dbgProxy.log(BasicLevel.DEBUG,
319                               "MessagePersistenceModule.deleteAll(" + agentId + ')');
320
321     Transaction tx = AgentServer.getTransaction();
322
323     // Retrieving the names of the persistence message previously saved.
324
String JavaDoc[] messageNames = tx.getList("msg" + agentId);
325
326     // Deleting the message.
327
for (int i = 0; i < messageNames.length; i++) {
328       tx.delete(messageNames[i]);
329       tx.delete(messageNames[i]+".body");
330     }
331   }
332 }
333
Popular Tags