01: /*
02: * $Id: FileMessageAdapter.java 10935 2008-02-22 03:48:54Z 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.ThreadSafeAccess;
15: import org.mule.api.transport.MessageTypeNotSupportedException;
16: import org.mule.transport.AbstractMessageAdapter;
17: import org.mule.transport.file.i18n.FileMessages;
18: import org.mule.util.ObjectUtils;
19:
20: import java.io.File;
21: import java.io.FileInputStream;
22: import java.io.IOException;
23: import java.io.InputStream;
24:
25: /**
26: * <code>FileMessageAdapter</code> provides a wrapper for a file reference. Users
27: * can obtain the contents of the message through the payload property and can get
28: * the filename and directory in the properties using FileConnector.PROPERTY_FILENAME and
29: * FileConnector.PROPERTY_DIRECTORY.
30: */
31: public class FileMessageAdapter extends AbstractMessageAdapter {
32: /** Serial version */
33: private static final long serialVersionUID = 4127485947547548996L;
34:
35: protected File file = null;
36: protected InputStream payload;
37:
38: public FileMessageAdapter(Object message) throws MessagingException {
39: super ();
40:
41: if (message instanceof File) {
42: this .setFileMessage((File) message);
43: } else if (message instanceof ReceiverFileInputStream) {
44: this .setStreamMessage((ReceiverFileInputStream) message);
45: } else {
46: throw new MessageTypeNotSupportedException(message, this
47: .getClass());
48: }
49: }
50:
51: protected FileMessageAdapter(FileMessageAdapter template) {
52: super (template);
53: file = template.file;
54: payload = template.payload;
55: }
56:
57: public Object getPayload() {
58: return payload;
59: }
60:
61: protected void setFileMessage(File message)
62: throws MessagingException {
63: try {
64: this .file = message;
65: this .payload = new FileInputStream(message);
66: } catch (IOException ex) {
67: throw new MessagingException(FileMessages
68: .fileDoesNotExist(ObjectUtils.toString(message,
69: "null")), ex);
70: }
71: setProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, this .file
72: .getName());
73: setProperty(FileConnector.PROPERTY_DIRECTORY, this .file
74: .getParent());
75: }
76:
77: protected void setStreamMessage(ReceiverFileInputStream message)
78: throws MessagingException {
79: this .file = message.getCurrentFile();
80: this .payload = message;
81: setProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, this .file
82: .getName());
83: setProperty(FileConnector.PROPERTY_DIRECTORY, this .file
84: .getParent());
85: }
86:
87: public String getUniqueId() {
88: return file.getAbsolutePath();
89: }
90:
91: public ThreadSafeAccess newThreadCopy() {
92: return new FileMessageAdapter(this);
93: }
94:
95: }
|