KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > repository > CopyArtifactTypeHandler


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.geronimo.system.repository;
18
19 import java.io.InputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.BufferedOutputStream JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.BufferedInputStream JavaDoc;
25
26 import org.apache.geronimo.kernel.repository.ArtifactTypeHandler;
27 import org.apache.geronimo.kernel.repository.Artifact;
28 import org.apache.geronimo.kernel.repository.FileWriteMonitor;
29
30 /**
31  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
32  */

33 public class CopyArtifactTypeHandler implements ArtifactTypeHandler {
34     private final static int TRANSFER_NOTIFICATION_SIZE = 10240; // announce every this many bytes
35
private final static int TRANSFER_BUF_SIZE = 10240; // try this many bytes at a time
36

37     public void install(InputStream JavaDoc source, int size, Artifact artifact, FileWriteMonitor monitor, File JavaDoc target) throws IOException JavaDoc {
38         // assure that the target directory exists
39
File JavaDoc parent = target.getParentFile();
40         if (!parent.exists() && !parent.mkdirs()) {
41             throw new RuntimeException JavaDoc("Unable to create directory " + parent.getAbsolutePath());
42         }
43
44         // copy it
45
if (monitor != null) {
46             monitor.writeStarted(artifact.toString(), size);
47         }
48         int total = 0;
49         BufferedOutputStream JavaDoc out = null;
50         BufferedInputStream JavaDoc in = null;
51         try {
52             int threshold = TRANSFER_NOTIFICATION_SIZE;
53             out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(target));
54             in = new BufferedInputStream JavaDoc(source);
55             byte[] buf = new byte[TRANSFER_BUF_SIZE];
56             int count;
57             while ((count = in.read(buf)) > -1) {
58                 out.write(buf, 0, count);
59                 if (monitor != null) {
60                     total += count;
61                     if (total > threshold) {
62                         threshold += TRANSFER_NOTIFICATION_SIZE;
63                         monitor.writeProgress(total);
64                     }
65                 }
66             }
67             out.close(); // also flushes the stream
68
out = null;
69             in.close();
70             in = null;
71         } finally {
72             if (out != null) {
73                 try {
74                     out.close();
75                 } catch (IOException JavaDoc ignored) {
76                 }
77             }
78             if (in != null) {
79                 try {
80                     in.close();
81                 } catch (IOException JavaDoc ignored) {
82                 }
83             }
84             if (monitor != null) {
85                 monitor.writeComplete(total);
86             }
87         }
88     }
89 }
90
Popular Tags