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 */ 017 package org.apache.geronimo.system.util; 018 019 import java.io.File; 020 import java.io.FileOutputStream; 021 import java.io.IOException; 022 import java.io.InputStream; 023 import java.io.OutputStream; 024 import java.net.JarURLConnection; 025 import java.net.URL; 026 import java.net.URLConnection; 027 import java.util.Enumeration; 028 import java.util.jar.JarEntry; 029 import java.util.jar.JarFile; 030 031 import org.apache.geronimo.gbean.GBeanInfo; 032 import org.apache.geronimo.gbean.GBeanInfoBuilder; 033 import org.apache.geronimo.system.serverinfo.ServerInfo; 034 035 /** 036 * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $ 037 */ 038 public class DirectoryInitializationGBean { 039 040 041 public DirectoryInitializationGBean(String prefix, String path, ServerInfo serverInfo, ClassLoader classLoader) throws IOException { 042 043 if (!prefix.endsWith("/")) { 044 prefix = prefix + "/"; 045 } 046 int prefixLength = prefix.length(); 047 if (!path.endsWith("/")) { 048 path = path + "/"; 049 } 050 051 URL sourceURL = classLoader.getResource(prefix + path); 052 URLConnection conn = sourceURL.openConnection(); 053 JarURLConnection jarConn = (JarURLConnection) conn; 054 JarFile jarFile = jarConn.getJarFile(); 055 JarEntry sourceEntry = jarConn.getJarEntry(); 056 byte[] buf = new byte[1024 * 8]; 057 for (Enumeration entries = jarFile.entries(); entries.hasMoreElements();) { 058 JarEntry entry = (JarEntry) entries.nextElement(); 059 if (entry.getName().startsWith(sourceEntry.getName())) { 060 String entryName = entry.getName(); 061 String entryPath = entryName.substring(prefixLength); 062 File targetPath = serverInfo.resolveServer(entryPath); 063 if (!targetPath.exists()) { 064 if (entry.isDirectory()) { 065 targetPath.mkdirs(); 066 } else { 067 InputStream in = jarFile.getInputStream(entry); 068 try { 069 OutputStream out = new FileOutputStream(targetPath); 070 try { 071 int chunk; 072 while ((chunk = in.read(buf)) > 0) { 073 out.write(buf, 0, chunk); 074 } 075 } finally { 076 out.close(); 077 } 078 } finally { 079 in.close(); 080 } 081 } 082 } 083 } 084 } 085 086 } 087 088 public static final GBeanInfo GBEAN_INFO; 089 090 static { 091 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(DirectoryInitializationGBean.class, "GBean"); 092 infoBuilder.addAttribute("prefix", String.class, true); 093 infoBuilder.addAttribute("path", String.class, true); 094 infoBuilder.addReference("ServerInfo", ServerInfo.class, "GBean"); 095 infoBuilder.addAttribute("classLoader", ClassLoader.class, false); 096 097 infoBuilder.setConstructor(new String[]{"prefix", "path", "ServerInfo", "classLoader"}); 098 099 GBEAN_INFO = infoBuilder.getBeanInfo(); 100 } 101 102 public static GBeanInfo getGBeanInfo() { 103 return GBEAN_INFO; 104 } 105 106 } 107 108