Browse Source

Added method to support unit testing, and made class more robust to null conditions.

Ben Alex 21 years ago
parent
commit
b886390a3f

+ 38 - 6
adapters/jboss/src/main/java/org/acegisecurity/adapters/jboss/JbossIntegrationFilter.java

@@ -22,6 +22,7 @@ import java.security.Principal;
 
 import java.util.Iterator;
 
+import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
 
@@ -48,10 +49,24 @@ public class JbossIntegrationFilter extends AbstractIntegrationFilter {
         Subject subject = null;
 
         try {
-            InitialContext ic = new InitialContext();
-            subject = (Subject) ic.lookup("java:comp/env/security/subject");
+            Context lc = this.getLookupContext();
+
+            if (lc == null) {
+                if (super.logger.isWarnEnabled()) {
+                    super.logger.warn(
+                        "Could not obtain a Context to perform lookup");
+                }
+
+                return null;
+            }
+
+            Object result = lc.lookup("java:comp/env/security/subject");
+
+            if (result instanceof Subject) {
+                subject = (Subject) result;
+            }
         } catch (NamingException ne) {
-            if (super.logger.isDebugEnabled()) {
+            if (super.logger.isWarnEnabled()) {
                 super.logger.warn("Lookup on Subject failed "
                     + ne.getLocalizedMessage());
             }
@@ -63,9 +78,15 @@ public class JbossIntegrationFilter extends AbstractIntegrationFilter {
             while (principals.hasNext()) {
                 Principal p = (Principal) principals.next();
 
-                if (super.logger.isDebugEnabled()) {
-                    super.logger.debug("Found Principal in container ("
-                        + p.getClass().getName() + ") : " + p.getName());
+                if (p == null) {
+                    if (super.logger.isDebugEnabled()) {
+                        super.logger.debug("Found null Principal in container");
+                    }
+                } else {
+                    if (super.logger.isDebugEnabled()) {
+                        super.logger.debug("Found Principal in container ("
+                            + p.getClass().getName() + ") : " + p.getName());
+                    }
                 }
 
                 if (p instanceof Authentication) {
@@ -76,4 +97,15 @@ public class JbossIntegrationFilter extends AbstractIntegrationFilter {
 
         return null;
     }
+
+    /**
+     * Provided so that unit tests can override.
+     *
+     * @return a <code>Context</code> that can be used for lookup
+     *
+     * @throws NamingException DOCUMENT ME!
+     */
+    protected Context getLookupContext() throws NamingException {
+        return new InitialContext();
+    }
 }