KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > file > FileMessageAdapter


1 /*
2  * $Id: FileMessageAdapter.java 3982 2006-11-22 14:28:01Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.file;
12
13 import java.io.File JavaDoc;
14
15 import org.apache.commons.lang.ObjectUtils;
16 import org.mule.MuleException;
17 import org.mule.config.i18n.Message;
18 import org.mule.config.i18n.Messages;
19 import org.mule.providers.AbstractMessageAdapter;
20 import org.mule.providers.file.transformers.FileToByteArray;
21 import org.mule.umo.MessagingException;
22 import org.mule.umo.provider.MessageTypeNotSupportedException;
23
24 /**
25  * <code>FileMessageAdapter</code> provides a wrapper for a file reference. Users
26  * can obtain the contents of the message through the payload property and can get
27  * the filename and directory in the properties using PROPERTY_FILENAME and
28  * PROPERTY_DIRECTORY.
29  */

30 public class FileMessageAdapter extends AbstractMessageAdapter
31 {
32     /**
33      * Serial version
34      */

35     private static final long serialVersionUID = 4127485947547548996L;
36
37     private static final FileToByteArray transformer = new FileToByteArray();
38
39     private File JavaDoc file = null;
40     private byte[] contents = null;
41
42     public FileMessageAdapter(Object JavaDoc message) throws MessagingException
43     {
44         super();
45
46         if (message instanceof File JavaDoc)
47         {
48             this.setMessage((File JavaDoc)message);
49         }
50         else
51         {
52             throw new MessageTypeNotSupportedException(message, this.getClass());
53         }
54     }
55
56     /*
57      * (non-Javadoc)
58      *
59      * @see org.mule.providers.UMOMessageAdapter#getPayload()
60      */

61     public Object JavaDoc getPayload()
62     {
63         return file;
64     }
65
66     /*
67      * (non-Javadoc)
68      *
69      * @see org.mule.providers.UMOMessageAdapter#getPayloadAsBytes()
70      */

71     public byte[] getPayloadAsBytes() throws Exception JavaDoc
72     {
73         synchronized (this)
74         {
75             if (contents == null)
76             {
77                 try
78                 {
79                     // TODO unfortunately reading the file here is required,
80
// since otherwise the FileMessageReceiver might delete the
81
// file
82
this.contents = (byte[])transformer.transform(file);
83                 }
84                 catch (Exception JavaDoc noPayloadException)
85                 {
86                     throw new MuleException(new Message(Messages.FAILED_TO_READ_PAYLOAD), noPayloadException);
87                 }
88             }
89             return contents;
90         }
91     }
92
93     /**
94      * Converts the message implementation into a String representation
95      *
96      * @param encoding The encoding to use when transforming the message (if
97      * necessary). The parameter is used when converting from a byte array
98      * @return String representation of the message payload
99      * @throws Exception Implementation may throw an endpoint specific exception
100      */

101     public String JavaDoc getPayloadAsString(String JavaDoc encoding) throws Exception JavaDoc
102     {
103         synchronized (this)
104         {
105             return new String JavaDoc(this.getPayloadAsBytes(), encoding);
106         }
107     }
108
109     /*
110      * (non-Javadoc)
111      *
112      * @see org.mule.providers.UMOMessageAdapter#setMessage(java.lang.Object)
113      */

114     protected void setMessage(File JavaDoc message) throws MessagingException
115     {
116         boolean fileIsValid;
117         Exception JavaDoc fileInvalidException;
118
119         try
120         {
121             fileIsValid = (message != null && message.isFile());
122             fileInvalidException = null;
123         }
124         catch (Exception JavaDoc ex)
125         {
126             // save any file access exceptions
127
fileInvalidException = ex;
128             fileIsValid = false;
129         }
130
131         if (!fileIsValid)
132         {
133             Object JavaDoc exceptionArg;
134
135             if (fileInvalidException != null)
136             {
137                 exceptionArg = fileInvalidException;
138             }
139             else
140             {
141                 exceptionArg = ObjectUtils.toString(message, "null");
142             }
143
144             Message msg = new Message(Messages.FILE_X_DOES_NOT_EXIST, ObjectUtils.toString(message, "null"));
145
146             throw new MessagingException(msg, exceptionArg);
147         }
148
149         this.file = message;
150         this.contents = null;
151         this.setProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, this.file.getName());
152         this.setProperty(FileConnector.PROPERTY_DIRECTORY, this.file.getParent());
153     }
154
155     public String JavaDoc getUniqueId()
156     {
157         return file.getAbsolutePath();
158     }
159
160 }
161
Popular Tags