KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > shared > messages > MessageSoftRef


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): ScalAgent Distributed Technologies
21  * Contributor(s):
22  */

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 JavaDoc;
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 /**
35  *
36  */

37 public class MessageSoftRef extends Message
38   implements Cloneable JavaDoc, Serializable {
39
40   transient SoftReference JavaDoc softRef = null;
41
42   /**
43    * Constructs a <code>MessageSoftRef</code> instance.
44    */

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 JavaDoc(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 JavaDoc exc) {
111       MessageTracing.dbgMessage.log(BasicLevel.ERROR,
112                                     "ERROR :: MessageSoftRef.loadMessageBody() : " + getSaveName());
113       return null;
114     }
115   }
116
117   public void save(String JavaDoc 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       // save Message
138
messagePersistent.setPin(false);
139       String JavaDoc saveName = MessagePersistenceModule.save(id,messagePersistent);
140       if (saveName != null) {
141         // body unpin and set body to null
142
setPin(false);
143         body = null;
144       }
145     }
146   }
147
148   public String JavaDoc toString() {
149     StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
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