KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > protocol > PayloadContainer


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 /*
47  * Created on 07/06/2004
48  *
49  * Manta LTD
50  */

51 package org.mr.core.protocol;
52
53
54
55 import java.io.IOException JavaDoc;
56 import java.nio.ByteBuffer JavaDoc;
57
58 import org.mr.core.util.byteable.ByteBufferFactory;
59 import org.mr.core.util.byteable.Byteable;
60 import org.mr.core.util.byteable.IncomingByteBufferPool;
61
62
63 /**
64  * PayloadContainer holds the payload of the manta bus message
65  * when needed (lazy) it will serialize the payload and dump the object
66  * and vise versa
67  *
68  * @author Amir Shevat
69  *
70  */

71 public class PayloadContainer {
72     
73     
74     // one of them should be null and the other not null
75
private Byteable payloadObject = null;
76     //private ByteBuffer serializedPayload = null;
77
private ByteBuffer JavaDoc payloadBuffer = null;
78     private ByteBuffer JavaDoc toReleaseBuffer = null;
79
80     public PayloadContainer(Byteable payload){
81         payloadObject = payload;
82     }
83
84     protected PayloadContainer(ByteBuffer JavaDoc buffer){
85         this.payloadBuffer = buffer.slice();
86         this.toReleaseBuffer = buffer;
87     }
88     
89     /**
90      * @return Returns the payloadObject.
91      */

92     public final synchronized Byteable getPayloadObject() throws IOException JavaDoc {
93         //if(payloadObject == null && serializedPayload!= null){
94
// payloadObject= MessageTransformer.byteableFormBuffer(serializedPayload);
95
// serializedPayload = null;
96
//}
97
if (this.payloadObject == null && this.payloadBuffer != null) {
98             this.payloadObject =
99                 MessageTransformer.byteableFormBuffer(this.payloadBuffer);
100             IncomingByteBufferPool.getInstance().release(this.toReleaseBuffer);
101         }
102         return payloadObject;
103     }
104     
105     /**
106      * @return Returns the serializedPayload.
107      */

108     public final synchronized ByteBuffer JavaDoc getSerializedPayload() throws IOException JavaDoc {
109         //if(serializedPayload == null ){
110
// serializedPayload = MessageTransformer.byteableToBuffer(payloadObject ,MessageTransformer.messageBodyBufferPool );
111
// payloadObject = null;
112
//}
113
//return serializedPayload.duplicate();
114
if (payloadObject == null && payloadBuffer != null) {
115             return payloadBuffer;
116         } else {
117             if(toReleaseBuffer ==null){
118                 if(payloadObject != null){
119                     synchronized(payloadObject){
120                         this.toReleaseBuffer = MessageTransformer.byteableToBuffer(payloadObject, MessageTransformer.messageBodyBufferPool);
121                     }
122                 }else{
123                     this.toReleaseBuffer = MessageTransformer.byteableToBuffer(payloadObject, MessageTransformer.messageBodyBufferPool);
124                 }
125             }
126             
127             
128             return toReleaseBuffer;
129 // return MessageTransformer.byteableToBuffer(payloadObject, MessageTransformer.messageBodyBufferPool);
130
}
131     }
132
133     /**
134      * Release the payload buffer. If payloadObject isn't null, then
135      * it was serialized into toReleaseBuffer (see
136      * PayloadContainer.getSerializedPayload()), and should be
137      * released by MessageTransformer.messageBodyBufferPool.
138      * Otherwise the toReleaseBuffer must have come from the network
139      * layer, and not yet parsed into a Byteable object. In this case
140      * the pool used by the network layer is used to release the
141      * buffer (IncomingByteBufferPool).
142      */

143     private int releaseCount = 0;
144     public synchronized final void release() {
145         releaseCount++;
146         ByteBufferFactory pool = null;
147
148         if (this.payloadObject != null) {
149             pool = MessageTransformer.messageBodyBufferPool;
150         } else {
151             pool = IncomingByteBufferPool.getInstance();
152         }
153
154         if (this.toReleaseBuffer != null) {
155             pool.release(this.toReleaseBuffer);
156             // prevent 2 releases of same buffer
157
toReleaseBuffer = null;
158         }
159         
160     }
161
162     public String JavaDoc toString(){
163         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
164         buff.append("{payload object = ");
165         buff.append(payloadObject);
166         //buff.append(" buffer = ");
167
//buff.append(serializedPayload);
168
buff.append("}");
169         return buff.toString();
170     }
171
172     
173
174
175 }
176
Popular Tags