KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileOutputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.util.zip.ZipInputStream JavaDoc;
25 import java.util.zip.ZipEntry JavaDoc;
26
27 import org.apache.geronimo.kernel.repository.ArtifactTypeHandler;
28 import org.apache.geronimo.kernel.repository.Artifact;
29 import org.apache.geronimo.kernel.repository.FileWriteMonitor;
30 import org.apache.geronimo.kernel.config.IOUtil;
31
32 /**
33  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
34  */

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

39     public void install(InputStream JavaDoc source, int size, Artifact artifact, FileWriteMonitor monitor, File JavaDoc target) throws IOException JavaDoc {
40         // assure that the target directory exists
41
File JavaDoc parent = target.getParentFile();
42         if (!parent.exists() && !parent.mkdirs()) {
43             throw new RuntimeException JavaDoc("Unable to create directory " + parent.getAbsolutePath());
44         }
45
46         // copy it
47
if (monitor != null) {
48             monitor.writeStarted(artifact.toString(), size);
49         }
50
51         int total = 0;
52         ZipInputStream JavaDoc in = new ZipInputStream JavaDoc(source);
53         try {
54
55             int threshold = UnpackArtifactTypeHandler.TRANSFER_NOTIFICATION_SIZE;
56             byte[] buffer = new byte[TRANSFER_BUF_SIZE];
57
58             for (ZipEntry JavaDoc entry = in.getNextEntry(); entry != null; entry = in.getNextEntry()) {
59                 File JavaDoc file = new File JavaDoc(target, entry.getName());
60                 if (entry.isDirectory()) {
61                     file.mkdirs();
62                 } else {
63                     if (!entry.getName().equals("META-INF/startup-jar")) {
64                         file.getParentFile().mkdirs();
65                         OutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
66                         try {
67                             int count;
68                             while ((count = in.read(buffer)) > 0) {
69                                 out.write(buffer, 0, count);
70                                 if (monitor != null) {
71                                     total += count;
72                                     if (total > threshold) {
73                                         threshold += UnpackArtifactTypeHandler.TRANSFER_NOTIFICATION_SIZE;
74                                         monitor.writeProgress(total);
75                                     }
76                                 }
77                             }
78                         } finally {
79                             IOUtil.flush(out);
80                             out.close();
81                         }
82                         in.closeEntry();
83                     }
84                 }
85             }
86         } catch (IOException JavaDoc e) {
87             IOUtil.recursiveDelete(target);
88             throw e;
89         } finally {
90             in.close();
91             if (monitor != null) {
92                 monitor.writeComplete(total);
93             }
94         }
95     }
96 }
97
Popular Tags