KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URISyntaxException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.jar.JarFile JavaDoc;
28 import java.util.zip.ZipEntry JavaDoc;
29 import java.util.zip.ZipFile JavaDoc;
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 JavaDoc PACKED_MODULED_SAVED_SUFFIX = ".saved";
38     
39     private final Configuration configuration;
40     private final URI JavaDoc inPlaceBaseConfigurationUri;
41     private final Set JavaDoc zipFilesToExpand = new HashSet JavaDoc();
42     
43     public InPlaceResourceContext(Configuration configuration, File JavaDoc 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 JavaDoc e) {
51                 throw new DeploymentException(e);
52             }
53         }
54     }
55
56     public void addIncludeAsPackedJar(URI JavaDoc targetPath, JarFile JavaDoc jarFile) throws IOException JavaDoc {
57         configuration.addToClassPath(targetPath.toString());
58     }
59
60     public void addInclude(URI JavaDoc targetPath, ZipFile JavaDoc zipFile, ZipEntry JavaDoc zipEntry) throws IOException JavaDoc {
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 JavaDoc targetPath, URL JavaDoc source) throws IOException JavaDoc {
72         configuration.addToClassPath(targetPath.toString());
73     }
74
75     public void addInclude(URI JavaDoc targetPath, File JavaDoc source) throws IOException JavaDoc {
76         configuration.addToClassPath(targetPath.toString());
77     }
78
79     public void addFile(URI JavaDoc targetPath, ZipFile JavaDoc zipFile, ZipEntry JavaDoc zipEntry) throws IOException JavaDoc {
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 JavaDoc targetPath, URL JavaDoc source) throws IOException JavaDoc {
89     }
90
91     public void addFile(URI JavaDoc targetPath, File JavaDoc source) throws IOException JavaDoc {
92     }
93
94     public void addFile(URI JavaDoc targetPath, String JavaDoc source) throws IOException JavaDoc {
95     }
96     
97     public File JavaDoc getTargetFile(URI JavaDoc targetPath) {
98         if (targetPath == null) throw new NullPointerException JavaDoc("targetPath is null");
99         if (targetPath.isAbsolute()) throw new IllegalArgumentException JavaDoc("targetPath is absolute");
100         if (targetPath.isOpaque()) throw new IllegalArgumentException JavaDoc("targetPath is opaque");
101         return new File JavaDoc(inPlaceBaseConfigurationUri.resolve(targetPath));
102     }
103     
104     public void flush() throws IOException JavaDoc {
105         for (Iterator JavaDoc iter = zipFilesToExpand.iterator(); iter.hasNext();) {
106             ZipFile JavaDoc zipFile = (ZipFile JavaDoc) iter.next();
107             String JavaDoc name = zipFile.getName();
108             zipFile.close();
109             File JavaDoc srcFile = new File JavaDoc(name);
110             File JavaDoc targetFile;
111             if (!srcFile.isAbsolute()) {
112                 srcFile = new File JavaDoc(inPlaceBaseConfigurationUri.resolve(name));
113                 try {
114                     targetFile = getTargetFile(new URI JavaDoc(name + PACKED_MODULED_SAVED_SUFFIX));
115                 } catch (URISyntaxException JavaDoc e) {
116                     throw new AssertionError JavaDoc(e);
117                 }
118             } else {
119                 targetFile = new File JavaDoc(name + PACKED_MODULED_SAVED_SUFFIX);
120             }
121             boolean success = new File JavaDoc(name).renameTo(targetFile);
122             if (!success) {
123                 throw new IOException JavaDoc("Cannot rename file " +
124                         name + " to " + targetFile.getAbsolutePath());
125             }
126             
127             DeploymentUtil.unzipToDirectory(new ZipFile JavaDoc(targetFile), srcFile);
128         }
129     }
130     
131     
132 }
Popular Tags