KickJava   Java API By Example, From Geeks To Geeks.

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


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 Dec 17, 2003
48  *
49  */

50 package org.mr.core.protocol;
51
52 import java.io.IOException JavaDoc;
53 import java.nio.ByteBuffer JavaDoc;
54
55 import org.mr.core.util.byteable.ByteBufferFactory;
56 import org.mr.core.util.byteable.ByteBufferPool;
57 import org.mr.core.util.byteable.ByteBufferPoolParameters;
58 import org.mr.core.util.byteable.Byteable;
59 import org.mr.core.util.byteable.ByteableInputStream;
60 import org.mr.core.util.byteable.ByteableInputStreamPool;
61 import org.mr.core.util.byteable.ByteableOutputStream;
62 import org.mr.core.util.byteable.UnboundedByteBufferPool;
63
64 /**
65  * MessageTransformer Transforms messages and byteable object to and from byte buffers
66  * @author Amir Shevat
67  *
68  */

69 public class MessageTransformer {
70
71     public static UnboundedByteBufferPool messageHeaderBufferPool = null;
72     
73     public static UnboundedByteBufferPool messageBodyBufferPool = null;
74     
75     public MessageTransformer(){
76         // TODO fine tune this pool and make it configurable
77
ByteBufferPoolParameters params = new ByteBufferPoolParameters();
78         // most headers are less the 400b
79
params.setSmallBufferSize(400);
80         params.setMediumBufferSize(1024);
81         params.setLargeBufferSize(2048);
82         
83         params.setNumInSmallPool(200);
84         params.setNumInMediumPool(50);
85         params.setNumInLargePool(20);
86         
87         messageHeaderBufferPool = new UnboundedByteBufferPool(params);
88     
89 // TODO fine tune this pool and make it configurable
90
params = new ByteBufferPoolParameters();
91         // most headers are less the 400b
92
params.setSmallBufferSize(1500);
93         params.setMediumBufferSize(5400);
94         params.setLargeBufferSize(11000);
95         
96         params.setNumInSmallPool(1000);
97         params.setNumInMediumPool(150);
98         params.setNumInLargePool(10);
99         
100         messageBodyBufferPool = new UnboundedByteBufferPool(params);
101     
102     }
103     
104     /**
105      * transforms the message into buffer in the oppesite way of the fromBuffer
106      * @param msg the out going message
107      * @param pool the pool you want to take the buffer from or null for new allocation
108      * @return the buffer to be sent to the net
109      */

110     public static final ByteBuffer JavaDoc byteableToBuffer(Byteable byteable ,ByteBufferFactory pool) throws IOException JavaDoc {
111         ByteableOutputStream out = new ByteableOutputStream( pool);
112         out.writeByteable(byteable);
113         return out.getByteBuffer();
114     }//toBuffer
115

116     /**
117      * transforms the message into buffer in the oppesite way of the fromBuffer
118      * @param msg the out going message
119      * @param pool the pool you want to take the buffer from
120      * @return the buffer to be sent to the net
121      */

122     public static final ByteBuffer JavaDoc toBuffer(MantaBusMessage msg,ByteBufferPool pool ) throws IOException JavaDoc {
123         ByteableOutputStream out = new ByteableOutputStream( pool);
124         out.writeByteable(msg);
125         return out.getByteBuffer();
126     }//toBuffer
127

128     
129
130     /**
131      * gets a buffer from the net and returns a manta message
132      * @param buffer the buffer from the net in a ready to read from state
133      * @return the manta message
134      */

135     public final static MantaBusMessage fromBuffer(ByteBuffer JavaDoc buffer) throws IOException JavaDoc {
136         return (MantaBusMessage)byteableFormBuffer( buffer);
137     }
138     /**
139      * gets a buffer from the net and returns a byteable object
140      * @param buffer the buffer from the net in a ready to read from state
141      * @return the byteable object corisponding to this buffer
142      */

143     public final static Byteable byteableFormBuffer(ByteBuffer JavaDoc buffer)throws IOException JavaDoc {
144         ByteableInputStream in = null;
145         Byteable result = null;
146         try{
147             in = (ByteableInputStream)ByteableInputStreamPool.getInstance().acquireObject();
148             in.setUnderLine(buffer);
149             result =in.readByteable();
150         }finally{
151             if(in!= null){
152                 in.release();
153             }
154         }
155         
156         return result;
157     }
158
159     
160 }
161
162
163
164
Popular Tags