KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > api > jms > MantaFileMessage


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 package org.mr.api.jms;
47
48 import java.io.File JavaDoc;
49 import java.io.FileInputStream JavaDoc;
50 import java.io.FileOutputStream JavaDoc;
51 import java.io.IOException JavaDoc;
52
53 import javax.jms.BytesMessage JavaDoc;
54 import javax.jms.JMSException JavaDoc;
55
56 import org.mr.core.util.byteable.Byteable;
57 import org.mr.core.util.byteable.ByteableInputStream;
58 import org.mr.core.util.byteable.ByteableOutputStream;
59 import org.mr.core.util.byteable.ByteableRegistry;
60
61 /**
62  * this object is in the spirit if the JMS spec but is an add-on to it
63  * this Message implements BytesMessage and adds functionality like save and load
64  * of file content
65  * @author Amir Shevat
66  *
67  */

68 public class MantaFileMessage extends MantaBytesMessage implements BytesMessage JavaDoc{
69     private String JavaDoc fileName;
70     private int fileSize =0;
71     
72     public MantaFileMessage()throws JMSException JavaDoc{
73         this(null);
74     }
75     protected MantaFileMessage(MantaSession sess) throws JMSException JavaDoc {
76         super(sess);
77     }
78     
79     /**
80      * Loads a file to the message the content of the file is stored in the message
81      * @param path - the full path of the file including file name (i.e. ./my.log)
82      * @throws JMSException if file not found or IO error
83      */

84     public void load(String JavaDoc path) throws JMSException JavaDoc{
85         try {
86             File JavaDoc f = new File JavaDoc(path);
87             if(f.exists() && f.isFile()){
88                 setFileName(f.getName());
89                 FileInputStream JavaDoc in = new FileInputStream JavaDoc(f);
90                 setFileSize(in.available());
91                 byte[] content = new byte[getFileSize()];
92                 in.read(content);
93                 in.close();
94                 super.writeBytes(content);
95                 
96             }else{
97                 throw new JMSException JavaDoc("MNJMS00036 : FILE "+path+" NOT FOUND IN SYSTEM. FAILED IN METHOD load().");
98             }
99         }catch (IOException JavaDoc e1) {
100             
101             throw new JMSException JavaDoc("MNJMS00037 : FAILED ON METHOD load(). ERROR TEXT : "+e1.getMessage());
102         }
103     }
104     
105     /**
106      * This method is good for saving the file in a different name then the original
107      * saves the content of the file to a specified file name in specified path
108      * @param path the folder were the file will be saved
109      * @param fileName the name of the file
110      * @throws JMSException in case of IO problem
111      */

112     public void save(String JavaDoc path, String JavaDoc fileName) throws JMSException JavaDoc{
113         
114         try {
115             int length = getFileSize();
116             byte[] content = new byte[length];
117             super.readBytes(content);
118             File JavaDoc f = new File JavaDoc(path+"/"+fileName);
119             FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(f);
120             out.write(content);
121             out.flush();
122             out.close();
123         } catch (IOException JavaDoc e) {
124             throw new JMSException JavaDoc("MNJMS00038 : FAILED ON METHOD save(). ERROR TEXT : "+e.getMessage());
125         }
126     }
127     
128     /**
129      * saves the content of the file to a file with the original name in specified path
130      * @param path the folder were the file will be saved
131      * @throws JMSException in case of IO problem
132      */

133     public void save(String JavaDoc path) throws JMSException JavaDoc{
134         save(path, getFileName());
135     }
136     
137     
138     
139     /**
140      * returns the name of the original file
141      * @return the name of the file (without the path)
142      */

143     public String JavaDoc getFileName() {
144         return fileName;
145     }
146     
147     /**
148      * used to change the name of the file after it has been loaded or saved
149      * @param fileName
150      */

151     public void setFileName(String JavaDoc fileName) {
152         this.fileName = fileName;
153     }
154     /**
155      * returns the size of the file
156      * @return the number of bytes in the file
157      */

158     public int getFileSize() {
159         return fileSize;
160     }
161     
162     protected void setFileSize(int fileSize) {
163         this.fileSize = fileSize;
164     }
165     
166     
167     
168     /**
169      * Create this message from the network stream.
170      *
171      * @return A BytesMessage
172      */

173     
174     public Byteable createInstance(ByteableInputStream in) throws IOException JavaDoc {
175         MantaFileMessage mfm;
176         try {
177             mfm = new MantaFileMessage(null);
178             
179             createHeadersAndProperties(mfm,in);
180             
181             byte[] msg = new byte[in.readInt()];
182             in.read(msg);
183             mfm.message = msg;
184             mfm.internalOffset = in.readInt();
185             mfm.fileSize = in.readInt();
186             mfm.fileName = in.readUTF();
187             
188             
189         }
190         catch (Exception JavaDoc ee) {
191             throw new IOException JavaDoc("MNJMS00FFE : METHOD createInstance() FAILED ON MantaFileMessage. EXCEPTION TEXT : "+ee.getMessage());
192         }
193         
194         return mfm;
195     }
196     
197     
198     /**
199      * Write this message to the network stream.
200      */

201     public void toBytes(ByteableOutputStream out) throws IOException JavaDoc {
202         super.toBytes(out);
203         
204         
205         out.writeInt(fileSize);
206         out.writeUTF(fileName);
207         
208     }
209     
210     
211     /**
212      * Register this type in the MantaRay's byteable registry.
213      */

214     public void registerToByteableRegistry() {
215         ByteableRegistry.registerByteableFactory(getByteableName() , this);
216     }
217     
218     /**
219      * Return the name for this Byteable object
220      */

221     final static String JavaDoc messName = "MantaFileMsg";
222     public String JavaDoc getByteableName() {
223         
224         return messName;
225     }
226     /**
227      * overwrites the super makeCopy()
228      */

229     public MantaMessage makeCopy() throws JMSException JavaDoc{
230         MantaFileMessage copy = new MantaFileMessage(null);
231         initCopy(copy);
232         // The underlying byte message.
233
if(message == null ||message == EMPTY ){
234             if(this.byteOutputStream!= null)
235                 copy.message = this.byteOutputStream.toByteArray();
236         }else{
237             copy.message = message;
238         }
239         //the current offset inside the message
240
copy.internalOffset = internalOffset;
241         copy.fileName = fileName;
242         copy.fileSize = fileSize;
243         return copy;
244    }
245     
246     /**
247      * Register this type in the MantaRay's byteable registry.
248      */

249     public static void register() throws JMSException JavaDoc{
250         MantaFileMessage instance = new MantaFileMessage();
251         instance.registerToByteableRegistry();
252     }
253 }
254
Popular Tags