diff options
author | Alexander Sulfrian <alexander@sulfrian.net> | 2010-06-08 09:01:43 +0200 |
---|---|---|
committer | Alexander Sulfrian <alexander@sulfrian.net> | 2010-06-08 09:01:43 +0200 |
commit | d1fa08fdc9cb11dccee76d668ff85df30458c295 (patch) | |
tree | 1d19df6405103577d872902486792e8c23bce711 /infrastructure/yuicompressor/src/com/yahoo/platform/yui/compressor/JarClassLoader.java | |
parent | d7c5ad7d6263fd1baf9bfdbaa4c50b70ef2fbdb2 (diff) | |
parent | 70d1f9d6fcaefe611e778b8dbf3bafea8934aa08 (diff) | |
download | etherpad-d1fa08fdc9cb11dccee76d668ff85df30458c295.tar.gz etherpad-d1fa08fdc9cb11dccee76d668ff85df30458c295.tar.bz2 etherpad-d1fa08fdc9cb11dccee76d668ff85df30458c295.zip |
Merge remote branch 'upstream/master'
Conflicts:
etherpad/src/etherpad/control/pro/admin/pro_admin_control.js
etherpad/src/etherpad/control/pro/pro_main_control.js
etherpad/src/etherpad/control/pro_help_control.js
etherpad/src/etherpad/globals.js
etherpad/src/etherpad/legacy_urls.js
etherpad/src/etherpad/pne/pne_utils.js
etherpad/src/etherpad/pro/pro_utils.js
etherpad/src/main.js
etherpad/src/plugins/fileUpload/templates/fileUpload.ejs
etherpad/src/plugins/testplugin/templates/page.ejs
etherpad/src/static/css/pad2_ejs.css
etherpad/src/static/css/pro-help.css
etherpad/src/static/img/jun09/pad/protop.gif
etherpad/src/static/js/store.js
etherpad/src/themes/default/templates/framed/framedheader-pro.ejs
etherpad/src/themes/default/templates/main/home.ejs
etherpad/src/themes/default/templates/pro-help/main.ejs
etherpad/src/themes/default/templates/pro-help/pro-help-template.ejs
infrastructure/com.etherpad/licensing.scala
trunk/etherpad/src/etherpad/collab/ace/contentcollector.js
trunk/etherpad/src/etherpad/collab/ace/linestylefilter.js
trunk/etherpad/src/static/css/home-opensource.css
trunk/etherpad/src/static/js/ace.js
trunk/etherpad/src/static/js/linestylefilter_client.js
trunk/etherpad/src/templates/email/eepnet_license_info.ejs
trunk/etherpad/src/templates/pad/pad_body2.ejs
trunk/etherpad/src/templates/pad/pad_content.ejs
trunk/etherpad/src/templates/pad/padfull_body.ejs
trunk/etherpad/src/templates/pro/admin/pne-license-manager.ejs
Diffstat (limited to 'infrastructure/yuicompressor/src/com/yahoo/platform/yui/compressor/JarClassLoader.java')
-rw-r--r-- | infrastructure/yuicompressor/src/com/yahoo/platform/yui/compressor/JarClassLoader.java | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/infrastructure/yuicompressor/src/com/yahoo/platform/yui/compressor/JarClassLoader.java b/infrastructure/yuicompressor/src/com/yahoo/platform/yui/compressor/JarClassLoader.java new file mode 100644 index 0000000..a6d3e13 --- /dev/null +++ b/infrastructure/yuicompressor/src/com/yahoo/platform/yui/compressor/JarClassLoader.java @@ -0,0 +1,158 @@ +/* + * YUI Compressor + * Author: Julien Lecomte <jlecomte@yahoo-inc.com> + * Copyright (c) 2007, Yahoo! Inc. All rights reserved. + * Code licensed under the BSD License: + * http://developer.yahoo.net/yui/license.txt + */ + +package com.yahoo.platform.yui.compressor; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Enumeration; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +public class JarClassLoader extends ClassLoader { + + private static String jarPath; + + public Class loadClass(String name) throws ClassNotFoundException { + + // First check if the class is already loaded + Class c = findLoadedClass(name); + if (c == null) { + c = findClass(name); + } + + if (c == null) { + c = ClassLoader.getSystemClassLoader().loadClass(name); + } + + return c; + } + + private static String getJarPath() { + + if (jarPath != null) { + return jarPath; + } + + String classname = JarClassLoader.class.getName().replace('.', '/') + ".class"; + String classpath = System.getProperty("java.class.path"); + String classpaths[] = classpath.split(System.getProperty("path.separator")); + + for (int i = 0; i < classpaths.length; i++) { + + String path = classpaths[i]; + JarFile jarFile = null; + JarEntry jarEntry = null; + + try { + jarFile = new JarFile(path); + jarEntry = findJarEntry(jarFile, classname); + } catch (IOException ioe) { + /* ignore */ + } finally { + if (jarFile != null) { + try { + jarFile.close(); + } catch (IOException ioe) { + /* ignore */ + } + } + } + + if (jarEntry != null) { + jarPath = path; + break; + } + } + + return jarPath; + } + + private static JarEntry findJarEntry(JarFile jarFile, String entryName) { + + Enumeration entries = jarFile.entries(); + + while (entries.hasMoreElements()) { + JarEntry entry = (JarEntry) entries.nextElement(); + if (entry.getName().equals(entryName)) { + return entry; + } + } + + return null; + } + + protected Class findClass(String name) { + + Class c = null; + String jarPath = getJarPath(); + + if (jarPath != null) { + JarFile jarFile = null; + try { + jarFile = new JarFile(jarPath); + c = loadClassData(jarFile, name); + } catch (IOException ioe) { + /* ignore */ + } finally { + if (jarFile != null) { + try { + jarFile.close(); + } catch (IOException ioe) { + /* ignore */ + } + } + } + } + + return c; + } + + private Class loadClassData(JarFile jarFile, String className) { + + String entryName = className.replace('.', '/') + ".class"; + JarEntry jarEntry = findJarEntry(jarFile, entryName); + if (jarEntry == null) { + return null; + } + + // Create the necessary package if needed... + int index = className.lastIndexOf('.'); + if (index >= 0) { + String packageName = className.substring(0, index); + if (getPackage(packageName) == null) { + definePackage(packageName, "", "", "", "", "", "", null); + } + } + + // Read the Jar File entry and define the class... + Class c = null; + try { + InputStream is = jarFile.getInputStream(jarEntry); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + copy(is, os); + byte[] bytes = os.toByteArray(); + c = defineClass(className, bytes, 0, bytes.length); + } catch (IOException ioe) { + /* ignore */ + } + + return c; + } + + private void copy(InputStream in, OutputStream out) throws IOException { + byte[] buf = new byte[1024]; + while (true) { + int len = in.read(buf); + if (len < 0) break; + out.write(buf, 0, len); + } + } +}
\ No newline at end of file |