You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2008/08/21 02:15:13 UTC

svn commit: r687516 - in /openejb/trunk/openejb3/container/openejb-core/src: main/java/org/apache/openejb/core/security/jaas/ main/java/org/apache/openejb/util/ test/java/org/apache/openejb/util/

Author: dblevins
Date: Wed Aug 20 17:15:13 2008
New Revision: 687516

URL: http://svn.apache.org/viewvc?rev=687516&view=rev
Log:
OPENEJB-898: Property overriding for logging configuration
Also improved the logging for PropertiesLoginModule

OPENEJB-890: Improved classpath configuration searching
Improved the ConfUtils so that it will look for all resources of the requested name, sort them by which is closest to the openejb.base, and then pick the first one.  What this does is allow for a more deterministic way to select things like users.properties, groups.properties, login.config, or embedded.logging.properties to be in the classpath severl times (perhaps once per each module being tested) and if the openejb.base is set to the path of the module currently being tested, it's files will always be chosen first.

Added:
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/IOUtils.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/UrlComparator.java
    openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/util/UrlComparatorTest.java
Modified:
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/security/jaas/PropertiesLoginModule.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/ConfUtils.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Log4jLog.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Log4jLogStreamFactory.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/URLs.java

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/security/jaas/PropertiesLoginModule.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/security/jaas/PropertiesLoginModule.java?rev=687516&r1=687515&r2=687516&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/security/jaas/PropertiesLoginModule.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/security/jaas/PropertiesLoginModule.java Wed Aug 20 17:15:13 2008
@@ -16,9 +16,11 @@
  */
 package org.apache.openejb.core.security.jaas;
 
+import static org.apache.openejb.util.IOUtils.readProperties;
 import org.apache.openejb.util.ConfUtils;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
+import org.apache.openejb.util.IOUtils;
 
 import javax.security.auth.Subject;
 import javax.security.auth.callback.Callback;
@@ -30,6 +32,7 @@
 import javax.security.auth.login.LoginException;
 import javax.security.auth.spi.LoginModule;
 import java.io.IOException;
+import java.io.BufferedInputStream;
 import java.net.URL;
 import java.util.Enumeration;
 import java.util.HashSet;
@@ -63,27 +66,28 @@
         this.subject = subject;
         this.callbackHandler = callbackHandler;
 
-        debug = "true".equalsIgnoreCase((String) options.get("Debug"));
+        debug = log.isDebugEnabled() || "true".equalsIgnoreCase((String) options.get("Debug"));
         String usersFile = (String) options.get(USER_FILE) + "";
         String groupsFile = (String) options.get(GROUP_FILE) + "";
 
         usersUrl = ConfUtils.getConfResource(usersFile);
         groupsUrl = ConfUtils.getConfResource(groupsFile);
 
