1 17 18 19 package org.apache.catalina.startup; 20 21 22 import java.io.File ; 23 import java.net.URL ; 24 import java.util.ArrayList ; 25 26 import org.apache.catalina.loader.StandardClassLoader; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 31 49 50 public final class ClassLoaderFactory { 51 52 53 private static Log log = LogFactory.getLog(ClassLoaderFactory.class); 54 55 protected static final Integer IS_DIR = new Integer (0); 56 protected static final Integer IS_JAR = new Integer (1); 57 protected static final Integer IS_GLOB = new Integer (2); 58 protected static final Integer IS_URL = new Integer (3); 59 60 62 63 78 public static ClassLoader createClassLoader(File unpacked[], 79 File packed[], 80 ClassLoader parent) 81 throws Exception { 82 return createClassLoader(unpacked, packed, null, parent); 83 } 84 85 86 105 public static ClassLoader createClassLoader(File unpacked[], 106 File packed[], 107 URL urls[], 108 ClassLoader parent) 109 throws Exception { 110 111 if (log.isDebugEnabled()) 112 log.debug("Creating new class loader"); 113 114 ArrayList list = new ArrayList (); 116 117 if (unpacked != null) { 119 for (int i = 0; i < unpacked.length; i++) { 120 File file = unpacked[i]; 121 if (!file.exists() || !file.canRead()) 122 continue; 123 file = new File (file.getCanonicalPath() + File.separator); 124 URL url = file.toURL(); 125 if (log.isDebugEnabled()) 126 log.debug(" Including directory " + url); 127 list.add(url); 128 } 129 } 130 131 if (packed != null) { 133 for (int i = 0; i < packed.length; i++) { 134 File directory = packed[i]; 135 if (!directory.isDirectory() || !directory.exists() || 136 !directory.canRead()) 137 continue; 138 String filenames[] = directory.list(); 139 for (int j = 0; j < filenames.length; j++) { 140 String filename = filenames[j].toLowerCase(); 141 if (!filename.endsWith(".jar")) 142 continue; 143 File file = new File (directory, filenames[j]); 144 if (log.isDebugEnabled()) 145 log.debug(" Including jar file " + file.getAbsolutePath()); 146 URL url = file.toURL(); 147 list.add(url); 148 } 149 } 150 } 151 152 URL [] array = (URL []) list.toArray(new URL [list.size()]); 154 StandardClassLoader classLoader = null; 155 if (parent == null) 156 classLoader = new StandardClassLoader(array); 157 else 158 classLoader = new StandardClassLoader(array, parent); 159 return (classLoader); 160 161 } 162 163 164 179 public static ClassLoader createClassLoader(String locations[], 180 Integer types[], 181 ClassLoader parent) 182 throws Exception { 183 184 if (log.isDebugEnabled()) 185 log.debug("Creating new class loader"); 186 187 ArrayList list = new ArrayList (); 189 190 if (locations != null && types != null && locations.length == types.length) { 191 for (int i = 0; i < locations.length; i++) { 192 String location = locations[i]; 193 if ( types[i] == IS_URL ) { 194 URL url = new URL (location); 195 if (log.isDebugEnabled()) 196 log.debug(" Including URL " + url); 197 list.add(url); 198 } else if ( types[i] == IS_DIR ) { 199 File directory = new File (location); 200 directory = new File (directory.getCanonicalPath()); 201 if (!directory.exists() || !directory.isDirectory() || 202 !directory.canRead()) 203 continue; 204 URL url = directory.toURL(); 205 if (log.isDebugEnabled()) 206 log.debug(" Including directory " + url); 207 list.add(url); 208 } else if ( types[i] == IS_JAR ) { 209 File file=new File (location); 210 file = new File (file.getCanonicalPath()); 211 if (!file.exists() || !file.canRead()) 212 continue; 213 URL url = file.toURL(); 214 if (log.isDebugEnabled()) 215 log.debug(" Including jar file " + url); 216 list.add(url); 217 } else if ( types[i] == IS_GLOB ) { 218 File directory=new File (location); 219 if (!directory.exists() || !directory.isDirectory() || 220 !directory.canRead()) 221 continue; 222 if (log.isDebugEnabled()) 223 log.debug(" Including directory glob " 224 + directory.getAbsolutePath()); 225 String filenames[] = directory.list(); 226 for (int j = 0; j < filenames.length; j++) { 227 String filename = filenames[j].toLowerCase(); 228 if (!filename.endsWith(".jar")) 229 continue; 230 File file = new File (directory, filenames[j]); 231 file = new File (file.getCanonicalPath()); 232 if (!file.exists() || !file.canRead()) 233 continue; 234 if (log.isDebugEnabled()) 235 log.debug(" Including glob jar file " 236 + file.getAbsolutePath()); 237 URL url = file.toURL(); 238 list.add(url); 239 } 240 } 241 } 242 } 243 244 URL [] array = (URL []) list.toArray(new URL [list.size()]); 246 if (log.isDebugEnabled()) 247 for (int i = 0; i < array.length; i++) { 248 log.debug(" location " + i + " is " + array[i]); 249 } 250 StandardClassLoader classLoader = null; 251 if (parent == null) 252 classLoader = new StandardClassLoader(array); 253 else 254 classLoader = new StandardClassLoader(array, parent); 255 return (classLoader); 256 257 } 258 259 260 } 261 | Popular Tags |