1 56 package org.objectstyle.cayenne.modeler; 57 58 import java.io.File ; 59 import java.net.MalformedURLException ; 60 import java.net.URL ; 61 import java.net.URLClassLoader ; 62 import java.util.ArrayList ; 63 import java.util.Collection ; 64 import java.util.Collections ; 65 import java.util.Iterator ; 66 import java.util.List ; 67 68 import org.apache.log4j.Logger; 69 70 76 public class FileClassLoadingService implements ClassLoadingService { 77 78 private static Logger logObj = Logger.getLogger(FileClassLoadingService.class); 79 80 private FileClassLoader classLoader; 81 protected List pathFiles; 82 83 public FileClassLoadingService() { 84 this.pathFiles = new ArrayList (15); 85 } 86 87 90 public synchronized Class loadClass(String className) throws ClassNotFoundException { 91 return nonNullClassLoader().loadClass(className); 92 } 93 94 97 public ClassLoader getClassLoader() { 98 return nonNullClassLoader(); 99 } 100 101 104 public synchronized List getPathFiles() { 105 return Collections.unmodifiableList(pathFiles); 106 } 107 108 public synchronized void setPathFiles(Collection files) { 109 110 pathFiles.clear(); 111 classLoader = null; 112 113 Iterator it = files.iterator(); 114 while (it.hasNext()) { 115 addFile((File ) it.next()); 116 } 117 } 118 119 122 private void addFile(File file) { 123 file = file.getAbsoluteFile(); 124 125 if (pathFiles.contains(file)) { 126 return; 127 } 128 129 if (classLoader != null) { 130 try { 131 classLoader.addURL(file.toURL()); 132 } 133 catch (MalformedURLException ex) { 134 logObj.warn("Invalid classpath entry, ignoring: " + file); 135 return; 136 } 137 } 138 139 pathFiles.add(file); 140 logObj.debug("Added CLASSPATH entry...: " + file.getAbsolutePath()); 141 } 142 143 private synchronized FileClassLoader nonNullClassLoader() { 144 if (classLoader == null) { 146 classLoader = new FileClassLoader(getClass().getClassLoader(), pathFiles); 147 } 148 149 return classLoader; 150 } 151 152 static class FileClassLoader extends URLClassLoader { 154 155 FileClassLoader(ClassLoader parent) { 156 super(new URL [0], parent); 157 } 158 159 FileClassLoader(ClassLoader parent, List files) { 160 this(parent); 161 162 Iterator it = files.iterator(); 163 while (it.hasNext()) { 164 File file = (File ) it.next(); 165 166 try { 168 addURL(file.toURL()); 169 } 170 catch (MalformedURLException ex) { 171 } 172 } 173 } 174 175 public void addURL(URL url) { 176 super.addURL(url); 177 } 178 } 179 } | Popular Tags |