001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.components.file;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.apache.servicemix.components.util.DefaultFileMarshaler;
022: import org.apache.servicemix.components.util.FileMarshaler;
023: import org.apache.servicemix.components.util.OutBinding;
024:
025: import javax.jbi.JBIException;
026: import javax.jbi.messaging.MessageExchange;
027: import javax.jbi.messaging.NormalizedMessage;
028: import java.io.BufferedOutputStream;
029: import java.io.File;
030: import java.io.FileOutputStream;
031: import java.io.IOException;
032: import java.io.OutputStream;
033:
034: /**
035: * A component which receives a message and writes the content to a file using the
036: * <a href="http://jakarta.apache.org/commons/vfs.html">Jakarta Commons VFS</a> library
037: * for handling various file systems like files, Samba, WebDAV, FTP, SFTP and temporary files.
038: *
039: * @version $Revision: 426415 $
040: */
041: public class FileWriter extends OutBinding {
042: private static final Log log = LogFactory.getLog(FileWriter.class);
043:
044: private File directory;
045: private FileMarshaler marshaler = new DefaultFileMarshaler();
046: private String tempFilePrefix = "servicemix-";
047: private String tempFileSuffix = ".xml";
048: private boolean autoCreateDirectory = true;
049:
050: // Properties
051: //-------------------------------------------------------------------------
052: public File getDirectory() {
053: return directory;
054: }
055:
056: public void setDirectory(File directory) {
057: this .directory = directory;
058: }
059:
060: public FileMarshaler getMarshaler() {
061: return marshaler;
062: }
063:
064: public void setMarshaler(FileMarshaler marshaler) {
065: this .marshaler = marshaler;
066: }
067:
068: public String getTempFilePrefix() {
069: return tempFilePrefix;
070: }
071:
072: public void setTempFilePrefix(String tempFilePrefix) {
073: this .tempFilePrefix = tempFilePrefix;
074: }
075:
076: public String getTempFileSuffix() {
077: return tempFileSuffix;
078: }
079:
080: public void setTempFileSuffix(String tempFileSuffix) {
081: this .tempFileSuffix = tempFileSuffix;
082: }
083:
084: public boolean isAutoCreateDirectory() {
085: return autoCreateDirectory;
086: }
087:
088: public void setAutoCreateDirectory(boolean autoCreateDirectory) {
089: this .autoCreateDirectory = autoCreateDirectory;
090: }
091:
092: // Implementation methods
093: //-------------------------------------------------------------------------
094: protected void init() throws JBIException {
095: if (directory == null) {
096: throw new IllegalArgumentException(
097: "You must specify the directory property");
098: }
099: if (isAutoCreateDirectory()) {
100: directory.mkdirs();
101: }
102: if (!directory.isDirectory()) {
103: throw new IllegalArgumentException(
104: "The directory property must be a directory but was: "
105: + directory);
106: }
107:
108: super .init();
109: }
110:
111: protected void process(MessageExchange exchange,
112: NormalizedMessage message) throws Exception {
113: OutputStream out = null;
114: try {
115: String name = marshaler.getOutputName(exchange, message);
116: File newFile = null;
117: if (name == null) {
118: newFile = File.createTempFile(tempFilePrefix,
119: tempFileSuffix, directory);
120: } else {
121: newFile = new File(directory, name);
122: }
123: out = new BufferedOutputStream(
124: new FileOutputStream(newFile));
125: marshaler.writeMessage(exchange, message, out, name);
126: done(exchange);
127: } finally {
128: if (out != null) {
129: try {
130: out.close();
131: } catch (IOException e) {
132: log.error(
133: "Caught exception while closing stream on error: "
134: + e, e);
135: }
136: }
137: }
138: }
139: }
|