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: */package org.apache.geronimo.deployment;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025: import java.net.URI;
026: import java.net.URL;
027: import java.util.jar.JarFile;
028: import java.util.zip.ZipEntry;
029: import java.util.zip.ZipFile;
030:
031: import org.apache.geronimo.common.DeploymentException;
032: import org.apache.geronimo.deployment.util.DeploymentUtil;
033: import org.apache.geronimo.kernel.config.Configuration;
034:
035: class CopyResourceContext implements ResourceContext {
036: private final Configuration configuration;
037: private final URI baseUri;
038: private final byte[] buffer = new byte[4096];
039:
040: public CopyResourceContext(Configuration configuration, File baseDir)
041: throws DeploymentException {
042: this .configuration = configuration;
043: baseUri = baseDir.toURI();
044:
045: if (baseDir.isFile()) {
046: try {
047: configuration.addToClassPath("");
048: } catch (IOException e) {
049: throw new DeploymentException(e);
050: }
051: }
052: }
053:
054: /**
055: * Copy a packed jar file into the deployment context and place it into the
056: * path specified in the target path. The newly added packed jar is added
057: * to the classpath of the configuration.
058: *
059: * @param targetPath where the packed jar file should be placed
060: * @param jarFile the jar file to copy
061: * @throws IOException if there's a problem copying the jar file
062: */
063: public void addIncludeAsPackedJar(URI targetPath, JarFile jarFile)
064: throws IOException {
065: if (targetPath.getPath().endsWith("/"))
066: throw new IllegalStateException(
067: "target path must not end with a '/' character: "
068: + targetPath);
069:
070: File targetFile = getTargetFile(targetPath);
071: DeploymentUtil.copyToPackedJar(jarFile, targetFile);
072:
073: if (!targetFile.isFile())
074: throw new IllegalStateException(
075: "target file should be a file: " + targetFile);
076: configuration.addToClassPath(targetPath.toString());
077: }
078:
079: /**
080: * Copy a ZIP file entry into the deployment context and place it into the
081: * path specified in the target path. The newly added entry is added
082: * to the classpath of the configuration.
083: *
084: * @param targetPath where the ZIP file entry should be placed
085: * @param zipFile the ZIP file
086: * @param zipEntry the ZIP file entry
087: * @throws IOException if there's a problem copying the ZIP entry
088: */
089: public void addInclude(URI targetPath, ZipFile zipFile,
090: ZipEntry zipEntry) throws IOException {
091: // if (!targetPath.getPath().endsWith("/")) throw new IllegalStateException("target path must end with a '/' character: " + targetPath);
092:
093: File targetFile = getTargetFile(targetPath);
094: addFile(targetFile, zipFile, zipEntry);
095:
096: // if (!targetFile.isDirectory()) throw new IllegalStateException("target file should be a directory: " + targetFile);
097: configuration.addToClassPath(targetPath.toString());
098: }
099:
100: /**
101: * Copy a file into the deployment context and place it into the
102: * path specified in the target path. The newly added file is added
103: * to the classpath of the configuration.
104: *
105: * @param targetPath where the file should be placed
106: * @param source the URL of file to be copied
107: * @throws IOException if there's a problem copying the ZIP entry
108: */
109: public void addInclude(URI targetPath, URL source)
110: throws IOException {
111: if (targetPath.getPath().endsWith("/"))
112: throw new IllegalStateException(
113: "target path must not end with a '/' character: "
114: + targetPath);
115:
116: File targetFile = getTargetFile(targetPath);
117: addFile(targetFile, source);
118:
119: if (!targetFile.isFile())
120: throw new IllegalStateException(
121: "target file should be a file: " + targetFile);
122: configuration.addToClassPath(targetPath.toString());
123: }
124:
125: /**
126: * Copy a file into the deployment context and place it into the
127: * path specified in the target path. The newly added file is added
128: * to the classpath of the configuration.
129: *
130: * @param targetPath where the file should be placed
131: * @param source the file to be copied
132: * @throws IOException if there's a problem copying the ZIP entry
133: */
134: public void addInclude(URI targetPath, File source)
135: throws IOException {
136: if (targetPath.getPath().endsWith("/"))
137: throw new IllegalStateException(
138: "target path must not end with a '/' character: "
139: + targetPath);
140:
141: File targetFile = getTargetFile(targetPath);
142: addFile(targetFile, source);
143:
144: if (!targetFile.isFile())
145: throw new IllegalStateException(
146: "target file should be a file: " + targetFile);
147: configuration.addToClassPath(targetPath.toString());
148: }
149:
150: public void addFile(URI targetPath, ZipFile zipFile,
151: ZipEntry zipEntry) throws IOException {
152: addFile(getTargetFile(targetPath), zipFile, zipEntry);
153: }
154:
155: public void addFile(URI targetPath, URL source) throws IOException {
156: addFile(getTargetFile(targetPath), source);
157: }
158:
159: public void addFile(URI targetPath, File source) throws IOException {
160: addFile(getTargetFile(targetPath), source);
161: }
162:
163: public void addFile(URI targetPath, String source)
164: throws IOException {
165: addFile(getTargetFile(targetPath), new ByteArrayInputStream(
166: source.getBytes()));
167: }
168:
169: public File getTargetFile(URI targetPath) {
170: if (targetPath == null)
171: throw new NullPointerException("targetPath is null");
172: if (targetPath.isAbsolute())
173: throw new IllegalArgumentException("targetPath is absolute");
174: if (targetPath.isOpaque())
175: throw new IllegalArgumentException("targetPath is opaque");
176: return new File(baseUri.resolve(targetPath));
177: }
178:
179: public void flush() throws IOException {
180: }
181:
182: private void addFile(File targetFile, ZipFile zipFile,
183: ZipEntry zipEntry) throws IOException {
184: if (zipEntry.isDirectory()) {
185: targetFile.mkdirs();
186: } else {
187: InputStream is = zipFile.getInputStream(zipEntry);
188: try {
189: addFile(targetFile, is);
190: } finally {
191: DeploymentUtil.close(is);
192: }
193: }
194: }
195:
196: private void addFile(File targetFile, URL source)
197: throws IOException {
198: InputStream in = null;
199: try {
200: in = source.openStream();
201: addFile(targetFile, in);
202: } finally {
203: DeploymentUtil.close(in);
204: }
205: }
206:
207: private void addFile(File targetFile, File source)
208: throws IOException {
209: InputStream in = null;
210: try {
211: in = new FileInputStream(source);
212: addFile(targetFile, in);
213: } finally {
214: DeploymentUtil.close(in);
215: }
216: }
217:
218: private void addFile(File targetFile, InputStream source)
219: throws IOException {
220: targetFile.getParentFile().mkdirs();
221: OutputStream out = null;
222: try {
223: out = new FileOutputStream(targetFile);
224: int count;
225: while ((count = source.read(buffer)) > 0) {
226: out.write(buffer, 0, count);
227: }
228: } finally {
229: DeploymentUtil.close(out);
230: }
231: }
232: }
|