1 46 package org.mr.api.jms; 47 48 import java.io.File ; 49 import java.io.FileInputStream ; 50 import java.io.FileOutputStream ; 51 import java.io.IOException ; 52 53 import javax.jms.BytesMessage ; 54 import javax.jms.JMSException ; 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 68 public class MantaFileMessage extends MantaBytesMessage implements BytesMessage { 69 private String fileName; 70 private int fileSize =0; 71 72 public MantaFileMessage()throws JMSException { 73 this(null); 74 } 75 protected MantaFileMessage(MantaSession sess) throws JMSException { 76 super(sess); 77 } 78 79 84 public void load(String path) throws JMSException { 85 try { 86 File f = new File (path); 87 if(f.exists() && f.isFile()){ 88 setFileName(f.getName()); 89 FileInputStream in = new FileInputStream (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 ("MNJMS00036 : FILE "+path+" NOT FOUND IN SYSTEM. FAILED IN METHOD load()."); 98 } 99 }catch (IOException e1) { 100 101 throw new JMSException ("MNJMS00037 : FAILED ON METHOD load(). ERROR TEXT : "+e1.getMessage()); 102 } 103 } 104 105 112 public void save(String path, String fileName) throws JMSException { 113 114 try { 115 int length = getFileSize(); 116 byte[] content = new byte[length]; 117 super.readBytes(content); 118 File f = new File (path+"/"+fileName); 119 FileOutputStream out = new FileOutputStream (f); 120 out.write(content); 121 out.flush(); 122 out.close(); 123 } catch (IOException e) { 124 throw new JMSException ("MNJMS00038 : FAILED ON METHOD save(). ERROR TEXT : "+e.getMessage()); 125 } 126 } 127 128 133 public void save(String path) throws JMSException { 134 save(path, getFileName()); 135 } 136 137 138 139 143 public String getFileName() { 144 return fileName; 145 } 146 147 151 public void setFileName(String fileName) { 152 this.fileName = fileName; 153 } 154 158 public int getFileSize() { 159 return fileSize; 160 } 161 162 protected void setFileSize(int fileSize) { 163 this.fileSize = fileSize; 164 } 165 166 167 168 173 174 public Byteable createInstance(ByteableInputStream in) throws IOException { 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 ee) { 191 throw new IOException ("MNJMS00FFE : METHOD createInstance() FAILED ON MantaFileMessage. EXCEPTION TEXT : "+ee.getMessage()); 192 } 193 194 return mfm; 195 } 196 197 198 201 public void toBytes(ByteableOutputStream out) throws IOException { 202 super.toBytes(out); 203 204 205 out.writeInt(fileSize); 206 out.writeUTF(fileName); 207 208 } 209 210 211 214 public void registerToByteableRegistry() { 215 ByteableRegistry.registerByteableFactory(getByteableName() , this); 216 } 217 218 221 final static String messName = "MantaFileMsg"; 222 public String getByteableName() { 223 224 return messName; 225 } 226 229 public MantaMessage makeCopy() throws JMSException { 230 MantaFileMessage copy = new MantaFileMessage(null); 231 initCopy(copy); 232 if(message == null ||message == EMPTY ){ 234 if(this.byteOutputStream!= null) 235 copy.message = this.byteOutputStream.toByteArray(); 236 }else{ 237 copy.message = message; 238 } 239 copy.internalOffset = internalOffset; 241 copy.fileName = fileName; 242 copy.fileSize = fileSize; 243 return copy; 244 } 245 246 249 public static void register() throws JMSException { 250 MantaFileMessage instance = new MantaFileMessage(); 251 instance.registerToByteableRegistry(); 252 } 253 } 254 | Popular Tags |