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 java.util.Arrays;
022    import java.util.Set;
023    import java.util.HashSet;
024    import java.util.LinkedHashSet;
025    import java.util.Iterator;
026    import java.net.URL;
027    import java.net.MalformedURLException;
028    import java.io.File;
029    
030    import org.apache.geronimo.system.serverinfo.ServerInfo;
031    import org.apache.geronimo.kernel.config.MultiParentClassLoader;
032    
033    /**
034     * @version $Rev: 549950 $ $Date: 2007-06-22 16:32:12 -0400 (Fri, 22 Jun 2007) $
035     */
036    public class SharedLib {
037        public SharedLib(ClassLoader classLoader, String[] classesDirs, String[] libDirs, ServerInfo serverInfo) throws MalformedURLException {
038            MultiParentClassLoader multiParentClassLoader = (MultiParentClassLoader) classLoader;
039            Set currentUrls = new HashSet(Arrays.asList(multiParentClassLoader.getURLs()));
040    
041            int size=0;
042            if (classesDirs != null) size += classesDirs.length;
043            if (libDirs != null) size += libDirs.length;
044    
045            LinkedHashSet newUrls = new LinkedHashSet(size);
046            if (classesDirs != null) {
047                for (int i = 0; i < classesDirs.length; i++) {
048                    String classesDir = classesDirs[i];
049                    File dir = serverInfo.resolveServer(classesDir);
050                    if (!dir.exists()) {
051                        if (!dir.mkdirs()) {
052                            throw new IllegalArgumentException("Failed to create classes dir: " + dir);
053                        }
054                    }
055                    if (!dir.isDirectory()) {
056                        throw new IllegalArgumentException("Shared classes dir is not a directory: " + dir);
057                    }
058                    URL location = dir.toURL();
059                    if (!currentUrls.contains(location)) {
060                        newUrls.add(location);
061                    }
062                }
063            }
064    
065            if (libDirs != null) {
066                for (int i = 0; i < libDirs.length; i++) {
067                    String libDir = libDirs[i];
068                    File dir = serverInfo.resolveServer(libDir);
069                    if (!dir.exists()) {
070                        if (!dir.mkdirs()) {
071                            throw new IllegalArgumentException("Failed to create lib dir: " + dir);
072                        }
073                    }
074                    if (!dir.isDirectory()) {
075                        throw new IllegalArgumentException("Shared lib dir is not a directory: " + dir);
076                    }
077    
078                    File[] files = dir.listFiles();
079                    for (int j = 0; j < files.length; j++) {
080                        File file = files[j];
081                        if (file.canRead() && (file.getName().endsWith(".jar") || file.getName().endsWith(".zip"))) {
082                            URL location = file.toURL();
083                            if (!currentUrls.contains(location)) {
084                                newUrls.add(location);
085                            }
086                        }
087                    }
088                }
089            }
090    
091            for (Iterator iterator = newUrls.iterator(); iterator.hasNext();) {
092                URL url = (URL) iterator.next();
093                multiParentClassLoader.addURL(url);
094            }
095        }
096    
097        public static final GBeanInfo GBEAN_INFO;
098    
099        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    }