1 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 ; 36 import java.io.IOException ; 37 38 39 43 public class MessagePersistenceModule { 44 45 static public String getSaveName(String agentId, 46 MessagePersistent message) { 47 String id = message.getIdentifier(); 48 return "msg" + agentId + id.substring(3); 49 } 50 51 57 static public String save(String 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 id = message.getIdentifier(); 69 String name = "msg" + agentId + id.substring(3); 70 Transaction tx = AgentServer.getTransaction(); 71 72 if (!tx.isPersistent()) 73 return null; 74 75 try { 76 tx.save(message,name); 78 79 MessageBody body = message.getMessageBody(); 81 if (! body.saved) { 82 body.saved = true; 83 tx.save(body,name+".body"); 84 } 85 } catch (Exception 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 106 static public String saveHeader(String 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 id = message.getIdentifier(); 118 String name = "msg" + agentId + id.substring(3); 119 Transaction tx = AgentServer.getTransaction(); 120 121 if (!tx.isPersistent()) 122 return null; 123 124 try { 125 tx.save(message,name); 127 } catch (Exception 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 146 static public String saveBody(String 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 id = message.getIdentifier(); 158 String name = "msg" + agentId + id.substring(3); 159 Transaction tx = AgentServer.getTransaction(); 160 161 if (!tx.isPersistent()) 162 return null; 163 164 try { 165 MessageBody body = message.getMessageBody(); 167 if (! body.saved) { 168 body.saved = true; 169 tx.save(body,name+".body"); 170 } 171 } catch (Exception 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 187 static public Message loadHeader(String name) 188 throws ClassNotFoundException { 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 exc) { 200 throw new TransactionError(exc.toString()); 201 } 202 } 203 204 205 static public MessageBody loadBody(String name) 206 throws ClassNotFoundException { 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 exc) { 218 throw new TransactionError(exc.toString()); 219 } 220 } 221 222 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 id = message.getIdentifier(); 236 String 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 257 static public Vector loadAll(String 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 messages = new Vector (); 266 267 Transaction tx = AgentServer.getTransaction(); 269 String [] messageNames = tx.getList("msg" + agentId); 270 271 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 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 313 static public void deleteAll(String 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 String [] messageNames = tx.getList("msg" + agentId); 325 326 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 |