KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > sharedlib > SharedLib


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.sharedlib;
18
19 import org.apache.geronimo.gbean.GBeanInfo;
20 import org.apache.geronimo.gbean.GBeanInfoBuilder;
21 import java.util.Arrays JavaDoc;
22 import java.util.Set JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.LinkedHashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.io.File JavaDoc;
29
30 import org.apache.geronimo.system.serverinfo.ServerInfo;
31 import org.apache.geronimo.kernel.config.MultiParentClassLoader;
32
33 /**
34  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
35  */

36 public class SharedLib {
37     public SharedLib(ClassLoader JavaDoc classLoader, String JavaDoc[] classesDirs, String JavaDoc[] libDirs, ServerInfo serverInfo) throws MalformedURLException JavaDoc {
38         MultiParentClassLoader multiParentClassLoader = (MultiParentClassLoader) classLoader;
39         Set JavaDoc currentUrls = new HashSet JavaDoc(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 JavaDoc newUrls = new LinkedHashSet JavaDoc(size);
46         if (classesDirs != null) {
47             for (int i = 0; i < classesDirs.length; i++) {
48                 String JavaDoc classesDir = classesDirs[i];
49                 File JavaDoc dir = serverInfo.resolve(classesDir);
50                 if (!dir.exists()) {
51                     if (!dir.mkdirs()) {
52                         throw new IllegalArgumentException JavaDoc("Failed to create classes dir: " + dir);
53                     }
54                 }
55                 if (!dir.isDirectory()) {
56                     throw new IllegalArgumentException JavaDoc("Classes dir is not a directory: " + dir);
57                 }
58                 URL JavaDoc 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 JavaDoc libDir = libDirs[i];
68                 File JavaDoc dir = serverInfo.resolve(libDir);
69                 if (!dir.exists()) {
70                     if (!dir.mkdirs()) {
71                         throw new IllegalArgumentException JavaDoc("Failed to create lib dir: " + dir);
72                     }
73                 }
74                 if (!dir.isDirectory()) {
75                     throw new IllegalArgumentException JavaDoc("Lib dir is not a directory: " + dir);
76                 }
77
78                 File JavaDoc[] files = dir.listFiles();
79                 for (int j = 0; j < files.length; j++) {
80                     File JavaDoc file = files[j];
81                     if (file.canRead() && (file.getName().endsWith(".jar") || file.getName().endsWith(".zip"))) {
82                         URL JavaDoc location = file.toURL();
83                         if (!currentUrls.contains(location)) {
84                             newUrls.add(location);
85                         }
86                     }
87                 }
88             }
89         }
90
91         for (Iterator JavaDoc iterator = newUrls.iterator(); iterator.hasNext();) {
92             URL JavaDoc url = (URL JavaDoc) 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 JavaDoc.class, false, false);
102         infoFactory.addAttribute("classesDirs", String JavaDoc[].class, true, true);
103         infoFactory.addAttribute("libDirs", String JavaDoc[].class, true, true);
104         infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");
105
106         infoFactory.setConstructor(new String JavaDoc[]{"classLoader", "classesDirs", "libDirs", "ServerInfo"});
107         GBEAN_INFO = infoFactory.getBeanInfo();
108     }
109
110     public static GBeanInfo getGBeanInfo() {
111         return GBEAN_INFO;
112     }
113 }
114
Popular Tags