1 48 49 package org.jpublish.action.jython; 50 51 import java.io.File ; 52 import java.io.IOException ; 53 import java.io.InputStream ; 54 import java.net.MalformedURLException ; 55 import java.net.URL ; 56 import java.util.ArrayList ; 57 import java.util.zip.ZipEntry ; 58 import java.util.zip.ZipFile ; 59 60 import com.anthonyeden.lib.util.IOUtilities; 61 import com.anthonyeden.lib.util.MessageUtilities; 62 import org.apache.commons.logging.Log; 63 import org.apache.commons.logging.LogFactory; 64 import org.jpublish.JPublishEngine; 65 66 71 72 public class JythonClassLoader extends ClassLoader { 73 74 private static Log log = LogFactory.getLog(JythonClassLoader.class); 75 76 private File searchDirectory; 77 78 83 84 public JythonClassLoader(String searchDirectory) { 85 this(new File (searchDirectory)); 86 } 87 88 95 96 public JythonClassLoader(String searchDirectory, ClassLoader parent) { 97 this(new File (searchDirectory), parent); 98 } 99 100 105 106 public JythonClassLoader(File searchDirectory) { 107 super(); 108 this.searchDirectory = searchDirectory; 109 } 110 111 118 119 public JythonClassLoader(File searchDirectory, ClassLoader parent) { 120 super(parent); 121 this.searchDirectory = searchDirectory; 122 } 123 124 129 130 public File getSearchDirectory() { 131 return searchDirectory; 132 } 133 134 139 140 public void setSearchDirectory(File searchDirectory) { 141 this.searchDirectory = searchDirectory; 142 } 143 144 151 152 public Class findClass(String name) throws ClassNotFoundException { 153 byte[] b = loadClassData(name); 154 return defineClass(name, b, 0, b.length); 155 } 156 157 163 164 public URL findResource(String name) { 165 File file = findFile(name); 166 if (file == null) { 167 return null; 168 } 169 170 try { 171 String urlString = "jar:" + file.toURL() + "!/" + name; 172 log.debug("URL: " + urlString); 173 174 return new URL (urlString); 175 } catch (MalformedURLException e) { 176 log.error("Malformed URL error: " + e.getMessage()); 177 return null; 178 } 179 } 180 181 188 189 private byte[] loadClassData(String name) throws ClassNotFoundException { 190 log.debug("Loading class data for " + name); 191 192 if (!name.endsWith(".class")) { 193 Object [] args = {name}; 194 throw new ClassNotFoundException (MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, "invalidClassName", 195 args)); 196 } 197 198 String path = name.replace('.', '/') + ".class"; 199 200 byte[] data = loadData(path); 201 if (data == null) { 202 Object [] args = {name, path}; 203 throw new ClassNotFoundException (MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, "classNotFound", 204 args)); 205 } 206 207 return data; 208 } 209 210 216 217 private File findFile(String path) { 218 File [] files = searchDirectory.listFiles(); 219 for (int i = 0; i < files.length; i++) { 220 if (files[i].getName().endsWith(".jar") || 221 files[i].getName().endsWith(".zip")) { 222 224 try { 225 ZipFile zipFile = new ZipFile (files[i]); 226 ZipEntry zipEntry = zipFile.getEntry(path); 227 228 if (zipEntry != null) { 229 return files[i]; 230 } 231 } catch (IOException e) { 232 Object [] args = {files[i].getPath(), e.getMessage()}; 233 String msg = MessageUtilities.getMessage(getClass(), 234 JPublishEngine.MESSAGE_PACKAGE, "findFileIOError", 235 args); 236 log.error(msg); 237 } 238 } 239 } 240 return null; 241 } 242 243 251 252 private byte[] loadData(String path) { 253 File file = findFile(path); 254 if (file == null) { 255 return null; 256 } 257 258 InputStream in = null; 259 try { 260 ZipFile zipFile = new ZipFile (file); 261 ZipEntry zipEntry = zipFile.getEntry(path); 262 263 long dataSize = zipEntry.getSize(); 264 log.debug("ZIP entry size: " + dataSize); 265 266 ArrayList dataList = new ArrayList (); 267 in = zipFile.getInputStream(zipEntry); 268 int b = -1; 269 while ((b = in.read()) != -1) { 270 dataList.add(new Byte ((byte) b)); 271 } 272 273 byte[] data = new byte[dataList.size()]; 274 for (int j = 0; j < data.length; j++) { 275 data[j] = ((Byte ) dataList.get(j)).byteValue(); 276 } 277 278 return data; 279 } catch (IOException e) { 280 log.error("IO error: " + e.getMessage()); 281 } finally { 282 IOUtilities.close(in); 283 } 284 285 return null; 286 } 287 288 } 289 | Popular Tags |