Title: [2530] trunk/openejb3/container/openejb-core/src/main/resources: Made logging configuration and path resolution more tolerant

Diff

Modified: trunk/openejb3/container/openejb-core/src/main/java/org/openejb/alt/config/ConfigUtils.java (2529 => 2530)

--- trunk/openejb3/container/openejb-core/src/main/java/org/openejb/alt/config/ConfigUtils.java	2006-03-06 21:19:52 UTC (rev 2529)
+++ trunk/openejb3/container/openejb-core/src/main/java/org/openejb/alt/config/ConfigUtils.java	2006-03-09 20:25:52 UTC (rev 2530)
@@ -21,6 +21,8 @@
 import java.io.Writer;
 import java.net.URL;
 import java.net.UnknownHostException;
+import java.net.URLClassLoader;
+import java.net.URI;
 import java.util.Enumeration;
 import java.util.Properties;
 
@@ -29,58 +31,10 @@
     public static Messages messages = new Messages("org.openejb.util.resources");
     public static Logger logger = Logger.getInstance("OpenEJB", "org.openejb.util.resources");
 
-    public static Openejb readConfig() throws OpenEJBException {
-        return readConfig(searchForConfiguration());
-    }
-
     public static Openejb readConfig(String confFile) throws OpenEJBException {
-        File file = new File(confFile);
-        return (Openejb) Unmarshaller.unmarshal(Openejb.class, file.getName(), file.getParent());
+        return (Openejb) Unmarshaller.unmarshal(Openejb.class, confFile);
     }
 
