jlaskowski    2004/07/05 11:47:46

  Modified:    modules/core/src/java/org/openejb/util ClasspathUtils.java
                        FileUtils.java Logger.java
  Log:

  Further enhancements to make OpenEJB instances to not interfere with each other when 
embedded in separate webapps
  
  Revision  Changes    Path
  1.4       +242 -227  
openejb1/modules/core/src/java/org/openejb/util/ClasspathUtils.java
  
  Index: ClasspathUtils.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb1/modules/core/src/java/org/openejb/util/ClasspathUtils.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ClasspathUtils.java       25 May 2004 07:43:11 -0000      1.3
  +++ ClasspathUtils.java       5 Jul 2004 15:47:46 -0000       1.4
  @@ -49,104 +49,122 @@
   import java.net.URLClassLoader;
   import java.security.AccessController;
   import java.security.PrivilegedAction;
  +import java.util.Hashtable;
   
   /**
  - * @author <a href="mailto:[EMAIL PROTECTED]">David Blevins</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]">David Blevins </a>
    */
  -public class ClasspathUtils{
  -    
  +public class ClasspathUtils {
  +
       private static Loader tomcatLoader = new ClasspathUtils().new TomcatLoader();
  +
       private static Loader webappLoader = new ClasspathUtils().new WebAppLoader();
  +
       private static Loader sysLoader = new ClasspathUtils().new SystemLoader();
  +
       private static Loader ctxLoader = new ClasspathUtils().new ContextLoader();
  -    
  +
       public static void addJarToPath(String jar) throws Exception {
  -        addJarToPath( FileUtils.getHome().getFile(jar) );
  -    }       
  +        addJarToPath(FileUtils.getHome().getFile(jar));
  +    }
   
       public static void addJarToPath(final File jar) throws Exception {
  -        addJarToPath( jar.toURL() );
  -    }       
  +        addJarToPath(jar.toURL());
  +    }
   
       public static void addJarToPath(final URL jar) throws Exception {
  -        getLoader().addJarToPath( jar );
  -    }       
  -    
  +        getLoader().addJarToPath(jar);
  +    }
  +
       public static void addJarToPath(String jar, String loaderName) throws Exception 
{
  -        addJarToPath( FileUtils.getHome().getFile(jar), loaderName );
  -    }       
  +        addJarToPath(FileUtils.getHome().getFile(jar), loaderName);
  +    }
   
       public static void addJarToPath(final File jar, String loaderName) throws 
Exception {
  -        addJarToPath( jar.toURL() , loaderName);
  -    }       
  +        addJarToPath(jar.toURL(), loaderName);
  +    }
   
       public static void addJarToPath(final URL jar, String loaderName) throws 
Exception {
  -        getLoader(loaderName).addJarToPath( jar );
  -    }       
  -    
  +        getLoader(loaderName).addJarToPath(jar);
  +    }
  +
       public static void addJarsToPath(String dir) throws Exception {
  -        addJarsToPath( FileUtils.getHome().getDirectory(dir) );
  -    }       
  +        addJarsToPath(FileUtils.getHome().getDirectory(dir));
  +    }
  +
  +    public static void addJarsToPath(String dir, String loaderName, Hashtable env) 
throws Exception {
  +        File dirAtHome = FileUtils.getBase(env).getDirectory(dir);
  +        if (dirAtHome != null && dirAtHome.exists()) {
  +            addJarsToPath(dirAtHome, loaderName);
  +        } else {
  +            addJarsToPath(FileUtils.getHome(env).getDirectory(dir), loaderName);
  +        }
  +    }
   
       public static void addJarsToPath(final File dir) throws Exception {
  -        if ( dir == null ) return;
  -        getLoader().addJarsToPath( dir );
  -    }       
  -    
  +        if (dir == null)
  +            return;
  +        getLoader().addJarsToPath(dir);
  +    }
  +
       public static void addJarsToPath(String dir, String loaderName) throws 
Exception {
  -        addJarsToPath( FileUtils.getHome().getDirectory(dir), loaderName );
  -    }       
  +        addJarsToPath(dir, loaderName, System.getProperties());
  +    }
   
       public static void addJarsToPath(final File dir, String loaderName) throws 
