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.sharedlib; 18 19 import org.apache.geronimo.gbean.GBeanInfo; 20 import org.apache.geronimo.gbean.GBeanInfoBuilder; 21 import java.util.Arrays; 22 import java.util.Set; 23 import java.util.HashSet; 24 import java.util.LinkedHashSet; 25 import java.util.Iterator; 26 import java.net.URL; 27 import java.net.MalformedURLException; 28 import java.io.File; 29 30 import org.apache.geronimo.system.serverinfo.ServerInfo; 31 import org.apache.geronimo.kernel.config.MultiParentClassLoader; 32 33 /** 34 * @version $Rev: 422567 $ $Date: 2006-07-16 16:20:59 -0700 (Sun, 16 Jul 2006) $ 35 */ 36 public class SharedLib { 37 public SharedLib(ClassLoader classLoader, String[] classesDirs, String[] libDirs, ServerInfo serverInfo) throws MalformedURLException { 38 MultiParentClassLoader multiParentClassLoader = (MultiParentClassLoader) classLoader; 39 Set currentUrls = new HashSet(Arrays.asList(multiParentClassLoader.getURLs())); 40 41 int size=0; 42 if (classesDirs != null) size += classesDirs.length; 43 if (libDirs != null) size += libDirs.length; 44 45 LinkedHashSet newUrls = new LinkedHashSet(size); 46 if (classesDirs != null) { 47 for (int i = 0; i < classesDirs.length; i++) { 48 String classesDir = classesDirs[i]; 49 File dir = serverInfo.resolve(classesDir); 50 if (!dir.exists()) { 51 if (!dir.mkdirs()) { 52 throw new IllegalArgumentException("Failed to create classes dir: " + dir); 53 } 54 } 55 if (!dir.isDirectory()) { 56 throw new IllegalArgumentException("Classes dir is not a directory: " + dir); 57 } 58 URL location = dir.toURL(); 59 if (!currentUrls.contains(location)) { 60 newUrls.add(location); 61 } 62 } 63 } 64 65 if (libDirs != null) { 66 for (int i = 0; i < libDirs.length; i++) { 67 String libDir = libDirs[i]; 68 File dir = serverInfo.resolve(libDir); 69 if (!dir.exists()) { 70 if (!dir.mkdirs()) { 71 throw new IllegalArgumentException("Failed to create lib dir: " + dir); 72 } 73 } 74 if (!dir.isDirectory()) { 75 throw new IllegalArgumentException("Lib dir is not a directory: " + dir); 76 } 77 78 File[] files = dir.listFiles(); 79 for (int j = 0; j < files.length; j++) { 80 File file = files[j]; 81 if (file.canRead() && (file.getName().endsWith(".jar") || file.getName().endsWith(".zip"))) { 82 URL location = file.toURL(); 83 if (!currentUrls.contains(location)) { 84 newUrls.add(location); 85 } 86 } 87 } 88 } 89 } 90 91 for (Iterator iterator = newUrls.iterator(); iterator.hasNext();) { 92 URL url = (URL) iterator.next(); 93 multiParentClassLoader.addURL(url); 94 } 95 } 96 97 public static final GBeanInfo GBEAN_INFO; 98 99 static { 100 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(SharedLib.class); 101 infoFactory.addAttribute("classLoader", ClassLoader.class, false, false); 102 infoFactory.addAttribute("classesDirs", String[].class, true, true); 103 infoFactory.addAttribute("libDirs", String[].class, true, true); 104 infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean"); 105 106 infoFactory.setConstructor(new String[]{"classLoader", "classesDirs", "libDirs", "ServerInfo"}); 107 GBEAN_INFO = infoFactory.getBeanInfo(); 108 } 109 110 public static GBeanInfo getGBeanInfo() { 111 return GBEAN_INFO; 112 } 113 }