-    /*
-    * TODO: Use the java.net.URL instead of java.io.File so configs
-    * and jars can be located remotely in the network
-    */
-    public static Openejb _readConfig(String confFile) throws OpenEJBException {
-        Openejb obj = null;
-        Reader reader = null;
-        try {
-            reader = new FileReader(confFile);
-            org.exolab.castor.xml.Unmarshaller unmarshaller = new org.exolab.castor.xml.Unmarshaller(Openejb.class);
-            unmarshaller.setWhitespacePreserve(true);
-            obj = (Openejb) unmarshaller.unmarshal(reader);
-        } catch (FileNotFoundException e) {
-            throw new OpenEJBException(messages.format("conf.1900", confFile, e.getLocalizedMessage()));
-        } catch (MarshalException e) {
-            if (e.getException() instanceof IOException) {
-                throw new OpenEJBException(messages.format("conf.1110", confFile, e.getLocalizedMessage()));
-            } else if (e.getException() instanceof UnknownHostException) {
-                throw new OpenEJBException(messages.format("conf.1121", confFile, e.getLocalizedMessage()));
-            } else {
-                throw new OpenEJBException(messages.format("conf.1120", confFile, e.getLocalizedMessage()));
-            }
-        } catch (ValidationException e) {
-            /* TODO: Implement informative error handling here. 
-               The exception will say "X doesn't match the regular 
-               _expression_ Y" 
-               This should be checked and more relevant information
-               should be given -- not everyone understands regular 
-               expressions. 
-             */
-            /*
-            NOTE: This doesn't seem to ever happen, anyone know why?
-            */
-            throw new OpenEJBException(messages.format("conf.1130", confFile, e.getLocalizedMessage()));
-        }
-        try {
-            reader.close();
-        } catch (Exception e) {
-            throw new OpenEJBException(messages.format("file.0020", confFile, e.getLocalizedMessage()));
-        }
-        return obj;
-    }
-
     public static void writeConfig(String confFile, Openejb confObject) throws OpenEJBException {
         /* TODO:  Just to be picky, the xml file created by
         Castor is really hard to read -- it is all on one line.
@@ -166,17 +120,17 @@
             } catch (IOException ignored) {
             }
 
+            logger.warning("Cannot find the configuration file [" + path + "], Trying conf/openejb.xml instead.");
         }
 
-        logger.warning("Cannot find the configuration file [" + path + "], Trying conf/openejb.conf instead.");
 
         try {
             /*
-             * [4] Try finding the standard openejb.conf file relative to the
+             * [4] Try finding the standard openejb.xml file relative to the
              * openejb.base directory
              */
             try {
-                file = SystemInstance.get().getBase().getFile("conf/openejb.conf");
+                file = SystemInstance.get().getBase().getFile("conf/openejb.xml");
                 if (file != null && file.exists() && file.isFile()) {
                     return file.getAbsolutePath();
                 }
@@ -185,26 +139,33 @@
 
             /*
              * [5] Try finding the standard openejb.conf file relative to the
-             * openejb.home directory
              */
             try {
-                file = SystemInstance.get().getHome().getFile("conf/openejb.conf");
+                file = SystemInstance.get().getBase().getFile("conf/openejb.conf");
                 if (file != null && file.exists() && file.isFile()) {
                     return file.getAbsolutePath();
                 }
             } catch (java.io.FileNotFoundException e) {
             }
 
-            logger.warning("Cannot find the configuration file [conf/openejb.conf], Creating one.");
 
             /* [6] No config found! Create a config for them
              *     using the default.openejb.conf file from 
              *     the openejb-x.x.x.jar
              */
 
-            File confDir = SystemInstance.get().getBase().getDirectory("conf", true);
+            File confDir = SystemInstance.get().getBase().getDirectory("conf", false);
 
-            file = createConfig(new File(confDir, "openejb.conf"));
+            if (confDir.exists()) {
+                File config = new File(confDir, "openejb.xml");
+                logger.warning("Cannot find the configuration file [conf/openejb.xml].  Creating one at "+config.getAbsolutePath());
+                file = createConfig(config);
+            } else {
+                logger.warning("Cannot find the configuration file [conf/openejb.xml].  Using the default configuration.");
+                ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+                URL resource = classLoader.getResource("default.openejb.conf");
+                return resource.toExternalForm();
+            }
 
         } catch (java.io.IOException e) {
             e.printStackTrace();

Modified: trunk/openejb3/container/openejb-core/src/main/java/org/openejb/alt/config/Unmarshaller.java (2529 => 2530)

--- trunk/openejb3/container/openejb-core/src/main/java/org/openejb/alt/config/Unmarshaller.java	2006-03-06 21:19:52 UTC (rev 2529)
+++ trunk/openejb3/container/openejb-core/src/main/java/org/openejb/alt/config/Unmarshaller.java	2006-03-09 20:25:52 UTC (rev 2530)
@@ -34,6 +34,35 @@
         return new Unmarshaller(clazz, xmlFile).unmarshal(jarLocation);
     }
 
+
+    public static Object unmarshal(Class clazz, String xmlFile) throws OpenEJBException {
+        try {
+            if (xmlFile.startsWith("jar:")) {
+                URL url = "" URL(xmlFile);
+                xmlFile = url.getFile();
+            }
+            if (xmlFile.startsWith("file:")){
+                URL url = "" URL(xmlFile);
+                xmlFile = url.getFile();
+            }
+        } catch (MalformedURLException e) {
+            throw new OpenEJBException("Unable to resolve location "+xmlFile,e);
+        }
+
+        String jarLocation = null;
+        int jarSeparator = xmlFile.indexOf("!");
+        if (jarSeparator > 0) {
+            jarLocation = xmlFile.substring(0, jarSeparator);
+            xmlFile = xmlFile.substring(jarSeparator + 2);
+        } else {
+            File file = new File(xmlFile);
+            xmlFile = file.getName();
+            jarLocation = file.getParent();
+        }
+
+        return new Unmarshaller(clazz, xmlFile).unmarshal(jarLocation);
+    }
+
     public Object unmarshal(String location) throws OpenEJBException {
         File file = new File(location);
         if (file.isDirectory()) {

Modified: trunk/openejb3/container/openejb-core/src/main/java/org/openejb/util/Logger.java (2529 => 2530)

--- trunk/openejb3/container/openejb-core/src/main/java/org/openejb/util/Logger.java	2006-03-06 21:19:52 UTC (rev 2529)
+++ trunk/openejb3/container/openejb-core/src/main/java/org/openejb/util/Logger.java	2006-03-09 20:25:52 UTC (rev 2530)
@@ -1,21 +1,25 @@
 package org.openejb.util;
 
+import org.openejb.loader.SystemInstance;
+import org.apache.log4j.Category;
+import org.apache.log4j.Level;
+import org.apache.log4j.PropertyConfigurator;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.FileNotFoundException;
 import java.io.InputStream;
-import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
 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;
-
 public class Logger {
 
     protected static final HashMap _loggers = new HashMap();
@@ -616,156 +620,128 @@
         }
 
         public void configure() {
+            Properties properties = null;
+
             String config = props.getProperty("log4j.configuration");
-            if (config == null) {
-                config = "conf/logging.conf";
-            }
-            try {
+            String[] search = {config, "logging.properties", "logging.conf"};
 
-                config = getAbsolutePath(config, "conf/default.logging.conf", false);
+            FileUtils base = SystemInstance.get().getBase();
+            File conf = new File(base.getDirectory(), "conf");
 
-                Properties log4jProps = loadProperties(config);
+            for (int i = 0; i < search.length && properties == null; i++) {
+                String fileName = search[i];
+                if (fileName == null) {
+                    continue;
+                }
+                File configFile = new File(conf, fileName);
 
-                PropertyConfigurator.configure(filterProperties(log4jProps));
-            } catch (Exception e) {
-                System.err.println("Failed to configure log4j. " + e.getMessage());
-            }
-        }
+                if (configFile.exists()) {
 
-        public Properties loadProperties(String file) throws Exception {
-            Properties props = new Properties();
-            FileInputStream fin = null;
-
-            try {
-                fin = new FileInputStream(file);
-                props.load(fin);
-            } finally {
-                if (fin != null) fin.close();
-            }
-            return props;
-        }
-
-        public Properties filterProperties(Properties log4jProps) {
-            Object[] names = log4jProps.keySet().toArray();
-            for (int i = 0; i < names.length; i++) {
-                String name = (String) names[i];
-                if (name.endsWith(".File")) {
-                    String path = log4jProps.getProperty(name);
+                    InputStream in = null;
                     try {
-                        File file = SystemInstance.get().getBase().getFile(path, false);
-                        if (!file.getParentFile().exists()) {
-                            file = SystemInstance.get().getHome().getFile(path, false);
+                        in = new FileInputStream(configFile);
+                        in = new BufferedInputStream(in);
+                        properties = new Properties();
+                        properties.load(in);
+                    } catch (IOException e) {
+                        org.apache.log4j.Logger logger = doFallbackConfiguration();
+                        logger.error("Unable to read logging config file " + configFile.getAbsolutePath(), e);
+                    } finally {
+                        try {
+                            in.close();
+                        } catch (IOException e) {
                         }
-                        path = file.getPath();
-                    } catch (IOException ignored) {
-
                     }
-                    log4jProps.setProperty(name, path);
                 }
             }
-            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
-                 */
+            if (properties == null) {
+                String configData = null;
                 try {
-                    file = SystemInstance.get().getBase().getFile(path);
-                    if (file != null && file.exists() && file.isFile()) {
-                        return file.getAbsolutePath();
-                    }
-                } catch (FileNotFoundException ignored) {
-                } catch (IOException ignored) {
+                    ResourceFinder finder = new ResourceFinder("");
+                    configData = finder.findString("default.logging.conf");
+                    properties = new Properties();
+                    properties.load(new ByteArrayInputStream(configData.getBytes()));
+                } catch (IOException e) {
+                    org.apache.log4j.Logger logger = doFallbackConfiguration();
+                    logger.error("Unable to read default logging config file.", e);
+                    return;
                 }
 
-                /*
-                 * [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();
+                if (conf.exists()) {
+                    OutputStream out = null;
+                    File configFile = new File(conf, "logging.properties");
+                    try {
+                        out = new FileOutputStream(configFile);
+                        out.write(configData.getBytes());
+                    } catch (IOException e) {
+                        org.apache.log4j.Logger logger = doFallbackConfiguration();
+                        logger.warn("Unable write default logging config file to " + configFile.getAbsolutePath(), e);
+                    } finally {
+                        try {
+                            out.close();
+                        } catch (IOException e) {
+                        }
                     }
-                } 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) {
-                }
+            File baseDir = base.getDirectory();
+            File userDir = new File("foo").getParentFile();
 
-                /*
-                 * [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) {
-                }
+            File[] paths = {baseDir, userDir};
 
-                if (create) {
-                    File confDir = SystemInstance.get().getBase().getDirectory("conf", true);
+            List missing = new ArrayList();
 
-                    file = createConfig(new File(confDir, secondaryPath));
-                }
-            } catch (java.io.IOException e) {
-                e.printStackTrace();
-                throw new OpenEJBException("Could not locate config file: ", e);
-            }
+            for (Iterator iterator = properties.entrySet().iterator(); iterator.hasNext();) {
+                Map.Entry entry = (Map.Entry) iterator.next();
+                String key = (String) entry.getKey();
+                String value = (String) entry.getValue();
 
-            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);
+                if (key.endsWith(".File")) {
 
-                int b = in.read();
+                    boolean found = false;
+                    for (int i = 0; i < paths.length && !found; i++) {
+                        File path = paths[i];
+                        File logfile = new File(path, value);
+                        if (logfile.getParentFile().exists()) {
+                            properties.setProperty(key, logfile.getAbsolutePath());
+                            found = true;
+                        }
+                    }
 
-                while (b != -1) {
-                    out.write(b);
-                    b = in.read();
+                    if (!found) {
+                        File logfile = new File(paths[0], value);
+                        missing.add(logfile);
+                    }
                 }
+            }
 
-                in.close();
-                out.close();
+            if (missing.size() > 0) {
+                org.apache.log4j.Logger logger = doFallbackConfiguration();
 
-            } catch (Exception e) {
-                e.printStackTrace();
+                logger.warn("Unable use logging config as there are "+missing.size()+" file references containing directories which have not been created.  See the list below.");
+                for (int i = 0; i < missing.size(); i++) {
+                    File file = (File) missing.get(i);
+                    logger.warn("["+i+"] "+file.getAbsolutePath());
+                }
+            } else {
+                PropertyConfigurator.configure(properties);
             }
 
-            return file;
         }
 
+        private org.apache.log4j.Logger doFallbackConfiguration() {
+            org.apache.log4j.Logger.getLogger("CastorCMP").setLevel(Level.ERROR);
+            org.apache.log4j.Logger.getLogger("org.exolab.castor").setLevel(Level.ERROR);
+            org.apache.log4j.Logger.getLogger("org.castor").setLevel(Level.ERROR);
+            org.apache.log4j.Logger.getLogger("org.openejb").setLevel(Level.WARN);
+            org.apache.log4j.Logger.getLogger("Transaction").setLevel(Level.WARN);
+            org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger("OpenEJB");
+            logger.setLevel(Level.WARN);
+            return logger;
+        }
+
     }
 }
\ No newline at end of file

Modified: trunk/openejb3/container/openejb-core/src/main/resources/default.logging.conf (2529 => 2530)

--- trunk/openejb3/container/openejb-core/src/main/resources/default.logging.conf	2006-03-06 21:19:52 UTC (rev 2529)
+++ trunk/openejb3/container/openejb-core/src/main/resources/default.logging.conf	2006-03-09 20:25:52 UTC (rev 2530)
@@ -35,10 +35,11 @@
 log4j.category.OpenEJB.server    = info, S
 log4j.category.OpenEJB.startup   = debug
 log4j.category.OpenEJB.CastorCMP = warn
-log4j.category.CastorCMP         = warn,  R
-log4j.category.org.exolab.castor = error,  R
+log4j.category.CastorCMP         = error,  R
+log4j.category.org.exolab.castor = error, R
+log4j.category.org.castor        = error, R
 log4j.category.CORBA-Adapter     = debug, R
-log4j.category.Transaction       = debug, TX
+log4j.category.Transaction       = warn, TX
 
 log4j.appender.R                          = org.apache.log4j.RollingFileAppender
 log4j.appender.R.File                     = logs/openejb.log

Reply via email to