1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.component.file; |
18 |
|
|
19 |
|
import org.apache.camel.Exchange; |
20 |
|
import org.apache.camel.Producer; |
21 |
|
import org.apache.camel.impl.DefaultProducer; |
22 |
|
import org.apache.camel.util.ExchangeHelper; |
23 |
|
import org.apache.commons.logging.Log; |
24 |
|
import org.apache.commons.logging.LogFactory; |
25 |
|
|
26 |
|
import java.io.File; |
27 |
|
import java.io.FileOutputStream; |
28 |
|
import java.io.IOException; |
29 |
|
import java.io.InputStream; |
30 |
|
import java.io.RandomAccessFile; |
31 |
|
import java.nio.ByteBuffer; |
32 |
|
import java.nio.channels.FileChannel; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
0 |
public class FileProducer extends DefaultProducer { |
40 |
3 |
private static final transient Log LOG = LogFactory.getLog(FileProducer.class); |
41 |
|
private final FileEndpoint endpoint; |
42 |
|
|
43 |
|
public FileProducer(FileEndpoint endpoint) { |
44 |
15 |
super(endpoint); |
45 |
15 |
this.endpoint = endpoint; |
46 |
15 |
} |
47 |
|
|
48 |
|
public FileEndpoint getEndpoint() { |
49 |
36 |
return (FileEndpoint) super.getEndpoint(); |
50 |
|
} |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
public void process(Exchange exchange) throws Exception { |
57 |
18 |
process(endpoint.toExchangeType(exchange)); |
58 |
18 |
} |
59 |
|
|
60 |
|
public void process(FileExchange exchange) throws Exception { |
61 |
18 |
InputStream in = ExchangeHelper.getMandatoryInBody(exchange, InputStream.class); |
62 |
18 |
File file = createFileName(exchange); |
63 |
18 |
buildDirectory(file); |
64 |
18 |
if (LOG.isDebugEnabled()) { |
65 |
0 |
LOG.debug("About to write to: " + file + " from exchange: " + exchange); |
66 |
|
} |
67 |
18 |
FileChannel fc = null; |
68 |
|
try { |
69 |
18 |
if (getEndpoint().isAppend()) { |
70 |
18 |
fc = new RandomAccessFile(file, "rw").getChannel(); |
71 |
18 |
fc.position(fc.size()); |
72 |
18 |
} |
73 |
|
else { |
74 |
0 |
fc = new FileOutputStream(file).getChannel(); |
75 |
|
} |
76 |
18 |
int size = getEndpoint().getBufferSize(); |
77 |
18 |
byte[] buffer = new byte[size]; |
78 |
18 |
ByteBuffer byteBuffer = ByteBuffer.wrap(buffer); |
79 |
|
while (true) { |
80 |
18 |
int count = in.read(buffer); |
81 |
18 |
if (count <= 0) { |
82 |
0 |
break; |
83 |
|
} |
84 |
18 |
else if (count < size) { |
85 |
18 |
byteBuffer = ByteBuffer.wrap(buffer, 0, count); |
86 |
18 |
fc.write(byteBuffer); |
87 |
18 |
break; |
88 |
|
} |
89 |
|
else { |
90 |
0 |
fc.write(byteBuffer); |
91 |
|
} |
92 |
0 |
} |
93 |
|
} |
94 |
|
finally { |
95 |
18 |
if (in != null) { |
96 |
|
try { |
97 |
18 |
in.close(); |
98 |
|
} |
99 |
0 |
catch (IOException e) { |
100 |
0 |
LOG.warn("Failed to close input: " + e, e); |
101 |
18 |
} |
102 |
|
} |
103 |
18 |
if (fc != null) { |
104 |
|
try { |
105 |
18 |
fc.close(); |
106 |
|
} |
107 |
0 |
catch (IOException e) { |
108 |
0 |
LOG.warn("Failed to close output: " + e, e); |
109 |
18 |
} |
110 |
0 |
} |
111 |
0 |
} |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
18 |
} |
145 |
|
|
146 |
|
protected File createFileName(FileExchange exchange) { |
147 |
18 |
String fileName = exchange.getIn().getMessageId(); |
148 |
|
|
149 |
18 |
File endpointFile = endpoint.getFile(); |
150 |
18 |
String name = exchange.getIn().getHeader(FileComponent.HEADER_FILE_NAME, String.class); |
151 |
18 |
if (name != null) { |
152 |
0 |
File answer = new File(endpointFile, name); |
153 |
0 |
if (answer.isDirectory()) { |
154 |
0 |
return new File(answer, fileName); |
155 |
|
} |
156 |
|
else { |
157 |
0 |
return answer; |
158 |
|
} |
159 |
|
} |
160 |
18 |
if (endpointFile != null && endpointFile.isDirectory()) { |
161 |
18 |
return new File(endpointFile, fileName); |
162 |
|
} |
163 |
|
else { |
164 |
0 |
return new File(fileName); |
165 |
|
} |
166 |
|
} |
167 |
|
|
168 |
|
private void buildDirectory(File file) { |
169 |
18 |
String dirName = file.getAbsolutePath(); |
170 |
18 |
int index = dirName.lastIndexOf(File.separatorChar); |
171 |
18 |
if (index > 0) { |
172 |
18 |
dirName = dirName.substring(0, index); |
173 |
18 |
File dir = new File(dirName); |
174 |
18 |
dir.mkdirs(); |
175 |
|
} |
176 |
18 |
} |
177 |
|
} |