dblevins    2005/07/09 04:51:00

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

  I can't fix this Tomcat integration issue with all the silly statics.  
Killing the statics.  Death to the statics!
  Added a class called SystemInstance, which is a singleton and hopefully the 
only important static for the 1.0 codebase someday.
  Changed FileUtils to get its instances from SystemInstance, then inlined all 
that code.
  Changed ClasspathUtils to get its Loader instance from SystemInstance and 
managed to inline all the remaining code.
  
  Revision  Changes    Path
  1.14      +28 -138   
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.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- ClasspathUtils.java       9 Jul 2005 05:53:21 -0000       1.13
  +++ ClasspathUtils.java       9 Jul 2005 08:51:00 -0000       1.14
  @@ -51,143 +51,32 @@
   import java.security.PrivilegedAction;
   import java.util.Hashtable;
   
  +import org.openejb.loader.SystemInstance;
  +
   /**
    * @author <a href="mailto:[EMAIL PROTECTED]">David Blevins </a>
    */
   public class ClasspathUtils {
   
  -    public static Loader tomcatLoader = new ClasspathUtils().new 
TomcatLoader();
  -
  -    public static Loader webappLoader = new ClasspathUtils().new 
WebAppLoader();
  -
  -    public static Loader sysLoader = new ClasspathUtils().new SystemLoader();
  -
  -    public static Loader ctxLoader = new ClasspathUtils().new 
ContextLoader();
  -
  -    public static void addJarToPath(String jar) throws Exception {
  -        addJarToPath(FileUtils.getHome().getFile(jar));
  -    }
  -
  -    public static void addJarToPath(final File jar) throws Exception {
  -        addJarToPath(jar.toURL());
  -    }
  -
  -    public static void addJarToPath(final URL jar) throws Exception {
  -        getLoader().addJarToPath(jar);
  -    }
  -
  -    public static void addJarToPath(String jar, String loaderName) throws 
Exception {
  -        addJarToPath(FileUtils.getHome().getFile(jar), loaderName);
  -    }
  -
  -    public static void addJarToPath(final File jar, String loaderName) 
throws Exception {
  -        addJarToPath(jar.toURL(), loaderName);
  -    }
  -
  -    public static void addJarToPath(final URL jar, String loaderName) throws 
Exception {
  -        getLoader(loaderName).addJarToPath(jar);
  -    }
  -
  -    /**
  -     * Used to shove all jar files in a directory into the classloader
  -     * This method resolves the directory relative to openejb.home and
  -     * instpects the name of the classloader to figure out which loader
  -     * to call.
  -     *
  -     * This method i used only by the deploy tool, validator, and testsuite
  -     * to add openejb.home/lib and openejb.home/dist to the classpath.
  -     *
  -     * @see #getLoader() 
  -     * @param dir
  -     * @throws Exception
  -     */
  -    public static void addJarsToPath(String dir) throws Exception {
  -        addJarsToPath(FileUtils.getHome().getDirectory(dir));
  -    }
  -
  -    public static void addJarsToPath(String dir, String loaderName, 
Hashtable env) throws Exception {
  -        File dirAtBase = FileUtils.getBase().getDirectory(dir);
  -        if (dirAtBase != null && dirAtBase.exists()) {
  -            addJarsToPath(dirAtBase, loaderName);
  -        }
  -        File dirAtHome = FileUtils.getHome().getDirectory(dir);
  -        if (! dirAtHome.equals(dirAtBase)) {
  -            addJarsToPath(dirAtHome, loaderName);
  -        }
  -    }
  -
  -    public static void addJarsToPath(final File dir) throws Exception {
  -        if (dir == null || !dir.exists())
  -            return;
  -        getLoader().addJarsToPath(dir);
  -    }
  -
  -    public static void addJarsToPath(String dir, String loaderName) throws 
Exception {
  -        addJarsToPath(dir, loaderName, System.getProperties());
  -    }
  -
  -    public static void addJarsToPath(final File dir, String loaderName) 
throws Exception {
  -        getLoader(loaderName).addJarsToPath(dir);
  -    }
  -
  -    /**
  -     * Appends the jar to the classpath of the classloader passed in.
  -     *
  -     * @param jar the URL to be added to the search path of URLs
  -     */
  -    public static void addJarToSystemPath(String jar) throws Exception {
  -        addJarToSystemPath(FileUtils.getHome().getFile(jar));
  -    }
  -
  -    /**
  -     * Appends the jar to the classpath of the classloader passed in.
  -     *
  -     * @param jar the URL to be added to the search path of URLs
  -     */
  -    public static void addJarToSystemPath(final File jar) throws Exception {
  -        addJarToSystemPath(jar.toURL());
  -    }
  -
  -    /**
  -     * Appends the jar to the classpath of the classloader passed in.
  -     *
  -     * @param jar the URL to be added to the search path of URLs
  -     */
  -    public static void addJarToSystemPath(final URL jar) throws Exception {
  -    }
  -
  -    protected static Loader getLoader() {
  -        String name = getContextClassLoader().getClass().getName();
  -
  -        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")) {
  -            return sysLoader;
  -        } else {
  -            return ctxLoader;
  -        }
  -    }
  -
  -    public static Loader getLoader(String name) {
  -
  -        if (name.equalsIgnoreCase("tomcat")) {
  -            return tomcatLoader;
  -        } else if (name.equalsIgnoreCase("tomcat-webapp")) {
  -            return webappLoader;
  -        } 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 class LoaderFactory {
  +        public static Loader createLoader(String name){
  +            if (name.equalsIgnoreCase("tomcat")) {
  +                return new TomcatLoader();
  +            } else if (name.equalsIgnoreCase("tomcat-common")) {
  +                return new TomcatLoader();
  +            } else if (name.equalsIgnoreCase("tomcat-webapp")) {
  +                return new WebAppLoader();
  +            } else if (name.equalsIgnoreCase("bootstrap")) {
  +                return new SystemLoader();
  +            } else if (name.equalsIgnoreCase("system")) {
  +                return new SystemLoader();
  +            } else if (name.equalsIgnoreCase("thread")) {
  +                return new ContextLoader();
  +            } else if (name.equalsIgnoreCase("context")) {
  +                return new ContextLoader();
  +            } else {
  +                return new ContextLoader();
  +            }
           }
       }
   
  @@ -197,7 +86,7 @@
           public void addJarToPath(URL dir) throws Exception;
       }
   
  -    public class BasicURLLoader implements Loader {
  +    public static class BasicURLLoader implements Loader {
           public void addJarsToPath(File dir) throws Exception {
           }
   
  @@ -211,6 +100,7 @@
           }
   
           protected void addJarsToPath(final File dir, final URLClassLoader 
loader) throws Exception {
  +            if (dir == null || !dir.exists()) return;
               //System.out.println("DIR "+dir);
               // Get the list of jars and zips
               String[] jarNames = dir.list(new java.io.FilenameFilter() {
  @@ -262,7 +152,7 @@
       /*-------------------------------------------------------*/
       /* System ClassLoader Support */
       /*-------------------------------------------------------*/
  -    public class SystemLoader extends BasicURLLoader {
  +    public static class SystemLoader extends BasicURLLoader {
   
           private URLClassLoader sysLoader;
   
  @@ -316,7 +206,7 @@
       /*-------------------------------------------------------*/
       /* Thread Context ClassLoader Support */
       /*-------------------------------------------------------*/
  -    public class ContextLoader extends BasicURLLoader {
  +    public static class ContextLoader extends BasicURLLoader {
   
           public void addJarsToPath(File dir) throws Exception {
               ClassLoader contextClassLoader = 
ClasspathUtils.getContextClassLoader();
  @@ -338,7 +228,7 @@
       /*-------------------------------------------------------*/
       /* Tomcat ClassLoader Support */
       /*-------------------------------------------------------*/
  -    public class TomcatLoader extends BasicURLLoader {
  +    public static class TomcatLoader extends BasicURLLoader {
   
           /**
            * The Tomcat Common ClassLoader
  @@ -478,7 +368,7 @@
           }
       }
   
  -    public class WebAppLoader extends TomcatLoader {
  +    public static class WebAppLoader extends TomcatLoader {
           ClassLoader webappLoader;
   
           protected ClassLoader getCommonLoader() {
  
  
  
  1.7       +3 -124    
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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- FileUtils.java    9 Jul 2005 05:53:21 -0000       1.6
  +++ FileUtils.java    9 Jul 2005 08:51:00 -0000       1.7
  @@ -55,26 +55,19 @@
   import java.util.Properties;
   
   import org.openejb.OpenEJBException;
  +import org.openejb.loader.SystemInstance;
   
   public class FileUtils {
   
       private static final java.util.Random _random = new java.util.Random();
   
  -    private static FileUtils openejbHomeUtils = new 
FileUtils("openejb.home", "user.dir");
  -    private static FileUtils openejbBaseUtils = new 
FileUtils("openejb.base", "openejb.home");
  -
  -    public static void init(Properties properties){
  -        openejbHomeUtils = new FileUtils("openejb.home", "user.dir", 
properties);
  -        openejbBaseUtils = new FileUtils("openejb.base", "openejb.home", 
properties);
  -    }
  -
       private File home;
   
       private FileUtils(String homeDir, String defaultDir) {
           this(homeDir, defaultDir, System.getProperties());
       }
   
  -    private FileUtils(String homeDir, String defaultDir, Hashtable env) {
  +    public FileUtils(String homeDir, String defaultDir, Hashtable env) {
           String homePath = null;
           try {
               homePath = (String) env.get(homeDir);
  @@ -98,14 +91,6 @@
           }
       }
   
  -    public static FileUtils getBase() {
  -        return openejbBaseUtils;
  -    }
  -    
  -    public static FileUtils getHome() {
  -        return openejbHomeUtils;
  -    }
  -
       /**
         * @see #getDirectory(String, boolean)
         */
  @@ -261,110 +246,4 @@
           }
       }
       
  -    public static String getAbsolutePath(String path, String secondaryPath, 
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().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().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().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().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().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.3       +6 -4      
openejb1/modules/core/src/java/org/openejb/util/Launcher.java
  
  Index: Launcher.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb1/modules/core/src/java/org/openejb/util/Launcher.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Launcher.java     16 Jun 2005 22:29:53 -0000      1.2
  +++ Launcher.java     9 Jul 2005 08:51:00 -0000       1.3
  @@ -58,6 +58,8 @@
   import java.util.Properties;
   import java.util.Set;
   
  +import org.openejb.loader.SystemInstance;
  +
   /**
    * Helper class to dynamically create the full classpath and launch an 
OpenEJB application
    * 
  @@ -78,8 +80,8 @@
           }
           
           StringBuffer classpath = new StringBuffer();
  -        
  -        FileUtils home = FileUtils.getHome();
  +
  +        FileUtils home = SystemInstance.get().getHome();
           
           //TODO make it configurable what directories to include in classpath
           try {
  
  
  
  1.10      +119 -5    
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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- Logger.java       9 Jul 2005 05:53:21 -0000       1.9
  +++ Logger.java       9 Jul 2005 08:51:00 -0000       1.10
  @@ -47,12 +47,18 @@
   import java.io.File;
   import java.io.FileInputStream;
   import java.io.IOException;
  +import java.io.FileNotFoundException;
  +import java.io.InputStream;
  +import java.io.FileOutputStream;
   import java.util.HashMap;
   import java.util.Properties;
  +import java.net.URL;
   
   import org.apache.log4j.Category;
   import org.apache.log4j.Level;
   import org.apache.log4j.PropertyConfigurator;
  +import org.openejb.loader.SystemInstance;
  +import org.openejb.OpenEJBException;
   
   
   /**
  @@ -1611,7 +1617,7 @@
               }
               try{
                   // resolve the config file location
  -                config = FileUtils.getAbsolutePath(config, 
"conf/default.logging.conf", false);
  +                config = getAbsolutePath(config, 
"conf/default.logging.conf", false);
   
                   // load the config
                   Properties log4jProps = loadProperties(config);
  @@ -1649,9 +1655,9 @@
                   if (name.endsWith(".File")) {
                       String path = log4jProps.getProperty(name);
                       try {
  -                        File file = FileUtils.getBase().getFile(path, false);
  +                        File file = 
SystemInstance.get().getBase().getFile(path, false);
                           if (!file.getParentFile().exists()) {
  -                            file = FileUtils.getHome().getFile(path, false);
  +                            file = 
SystemInstance.get().getHome().getFile(path, false);
                           }
                           path = file.getPath();
                       } catch (IOException ignored) {
  @@ -1663,5 +1669,113 @@
               }
               return log4jProps;
           }
  +
  +        public String getAbsolutePath(String path, String secondaryPath, 
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 = SystemInstance.get().getBase().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 = SystemInstance.get().getHome().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 = 
SystemInstance.get().getBase().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 = 
SystemInstance.get().getHome().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 = 
SystemInstance.get().getBase().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;
  +        }
  +
       }
   }
  
  
  

Reply via email to