KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > util > DirectoryInitializationGBean


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.system.util;
18
19 import java.io.File JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.net.JarURLConnection JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLConnection JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.jar.JarEntry JavaDoc;
29 import java.util.jar.JarFile JavaDoc;
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: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
37  */

38 public class DirectoryInitializationGBean {
39
40
41     public DirectoryInitializationGBean(String JavaDoc prefix, String JavaDoc path, ServerInfo serverInfo, ClassLoader JavaDoc classLoader) throws IOException JavaDoc {
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 JavaDoc sourceURL = classLoader.getResource(prefix + path);
52         URLConnection JavaDoc conn = sourceURL.openConnection();
53         JarURLConnection JavaDoc jarConn = (JarURLConnection JavaDoc) conn;
54         JarFile JavaDoc jarFile = jarConn.getJarFile();
55         JarEntry JavaDoc sourceEntry = jarConn.getJarEntry();
56         byte[] buf = new byte[1024 * 8];
57         for (Enumeration JavaDoc entries = jarFile.entries(); entries.hasMoreElements();) {
58             JarEntry JavaDoc entry = (JarEntry JavaDoc) entries.nextElement();
59             if (entry.getName().startsWith(sourceEntry.getName())) {
60                 String JavaDoc entryName = entry.getName();
61                 String JavaDoc entryPath = entryName.substring(prefixLength);
62                 File JavaDoc targetPath = serverInfo.resolveServer(entryPath);
63                 if (!targetPath.exists()) {
64                     if (entry.isDirectory()) {
65                         targetPath.mkdirs();
66                     } else {
67                         InputStream JavaDoc in = jarFile.getInputStream(entry);
68                         try {
69                             OutputStream JavaDoc out = new FileOutputStream JavaDoc(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 JavaDoc.class, true);
93         infoBuilder.addAttribute("path", String JavaDoc.class, true);
94         infoBuilder.addReference("ServerInfo", ServerInfo.class, "GBean");
95         infoBuilder.addAttribute("classLoader", ClassLoader JavaDoc.class, false);
96
97         infoBuilder.setConstructor(new String JavaDoc[]{"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
109
Popular Tags