Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jruby.util;
  
  
  import java.io.Closeable;
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 public class JRubyClassLoader extends URLClassLoader implements ClassDefiningClassLoader {
 
     private static final Logger LOG = LoggerFactory.getLogger("JRubyClassLoader");
 
     private final static ProtectionDomain DEFAULT_DOMAIN
             = JRubyClassLoader.class.getProtectionDomain();
 
     private final Map<URL,Set<String>> jarIndexes = new LinkedHashMap<URL,Set<String>>();
 
     private Runnable unloader;
 
     public JRubyClassLoader(ClassLoader parent) {
         super(new URL[0], parent);
     }
 
     // Change visibility so others can see it
     @Override
     public void addURL(URL url) {
         super.addURL(url);
         indexJarContents(url);
     }

    
Called when the parent runtime is torn down.
 
     public void tearDown(boolean debug) {
         try {
             // A hack to allow unloading all JDBC Drivers loaded by this classloader.
             // See http://bugs.jruby.org/4226
             getJDBCDriverUnloader().run();
         } catch (Exception e) {
             if (debug) {
                 .debug(e);
             }
         }
     }
 
     public synchronized Runnable getJDBCDriverUnloader() {
         if ( == null) {
             try {
                 InputStream unloaderStream = getClass().getResourceAsStream("/org/jruby/util/JDBCDriverUnloader.class");
                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
                 byte[] buf = new byte[4096];
                 int bytesRead;
                 while ((bytesRead = unloaderStream.read(buf)) != -1) {
                     baos.write(buf, 0, bytesRead);
                 }
 
                 Class unloaderClass = defineClass("org.jruby.util.JDBCDriverUnloader"baos.toByteArray());
                  = (RunnableunloaderClass.newInstance();
             } catch (Exception e) {
                 throw new RuntimeException(e);
             }
         }
         return ;
     }
 
     public Class<?> defineClass(String namebyte[] bytes) {
         return super.defineClass(namebytes, 0, bytes.length);
      }
 
     public Class<?> defineClass(String namebyte[] bytesProtectionDomain domain) {
        return super.defineClass(namebytes, 0, bytes.lengthdomain);
     }
 
     @Override
     protected Class<?> findClass(String classNamethrows ClassNotFoundException {
         try {
             return super.findClass(className);
         } catch (ClassNotFoundException ex) {
             String resourceName = className.replace('.''/').concat(".class");
 
             URL classUrl = null;
             synchronized () {
                 for (URL jarUrl : .keySet()) {
                     if (.get(jarUrl).contains(resourceName)) {
                        try {
                            classUrl = CompoundJarURLStreamHandler.createUrl(jarUrlresourceName);
                            break;
                        } catch (IOException e) {
                            // keep going to next URL
                        }
                    }
                }
            }
            if (classUrl != null) {
                try {
                    InputStream input = classUrl.openStream();
                    try {
                        byte[] buffer = new byte[4096];
                        ByteArrayOutputStream output = new ByteArrayOutputStream();
                        for (int count = input.read(buffer); count > 0; count = input.read(buffer)) {
                            output.write(buffer, 0, count);
                        }
                        byte[] data = output.toByteArray();
                        return defineClass(classNamedata, 0, data.length);
                    } finally {
                        close(input);
                    }
                } catch (IOException e) {
                    // just fall-through to the re-throw below
                }
            }
            throw ex;
        }
    }
    @Override
    public URL findResource(String resourceName) {
        URL result = super.findResource(resourceName);
        if (result == null) {
            synchronized () {
                for (URL jarUrl : .keySet()) {
                    if (.get(jarUrl).contains(resourceName)) {
                        try {
                            return CompoundJarURLStreamHandler.createUrl(jarUrlresourceName);
                        } catch (IOException e) {
                            // keep going
                        }
                    }
                }
            }
        }
        return result;
    }
    @Override
    public Enumeration<URLfindResources(String resourceNamethrows IOException {
        final List<URLembeddedUrls = new ArrayList<URL>();
        synchronized () {
            for (URL jarUrl : .keySet()) {
                if (.get(jarUrl).contains(resourceName)) {
                    try {
                        embeddedUrls.add(CompoundJarURLStreamHandler.createUrl(jarUrlresourceName));
                    } catch (IOException e) {
                        // keep going
                    }
                }
            }
        }
        if (embeddedUrls.isEmpty()) {
            return super.findResources(resourceName);
        } else {
            final Enumeration<URLoriginalResult = super.findResources(resourceName);
            return new Enumeration<URL>() {
                private Iterator<URLextendedResult;
                public URL nextElement() {
                    if ( == null) {
                        return originalResult.nextElement();
                    } else {
                        return .next();
                    }
                }
                public boolean hasMoreElements() {
                    if ( == null) {
                        boolean result = originalResult.hasMoreElements();
                        if (!result) {
                            // original result is consumed, switching to result
                            // from embedded jars processing.
                             = embeddedUrls.iterator();
                            result = .hasNext();
                        }
                        return result;
                    } else {
                        return .hasNext();
                    }
                }
            };
        }
    }
    private void indexJarContents(URL jarUrl) {
        String proto = jarUrl.getProtocol();
        // we only need to index jar: and compoundjar: URLs
        // 1st-level jar files with file: URLs are handled by the JDK
        if (proto.equals("jar") || proto.equals(.)) {
            synchronized () {
                Set<Stringentries = new HashSet<String>();
                .put(jarUrlentries);
                try {
                    InputStream baseInputStream = jarUrl.openStream();
                    try {
                        JarInputStream baseJar = new JarInputStream(baseInputStream);
                        for (JarEntry entry = baseJar.getNextJarEntry(); entry != nullentry = baseJar.getNextJarEntry()) {
                            entries.add(entry.getName());
                        }
                    } finally {
                        close(baseInputStream);
                    }
                } catch (IOException ex) {
                    // can't read the stream, keep going
                }
            }
        }
    }
    private static void close(Closeable resource) {
        if (resource != null) {
            try {
                resource.close();
            } catch (IOException ignore) {
            }
        }
    }
New to GrepCode? Check out our FAQ X