001: /*
002: * $Id: FileMessageDispatcher.java 10961 2008-02-22 19:01:02Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transport.file;
012:
013: import org.mule.DefaultMuleMessage;
014: import org.mule.api.DefaultMuleException;
015: import org.mule.api.MuleEvent;
016: import org.mule.api.MuleException;
017: import org.mule.api.MuleMessage;
018: import org.mule.api.endpoint.OutboundEndpoint;
019: import org.mule.api.transport.OutputHandler;
020: import org.mule.transport.AbstractMessageDispatcher;
021: import org.mule.transport.file.i18n.FileMessages;
022: import org.mule.util.FileUtils;
023: import org.mule.util.IOUtils;
024:
025: import java.io.File;
026: import java.io.FileOutputStream;
027: import java.io.FilenameFilter;
028: import java.io.InputStream;
029:
030: /**
031: * <code>FileMessageDispatcher</code> is used to read/write files to the filesystem
032: */
033: public class FileMessageDispatcher extends AbstractMessageDispatcher {
034: private final FileConnector connector;
035:
036: public FileMessageDispatcher(OutboundEndpoint endpoint) {
037: super (endpoint);
038: this .connector = (FileConnector) endpoint.getConnector();
039: }
040:
041: /*
042: * (non-Javadoc)
043: *
044: * @see org.mule.api.transport.UMOConnectorSession#dispatch(org.mule.api.MuleEvent)
045: */
046: protected void doDispatch(MuleEvent event) throws Exception {
047: Object data = event.transformMessage();
048: // Wrap the transformed message before passing it to the filename parser
049: MuleMessage message = new DefaultMuleMessage(data, event
050: .getMessage());
051:
052: FileOutputStream fos = (FileOutputStream) connector
053: .getOutputStream(event.getEndpoint(), message);
054: try {
055: if (event.getMessage().getStringProperty(
056: FileConnector.PROPERTY_FILENAME, null) == null) {
057: event.getMessage().setStringProperty(
058: FileConnector.PROPERTY_FILENAME,
059: message.getStringProperty(
060: FileConnector.PROPERTY_FILENAME, ""));
061: }
062:
063: if (data instanceof byte[]) {
064: fos.write((byte[]) data);
065: } else if (data instanceof String) {
066: fos
067: .write(data.toString().getBytes(
068: event.getEncoding()));
069: } else if (data instanceof OutputHandler) {
070: ((OutputHandler) data).write(event, fos);
071: } else {
072: InputStream is = (InputStream) event
073: .transformMessage(InputStream.class);
074: IOUtils.copyLarge(is, fos);
075: is.close();
076: }
077: } finally {
078: logger.debug("Closing file");
079: fos.close();
080: }
081: }
082:
083: /**
084: * There is no associated session for a file connector
085: *
086: * @throws MuleException
087: */
088: public Object getDelegateSession() throws MuleException {
089: return null;
090: }
091:
092: protected static File getNextFile(String dir, FilenameFilter filter)
093: throws MuleException {
094: File[] files;
095: File file = FileUtils.newFile(dir);
096: File result = null;
097: try {
098: if (file.exists()) {
099: if (file.isFile()) {
100: result = file;
101: } else if (file.isDirectory()) {
102: if (filter != null) {
103: files = file.listFiles(filter);
104: } else {
105: files = file.listFiles();
106: }
107: if (files.length > 0) {
108: result = files[0];
109: }
110: }
111: }
112: return result;
113: } catch (Exception e) {
114: throw new DefaultMuleException(FileMessages
115: .errorWhileListingFiles(), e);
116: }
117: }
118:
119: /*
120: * (non-Javadoc)
121: *
122: * @see org.mule.api.transport.UMOConnectorSession#send(org.mule.api.MuleEvent)
123: */
124: protected MuleMessage doSend(MuleEvent event) throws Exception {
125: doDispatch(event);
126: return event.getMessage();
127: }
128:
129: protected void doDispose() {
130: // no op
131: }
132:
133: protected void doConnect() throws Exception {
134: // no op
135: }
136:
137: protected void doDisconnect() throws Exception {
138: // no op
139: }
140:
141: }
|