-        if (debug) {
-            log.debug("Initialized debug=" + debug + " usersFile=" + usersFile + " groupsFile=" + groupsFile);
+        if (debug){
+            log.debug("Users file: " + usersUrl.toExternalForm());
+            log.debug("Groups file: " + groupsUrl.toExternalForm());
         }
     }
 
     public boolean login() throws LoginException {
         try {
-            users.load(usersUrl.openStream());
+            users = readProperties(usersUrl);
         } catch (IOException ioe) {
             throw new LoginException("Unable to load user properties file " + usersUrl.getFile());
         }
 
         try {
-            groups.load(groupsUrl.openStream());
+            groups = readProperties(groupsUrl);
         } catch (IOException ioe) {
             throw new LoginException("Unable to load group properties file " + groupsUrl.getFile());
         }
@@ -112,7 +116,7 @@
         users.clear();
 
         if (debug) {
-            log.debug("login " + user);
+            log.debug("Logged in as '" + user+"'");
         }
         return true;
     }

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/ConfUtils.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/ConfUtils.java?rev=687516&r1=687515&r2=687516&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/ConfUtils.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/ConfUtils.java Wed Aug 20 17:15:13 2008
@@ -26,6 +26,9 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.util.Enumeration;
+import java.util.Collections;
+import java.util.ArrayList;
 
 /**
  * @version $Rev$ $Date$
@@ -33,7 +36,7 @@
 public class ConfUtils {
 
     public static URL getConfResource(String name) {
-        URL resource = Thread.currentThread().getContextClassLoader().getResource(name);
+        URL resource = getResource(name);
 
         try {
 
@@ -48,6 +51,32 @@
         return resource;
     }
 
+    public static URL getResource(String name) {
+        Enumeration<URL> resources = null;
+        try {
+            resources = Thread.currentThread().getContextClassLoader().getResources(name);
+        } catch (IOException e) {
+            // DMB: Not sure why this version of getResource doesn't require checking
+            // for IOException, but no matter.  Perhpas it may succeed where the other fails.
+            return Thread.currentThread().getContextClassLoader().getResource(name);
+        }
+
+        URL resource = select(resources);
+        return resource;
+    }
+
+    private static URL select(Enumeration<URL> enumeration) {
+        if (enumeration == null) return null;
+        ArrayList<URL> urls = Collections.list(enumeration);
+        if (urls.size() == 0) return null;
+        if (urls.size() == 1) return urls.get(0);
+
+        // Sort so that the URL closest to openejb.base is first
+        Collections.sort(urls, new UrlComparator(SystemInstance.get().getBase().getDirectory()));
+
+        return urls.get(0);
+    }
+
     public static File install(String source, String name) throws IOException {
         ClassLoader cl = Thread.currentThread().getContextClassLoader();
         URL resource = cl.getResource(source);
@@ -96,4 +125,5 @@
 
         return file;
     }
+
 }

Added: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/IOUtils.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/IOUtils.java?rev=687516&view=auto
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/IOUtils.java (added)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/IOUtils.java Wed Aug 20 17:15:13 2008
@@ -0,0 +1,46 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.openejb.util;
+
+import java.util.Properties;
+import java.net.URL;
+import java.io.InputStream;
+import java.io.BufferedInputStream;
+import java.io.IOException;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class IOUtils {
+
+    public static Properties readProperties(URL resource) throws IOException {
+        Properties properties = new Properties();
+        InputStream in = null;
+        try {
+            in = resource.openStream();
+            in = new BufferedInputStream(in);
+            properties.load(in);
+        } finally{
+            try {
+                if (in != null) in.close();
+            } catch (IOException e) {
+            }
+        }
+        return properties;
+    }
+
+}

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Log4jLog.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Log4jLog.java?rev=687516&r1=687515&r2=687516&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Log4jLog.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Log4jLog.java Wed Aug 20 17:15:13 2008
@@ -146,6 +146,7 @@
                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(loggingPropertiesFile));
                 Properties props = new Properties();
                 props.load(bis);
+                applyOverrides(props);
                 preprocessProperties(props);
                 PropertyConfigurator.configure(props);
                 try {
@@ -161,6 +162,16 @@
         }
     }
 
+    private static void applyOverrides(Properties properties) {
+        Properties system = SystemInstance.get().getProperties();
+        for (Map.Entry<Object, Object> entry : system.entrySet()) {
+            String key = entry.getKey().toString();
+            if (key.startsWith("log4j.") && !key.equals("log4j.configuration")){
+                properties.put(key, entry.getValue());
+            }
+        }
+    }
+
     private static void preprocessProperties(Properties properties) {
         FileUtils base = SystemInstance.get().getBase();
         File confDir = new File(base.getDirectory(), "conf");
@@ -216,11 +227,33 @@
     }
 
     private static void configureEmbedded() {
-        URL resource = Thread.currentThread().getContextClassLoader().getResource(EMBEDDED_PROPERTIES_FILE);
-        if (resource != null)
-            PropertyConfigurator.configure(resource);
-        else
+        URL resource = ConfUtils.getResource(EMBEDDED_PROPERTIES_FILE);
+        Properties properties = asProperies(resource);
+
+        applyOverrides(properties);
+
+        if (resource != null) {
+            PropertyConfigurator.configure(properties);
+        } else {
             System.out.println("FATAL ERROR WHILE CONFIGURING LOGGING!!!. MISSING embedded.logging.properties FILE ");
+        }
+    }
+
+    private static Properties asProperies(URL resource) {
+        Properties properties = new Properties();
+        InputStream in = null;
+        try {
+            in = resource.openStream();
+            in = new BufferedInputStream(in);
+            properties.load(in);
+        } catch (IOException e) {
+        } finally{
+            try {
+                if (in != null) in.close();
+            } catch (IOException e) {
+            }
+        }
+        return properties;
     }
 
     private static void installLoggingPropertiesFile(File loggingPropertiesFile) throws IOException {

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Log4jLogStreamFactory.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Log4jLogStreamFactory.java?rev=687516&r1=687515&r2=687516&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Log4jLogStreamFactory.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/Log4jLogStreamFactory.java Wed Aug 20 17:15:13 2008
@@ -70,6 +70,7 @@
                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(loggingPropertiesFile));
                 Properties props = new Properties();
                 props.load(bis);
+                applyOverrides(props);
                 preprocessProperties(props);
                 PropertyConfigurator.configure(props);
                 try {
@@ -88,6 +89,16 @@
         }
     }
 
+    private static void applyOverrides(Properties properties) {
+        Properties system = SystemInstance.get().getProperties();
+        for (Map.Entry<Object, Object> entry : system.entrySet()) {
+            String key = entry.getKey().toString();
+            if (key.startsWith("log4j.") && !key.equals("log4j.configuration")){
+                properties.put(key, entry.getValue());
+            }
+        }
+    }
+
     private void preprocessProperties(Properties properties) {
         FileUtils base = SystemInstance.get().getBase();
         File confDir = new File(base.getDirectory(), "conf");
@@ -141,11 +152,33 @@
     }
 
     private void configureEmbedded() {
-        URL resource = Thread.currentThread().getContextClassLoader().getResource(EMBEDDED_PROPERTIES_FILE);
-        if (resource != null)
-            PropertyConfigurator.configure(resource);
-        else
+        URL resource = ConfUtils.getResource(EMBEDDED_PROPERTIES_FILE);
+        Properties properties = asProperies(resource);
+
+        applyOverrides(properties);
+
+        if (resource != null) {
+            PropertyConfigurator.configure(properties);
+        } else {
             System.out.println("FATAL ERROR WHILE CONFIGURING LOGGING!!!. MISSING embedded.logging.properties FILE ");
+        }
+    }
+
+    private static Properties asProperies(URL resource) {
+        Properties properties = new Properties();
+        InputStream in = null;
+        try {
+            in = resource.openStream();
+            in = new BufferedInputStream(in);
+            properties.load(in);
+        } catch (IOException e) {
+        } finally{
+            try {
+                if (in != null) in.close();
+            } catch (IOException e) {
+            }
+        }
+        return properties;
     }
 
     private void installLoggingPropertiesFile(File loggingPropertiesFile) throws IOException {

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/URLs.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/URLs.java?rev=687516&r1=687515&r2=687516&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/URLs.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/URLs.java Wed Aug 20 17:15:13 2008
@@ -19,6 +19,7 @@
 import java.io.File;
 import java.net.URL;
 import java.net.URLDecoder;
+import java.net.MalformedURLException;
 
 /**
  * @version $Rev$ $Date$
@@ -26,10 +27,27 @@
 public class URLs {
 
     public static File toFile(URL url) {
-        return new File(URLDecoder.decode(url.getPath()));
+
+        String path = url.getPath();
+
+        if (path.contains("!")){
+            path = path.substring(0, path.indexOf('!'));
+        }
+
+        if (path.startsWith("file:")){
+            try {
+                url = new URL(path);
+                path = url.getPath();
+            } catch (MalformedURLException e) {
+            }
+        }
+
+        return new File(URLDecoder.decode(path));
     }
 
     public static String toFilePath(URL url) {
         return toFile(url).getAbsolutePath();
     }
+
+
 }

Added: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/UrlComparator.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/UrlComparator.java?rev=687516&view=auto
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/UrlComparator.java (added)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/UrlComparator.java Wed Aug 20 17:15:13 2008
@@ -0,0 +1,80 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.openejb.util;
+
+import java.net.URL;
+import java.util.Comparator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.ArrayList;
+import java.io.File;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class UrlComparator implements Comparator<URL> {
+    private File dir;
+    private List<String> rootPath;
+
+    public UrlComparator(File directory) {
+        dir = directory;
+        rootPath = path(dir);
+    }
+
+    public UrlComparator(URL base) {
+        this(URLs.toFile(base));
+    }
+
+    public int compare(URL a, URL b) {
+        return score(b) - score(a);
+    }
+
+    private int score(URL url){
+        File file = URLs.toFile(url);
+        List<String> filePath = path(file);
+        int matches = 0;
+
+        ListIterator<String> a = rootPath.listIterator();
+        ListIterator<String> b = filePath.listIterator();
+        while(a.hasNext() && b.hasNext()) {
+            String nameA = a.next();
+            String nameB = b.next();
+
+            if (nameA.equals(nameB)) {
+                matches++;
+            } else {
+                break;
+            }
+        }
+
+        return matches;
+    }
+
+    private List<String> path(File file){
+        ArrayList<String> path = new ArrayList<String>();
+        path(file, path);
+        return path;
+    }
+
+    private void path(File file, List<String> path){
+        if (file == null) return;
+
+        path(file.getParentFile(), path);
+
+        path.add(file.getName());
+    }
+}

Added: openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/util/UrlComparatorTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/util/UrlComparatorTest.java?rev=687516&view=auto
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/util/UrlComparatorTest.java (added)
+++ openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/util/UrlComparatorTest.java Wed Aug 20 17:15:13 2008
@@ -0,0 +1,46 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.openejb.util;
+
+import junit.framework.TestCase;
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class UrlComparatorTest extends TestCase {
+
+    public void test() throws Exception {
+        ArrayList<URL> urls = new ArrayList<URL>();
+
+        urls.add(new URL("file:///Users/lblack/four"));
+        urls.add(new URL("file:///Users/jstuart/two"));
+        urls.add(new URL("file:///Users/jstuart/one"));
+        urls.add(new URL("file:///Users/scobert/three"));
+
+        Collections.sort(urls, new UrlComparator(new URL("file:///Users/jstuart")));
+
+        assertEquals(new URL("file:///Users/jstuart/two"), urls.get(0));
+
+        Collections.sort(urls, new UrlComparator(new URL("file:///Users/jstuart/one")));
+
+        assertEquals(new URL("file:///Users/jstuart/one"), urls.get(0));
+    }
+}