Exception {
  -        getLoader(loaderName).addJarsToPath( dir );
  -    }       
  +        getLoader(loaderName).addJarsToPath(dir);
  +    }
   
  -    
       /**
        * Appends the jar to the classpath of the classloader passed in.
  -     *
  -     * @param url the URL to be added to the search path of URLs
  +     * 
  +     * @param url
  +     *            the URL to be added to the search path of URLs
        */
       public static void addJarToSystemPath(String jar) throws Exception {
  -        addJarToSystemPath( FileUtils.getHome().getFile(jar) );
  -    }       
  +        addJarToSystemPath(FileUtils.getHome().getFile(jar));
  +    }
   
       /**
        * Appends the jar to the classpath of the classloader passed in.
  -     *
  -     * @param url the URL to be added to the search path of URLs
  +     * 
  +     * @param url
  +     *            the URL to be added to the search path of URLs
        */
       public static void addJarToSystemPath(final File jar) throws Exception {
  -        addJarToSystemPath( jar.toURL() );
  -    }       
  +        addJarToSystemPath(jar.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
  +     * 
  +     * @param url
  +     *            the URL to be added to the search path of URLs
        */
       public static void addJarToSystemPath(final URL jar) throws Exception {
  -    }       
  -    
  -    protected static Loader getLoader(){
  +    }
  +
  +    protected static Loader getLoader() {
           String name = getContextClassLoader().getClass().getName();
   
  -        if (name.startsWith("org.apache.catalina.loader")) {
  +        if (name.equals("org.apache.catalina.loader.WebappClassLoader")) {
  +            return webappLoader;
  +     } else 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" )) {
  +        } else if (name.startsWith("sun.misc.Launcher")) {
               return sysLoader;
           } else {
               return ctxLoader;
           }
       }
  -    
  -    protected static Loader getLoader(String name){
  +
  +    protected static Loader getLoader(String name) {
   
           if (name.equalsIgnoreCase("tomcat")) {
               return tomcatLoader;
  -        }else if (name.equalsIgnoreCase("tomcat-webapp")) {
  +        } else if (name.equalsIgnoreCase("tomcat-webapp")) {
               return webappLoader;
           } else if (name.equalsIgnoreCase("bootstrap")) {
               return sysLoader;
  @@ -161,196 +179,195 @@
           }
       }
   
  -    
       public static ClassLoader getContextClassLoader() {
  -        return (ClassLoader) java.security.AccessController.doPrivileged(
  -            new java.security.PrivilegedAction() {
  -                public Object run() {
  -                    return Thread.currentThread().getContextClassLoader();
  -                }
  +        return (ClassLoader) java.security.AccessController.doPrivileged(new 
java.security.PrivilegedAction() {
  +            public Object run() {
  +                return Thread.currentThread().getContextClassLoader();
               }
  -        );
  +        });
       }
   
  -    public static void rebuildJavaClassPathVariable() throws Exception{
  +    public static void rebuildJavaClassPathVariable() throws Exception {
   
       }
   
  -
       interface Loader {
           public void addJarsToPath(File dir) throws Exception;
  +
           public void addJarToPath(URL dir) throws Exception;
       }
  -    
  -    class BasicURLLoader implements Loader{
  +
  +    class BasicURLLoader implements Loader {
           public void addJarsToPath(File dir) throws Exception {
           }
  -        
  +
           public void addJarToPath(URL jar) throws Exception {
           }
  -    
  +
           private java.lang.reflect.Field ucpField;
  -        
  -    
  +
           protected void addJarToPath(final URL jar, final URLClassLoader loader) 
throws Exception {
  -            this.getURLClassPath(loader).addURL( jar );
  +            this.getURLClassPath(loader).addURL(jar);
           }
  -    
  +
           protected void addJarsToPath(final File dir, final URLClassLoader loader) 
throws 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"));
  +            //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[j] = new File(dir, jarNames[j]).toURL();
               }
  -        });
   
  -        // Create URLs from them
  -        final URL[] jars = new URL[jarNames.length];
  -        for (int j=0; j < jarNames.length; j++){
  -            jars[j] = new File( dir, jarNames[j]).toURL();
  +            sun.misc.URLClassPath path = getURLClassPath(loader);
  +            for (int i = 0; i < jars.length; i++) {
  +                //System.out.println("URL "+jars[i]);
  +                path.addURL(jars[i]);
               }
  -        
  -        sun.misc.URLClassPath path = getURLClassPath(loader);
  -        for (int i=0; i < jars.length; i++){
  -            //System.out.println("URL "+jars[i]);
  -            path.addURL( jars[i] );
           }
  -    }
   
  -        protected sun.misc.URLClassPath getURLClassPath(URLClassLoader loader) 
throws Exception{
  -        return (sun.misc.URLClassPath)getUcpField().get(loader);
  -    }
  +        protected sun.misc.URLClassPath getURLClassPath(URLClassLoader loader) 
throws 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() { 
  +        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){
  +                        try {
  +                            ucp = URLClassLoader.class.getDeclaredField("ucp");
  +                            ucp.setAccessible(true);
  +                        } catch (Exception e2) {
                               e2.printStackTrace();
                           }
                           return ucp;
                       }
  -                }
  -            );
  +                });
  +            }
  +
  +            return ucpField;
           }
  -        
  -        return ucpField;
  -    }
   
  -}
  +    }
   
       /*-------------------------------------------------------*/
  -    /*    System ClassLoader Support                         */
  +    /* System ClassLoader Support */
       /*-------------------------------------------------------*/
  -    class SystemLoader extends BasicURLLoader{
  -        
  +    class SystemLoader extends BasicURLLoader {
  +
           private URLClassLoader sysLoader;
  -    
  +
           public void addJarsToPath(File dir) throws Exception {
  -            this.addJarsToPath( dir , getSystemLoader() );
  +            this.addJarsToPath(dir, getSystemLoader());
               this.rebuildJavaClassPathVariable();
           }
  -        
  +
           public void addJarToPath(URL jar) throws Exception {
               //System.out.println("[|] SYSTEM "+jar.toExternalForm());
  -            this.addJarToPath( jar, getSystemLoader() );
  +            this.addJarToPath(jar, getSystemLoader());
               this.rebuildJavaClassPathVariable();
           }
  -    
  -        private URLClassLoader getSystemLoader() throws Exception{
  +
  +        private URLClassLoader getSystemLoader() throws Exception {
               if (sysLoader == null) {
  -                sysLoader = 
(java.net.URLClassLoader)ClassLoader.getSystemClassLoader();
  +                sysLoader = (java.net.URLClassLoader) 
ClassLoader.getSystemClassLoader();
               }
               return sysLoader;
           }
  -        
  -        private void rebuildJavaClassPathVariable() throws Exception{
  +
  +        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 < 1) return;
  -    
  -            StringBuffer path = new StringBuffer(urls.length*32);
  -            
  -            File s = new File( urls[0].getFile() );
  -            path.append( s.getPath() );
  +            if (urls.length < 1)
  +                return;
  +
  +            StringBuffer path = new StringBuffer(urls.length * 32);
  +
  +            File s = new File(urls[0].getFile());
  +            path.append(s.getPath());
               //System.out.println(s.getPath());
  -    
  -            for (int i=1; i < urls.length; i++){
  -                path.append( File.pathSeparator );
  -                
  -                s = new File( urls[i].getFile() );
  +
  +            for (int i = 1; i < urls.length; i++) {
  +                path.append(File.pathSeparator);
  +
  +                s = new File(urls[i].getFile());
                   //System.out.println(s.getPath());
  -                path.append( s.getPath() );
  +                path.append(s.getPath());
  +            }
  +            try {
  +                System.setProperty("java.class.path", path.toString());
  +            } catch (Exception e) {
               }
  -            try{
  -                System.setProperty("java.class.path", path.toString() );
  -            } catch (Exception e){}
           }
       }
  -    
  +
       /*-------------------------------------------------------*/
  -    /*    Thread Context ClassLoader Support                 */
  +    /* Thread Context ClassLoader Support */
       /*-------------------------------------------------------*/
  -    class ContextLoader extends BasicURLLoader{
  -        
  +    class ContextLoader extends BasicURLLoader {
  +
           public void addJarsToPath(File dir) throws Exception {
               ClassLoader contextClassLoader = ClasspathUtils.getContextClassLoader();
  -            if (contextClassLoader instanceof URLClassLoader){
  -                URLClassLoader loader = (URLClassLoader)contextClassLoader;
  -                this.addJarsToPath( dir , loader );
  +            if (contextClassLoader instanceof URLClassLoader) {
  +                URLClassLoader loader = (URLClassLoader) contextClassLoader;
  +                this.addJarsToPath(dir, loader);
               }
           }
  -        
  +
           public void addJarToPath(URL jar) throws Exception {
               ClassLoader contextClassLoader = ClasspathUtils.getContextClassLoader();
  -            if (contextClassLoader instanceof URLClassLoader){
  -                URLClassLoader loader = (URLClassLoader)contextClassLoader;
  -                this.addJarToPath( jar, loader );
  +            if (contextClassLoader instanceof URLClassLoader) {
  +                URLClassLoader loader = (URLClassLoader) contextClassLoader;
  +                this.addJarToPath(jar, loader);
               }
           }
       }
  -    
  +
       /*-------------------------------------------------------*/
  -    /*    Tomcat ClassLoader Support                         */
  +    /* Tomcat ClassLoader Support */
       /*-------------------------------------------------------*/
  -    class TomcatLoader extends BasicURLLoader{
  -    
  +    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 dir) throws Exception {
  -            String[] jarNames = dir.list(new java.io.FilenameFilter(){
  +            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"));
  +                    return (name.endsWith(".jar") || name.endsWith(".zip"));
                   }
               });
  -        
  -            for (int j=0; j < jarNames.length; j++){
  -                this.addJarToPath( new File( dir, jarNames[j]).toURL() );
  +
  +            if (jarNames == null) {
  +                return;
  +            }
  +
  +            for (int j = 0; j < jarNames.length; j++) {
  +                this.addJarToPath(new File(dir, jarNames[j]).toURL());
               }
               rebuild();
           }
  -        
  +
           public void addJarToPath(URL jar) throws Exception {
               //System.out.println("[|] TOMCAT "+jar.toExternalForm());
               this._addJarToPath(jar);
  @@ -360,110 +377,109 @@
           public void _addJarToPath(URL jar) throws 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.addRepository( path );
  -          //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 path) throws 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 (path.startsWith("file:/C")) {
  +            //    path = path.substring("file:/C".length());
  +            //    path = "file:C"+path;
               //}
  -            if (urls.length < 1) return;
  -            
  -            StringBuffer path = new StringBuffer(urls.length*32);
  -            
  -            File s = new File( urls[0].getFile() );
  -            path.append( s.getPath() );
  -            //System.out.println(s.getPath());
  -            
  -            for (int i=1; i < urls.length; i++){
  -                path.append( File.pathSeparator );
  -            
  -                s = new File( urls[i].getFile() );
  +            this.addRepository(path);
  +            //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 path) throws 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 < 1)
  +                    return;
  +
  +                StringBuffer path = new StringBuffer(urls.length * 32);
  +
  +                File s = new File(urls[0].getFile());
  +                path.append(s.getPath());
                   //System.out.println(s.getPath());
  -                path.append( s.getPath() );
  +
  +                for (int i = 1; i < urls.length; i++) {
  +                    path.append(File.pathSeparator);
  +
  +                    s = new File(urls[i].getFile());
  +                    //System.out.println(s.getPath());
  +                    path.append(s.getPath());
  +                }
  +                System.setProperty("java.class.path", path.toString());
  +            } catch (Exception e) {
               }
  -            System.setProperty("java.class.path", path.toString() );
  -            } catch (Exception e){}
   
           }
  -        
  -        protected ClassLoader getCommonLoader(){
  +
  +        protected ClassLoader getCommonLoader() {
               if (tomcatLoader == null) {
                   tomcatLoader = 
this.getCommonLoader(ClasspathUtils.getContextClassLoader()).getParent();
               }
               return tomcatLoader;
           }
  -    
   
  -        private ClassLoader getCommonLoader(ClassLoader loader){
  +        private ClassLoader getCommonLoader(ClassLoader loader) {
               if 
(loader.getClass().getName().equals("org.apache.catalina.loader.StandardClassLoader")) 
{
  -                return loader;                
  +                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.
  +         * 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
  +         * @return @exception
  +         *         Exception
            */
  -        private java.lang.reflect.Method getAddRepositoryMethod() 
  -        throws 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();
  +                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 method;
  -                        }
  -                    }
  -                );
  +                        });
               }
  -            
  +
               return addRepositoryMethod;
           }
       }
   
       class WebAppLoader extends TomcatLoader {
           ClassLoader webappLoader;
  -        
  -        protected ClassLoader getCommonLoader(){
  +
  +        protected ClassLoader getCommonLoader() {
               if (webappLoader == null) {
                   webappLoader = ClasspathUtils.getContextClassLoader();
               }
  @@ -471,5 +487,4 @@
           }
       }
   }
  -
   
  
  
  
  1.3       +134 -8    openejb1/modules/core/src/java/org/openejb/util/FileUtils.java
  
  Index: FileUtils.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb1/modules/core/src/java/org/openejb/util/FileUtils.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- FileUtils.java    25 May 2004 07:43:11 -0000      1.2
  +++ FileUtils.java    5 Jul 2004 15:47:46 -0000       1.3
  @@ -49,6 +49,12 @@
   import java.io.FileNotFoundException;
   import java.io.FileOutputStream;
   import java.io.IOException;
  +import java.io.InputStream;
  +import java.net.URL;
  +import java.util.Hashtable;
  +import java.util.Properties;
  +
  +import org.openejb.OpenEJBException;
   
   public class FileUtils {
   
  @@ -60,19 +66,24 @@
       private File home;
   
       private FileUtils(String homeDir, String defaultDir) {
  +        this(homeDir, defaultDir, System.getProperties());
  +    }
  +
  +    private FileUtils(String homeDir, String defaultDir, Hashtable env) {
           String homePath = null;
           try {
  -            homePath = (String) System.getProperty(homeDir);
  +            homePath = (String) env.get(homeDir);
               if (homePath == null) {
  -                homePath = System.getProperty(defaultDir);
  -                System.setProperty(homeDir, homePath);
  +                homePath = (String) env.get(defaultDir);
               }
  -
  +            
  +            if (homePath == null) {
  +                homePath = (String) env.get("user.dir");
  +            }
  +                
               home = new File(homePath);
  -
               if (!home.exists() || (home.exists() && !home.isDirectory())) {
  -                homePath = System.getProperty("user.dir");
  -                System.setProperty(homeDir, homePath);
  +                homePath = (String) env.get("user.dir");
                   home = new File(homePath);
               }
   
  @@ -85,11 +96,19 @@
       public static FileUtils getBase() {
           return openejbBaseUtils;
       }
  +    
  +    public static FileUtils getBase(Hashtable env) {
  +        return new FileUtils("openejb.base", "openejb.home", env);
  +    }
   
       public static FileUtils getHome() {
           return openejbHomeUtils;
       }
   
  +    public static FileUtils getHome(Hashtable env) {
  +        return new FileUtils("openejb.home", "user.dir", env);
  +    }
  +
       /**
         * @see #getDirectory(String, boolean)
         */
  @@ -243,5 +262,112 @@
           if (deleteSourceFile) {
               source.delete();
           }
  +    }
  +    
  +    public static String getAbsolutePath(String path, String secondaryPath, 
Properties props, boolean create)
  +            throws OpenEJBException {
  +        File file = null;
  +
  +        if (path != null) {
  +            /*
  +             * [1] Try finding the file relative to the current working
  +             * directory
  +             */
  +            file = new File(path);
  +            if (file != null && file.exists() && file.isFile()) {
  +                return file.getAbsolutePath();
  +            }
  +
  +            /*
  +             * [2] Try finding the file relative to the openejb.base directory
  +             */
  +            try {
  +                file = FileUtils.getBase(props).getFile(path);
  +                if (file != null && file.exists() && file.isFile()) {
  +                    return file.getAbsolutePath();
  +                }
  +            } catch (FileNotFoundException ignored) {
  +            } catch (IOException ignored) {
  +            }
  +
  +            /*
  +             * [3] Try finding the file relative to the openejb.home directory
  +             */
  +            try {
  +                file = FileUtils.getHome(props).getFile(path);
  +                if (file != null && file.exists() && file.isFile()) {
  +                    return file.getAbsolutePath();
  +                }
  +            } catch (FileNotFoundException ignored) {
  +            } catch (IOException ignored) {
  +            }
  +
  +        }
  +
  +        try {
  +            /*
  +             * [4] Try finding the secondaryPath file relative to the
  +             * openejb.base directory
  +             */
  +            try {
  +                file = FileUtils.getBase(props).getFile(secondaryPath);
  +                if (file != null && file.exists() && file.isFile()) {
  +                    return file.getAbsolutePath();
  +                }
  +            } catch (java.io.FileNotFoundException ignored) {
  +            }
  +
  +            /*
  +             * [5] Try finding the secondaryPath file relative to the
  +             * openejb.home directory
  +             */
  +            try {
  +                file = FileUtils.getHome(props).getFile(secondaryPath);
  +                if (file != null && file.exists() && file.isFile()) {
  +                    return file.getAbsolutePath();
  +                }
  +            } catch (java.io.FileNotFoundException ignored) {
  +            }
  +
  +            // Nothing found. Create if asked.
  +            //
  +            // TODO:1: We cannot find the user's conf file and
  +            // are taking the liberty of creating one for them.
  +            // We should log this.                   
  +            if (create)
  +            {
  +                File confDir = FileUtils.getBase(props).getDirectory("conf", true);
  +                
  +                file = createConfig(new File(confDir, secondaryPath));
  +            }
  +        } catch (java.io.IOException e) {
  +            e.printStackTrace();
  +            throw new OpenEJBException("Could not locate config file: ", e);
  +        }
  +
  +        return (file == null) ? null : file.getAbsolutePath();
  +    }
  +    
  +    private static File createConfig(File file) throws java.io.IOException{
  +        try{
  +            URL defaultConfig = new URL("resource:/" + file.getName());
  +            InputStream in = defaultConfig.openStream();
  +            FileOutputStream out = new FileOutputStream(file);
  +
  +            int b = in.read();
  +
  +            while (b != -1) {
  +                out.write(b);
  +                b = in.read();
  +            }
  +
  +            in.close();
  +            out.close();
  +
  +        } catch (Exception e){
  +            e.printStackTrace();
  +        }
  +
  +        return file;
       }
   }
  
  
  
  1.2       +21 -120   openejb1/modules/core/src/java/org/openejb/util/Logger.java
  
  Index: Logger.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb1/modules/core/src/java/org/openejb/util/Logger.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Logger.java       26 Mar 2004 21:43:02 -0000      1.1
  +++ Logger.java       5 Jul 2004 15:47:46 -0000       1.2
  @@ -44,18 +44,13 @@
    */
   package org.openejb.util;
   
  -import java.io.File;
   import java.io.FileInputStream;
  -import java.io.FileOutputStream;
  -import java.io.InputStream;
  -import java.net.URL;
   import java.util.HashMap;
   import java.util.Properties;
   
   import org.apache.log4j.Category;
   import org.apache.log4j.Level;
   import org.apache.log4j.PropertyConfigurator;
  -import org.openejb.OpenEJBException;
   
   
   /**
  @@ -73,17 +68,18 @@
    */
   public class Logger {
   
  -    static {
  -        Log4jConfigUtils log4j = new Logger.Log4jConfigUtils();
  -
  -        log4j.configure();
  -
  -    }
  -
       static protected HashMap _loggers = new HashMap();
       protected Category       _logger  = null;
       public    I18N           i18n     = null;
  +    
  +    private static Properties props;
  +    
  +    public static void initialize(Properties props)
  +    {
  +        Log4jConfigUtils log4j = new Logger.Log4jConfigUtils(props);
   
  +        log4j.configure();
  +    }
   
       /**
        * Returns a shared instance of Logger.
  @@ -1598,19 +1594,23 @@
   
       static class Log4jConfigUtils {
   
  +        Properties props;
  +        
  +        public Log4jConfigUtils(Properties props)
  +        {
  +            this.props = props;
  +        }
  +        
           public void configure(){
  -            String config = System.getProperty( "log4j.configuration" );
  +            String config = props.getProperty( "log4j.configuration" );
               try{
                   // resolve the config file location
  -                config = searchForConfiguration(config);
  +                config = FileUtils.getAbsolutePath(config, "default.logging.conf", 
props, false);
   
                   // load the config
  -                Properties props = loadProperties(config);
  +                Properties log4jProps = loadProperties(config);
   
  -                // filter the config
  -                props = filterProperties(props);
  -
  -                PropertyConfigurator.configure(props);
  +                PropertyConfigurator.configure(filterProperties(log4jProps));
               } catch (Exception e){
                   System.err.println("Failed to configure log4j. "+e.getMessage());
               }
  @@ -1641,104 +1641,5 @@
               }
               return props;
           }
  -        /**
  -         * Search for the config file.
  -         * 
  -         * OPENJB_HOME/conf/logging.conf
  -         * OPENJB_HOME/conf/default.logging.conf
  -         * 
  -         * @return 
  -         */
  -        public String searchForConfiguration() throws Exception{
  -            return searchForConfiguration(null);
  -        }
  -
  -        public String searchForConfiguration(String path) throws Exception{
  -            File file = null;
  -            try{
  -
  -                /* [1] Try finding the file relative to the 
  -                 *     current working directory
  -                 */
  -                try{
  -                    file = new File(path);
  -                    if (file != null && file.exists() && file.isFile()) {
  -                        return file.getAbsolutePath();
  -                    }
  -                } catch (NullPointerException e){
  -                }
  -
  -                /* [2] Try finding the file relative to the 
  -                 *     openejb.home directory
  -                 */
  -                try{
  -                    file = FileUtils.getBase().getFile(path);
  -                    if (file != null && file.exists() && file.isFile()) {
  -                        return file.getAbsolutePath();
  -                    }
  -                } catch (NullPointerException e){
  -                } catch (java.io.FileNotFoundException e){
  -                    System.err.println("Cannot find the logging configuration file 
["+path+"], Using default OPENEJB_HOME/conf/logging.conf instead.");
  -                }
  -
  -                /* [3] Try finding the standard logging.conf file 
  -                 *     relative to the openejb.home directory
  -                 */
  -                try{
  -                    file = FileUtils.getBase().getFile("conf/logging.conf");
  -                    if (file != null && file.exists() && file.isFile()) {
  -                        return file.getAbsolutePath();
  -                    }
  -                } catch (java.io.FileNotFoundException e){
  -                }
  -
  -                /* [4] No config found! Create a config for them
  -                 *     using the default.logging.conf file from 
  -                 *     the openejb-x.x.x.jar
  -                 */
  -                //Gets the conf directory, creating it if needed.
  -                File confDir = FileUtils.getBase().getDirectory("conf", true);
  -
  -                //TODO:1: We cannot find the user's conf file and
  -                // are taking the liberty of creating one for them.
  -                // We should log this.                   
  -                file = createConfig(new File(confDir, "logging.conf"));
  -
  -            } catch (java.io.IOException e){
  -                //e.printStackTrace();
  -                throw new OpenEJBException("Could not locate config file: ", e);
  -            }
  -
  -            /*TODO:2: Check these too.
  -            * OPENJB_HOME/lib/openejb-x.x.x.jar
  -            * OPENJB_HOME/dist/openejb-x.x.x.jar
  -            */
  -            //return (file == null)? null: 
file.getAbsoluteFile().toURL().toExternalForm();
  -            return (file == null)? null: file.getAbsolutePath();
  -        }
  -
  -        public File createConfig(File config) throws java.io.IOException{
  -            try{
  -                URL defaultConfig = new URL("resource:/default.logging.conf");
  -                InputStream in = defaultConfig.openStream();
  -                FileOutputStream out = new FileOutputStream(config);
  -
  -                int b = in.read();
  -
  -                while (b != -1) {
  -                    out.write(b);
  -                    b = in.read();
  -                }
  -
  -                in.close();
  -                out.close();
  -
  -            } catch (Exception e){
  -                e.printStackTrace();
  -            }
  -
  -            return config;
  -        }
  -
       }
   }
  
  
  

Reply via email to