Classpath Utils : ClassPath « Reflection « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Reflection » ClassPathScreenshots 
Classpath Utils
 
/**
 * Redistribution and use of this software and associated documentation
 * ("Software"), with or without modification, are permitted provided
 * that the following conditions are met:
 *
 * 1. Redistributions of source code must retain copyright
 *    statements and notices.  Redistributions must also contain a
 *    copy of this document.
 *
 * 2. Redistributions in binary form must reproduce the
 *    above copyright notice, this list of conditions and the
 *    following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. The name "OpenEJB" must not be used to endorse or promote
 *    products derived from this Software without prior written
 *    permission of The OpenEJB Group.  For written permission,
 *    please contact openejb-group@openejb.sf.net.
 *
 * 4. Products derived from this Software may not be called "OpenEJB"
 *    nor may "OpenEJB" appear in their names without prior written
 *    permission of The OpenEJB Group. OpenEJB is a registered
 *    trademark of The OpenEJB Group.
 *
 * 5. Due credit should be given to the OpenEJB Project
 *    (http://openejb.sf.net/).
 *
 * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
 *
 * $Id: ClasspathUtils.java 1076 2004-03-01 07:17:26Z dblevins $
 */

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessController;
import java.security.PrivilegedAction;

/**
 */
public class ClasspathUtils{
    
    private static Loader tomcatLoader = new ClasspathUtils().new TomcatLoader();
    private static Loader sysLoader = new ClasspathUtils().new SystemLoader();
    private static Loader ctxLoader = new ClasspathUtils().new ContextLoader();
    
   

    public static void addJarToPath(final File jarthrows Exception {
        addJarToPathjar.toURI().toURL() );
    }       

    public static void addJarToPath(final URL jarthrows Exception {
        getLoader().addJarToPathjar );
    }       
    
     

    public static void addJarToPath(final File jar, String loaderNamethrows Exception {
        addJarToPathjar.toURI().toURL() , loaderName);
    }       

    public static void addJarToPath(final URL jar, String loaderNamethrows Exception {
        getLoader(loaderName).addJarToPathjar );
    }       
    
     

    public static void addJarsToPath(final File dirthrows Exception {
        if dir == null return;
        getLoader().addJarsToPathdir );
    }       
    
    

    public static void addJarsToPath(final File dir, String loaderNamethrows Exception {
        getLoader(loaderName).addJarsToPathdir );
    }       

   

    /**
     * Appends the jar to the classpath of the classloader passed in.
     *
     @param url the URL to be added to the search path of URLs
     */
    public static void addJarToSystemPath(final File jarthrows Exception {
        addJarToSystemPathjar.toURI().toURL() );
    }       

