View Javadoc

1   /**
2    *
3    * Copyright 2006 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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;
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  import java.net.URI;
27  import java.net.URL;
28  import java.util.jar.JarFile;
29  import java.util.zip.ZipEntry;
30  import java.util.zip.ZipFile;
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 baseUri;
39      private final byte[] buffer = new byte[4096];
40      
41      public CopyResourceContext(Configuration configuration, File baseDir) throws DeploymentException {
42          this.configuration = configuration;
43          baseUri = baseDir.toURI();
44          
45          if (baseDir.isFile()) {
46              try {
47                  configuration.addToClassPath("");
48              } catch (IOException 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 targetPath, JarFile jarFile) throws IOException {
64          if (targetPath.getPath().endsWith("/")) throw new IllegalStateException("target path must not end with a '/' character: " + targetPath);
65  
66          File targetFile = getTargetFile(targetPath);
67          DeploymentUtil.copyToPackedJar(jarFile, targetFile);
68  
69          if (!targetFile.isFile()) throw new IllegalStateException("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 targetPath, ZipFile zipFile, ZipEntry zipEntry) throws IOException {
84  //        if (!targetPath.getPath().endsWith("/")) throw new IllegalStateException("target path must end with a '/' character: " + targetPath);
85  
86          File 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 targetPath, URL source) throws IOException {
103         if (targetPath.getPath().endsWith("/")) throw new IllegalStateException("target path must not end with a '/' character: " + targetPath);
104 
105         File targetFile = getTargetFile(targetPath);
106         addFile(targetFile, source);
107 
108         if (!targetFile.isFile()) throw new IllegalStateException("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 targetPath, File source) throws IOException {
122         if (targetPath.getPath().endsWith("/")) throw new IllegalStateException("target path must not end with a '/' character: " + targetPath);
123 
124         File targetFile = getTargetFile(targetPath);
125         addFile(targetFile, source);
126 
127         if (!targetFile.isFile()) throw new IllegalStateException("target file should be a file: " + targetFile);
128         configuration.addToClassPath(targetPath.toString());
129     }
130 
131     public void addFile(URI targetPath, ZipFile zipFile, ZipEntry zipEntry) throws IOException {
132         addFile(getTargetFile(targetPath), zipFile, zipEntry);
133     }
134 
135     public void addFile(URI targetPath, URL source) throws IOException {
136         addFile(getTargetFile(targetPath), source);
137     }
138 
139     public void addFile(URI targetPath, File source) throws IOException {
140         addFile(getTargetFile(targetPath), source);
141     }
142 
143     public void addFile(URI targetPath, String source) throws IOException {
144         addFile(getTargetFile(targetPath), new ByteArrayInputStream(source.getBytes()));
145     }
146 
147     public File getTargetFile(URI targetPath) {
148         if (targetPath == null) throw new NullPointerException("targetPath is null");
149         if (targetPath.isAbsolute()) throw new IllegalArgumentException("targetPath is absolute");
150         if (targetPath.isOpaque()) throw new IllegalArgumentException("targetPath is opaque");
151         return new File(baseUri.resolve(targetPath));
152     }
153 
154     public void flush() throws IOException {
155     }
156     
157     private void addFile(File targetFile, ZipFile zipFile, ZipEntry zipEntry) throws IOException {
158         if (zipEntry.isDirectory()) {
159             targetFile.mkdirs();
160         } else {
161             InputStream 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 targetFile, URL source) throws IOException {
171         InputStream 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 targetFile, File source) throws IOException {
181         InputStream in = null;
182         try {
183             in = new FileInputStream(source);
184             addFile(targetFile, in);
185         } finally {
186             DeploymentUtil.close(in);
187         }
188     }
189 
190     private void addFile(File targetFile, InputStream source) throws IOException {
191         targetFile.getParentFile().mkdirs();
192         OutputStream out = null;
193         try {
194             out = new FileOutputStream(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 }