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.geronimo.kernel.repository;
018    
019    import java.io.InputStream;
020    import java.io.File;
021    import java.io.IOException;
022    import java.io.BufferedOutputStream;
023    import java.io.FileOutputStream;
024    import java.io.BufferedInputStream;
025    
026    import org.apache.geronimo.kernel.repository.ArtifactTypeHandler;
027    import org.apache.geronimo.kernel.repository.Artifact;
028    import org.apache.geronimo.kernel.repository.FileWriteMonitor;
029    
030    /**
031     * @version $Rev: 476049 $ $Date: 2006-11-17 15:35:17 +1100 (Fri, 17 Nov 2006) $
032     */
033    public class CopyArtifactTypeHandler implements ArtifactTypeHandler {
034        private final static int TRANSFER_NOTIFICATION_SIZE = 10240;  // announce every this many bytes
035        private final static int TRANSFER_BUF_SIZE = 10240;  // try this many bytes at a time
036    
037        public void install(InputStream source, int size, Artifact artifact, FileWriteMonitor monitor, File target) throws IOException {
038            // assure that the target directory exists
039            File parent = target.getParentFile();
040            if (!parent.exists() && !parent.mkdirs()) {
041                throw new RuntimeException("Unable to create directory " + parent.getAbsolutePath());
042            }
043    
044            // copy it
045            if (monitor != null) {
046                monitor.writeStarted(artifact.toString(), size);
047            }
048            int total = 0;
049            BufferedOutputStream out = null;
050            BufferedInputStream in = null;
051            try {
052                int threshold = TRANSFER_NOTIFICATION_SIZE;
053                out = new BufferedOutputStream(new FileOutputStream(target));
054                in = new BufferedInputStream(source);
055                byte[] buf = new byte[TRANSFER_BUF_SIZE];
056                int count;
057                while ((count = in.read(buf)) > -1) {
058                    out.write(buf, 0, count);
059                    if (monitor != null) {
060                        total += count;
061                        if (total > threshold) {
062                            threshold += TRANSFER_NOTIFICATION_SIZE;
063                            monitor.writeProgress(total);
064                        }
065                    }
066                }
067                out.close(); // also flushes the stream
068                out = null;
069                in.close();
070                in = null;
071            } finally {
072                if (out != null) {
073                    try {
074                        out.close();
075                    } catch (IOException ignored) {
076                    }
077                }
078                if (in != null) {
079                    try {
080                        in.close();
081                    } catch (IOException ignored) {
082                    }
083                }
084                if (monitor != null) {
085                    monitor.writeComplete(total);
086                }
087            }
088        }
089    }