1 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 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 83 public void addInclude(URI targetPath, ZipFile zipFile, ZipEntry zipEntry) throws IOException { 84 86 File targetFile = getTargetFile(targetPath); 87 addFile(targetFile, zipFile, zipEntry); 88 89 configuration.addToClassPath(targetPath.toString()); 91 } 92 93 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 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 } | Popular Tags |