01: /*
02: * $Id: FileContentsMessageAdapter.java 10489 2008-01-23 17:53:38Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.transport.file;
12:
13: import org.mule.api.MessagingException;
14: import org.mule.api.MuleRuntimeException;
15: import org.mule.api.ThreadSafeAccess;
16: import org.mule.config.i18n.CoreMessages;
17: import org.mule.util.IOUtils;
18:
19: /**
20: * <code>FileContentsMessageAdapter</code> provides a wrapper for file data. Users
21: * can obtain the contents of the message through the payload property and can get
22: * the filename and directory in the properties using PROPERTY_FILENAME and
23: * PROPERTY_DIRECTORY.
24: */
25: public class FileContentsMessageAdapter extends FileMessageAdapter {
26: /**
27: * Serial version
28: */
29: private static final long serialVersionUID = 7368719494535568721L;
30:
31: private byte[] contents = null;
32:
33: public FileContentsMessageAdapter(Object message)
34: throws MessagingException {
35: super (message);
36: this .getPayload();
37: }
38:
39: public FileContentsMessageAdapter(
40: FileContentsMessageAdapter template) {
41: super (template);
42: contents = template.contents;
43: this .getPayload();
44: }
45:
46: public Object getPayload() {
47: if (contents == null) {
48: synchronized (this ) {
49: try {
50: contents = IOUtils.toByteArray(payload);
51: payload.close();
52: } catch (Exception noPayloadException) {
53: throw new MuleRuntimeException(CoreMessages
54: .failedToReadPayload(), noPayloadException);
55: }
56: }
57: }
58: return contents;
59: }
60:
61: public ThreadSafeAccess newThreadCopy() {
62: return new FileContentsMessageAdapter(this);
63: }
64: }
|