KickJava   Java API By Example, From Geeks To Geeks.

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

41 public class FileWriter extends OutBinding {
42     private static final Log log = LogFactory.getLog(FileWriter.class);
43
44     private File JavaDoc directory;
45     private FileMarshaler marshaler = new DefaultFileMarshaler();
46     private String JavaDoc tempFilePrefix = "servicemix-";
47     private String JavaDoc tempFileSuffix = ".xml";
48     private boolean autoCreateDirectory = true;
49
50     // Properties
51
//-------------------------------------------------------------------------
52
public File JavaDoc getDirectory() {
53         return directory;
54     }
55
56     public void setDirectory(File JavaDoc directory) {
57         this.directory = directory;
58     }
59
60     public FileMarshaler getMarshaler() {
61         return marshaler;
62     }
63
64     public void setMarshaler(FileMarshaler marshaler) {
65         this.marshaler = marshaler;
66     }
67
68     public String JavaDoc getTempFilePrefix() {
69         return tempFilePrefix;
70     }
71
72     public void setTempFilePrefix(String JavaDoc tempFilePrefix) {
73         this.tempFilePrefix = tempFilePrefix;
74     }
75
76     public String JavaDoc getTempFileSuffix() {
77         return tempFileSuffix;
78     }
79
80     public void setTempFileSuffix(String JavaDoc tempFileSuffix) {
81         this.tempFileSuffix = tempFileSuffix;
82     }
83
84     public boolean isAutoCreateDirectory() {
85         return autoCreateDirectory;
86     }
87
88     public void setAutoCreateDirectory(boolean autoCreateDirectory) {
89         this.autoCreateDirectory = autoCreateDirectory;
90     }
91
92     // Implementation methods
93
//-------------------------------------------------------------------------
94
protected void init() throws JBIException {
95         if (directory == null) {
96             throw new IllegalArgumentException JavaDoc("You must specify the directory property");
97         }
98         if (isAutoCreateDirectory()) {
99             directory.mkdirs();
100         }
101         if (!directory.isDirectory()) {
102             throw new IllegalArgumentException JavaDoc("The directory property must be a directory but was: " + directory);
103         }
104
105         super.init();
106     }
107
108     protected void process(MessageExchange exchange, NormalizedMessage message) throws Exception JavaDoc {
109         OutputStream JavaDoc out = null;
110         try {
111             String JavaDoc name = marshaler.getOutputName(exchange, message);
112             File JavaDoc newFile = null;
113             if (name == null) {
114                 newFile = File.createTempFile(tempFilePrefix, tempFileSuffix, directory);
115             }
116             else {
117                 newFile = new File JavaDoc(directory, name);
118             }
119             out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(newFile));
120             marshaler.writeMessage(exchange, message, out, name);
121             done(exchange);
122         }
123         finally {
124             if (out != null) {
125                 try {
126                     out.close();
127                 }
128                 catch (IOException JavaDoc e) {
129                     log.error("Caught exception while closing stream on error: " + e, e);
130                 }
131             }
132         }
133     }
134 }
135
136
Popular Tags