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.sharedlib;
018    
019    import org.apache.geronimo.gbean.GBeanInfo;
020    import org.apache.geronimo.gbean.GBeanInfoBuilder;
021    import org.apache.geronimo.gbean.SingleElementCollection;
022    
023    import java.util.Arrays;
024    import java.util.Collection;
025    import java.util.Set;
026    import java.util.HashSet;
027    import java.util.LinkedHashSet;
028    import java.util.Iterator;
029    import java.net.URL;
030    import java.net.MalformedURLException;
031    import java.io.File;
032    
033    import org.apache.geronimo.system.serverinfo.ServerInfo;
034    import org.apache.geronimo.kernel.config.MultiParentClassLoader;
035    
036    /**
037     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
038     */
039    public class SharedLib {
040        public SharedLib(ClassLoader classLoader, String[] classesDirs, String[] libDirs, Collection<ServerInfo> serverInfos) throws MalformedURLException {
041            this(classLoader, classesDirs, libDirs, new SingleElementCollection<ServerInfo>(serverInfos).getElement());
042        }
043    
044        private SharedLib(ClassLoader classLoader, String[] classesDirs, String[] libDirs, ServerInfo serverInfo) throws MalformedURLException {
045            MultiParentClassLoader multiParentClassLoader = (MultiParentClassLoader) classLoader;
046            Set currentUrls = new HashSet(Arrays.asList(multiParentClassLoader.getURLs()));
047    
048            int size=0;
049            if (classesDirs != null) size += classesDirs.length;
050            if (libDirs != null) size += libDirs.length;
051    
052            LinkedHashSet newUrls = new LinkedHashSet(size);
053            if (classesDirs != null) {
054                for (int i = 0; i < classesDirs.length; i++) {
055                    String classesDir = classesDirs[i];
056                    File dir = serverInfo.resolveServer(classesDir);
057                    if (!dir.exists()) {
058                        if (!dir.mkdirs()) {
059                            throw new IllegalArgumentException("Failed to create classes dir: " + dir);
060                        }
061                    }
062                    if (!dir.isDirectory()) {
063                        throw new IllegalArgumentException("Shared classes dir is not a directory: " + dir);
064                    }
065                    URL location = dir.toURL();
066                    if (!currentUrls.contains(location)) {
067                        newUrls.add(location);
068                    }
069                }
070            }
071    
072            if (libDirs != null) {
073                for (int i = 0; i < libDirs.length; i++) {
074                    String libDir = libDirs[i];
075                    File dir = serverInfo.resolveServer(libDir);
076                    if (!dir.exists()) {
077                        if (!dir.mkdirs()) {
078                            throw new IllegalArgumentException("Failed to create lib dir: " + dir);
079                        }
080                    }
081                    if (!dir.isDirectory()) {
082                        throw new IllegalArgumentException("Shared lib dir is not a directory: " + dir);
083                    }
084    
085                    File[] files = dir.listFiles();
086                    for (int j = 0; j < files.length; j++) {
087                        File file = files[j];
088                        if (file.canRead() && (file.getName().endsWith(".jar") || file.getName().endsWith(".zip"))) {
089                            URL location = file.toURL();
090                            if (!currentUrls.contains(location)) {
091                                newUrls.add(location);
092                            }
093                        }
094                    }
095                }
096            }
097    
098            for (Iterator iterator = newUrls.iterator(); iterator.hasNext();) {
099                URL url = (URL) iterator.next();
100                multiParentClassLoader.addURL(url);
101            }
102        }
103    
104        public static final GBeanInfo GBEAN_INFO;
105    
106        static {
107            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(SharedLib.class);
108            infoFactory.addAttribute("classLoader", ClassLoader.class, false, false);
109            infoFactory.addAttribute("classesDirs", String[].class, true, true);
110            infoFactory.addAttribute("libDirs", String[].class, true, true);
111            infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");
112    
113            infoFactory.setConstructor(new String[]{"classLoader", "classesDirs", "libDirs", "ServerInfo"});  
114            GBEAN_INFO = infoFactory.getBeanInfo();
115        }
116    
117        public static GBeanInfo getGBeanInfo() {
118            return GBEAN_INFO;
119        }
120    }