KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > api > blocks > MantaOutputStream


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 02/06/2004
48  *
49  * Coridan LTD
50  */

51 package org.mr.api.blocks;
52
53 import java.io.IOException JavaDoc;
54 import java.io.OutputStream JavaDoc;
55 import java.nio.ByteBuffer JavaDoc;
56
57 import javax.jms.BytesMessage JavaDoc;
58 import javax.jms.JMSException JavaDoc;
59 import javax.jms.MessageProducer JavaDoc;
60 import javax.jms.QueueConnection JavaDoc;
61 import javax.jms.QueueConnectionFactory JavaDoc;
62 import javax.jms.Session JavaDoc;
63 import javax.jms.TopicConnection JavaDoc;
64 import javax.jms.TopicConnectionFactory JavaDoc;
65
66 import org.mr.MantaAgent;
67 import org.mr.MantaAgentConstants;
68 import org.mr.MantaException;
69 import org.mr.api.jms.MantaBytesMessage;
70 import org.mr.api.jms.MantaQueueConnectionFactory;
71 import org.mr.api.jms.MantaTopicConnectionFactory;
72 import org.mr.kernel.services.MantaService;
73
74
75
76 /**
77  * MantaOutputStream in an implementation of an OutputStream over JMS queue/topic.
78  *
79  * @author Amir Shevat
80  *
81  */

82 public class MantaOutputStream extends OutputStream JavaDoc{
83     
84     public static byte TOPIC = MantaService.SERVICE_TYPE_TOPIC;
85     public static byte QUEUE = MantaService.SERVICE_TYPE_QUEUE;
86     private MantaAgent agent;
87     private int packetBuffSize = 1024;
88     private ByteBuffer JavaDoc buff ;
89     
90     private Session JavaDoc sendSession = null;
91     private MessageProducer JavaDoc producer = null;
92     
93     private boolean connected = false;
94     
95     public MantaOutputStream(){
96         agent = MantaAgent.getInstance();
97         agent.init();
98         buff = ByteBuffer.allocate(packetBuffSize);
99     }
100     
101     public MantaOutputStream( int packetBuffSize){
102         agent = MantaAgent.getInstance();
103         agent.init();
104         this.packetBuffSize = packetBuffSize;
105         buff = ByteBuffer.allocate(packetBuffSize);
106         agent = MantaAgent.getInstance();
107     }
108     
109     public synchronized void connect(String JavaDoc destinationName, byte destinationType) throws IOException JavaDoc, MantaException{
110          
111         try{
112             if(destinationType == QUEUE){
113                 QueueConnectionFactory JavaDoc conFactory = (QueueConnectionFactory JavaDoc) new MantaQueueConnectionFactory();
114                 QueueConnection JavaDoc con = conFactory.createQueueConnection();
115                 con.start();
116                 sendSession = con.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
117                 producer =sendSession.createProducer(sendSession.createQueue(destinationName));
118             }else{
119                 TopicConnectionFactory JavaDoc conFactory = (TopicConnectionFactory JavaDoc) new MantaTopicConnectionFactory();
120                 TopicConnection JavaDoc con = conFactory.createTopicConnection();
121                 con.start();
122                 sendSession = con.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
123                 producer =sendSession.createProducer(sendSession.createTopic(destinationName));
124             }
125             
126             
127         }catch(Exception JavaDoc e){
128             throw new IOException JavaDoc(e.getLocalizedMessage());
129         }
130         connected = true;
131     }
132     
133     
134     /* (non-Javadoc)
135      * @see java.io.OutputStream#write(int)
136      */

137     public synchronized void write(int b) throws IOException JavaDoc {
138         if(!connected)
139             throw new IOException JavaDoc("stream no connected - use the connect method");
140         buff.put((byte)b);
141         if(!buff.hasRemaining()){
142             flush();
143         }
144         
145     }
146     
147     
148     /**
149      * Flushes this output stream and forces any buffered output bytes
150      * to be written out. The general contract of <code>flush</code> is
151      * that calling it is an indication that, if any bytes previously
152      * written have been buffered by the implementation of the output
153      * stream, such bytes should immediately be written to their
154      * intended destination.
155      * <p>
156      * The <code>flush</code> method of <code>OutputStream</code> does nothing.
157      *
158      * @exception IOException if an I/O error occurs.
159      */

160     public synchronized void flush() throws IOException JavaDoc {
161         if(buff.position() == 0)
162             return;
163         try {
164             // buffer not empty write to coridan message and send to the Queue
165
BytesMessage JavaDoc msg = generateMessage();
166             msg.setBooleanProperty("endMarker",false);
167             producer.send(msg, MantaAgentConstants.NON_PERSISTENT, 9, 600000) ;
168         } catch (Exception JavaDoc e) {
169             e.printStackTrace();
170             throw new IOException JavaDoc(e.getLocalizedMessage());
171         }
172         buff.position(0);
173     }
174     
175     /**
176      * Closes this output stream and releases any system resources
177      * associated with this stream. The general contract of <code>close</code>
178      * is that it closes the output stream. A closed stream cannot perform
179      * output operations and cannot be reopened.
180      * <p>
181      * The <code>close</code> method of <code>OutputStream</code> does nothing.
182      *
183      * @exception IOException if an I/O error occurs.
184      */

185     public void close() throws IOException JavaDoc {
186         if(!connected)
187             return;
188         flush();
189         // send end marker to the other side
190
try {
191             BytesMessage JavaDoc endMarker = generateMessage();
192             endMarker.setBooleanProperty("endMarker",true);
193             producer.send(endMarker, MantaAgentConstants.NON_PERSISTENT, 9, 600000) ;
194         } catch (Exception JavaDoc e) {
195             throw new IOException JavaDoc(e.getLocalizedMessage());
196         }
197     }
198     
199     
200     private synchronized BytesMessage JavaDoc generateMessage() throws JMSException JavaDoc{
201         MantaBytesMessage result =(MantaBytesMessage) sendSession.createBytesMessage();
202         result.writeBytes(buff.array(), 0 ,buff.position());
203         return result;
204     }//generateMessage
205
}
206
Popular Tags