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.File;
20 import java.io.IOException;
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import java.net.URL;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.Set;
27 import java.util.jar.JarFile;
28 import java.util.zip.ZipEntry;
29 import java.util.zip.ZipFile;
30
31 import org.apache.geronimo.common.DeploymentException;
32 import org.apache.geronimo.deployment.util.DeploymentUtil;
33 import org.apache.geronimo.deployment.util.NestedJarFile;
34 import org.apache.geronimo.kernel.config.Configuration;
35
36 class InPlaceResourceContext implements ResourceContext {
37 private static final String PACKED_MODULED_SAVED_SUFFIX = ".saved";
38
39 private final Configuration configuration;
40 private final URI inPlaceBaseConfigurationUri;
41 private final Set zipFilesToExpand = new HashSet();
42
43 public InPlaceResourceContext(Configuration configuration, File inPlaceBaseConfigurationDir) throws DeploymentException {
44 this.configuration = configuration;
45 this.inPlaceBaseConfigurationUri = inPlaceBaseConfigurationDir.toURI();
46
47 if (inPlaceBaseConfigurationDir.isFile()) {
48 try {
49 configuration.addToClassPath("");
50 } catch (IOException e) {
51 throw new DeploymentException(e);
52 }
53 }
54 }
55
56 public void addIncludeAsPackedJar(URI targetPath, JarFile jarFile) throws IOException {
57 configuration.addToClassPath(targetPath.toString());
58 }
59
60 public void addInclude(URI targetPath, ZipFile zipFile, ZipEntry zipEntry) throws IOException {
61 if (zipFile instanceof NestedJarFile) {
62 NestedJarFile nestedJarFile = (NestedJarFile) zipFile;
63 if (nestedJarFile.isPacked()) {
64 zipFilesToExpand.add(zipFile);
65 }
66 }
67
68 configuration.addToClassPath(targetPath.toString());
69 }
70
71 public void addInclude(URI targetPath, URL source) throws IOException {
72 configuration.addToClassPath(targetPath.toString());
73 }
74
75 public void addInclude(URI targetPath, File source) throws IOException {
76 configuration.addToClassPath(targetPath.toString());
77 }
78
79 public void addFile(URI targetPath, ZipFile zipFile, ZipEntry zipEntry) throws IOException {
80 if (zipFile instanceof NestedJarFile) {
81 NestedJarFile nestedJarFile = (NestedJarFile) zipFile;
82 if (nestedJarFile.isPacked()) {
83 zipFilesToExpand.add(zipFile);
84 }
85 }
86 }
87
88 public void addFile(URI targetPath, URL source) throws IOException {
89 }
90
91 public void addFile(URI targetPath, File source) throws IOException {
92 }
93
94 public void addFile(URI targetPath, String source) throws IOException {
95 }
96
97 public File getTargetFile(URI targetPath) {
98 if (targetPath == null) throw new NullPointerException("targetPath is null");
99 if (targetPath.isAbsolute()) throw new IllegalArgumentException("targetPath is absolute");
100 if (targetPath.isOpaque()) throw new IllegalArgumentException("targetPath is opaque");
101 return new File(inPlaceBaseConfigurationUri.resolve(targetPath));
102 }
103
104 public void flush() throws IOException {
105 for (Iterator iter = zipFilesToExpand.iterator(); iter.hasNext();) {
106 ZipFile zipFile = (ZipFile) iter.next();
107 String name = zipFile.getName();
108 zipFile.close();
109 File srcFile = new File(name);
110 File targetFile;
111 if (!srcFile.isAbsolute()) {
112 srcFile = new File(inPlaceBaseConfigurationUri.resolve(name));
113 try {
114 targetFile = getTargetFile(new URI(name + PACKED_MODULED_SAVED_SUFFIX));
115 } catch (URISyntaxException e) {
116 throw new AssertionError(e);
117 }
118 } else {
119 targetFile = new File(name + PACKED_MODULED_SAVED_SUFFIX);
120 }
121 boolean success = new File(name).renameTo(targetFile);
122 if (!success) {
123 throw new IOException("Cannot rename file " +
124 name + " to " + targetFile.getAbsolutePath());
125 }
126
127 DeploymentUtil.unzipToDirectory(new ZipFile(targetFile), srcFile);
128 }
129 }
130
131
132 }