1 /**
2 *
3 * Copyright 2005 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.system.util;
18
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.net.JarURLConnection;
25 import java.net.URL;
26 import java.net.URLConnection;
27 import java.util.Enumeration;
28 import java.util.jar.JarEntry;
29 import java.util.jar.JarFile;
30
31 import org.apache.geronimo.gbean.GBeanInfo;
32 import org.apache.geronimo.gbean.GBeanInfoBuilder;
33 import org.apache.geronimo.system.serverinfo.ServerInfo;
34
35 /**
36 * @version $Rev: 407990 $ $Date: 2006-05-20 05:29:57 -0700 (Sat, 20 May 2006) $
37 */
38 public class DirectoryInitializationGBean {
39
40
41 public DirectoryInitializationGBean(String prefix, String path, ServerInfo serverInfo, ClassLoader classLoader) throws IOException {
42
43 if (!prefix.endsWith("/")) {
44 prefix = prefix + "/";
45 }
46 int prefixLength = prefix.length();
47 if (!path.endsWith("/")) {
48 path = path + "/";
49 }
50
51 URL sourceURL = classLoader.getResource(prefix + path);
52 URLConnection conn = sourceURL.openConnection();
53 JarURLConnection jarConn = (JarURLConnection) conn;
54 JarFile jarFile = jarConn.getJarFile();
55 JarEntry sourceEntry = jarConn.getJarEntry();
56 byte[] buf = new byte[1024 * 8];
57 for (Enumeration entries = jarFile.entries(); entries.hasMoreElements();) {
58 JarEntry entry = (JarEntry) entries.nextElement();
59 if (entry.getName().startsWith(sourceEntry.getName())) {
60 String entryName = entry.getName();
61 String entryPath = entryName.substring(prefixLength);
62 File targetPath = serverInfo.resolveServer(entryPath);
63 if (!targetPath.exists()) {
64 if (entry.isDirectory()) {
65 targetPath.mkdirs();
66 } else {
67 InputStream in = jarFile.getInputStream(entry);
68 try {
69 OutputStream out = new FileOutputStream(targetPath);
70 try {
71 int chunk;
72 while ((chunk = in.read(buf)) > 0) {
73 out.write(buf, 0, chunk);
74 }
75 } finally {
76 out.close();
77 }
78 } finally {
79 in.close();
80 }
81 }
82 }
83 }
84 }
85
86 }
87
88 public static final GBeanInfo GBEAN_INFO;
89
90 static {
91 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(DirectoryInitializationGBean.class, "GBean");
92 infoBuilder.addAttribute("prefix", String.class, true);
93 infoBuilder.addAttribute("path", String.class, true);
94 infoBuilder.addReference("ServerInfo", ServerInfo.class, "GBean");
95 infoBuilder.addAttribute("classLoader", ClassLoader.class, false);
96
97 infoBuilder.setConstructor(new String[]{"prefix", "path", "ServerInfo", "classLoader"});
98
99 GBEAN_INFO = infoBuilder.getBeanInfo();
100 }
101
102 public static GBeanInfo getGBeanInfo() {
103 return GBEAN_INFO;
104 }
105
106 }
107
108