1 23 package org.objectweb.joram.shared.messages; 24 25 import org.objectweb.joram.shared.excepts.*; 26 27 import java.io.*; 28 import java.util.*; 29 import java.lang.ref.SoftReference ; 30 import org.objectweb.joram.mom.util.MessagePersistenceModule; 31 import org.objectweb.joram.shared.messages.MessageTracing; 32 import org.objectweb.util.monolog.api.BasicLevel; 33 34 37 public class MessageSoftRef extends Message 38 implements Cloneable , Serializable { 39 40 transient SoftReference softRef = null; 41 42 45 public MessageSoftRef() { 46 this.type = MessageType.SIMPLE; 47 48 if (MessageTracing.dbgMessage.isLoggable(BasicLevel.DEBUG)) 49 MessageTracing.dbgMessage.log(BasicLevel.DEBUG, 50 "MessageSoftRef <init>"); 51 } 52 53 public void setPin(boolean pin) { 54 if (MessageTracing.dbgMessage.isLoggable(BasicLevel.DEBUG)) 55 MessageTracing.dbgMessage.log(BasicLevel.DEBUG, 56 "MessageSoftRef.setPin(" + pin + ')'); 57 58 super.setPin(pin); 59 if (isPin() && ! noBody) { 60 if (softRef != null) 61 body = (MessageBody) softRef.get(); 62 if (body == null) 63 body = loadMessageBody(); 64 } 65 } 66 67 protected MessageBody getMessageBody() { 68 if (MessageTracing.dbgMessage.isLoggable(BasicLevel.DEBUG)) 69 MessageTracing.dbgMessage.log(BasicLevel.DEBUG, 70 "MessageSoftRef.getMessageBody() isPin=" + isPin()); 71 72 if (isPin()) return body; 73 74 MessageBody mb = null; 75 76 if (softRef != null) 77 mb = (MessageBody) softRef.get(); 78 if (MessageTracing.dbgMessage.isLoggable(BasicLevel.DEBUG)) 79 MessageTracing.dbgMessage.log(BasicLevel.DEBUG, 80 "MessageSoftRef.getMessageBody : mb=" + mb); 81 if (mb == null) { 82 mb = loadMessageBody(); 83 setMessageBody(mb); 84 } 85 return mb; 86 } 87 88 public void setMessageBody(MessageBody msgBody) { 89 if (MessageTracing.dbgMessage.isLoggable(BasicLevel.DEBUG)) 90 MessageTracing.dbgMessage.log(BasicLevel.DEBUG, 91 "MessageSoftRef.setMessageBody(" + 92 msgBody + ')'); 93 94 softRef = new SoftReference (msgBody); 95 if (isPin()) 96 body = (MessageBody) softRef.get(); 97 else 98 body = null; 99 } 100 101 private MessageBody loadMessageBody() { 102 if (MessageTracing.dbgMessage.isLoggable(BasicLevel.DEBUG)) 103 MessageTracing.dbgMessage.log(BasicLevel.DEBUG, 104 "MessageSoftRef.loadMessageBody() : saveName=" + getSaveName()); 105 if (noBody || getSaveName() == null) 106 return null; 107 108 try { 109 return MessagePersistenceModule.loadBody(getSaveName()); 110 } catch (ClassNotFoundException exc) { 111 MessageTracing.dbgMessage.log(BasicLevel.ERROR, 112 "ERROR :: MessageSoftRef.loadMessageBody() : " + getSaveName()); 113 return null; 114 } 115 } 116 117 public void save(String id) { 118 if (MessageTracing.dbgMessage.isLoggable(BasicLevel.DEBUG)) 119 MessageTracing.dbgMessage.log(BasicLevel.DEBUG, 120 "MessageSoftRef.save(" + id + ')'); 121 122 if (! getPersistent()) 123 return; 124 125 if (messagePersistent == null) 126 messagePersistent = new MessagePersistent(this); 127 128 setSaveName(MessagePersistenceModule.getSaveName(id,messagePersistent)); 129 130 if (MessageTracing.dbgMessage.isLoggable(BasicLevel.DEBUG)) 131 MessageTracing.dbgMessage.log(BasicLevel.DEBUG, 132 "MessageSoftRef.save : isPin()=" + isPin() + 133 ", bodyRO=" + bodyRO + ", saveName=" + getSaveName()); 134 if (!isPin() && bodyRO) { 135 MessagePersistenceModule.saveHeader(id,messagePersistent); 136 } else { 137 messagePersistent.setPin(false); 139 String saveName = MessagePersistenceModule.save(id,messagePersistent); 140 if (saveName != null) { 141 setPin(false); 143 body = null; 144 } 145 } 146 } 147 148 public String toString() { 149 StringBuffer buff = new StringBuffer (); 150 buff.append('('); 151 buff.append(super.toString()); 152 buff.append(",softRef="); 153 buff.append(softRef); 154 buff.append(')'); 155 return buff.toString(); 156 } 157 } 158 | Popular Tags |