    /**
     * Appends the jar to the classpath of the classloader passed in.
     *
     @param url the URL to be added to the search path of URLs
     */
    public static void addJarToSystemPath(final URL jarthrows Exception {
    }       
    
    protected static Loader getLoader(){
        String name = getContextClassLoader().getClass().getName();

        if (name.startsWith("org.apache.catalina.loader")) {
            return tomcatLoader;
        else if (name.startsWith("org.apache.jasper.servlet")) {
            return tomcatLoader;
        else if (name.startsWith"sun.misc.Launcher" )) {
            return sysLoader;
        else {
            return ctxLoader;
        }
    }
    
    protected static Loader getLoader(String name){

        if (name.equalsIgnoreCase("tomcat")) {
            return tomcatLoader;
        else if (name.equalsIgnoreCase("bootstrap")) {
            return sysLoader;
        else if (name.equalsIgnoreCase("system")) {
            return sysLoader;
        else if (name.equalsIgnoreCase("thread")) {
            return ctxLoader;
        else if (name.equalsIgnoreCase("context")) {
            return ctxLoader;
        else {
            return ctxLoader;
        }
    }

    
    public static ClassLoader getContextClassLoader() {
        return (ClassLoaderjava.security.AccessController.doPrivileged(
            new java.security.PrivilegedAction() {
                public Object run() {
                    return Thread.currentThread().getContextClassLoader();
                }
            }
        );
    }

    public static void rebuildJavaClassPathVariable() throws Exception{

    }


    interface Loader {
        public void addJarsToPath(File dirthrows Exception;
        public void addJarToPath(URL dirthrows Exception;
    }
    
    class BasicURLLoader implements Loader{
        public void addJarsToPath(File dirthrows Exception {
        }
        
        public void addJarToPath(URL jarthrows Exception {
        }
    
        private java.lang.reflect.Field ucpField;
        
    
        protected void addJarToPath(final URL jar, final URLClassLoader loaderthrows Exception {
            this.getURLClassPath(loader).addURLjar );
        }
    
        protected void addJarsToPath(final File dir, final URLClassLoader loaderthrows Exception {
        //System.out.println("DIR "+dir);
        // Get the list of jars and zips
        String[] jarNames = dir.list(new java.io.FilenameFilter(){
            public boolean accept(File dir, String name) {
                //System.out.println("FILE "+name);
                return (name.endsWith(".jar"||name.endsWith(".zip"));
            }
        });

        // Create URLs from them
        final URL[] jars = new URL[jarNames.length];
        for (int j=0; j < jarNames.length; j++){
            jars[jnew Filedir, jarNames[j]).toURI().toURL();
            }
        
        sun.misc.URLClassPath path = getURLClassPath(loader);
        for (int i=0; i < jars.length; i++){
            //System.out.println("URL "+jars[i]);
            path.addURLjars[i] );
        }
    }

        protected sun.misc.URLClassPath getURLClassPath(URLClassLoader loaderthrows Exception{
        return (sun.misc.URLClassPath)getUcpField().get(loader);
    }

        private java.lang.reflect.Field getUcpField() throws Exception{
        if (ucpField == null) {
            // Add them to the URLClassLoader's classpath
            ucpField = (java.lang.reflect.Field)AccessController.doPrivileged(
                new PrivilegedAction(){
                    public Object run() { 
                        java.lang.reflect.Field ucp = null;
                        try{
                        ucp = URLClassLoader.class.getDeclaredField("ucp");
                        ucp.setAccessible(true);
                        catch (Exception e2){
                            e2.printStackTrace();
                        }
                        return ucp;
                    }
                }
            );
        }
        
        return ucpField;
    }

}

    /*-------------------------------------------------------*/
    /*    System ClassLoader Support                         */
    /*-------------------------------------------------------*/
    class SystemLoader extends BasicURLLoader{
        
        private URLClassLoader sysLoader;
    
        public void addJarsToPath(File dirthrows Exception {
            this.addJarsToPathdir , getSystemLoader() );
            this.rebuildJavaClassPathVariable();
        }
        
        public void addJarToPath(URL jarthrows Exception {
            //System.out.println("[|] SYSTEM "+jar.toExternalForm());
            this.addJarToPathjar, getSystemLoader() );
            this.rebuildJavaClassPathVariable();
        }
    
        private URLClassLoader getSystemLoader() throws Exception{
            if (sysLoader == null) {
                sysLoader = (java.net.URLClassLoader)ClassLoader.getSystemClassLoader();
            }
            return sysLoader;
        }
        
        private void rebuildJavaClassPathVariable() throws Exception{
            sun.misc.URLClassPath cp = getURLClassPath(getSystemLoader());
            URL[] urls = cp.getURLs();
            //for (int i=0; i < urls.length; i++){
            //    System.out.println(urls[i].toExternalForm());
            //}
            if (urls.length < 1return;
    
            StringBuffer path = new StringBuffer(urls.length*32);
            
            File s = new Fileurls[0].getFile() );
            path.appends.getPath() );
            //System.out.println(s.getPath());
    
            for (int i=1; i < urls.length; i++){
                path.appendFile.pathSeparator );
                
                s = new Fileurls[i].getFile() );
                //System.out.println(s.getPath());
                path.appends.getPath() );
            }
            try{
                System.setProperty("java.class.path", path.toString() );
            catch (Exception e){}
        }
    }
    
    /*-------------------------------------------------------*/
    /*    Thread Context ClassLoader Support                 */
    /*-------------------------------------------------------*/
    class ContextLoader extends BasicURLLoader{
        
        public void addJarsToPath(File dirthrows Exception {
            URLClassLoader loader = (URLClassLoader)ClasspathUtils.getContextClassLoader();
            this.addJarsToPathdir , loader );
        }
        
        public void addJarToPath(URL jarthrows Exception {
            URLClassLoader loader = (URLClassLoader)ClasspathUtils.getContextClassLoader();
            this.addJarToPathjar, loader );
        }
    }
    
    /*-------------------------------------------------------*/
    /*    Tomcat ClassLoader Support                         */
    /*-------------------------------------------------------*/
    class TomcatLoader extends BasicURLLoader{
    
        /**
         * The Tomcat Common ClassLoader
         */
        private ClassLoader tomcatLoader;
    
    
        /**
         * The addRepository(String jar) method of the Tomcat Common ClassLoader
         */
        private java.lang.reflect.Method addRepositoryMethod;
        
        public void addJarsToPath(File dirthrows Exception {
            String[] jarNames = dir.list(new java.io.FilenameFilter(){
                public boolean accept(File dir, String name) {
                    //System.out.println("FILE "+name);
                    return (name.endsWith(".jar"||name.endsWith(".zip"));
                }
            });
        
            for (int j=0; j < jarNames.length; j++){
                this.addJarToPathnew Filedir, jarNames[j]).toURL() );
            }
            rebuild();
        }
        
        public void addJarToPath(URL jarthrows Exception {
            //System.out.println("[|] TOMCAT "+jar.toExternalForm());
            this._addJarToPath(jar);
            rebuild();
        }

        public void _addJarToPath(URL jarthrows Exception {
            String path = jar.toExternalForm();
            //System.out.println("[] PATH "+path);
          //if (path.startsWith("file:/C")) {
          //    path = path.substring("file:/C".length());
          //    path = "file:C"+path;
          //}
            this.addRepositorypath );
          //ClassLoader cl = ClasspathUtils.getContextClassLoader();
          //cl = getCommonLoader(cl);
          //System.out.println("[] "+cl.getClass().getName());
          //System.out.println("[] "+cl);
          //
          ////Reloader loader = (Reloader)cl.getParent();
          //cl = cl.getParent();
          //java.lang.reflect.Method m = getAddRepositoryMethod( cl.getClass());        
          //m.invoke( cl, new Object[]{jar.toExternalForm()});
          ////loader.addRepository( jar.toExternalForm() );
        }
        
        public void addRepository(String paththrows Exception{
            this.getAddRepositoryMethod().invoke(getCommonLoader()new Object[]{path});        
        }
    
        private void rebuild(){

            try{
            sun.misc.URLClassPath cp = getURLClassPath((URLClassLoader)getCommonLoader());
            URL[] urls = cp.getURLs();
            //for (int i=0; i < urls.length; i++){
            //    System.out.println(urls[i].toExternalForm());
            //}
            if (urls.length < 1return;
            
            StringBuffer path = new StringBuffer(urls.length*32);
            
            File s = new Fileurls[0].getFile() );
            path.appends.getPath() );
            //System.out.println(s.getPath());
            
            for (int i=1; i < urls.length; i++){
                path.appendFile.pathSeparator );
            
                s = new Fileurls[i].getFile() );
                //System.out.println(s.getPath());
                path.appends.getPath() );
            }
            System.setProperty("java.class.path", path.toString() );
            catch (Exception e){}

        }
        private ClassLoader getCommonLoader(){
            if (tomcatLoader == null) {
                tomcatLoader = this.getCommonLoader(ClasspathUtils.getContextClassLoader()).getParent();
            }
            return tomcatLoader;
        }
    

        private ClassLoader getCommonLoader(ClassLoader loader){
            if (loader.getClass().getName().equals("org.apache.catalina.loader.StandardClassLoader")) {
                return loader;                
            else {
                return this.getCommonLoader(loader.getParent());
            }
        }
    
        /**
         * This method gets the Tomcat StandardClassLoader.addRepository method via
         * reflection.  This allows us to call the addRepository method for Tomcat
         * integration, but doesn't require us to include or ship any Tomcat 
         * libraries.
         
         @param clazz
         @return 
         @exception Exception
         */
        private java.lang.reflect.Method getAddRepositoryMethod() 
        throws Exception{
            if (addRepositoryMethod == null) {
                final Class clazz = getCommonLoader().getClass();
                this.addRepositoryMethod = (java.lang.reflect.Method)AccessController.doPrivileged(
                    new PrivilegedAction(){
                        public Object run() { 
                            java.lang.reflect.Method method = null;
                            try{
                                method = clazz.getDeclaredMethod("addRepository"
                                                                 new Class[]{String.class});
                                method.setAccessible(true);
                            catch (Exception e2){
                                e2.printStackTrace();
                            }
                            return method;
                        }
                    }
                );
            }
            
            return addRepositoryMethod;
        }
    }

}

   
  
Related examples in the same category
1. Find occurrencens of a classname in system classpath or the specified ClassLoader
2. Parses the CLASSPATH and returns the directories used within it.
3. Use reflection to access the true classpath info.
4. Responsible for loading (class) files from the CLASSPATH. Inspired by sun.tools.ClassPath.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.