KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > vfs > FileWriter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.vfs;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.commons.vfs.FileContent;
22 import org.apache.commons.vfs.FileObject;
23 import org.apache.commons.vfs.FileSystemManager;
24 import org.apache.servicemix.components.util.DefaultFileMarshaler;
25 import org.apache.servicemix.components.util.FileMarshaler;
26 import org.apache.servicemix.components.util.OutBinding;
27
28 import javax.jbi.JBIException;
29 import javax.jbi.messaging.MessageExchange;
30 import javax.jbi.messaging.MessagingException;
31 import javax.jbi.messaging.NormalizedMessage;
32 import java.io.IOException JavaDoc;
33 import java.io.OutputStream JavaDoc;
34
35 /**
36  * A component which receives a message and writes the content to a file using the
37  * <a HREF="http://jakarta.apache.org/commons/vfs.html">Jakarta Commons VFS</a> library
38  * for handling various file systems like files, Samba, WebDAV, FTP, SFTP and temporary files.
39  *
40  * @version $Revision: 426415 $
41  */

42 public class FileWriter extends OutBinding {
43     private static final Log log = LogFactory.getLog(FileWriter.class);
44
45     private FileObject directory;
46     private FileObjectEditor editor = new FileObjectEditor();
47     private FileMarshaler marshaler = new DefaultFileMarshaler();
48     private String JavaDoc uniqueFileName = "ServiceMix";
49
50     // Properties
51
//-------------------------------------------------------------------------
52
public FileObject getDirectory() {
53         return directory;
54     }
55
56     public void setDirectory(FileObject directory) {
57         this.directory = directory;
58     }
59
60     public String JavaDoc getPath() {
61         return editor.getPath();
62     }
63
64     public void setPath(String JavaDoc path) {
65         editor.setPath(path);
66     }
67
68     public FileSystemManager getFileSystemManager() {
69         return editor.getFileSystemManager();
70     }
71
72     public void setFileSystemManager(FileSystemManager fileSystemManager) {
73         editor.setFileSystemManager(fileSystemManager);
74     }
75
76     public FileMarshaler getMarshaler() {
77         return marshaler;
78     }
79
80     public void setMarshaler(FileMarshaler marshaler) {
81         this.marshaler = marshaler;
82     }
83
84     public String JavaDoc getUniqueFileName() {
85         return uniqueFileName;
86     }
87
88     /**
89      * Sets the name used to make a unique name if no file name is available on the message.
90      *
91      * @param uniqueFileName the new value of the unique name to use for generating unique names
92      */

93     public void setUniqueFileName(String JavaDoc uniqueFileName) {
94         this.uniqueFileName = uniqueFileName;
95     }
96
97
98     // Implementation methods
99
//-------------------------------------------------------------------------
100
protected void init() throws JBIException {
101         if (directory == null) {
102             directory = editor.getFileObject();
103         }
104         super.init();
105     }
106
107     protected void process(MessageExchange exchange, NormalizedMessage message) throws Exception JavaDoc {
108         OutputStream JavaDoc out = null;
109         try {
110             String JavaDoc name = marshaler.getOutputName(exchange, message);
111             if (name == null) {
112                 throw new MessagingException("No output name available. Cannot output message!");
113             }
114             directory.close(); // remove any cached informations
115
FileObject newFile = directory.resolveFile(name);
116             newFile.close(); // remove any cached informations
117
FileContent content = newFile.getContent();
118             content.close();
119             if (content != null) {
120                 out = content.getOutputStream();
121             }
122             if (out == null) {
123                 throw new MessagingException("No output stream available for output name: " + name);
124             }
125             marshaler.writeMessage(exchange, message, out, name);
126             done(exchange);
127         }
128         finally {
129             if (out != null) {
130                 try {
131                     out.close();
132                 }
133                 catch (IOException JavaDoc e) {
134                     log.error("Caught exception while closing stream on error: " + e, e);
135                 }
136             }
137         }
138     }
139 }
140
141
Popular Tags