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.file;
018:
019: import java.io.*;
020: import javax.jbi.management.DeploymentException;
021: import javax.jbi.messaging.MessageExchange;
022: import javax.jbi.messaging.NormalizedMessage;
023: import javax.jbi.servicedesc.ServiceEndpoint;
024:
025: import org.apache.servicemix.common.endpoints.ProviderEndpoint;
026: import org.apache.servicemix.components.util.DefaultFileMarshaler;
027: import org.apache.servicemix.components.util.FileMarshaler;
028:
029: /**
030: * An endpoint which receives a message and writes the content to a file.
031: *
032: * @org.apache.xbean.XBean element="sender"
033: *
034: * @version $Revision: $
035: */
036: public class FileSenderEndpoint extends ProviderEndpoint implements
037: FileEndpointType {
038:
039: private File directory;
040: private FileMarshaler marshaler = new DefaultFileMarshaler();
041: private String tempFilePrefix = "servicemix-";
042: private String tempFileSuffix = ".xml";
043: private boolean autoCreateDirectory = true;
044: private boolean append = true;
045:
046: public FileSenderEndpoint() {
047: append = false;
048: }
049:
050: public FileSenderEndpoint(FileComponent component,
051: ServiceEndpoint endpoint) {
052: super (component, endpoint);
053: append = false;
054: }
055:
056: public void validate() throws DeploymentException {
057: super .validate();
058: if (directory == null) {
059: throw new DeploymentException(
060: "You must specify the directory property");
061: }
062: if (isAutoCreateDirectory()) {
063: directory.mkdirs();
064: }
065: if (!directory.isDirectory()) {
066: throw new DeploymentException(
067: "The directory property must be a directory but was: "
068: + directory);
069: }
070: }
071:
072: protected void processInOnly(MessageExchange exchange,
073: NormalizedMessage in) throws Exception {
074: OutputStream out = null;
075: File newFile = null;
076: boolean success = false;
077: try {
078: String name = marshaler.getOutputName(exchange, in);
079: if (name == null) {
080: newFile = File.createTempFile(tempFilePrefix,
081: tempFileSuffix, directory);
082: } else {
083: newFile = new File(directory, name);
084: }
085: if (!newFile.getParentFile().exists()
086: && isAutoCreateDirectory()) {
087: newFile.getParentFile().mkdirs();
088: }
089: if (logger.isDebugEnabled()) {
090: logger.debug("Writing to file: "
091: + newFile.getCanonicalPath());
092: }
093: out = new BufferedOutputStream(new FileOutputStream(
094: newFile, append));
095: marshaler.writeMessage(exchange, in, out, name);
096: success = true;
097: } finally {
098: if (out != null) {
099: try {
100: out.close();
101: } catch (IOException e) {
102: logger.error(
103: "Caught exception while closing stream on error: "
104: + e, e);
105: }
106: }
107: //cleaning up incomplete files after things went wrong
108: if (!success) {
109: logger.debug("An error occurred while writing file "
110: + newFile.getCanonicalPath()
111: + ", deleting the invalid file");
112: if (!newFile.delete()) {
113: logger.warn("Unable to delete the file "
114: + newFile.getCanonicalPath()
115: + " after an error had occurred");
116: }
117: }
118: }
119: }
120:
121: protected void processInOut(MessageExchange exchange,
122: NormalizedMessage in, NormalizedMessage out)
123: throws Exception {
124: /** TODO list the files? */
125: super .processInOut(exchange, in, out);
126: }
127:
128: // Properties
129: //-------------------------------------------------------------------------
130: public File getDirectory() {
131: return directory;
132: }
133:
134: public void setDirectory(File directory) {
135: this .directory = directory;
136: }
137:
138: public FileMarshaler getMarshaler() {
139: return marshaler;
140: }
141:
142: public void setMarshaler(FileMarshaler marshaler) {
143: this .marshaler = marshaler;
144: }
145:
146: public String getTempFilePrefix() {
147: return tempFilePrefix;
148: }
149:
150: public void setTempFilePrefix(String tempFilePrefix) {
151: this .tempFilePrefix = tempFilePrefix;
152: }
153:
154: public String getTempFileSuffix() {
155: return tempFileSuffix;
156: }
157:
158: public void setTempFileSuffix(String tempFileSuffix) {
159: this .tempFileSuffix = tempFileSuffix;
160: }
161:
162: public boolean isAutoCreateDirectory() {
163: return autoCreateDirectory;
164: }
165:
166: public void setAutoCreateDirectory(boolean autoCreateDirectory) {
167: this .autoCreateDirectory = autoCreateDirectory;
168: }
169:
170: public boolean isAppend() {
171: return append;
172: }
173:
174: public void setAppend(boolean append) {
175: this.append = append;
176: }
177:
178: }
|