KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > CopyResourceContext


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.deployment;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.jar.JarFile JavaDoc;
29 import java.util.zip.ZipEntry JavaDoc;
30 import java.util.zip.ZipFile JavaDoc;
31
32 import org.apache.geronimo.common.DeploymentException;
33 import org.apache.geronimo.deployment.util.DeploymentUtil;
34 import org.apache.geronimo.kernel.config.Configuration;
35
36 class CopyResourceContext implements ResourceContext {
37     private final Configuration configuration;
38     private final URI JavaDoc baseUri;
39     private final byte[] buffer = new byte[4096];
40     
41     public CopyResourceContext(Configuration configuration, File JavaDoc baseDir) throws DeploymentException {
42         this.configuration = configuration;
43         baseUri = baseDir.toURI();
44         
45         if (baseDir.isFile()) {
46             try {
47                 configuration.addToClassPath("");
48             } catch (IOException JavaDoc e) {
49                 throw new DeploymentException(e);
50             }
51         }
52     }
53
54     /**
55      * Copy a packed jar file into the deployment context and place it into the
56      * path specified in the target path. The newly added packed jar is added
57      * to the classpath of the configuration.
58      *
59      * @param targetPath where the packed jar file should be placed
60      * @param jarFile the jar file to copy
61      * @throws IOException if there's a problem copying the jar file
62      */

63     public void addIncludeAsPackedJar(URI JavaDoc targetPath, JarFile JavaDoc jarFile) throws IOException JavaDoc {
64         if (targetPath.getPath().endsWith("/")) throw new IllegalStateException JavaDoc("target path must not end with a '/' character: " + targetPath);
65
66         File JavaDoc targetFile = getTargetFile(targetPath);
67         DeploymentUtil.copyToPackedJar(jarFile, targetFile);
68
69         if (!targetFile.isFile()) throw new IllegalStateException JavaDoc("target file should be a file: " + targetFile);
70         configuration.addToClassPath(targetPath.toString());
71     }
72
73     /**
74      * Copy a ZIP file entry into the deployment context and place it into the
75      * path specified in the target path. The newly added entry is added
76      * to the classpath of the configuration.
77      *
78      * @param targetPath where the ZIP file entry should be placed
79      * @param zipFile the ZIP file
80      * @param zipEntry the ZIP file entry
81      * @throws IOException if there's a problem copying the ZIP entry
82      */

83     public void addInclude(URI JavaDoc targetPath, ZipFile JavaDoc zipFile, ZipEntry JavaDoc zipEntry) throws IOException JavaDoc {
84 // if (!targetPath.getPath().endsWith("/")) throw new IllegalStateException("target path must end with a '/' character: " + targetPath);
85

86         File JavaDoc targetFile = getTargetFile(targetPath);
87         addFile(targetFile, zipFile, zipEntry);
88
89 // if (!targetFile.isDirectory()) throw new IllegalStateException("target file should be a directory: " + targetFile);
90
configuration.addToClassPath(targetPath.toString());
91     }
92
93     /**
94      * Copy a file into the deployment context and place it into the
95      * path specified in the target path. The newly added file is added
96      * to the classpath of the configuration.
97      *
98      * @param targetPath where the file should be placed
99      * @param source the URL of file to be copied
100      * @throws IOException if there's a problem copying the ZIP entry
101      */

102     public void addInclude(URI JavaDoc targetPath, URL JavaDoc source) throws IOException JavaDoc {
103         if (targetPath.getPath().endsWith("/")) throw new IllegalStateException JavaDoc("target path must not end with a '/' character: " + targetPath);
104
105         File JavaDoc targetFile = getTargetFile(targetPath);
106         addFile(targetFile, source);
107
108         if (!targetFile.isFile()) throw new IllegalStateException JavaDoc("target file should be a file: " + targetFile);
109         configuration.addToClassPath(targetPath.toString());
110     }
111
112     /**
113      * Copy a file into the deployment context and place it into the
114      * path specified in the target path. The newly added file is added
115      * to the classpath of the configuration.
116      *
117      * @param targetPath where the file should be placed
118      * @param source the file to be copied
119      * @throws IOException if there's a problem copying the ZIP entry
120      */

121     public void addInclude(URI JavaDoc targetPath, File JavaDoc source) throws IOException JavaDoc {
122         if (targetPath.getPath().endsWith("/")) throw new IllegalStateException JavaDoc("target path must not end with a '/' character: " + targetPath);
123
124         File JavaDoc targetFile = getTargetFile(targetPath);
125         addFile(targetFile, source);
126
127         if (!targetFile.isFile()) throw new IllegalStateException JavaDoc("target file should be a file: " + targetFile);
128         configuration.addToClassPath(targetPath.toString());
129     }
130
131     public void addFile(URI JavaDoc targetPath, ZipFile JavaDoc zipFile, ZipEntry JavaDoc zipEntry) throws IOException JavaDoc {
132         addFile(getTargetFile(targetPath), zipFile, zipEntry);
133     }
134
135     public void addFile(URI JavaDoc targetPath, URL JavaDoc source) throws IOException JavaDoc {
136         addFile(getTargetFile(targetPath), source);
137     }
138
139     public void addFile(URI JavaDoc targetPath, File JavaDoc source) throws IOException JavaDoc {
140         addFile(getTargetFile(targetPath), source);
141     }
142
143     public void addFile(URI JavaDoc targetPath, String JavaDoc source) throws IOException JavaDoc {
144         addFile(getTargetFile(targetPath), new ByteArrayInputStream JavaDoc(source.getBytes()));
145     }
146
147     public File JavaDoc getTargetFile(URI JavaDoc targetPath) {
148         if (targetPath == null) throw new NullPointerException JavaDoc("targetPath is null");
149         if (targetPath.isAbsolute()) throw new IllegalArgumentException JavaDoc("targetPath is absolute");
150         if (targetPath.isOpaque()) throw new IllegalArgumentException JavaDoc("targetPath is opaque");
151         return new File JavaDoc(baseUri.resolve(targetPath));
152     }
153
154     public void flush() throws IOException JavaDoc {
155     }
156     
157     private void addFile(File JavaDoc targetFile, ZipFile JavaDoc zipFile, ZipEntry JavaDoc zipEntry) throws IOException JavaDoc {
158         if (zipEntry.isDirectory()) {
159             targetFile.mkdirs();
160         } else {
161             InputStream JavaDoc is = zipFile.getInputStream(zipEntry);
162             try {
163                 addFile(targetFile, is);
164             } finally {
165                 DeploymentUtil.close(is);
166             }
167         }
168     }
169
170     private void addFile(File JavaDoc targetFile, URL JavaDoc source) throws IOException JavaDoc {
171         InputStream JavaDoc in = null;
172         try {
173             in = source.openStream();
174             addFile(targetFile, in);
175         } finally {
176             DeploymentUtil.close(in);
177         }
178     }
179
180     private void addFile(File JavaDoc targetFile, File JavaDoc source) throws IOException JavaDoc {
181         InputStream JavaDoc in = null;
182         try {
183             in = new FileInputStream JavaDoc(source);
184             addFile(targetFile, in);
185         } finally {
186             DeploymentUtil.close(in);
187         }
188     }
189
190     private void addFile(File JavaDoc targetFile, InputStream JavaDoc source) throws IOException JavaDoc {
191         targetFile.getParentFile().mkdirs();
192         OutputStream JavaDoc out = null;
193         try {
194             out = new FileOutputStream JavaDoc(targetFile);
195             int count;
196             while ((count = source.read(buffer)) > 0) {
197                 out.write(buffer, 0, count);
198             }
199         } finally {
200             DeploymentUtil.close(out);
201         }
202     }
203 }
Popular Tags