[jboss-svn-commits] JBL Code SVN: r37673 - in labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta: src/org/jboss/soa/esb/services/security and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Oct 26 01:12:39 EDT 2011


Author: tcunning
Date: 2011-10-26 01:12:39 -0400 (Wed, 26 Oct 2011)
New Revision: 37673

Added:
   labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/FilePasswordPlugin.java
   labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/PasswordHandler.java
   labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/PasswordPlugin.java
   labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/PasswordPluginManager.java
   labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/tests/src/org/jboss/soa/esb/services/security/PasswordPluginManagerUnitTest.java
Modified:
   labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/common/Environment.java
   labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/PasswordUtil.java
Log:
JBESB-3555
Commit pluggable password encryption/decryption mechanism patch.


Modified: labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/common/Environment.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/common/Environment.java	2011-10-26 02:50:08 UTC (rev 37672)
+++ labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/common/Environment.java	2011-10-26 05:12:39 UTC (rev 37673)
@@ -270,6 +270,8 @@
         public static final String SECURITY_SERVICE_PUBLIC_KEY_PASS             = "org.jboss.soa.esb.services.security.publicKeyPassword";
         public static final String SECURITY_SERVICE_PUBLIC_KEY_TRANSFORMATION = "org.jboss.soa.esb.services.security.publicKeyTransformation";
 
+        public static final String SECURITY_SERVICE_PASSWORD_PLUGINS = "org.jboss.soa.esb.services.security.passwordPlugin.";
+
         /** Message property name for EBWS WS-A properties*/
 
         public static final String WSA_MESSAGE_ID = "org.jboss.soa.esb.gateway.ebws.messageID" ;

Added: labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/FilePasswordPlugin.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/FilePasswordPlugin.java	                        (rev 0)
+++ labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/FilePasswordPlugin.java	2011-10-26 05:12:39 UTC (rev 37673)
@@ -0,0 +1,110 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA  02110-1301, USA.
+ */
+package org.jboss.soa.esb.services.security;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
+
+import org.jboss.logging.Logger;
+import org.jboss.security.plugins.FilePassword;
+
+
+/**
+ * Password plugin supporting FilePlugin.
+ */
+public class FilePasswordPlugin implements PasswordPlugin
+{
+	/**
+	 * The logger for this plugin.
+	 */
+	private final Logger logger = Logger.getLogger(getClass()) ;
+	
+	/**
+	 * Does the password plugin support the specified uri?
+	 * @param uri The uri representing the password file.
+	 * @return true if supported, false otherwise
+	 */
+	public boolean supportsPasswordFile(final URI uri)
+	{
+		try
+		{
+			uri.toURL() ;
+			return true ;
+		}
+		catch (final IllegalArgumentException iae)
+		{
+			return false ;
+		}
+		catch (final MalformedURLException murle)
+		{
+			return false ;
+		}
+	}
+
+	/**
+	 * Return the plugin handler for the specified uri.
+	 * @param uri The uri representing the password file.
+	 * @return The PluginHandler or null if not supported
+	 */
+	public PasswordHandler pluginHandler(final URI uri)
+	{
+		final URL url ;
+		try
+		{
+			url = uri.toURL() ;
+		}
+		catch (final IllegalArgumentException iae)
+		{
+			logger.warn("URI was not absolute", iae) ;
+			return null ;
+		}
+		catch (final MalformedURLException murle)
+		{
+			logger.warn("Could not recognize URI", murle) ;
+			return null ;
+		}
+		
+		return new FilePasswordHandler(url) ;
+	}
+	
+	/**
+	 * The password handler for FilePassword
+	 * @author kevin
+	 */
+	private static class FilePasswordHandler implements PasswordHandler
+	{
+		private final FilePassword filePassword ;
+		
+		FilePasswordHandler(final URL url)
+		{
+			filePassword = new FilePassword(url.toExternalForm()) ;
+		}
+
+		/**
+		 * Retrieve the password
+		 * @return the password
+		 */
+		public String getPassword()
+			throws IOException
+		{
+			return new String(filePassword.toCharArray()) ;
+		}
+	}
+}

Added: labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/PasswordHandler.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/PasswordHandler.java	                        (rev 0)
+++ labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/PasswordHandler.java	2011-10-26 05:12:39 UTC (rev 37673)
@@ -0,0 +1,35 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA  02110-1301, USA.
+ */
+package org.jboss.soa.esb.services.security;
+
+import java.io.IOException;
+
+
+/**
+ * Interface for password handlers
+ */
+public interface PasswordHandler
+{
+	/**
+	 * Retrieve the password
+	 * @return the password
+	 * @throws IOException for any errors retrieving the password.
+	 */
+	public String getPassword()
+		throws IOException ;
+}

Added: labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/PasswordPlugin.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/PasswordPlugin.java	                        (rev 0)
+++ labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/PasswordPlugin.java	2011-10-26 05:12:39 UTC (rev 37673)
@@ -0,0 +1,42 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA  02110-1301, USA.
+ */
+package org.jboss.soa.esb.services.security;
+
+import java.net.URI;
+
+
+
+/**
+ * Interface for password plugins
+ */
+public interface PasswordPlugin
+{
+	/**
+	 * Does the password plugin support the specified uri?
+	 * @param uri The uri representing the password file.
+	 * @return true if supported, false otherwise
+	 */
+	public boolean supportsPasswordFile(final URI uri);
+
+	/**
+	 * Return the plugin handler for the specified uri.
+	 * @param uri The uri representing the password file.
+	 * @return The PluginHandler or null if not supported
+	 */
+	public PasswordHandler pluginHandler(final URI uri) ;
+}

Added: labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/PasswordPluginManager.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/PasswordPluginManager.java	                        (rev 0)
+++ labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/PasswordPluginManager.java	2011-10-26 05:12:39 UTC (rev 37673)
@@ -0,0 +1,206 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA  02110-1301, USA.
+ */
+package org.jboss.soa.esb.services.security;
+
+import java.io.File;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.Properties;
+import java.util.TreeMap;
+
+import org.jboss.logging.Logger;
+import org.jboss.soa.esb.common.Environment;
+import org.jboss.soa.esb.common.ModulePropertyManager;
+import org.jboss.soa.esb.util.ClassUtil;
+
+import com.arjuna.common.util.propertyservice.PropertyManager;
+
+/**
+ * Manage password plugin functionality.
+ * 
+ * @author <a href="mailto:kevin.conner at jboss.com">Kevin Conner</a>
+ */
+public final class PasswordPluginManager
+{
+	private static final Logger logger = Logger.getLogger(PasswordPluginManager.class) ;
+	// Default plugins
+	private static final PasswordPlugin[] DEFAULT_PLUGINS = { new FilePasswordPlugin() } ;
+	
+	// The singleton representing the manager.
+	private static final PasswordPluginManager MANAGER = new PasswordPluginManager() ;
+
+	// The list of plugins which are configured
+	private final PasswordPlugin[] plugins ;
+	
+	/**
+	 * Retrieve a password plugin for the specified file.
+	 * @param passwordFile The passwordFile 
+	 * @return The PasswordHandler or null if not supported
+	 */
+	public PasswordHandler passwordHandler(final String passwordFile)
+	{
+		final URI uri = obtainURI(passwordFile) ;
+        
+        PasswordHandler passwordHandler = null ;
+        if (uri != null)
+        {
+        	for (PasswordPlugin plugin: plugins)
+        	{
+        		if (plugin.supportsPasswordFile(uri))
+        		{
+	        		passwordHandler = plugin.pluginHandler(uri) ;
+	        		if (passwordHandler != null)
+	        		{
+	        			break ;
+	        		}
+        		}
+        	}
+        }
+		return passwordHandler ;
+    }
+	
+	/**
+	 * Retrieve a password plugin for the specified file.
+	 * @param passwordFile The passwordFile 
+	 * @return The PasswordHandler or null if not supported
+	 */
+	public boolean isPasswordSupported(final String passwordFile)
+	{
+		final URI uri = obtainURI(passwordFile) ;
+        
+        if (uri != null)
+        {
+        	for (PasswordPlugin plugin: plugins)
+        	{
+        		if (plugin.supportsPasswordFile(uri))
+        		{
+        			return true ;
+        		}
+        	}
+        }
+		return false ;
+    }
+	
+	{
+    	final PropertyManager pm = ModulePropertyManager.getPropertyManager(ModulePropertyManager.SECURITY_MODULE) ;
+    	final Properties props = pm.getProperties() ;
+		if (props != null)
+		{
+		    final Enumeration<?> names = props.propertyNames() ;
+		    final TreeMap<Integer, String> map = new TreeMap<Integer, String>() ;
+		    while (names.hasMoreElements())
+		    {
+		    	final String name = (String) names.nextElement() ;
+
+		    	if (name.startsWith(Environment.SECURITY_SERVICE_PASSWORD_PLUGINS))
+		    	{
+		    		final String order = name.substring(Environment.SECURITY_SERVICE_PASSWORD_PLUGINS.length()) ;
+				    final Integer value ;
+	
+				    try
+				    {
+				    	value = Integer.valueOf(order) ;
+				    }
+				    catch (NumberFormatException ex)
+				    {
+				    	throw new RuntimeException("Password plugin name " + order + " is invalid!");
+				    }
+
+				    map.put(value, props.getProperty(name)) ;
+		    	}
+		    }
+		    
+	    	final ArrayList<PasswordPlugin> pluginList = new ArrayList<PasswordPlugin>(map.size()) ;
+	    	
+		    if (map.size() > 0)
+		    {
+		    	final Collection<String> ordered = map.values() ;
+		    	final Iterator<String> iter = ordered.iterator() ;
+
+		    	while (iter.hasNext())
+		    	{
+		    		final String pluginName = iter.next() ;
+
+		    		try
+		    		{
+		    			final Class<?> c = ClassUtil.forName(pluginName, PasswordPluginManager.class) ;
+		    			final PasswordPlugin plugin = (PasswordPlugin) c.newInstance() ;
+
+		    			pluginList.add(plugin);
+				    }
+				    catch (final ClassNotFoundException cnfe)
+				    {
+				    	logger.warn("problem loading class " + pluginName, cnfe) ;
+				    }
+				    catch (final Throwable th)
+				    {
+				    	logger.warn("problem during load " + pluginName, th) ;
+				    }
+		    	}
+		    	if (pluginList.size() > 0)
+		    	{
+		    		plugins = pluginList.toArray(new PasswordPlugin[0]) ;
+		    	}
+		    	else
+		    	{
+		    		plugins = DEFAULT_PLUGINS ;
+		    	}
+		    }
+		    else
+		    {
+		    	plugins = DEFAULT_PLUGINS ;
+		    }
+		}
+		else
+		{
+	    	plugins = DEFAULT_PLUGINS ;
+		}
+    }
+    
+    private static URI obtainURI(final String passwordFile)
+    {
+        if (passwordFile == null || "".equals(passwordFile))
+            return null ;
+        
+        URI uri = null ;
+        try
+		{
+        	uri = new URI(passwordFile) ;
+		}
+		catch (final URISyntaxException ignore) {}
+
+        if ((uri == null) || !uri.isAbsolute())
+        {
+	        final File pwFile = new File(passwordFile);
+			if (pwFile.exists() && !pwFile.isDirectory())
+			{
+				uri = pwFile.toURI() ;
+			}
+        }
+        return uri ;
+    }
+    
+    public static PasswordPluginManager getManager()
+    {
+    	return MANAGER ;
+    }
+}

Modified: labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/PasswordUtil.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/PasswordUtil.java	2011-10-26 02:50:08 UTC (rev 37672)
+++ labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/src/org/jboss/soa/esb/services/security/PasswordUtil.java	2011-10-26 05:12:39 UTC (rev 37673)
@@ -20,10 +20,7 @@
  */
 package org.jboss.soa.esb.services.security;
 
-import java.io.File;
 import java.io.IOException;
-import java.net.MalformedURLException;
-import java.net.URL;
 
 import org.jboss.internal.soa.esb.assertion.AssertArgument;
 import org.jboss.security.plugins.FilePassword;
@@ -51,42 +48,30 @@
  */
 public final class PasswordUtil
 {
-    private FilePassword filePassword;
-    
+	private final PasswordHandler passwordHandler ;
+	
     public PasswordUtil(final String passwordFile)
     {
         AssertArgument.isNotNull(passwordFile, "passwordFile");
-        filePassword = new FilePassword(passwordFile);
+        passwordHandler = PasswordPluginManager.getManager().passwordHandler(passwordFile) ;
+        if (passwordHandler == null)
+        {
+        	throw new IllegalArgumentException("Could not determine password handler for " + passwordFile) ;
+        }
     }
     
     public char[] getPassword() throws IOException
     {
-        return filePassword.toCharArray();
+        return passwordHandler.getPassword().toCharArray() ;
     }
     
     public String getPasswordAsString() throws IOException
     {
-        return new String(filePassword.toCharArray());
+        return passwordHandler.getPassword() ;
     }
     
     public static boolean isPasswordFile(final String passwordFile)
     {
-        if (passwordFile == null || "".equals(passwordFile))
-            return false;
-        
-		try
-		{
-			final URL url = new URL(passwordFile);
-			if (url != null)
-				return true;
-		}
-		catch (final MalformedURLException ignored)
-		{
-			final File pwFile = new File(passwordFile);
-			if (pwFile.exists() && !pwFile.isDirectory())
-				return true;
-		}
-		return false;
+    	return (PasswordPluginManager.getManager().isPasswordSupported(passwordFile)) ;
     }
-
 }

Added: labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/tests/src/org/jboss/soa/esb/services/security/PasswordPluginManagerUnitTest.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/tests/src/org/jboss/soa/esb/services/security/PasswordPluginManagerUnitTest.java	                        (rev 0)
+++ labs/jbossesb/branches/JBESB_4_10_CP/product/rosetta/tests/src/org/jboss/soa/esb/services/security/PasswordPluginManagerUnitTest.java	2011-10-26 05:12:39 UTC (rev 37673)
@@ -0,0 +1,196 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA  02110-1301, USA.
+ */
+package org.jboss.soa.esb.services.security;
+
+import java.io.IOException;
+import java.net.URI;
+
+import junit.framework.JUnit4TestAdapter;
+
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.common.Environment;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+
+/**
+ * Unit tests for password plugin functionality.
+ * 
+ * @author <a href="mailto:kevin.conner at jboss.com">Kevin Conner</a>
+ */
+public final class PasswordPluginManagerUnitTest
+{
+    @Test
+    public void checkOrder()
+    	throws Exception
+    {
+    	final PasswordPluginManager manager = new PasswordPluginManager() ;
+    	
+    	BasePasswordPlugin.checked = new boolean[3] ;
+    	BasePasswordPlugin.currentId = 0 ;
+    	
+    	Assert.assertTrue("First plugin isPasswordSupported", manager.isPasswordSupported("dummy")) ;
+    	Assert.assertTrue("First plugin checked", BasePasswordPlugin.checked[0]) ;
+    	Assert.assertFalse("Second plugin called", BasePasswordPlugin.checked[1]) ;
+    	Assert.assertFalse("Third plugin called", BasePasswordPlugin.checked[2]) ;
+    	
+    	BasePasswordPlugin.checked = new boolean[3] ;
+    	BasePasswordPlugin.currentId = 0 ;
+    	
+    	PasswordHandler passwordHandler = manager.passwordHandler("dummy") ;
+    	
+    	Assert.assertNotNull("First plugin passwordHandler", passwordHandler) ;
+    	Assert.assertEquals("First plugin password", "password0", passwordHandler.getPassword()) ;
+    	Assert.assertTrue("First plugin checked", BasePasswordPlugin.checked[0]) ;
+    	Assert.assertFalse("Second plugin called", BasePasswordPlugin.checked[1]) ;
+    	Assert.assertFalse("Third plugin called", BasePasswordPlugin.checked[2]) ;
+    	
+    	BasePasswordPlugin.checked = new boolean[3] ;
+    	BasePasswordPlugin.currentId = 1 ;
+    	
+    	Assert.assertTrue("Second plugin isPasswordSupported", manager.isPasswordSupported("dummy")) ;
+    	Assert.assertTrue("First plugin checked", BasePasswordPlugin.checked[0]) ;
+    	Assert.assertTrue("Second plugin called", BasePasswordPlugin.checked[1]) ;
+    	Assert.assertFalse("Third plugin called", BasePasswordPlugin.checked[2]) ;
+    	
+    	BasePasswordPlugin.checked = new boolean[3] ;
+    	BasePasswordPlugin.currentId = 1 ;
+    	
+    	passwordHandler = manager.passwordHandler("dummy") ;
+    	
+    	Assert.assertNotNull("Second plugin passwordHandler", passwordHandler) ;
+    	Assert.assertEquals("Second plugin password", "password1", passwordHandler.getPassword()) ;
+    	Assert.assertTrue("First plugin checked", BasePasswordPlugin.checked[0]) ;
+    	Assert.assertTrue("Second plugin called", BasePasswordPlugin.checked[1]) ;
+    	Assert.assertFalse("Third plugin called", BasePasswordPlugin.checked[2]) ;
+    	
+    	BasePasswordPlugin.checked = new boolean[3] ;
+    	BasePasswordPlugin.currentId = 2 ;
+    	
+    	Assert.assertTrue("Third plugin isPasswordSupported", manager.isPasswordSupported("dummy")) ;
+    	Assert.assertTrue("First plugin checked", BasePasswordPlugin.checked[0]) ;
+    	Assert.assertTrue("Second plugin called", BasePasswordPlugin.checked[1]) ;
+    	Assert.assertTrue("Third plugin called", BasePasswordPlugin.checked[2]) ;
+    	
+    	BasePasswordPlugin.checked = new boolean[3] ;
+    	BasePasswordPlugin.currentId = 2 ;
+    	
+    	passwordHandler = manager.passwordHandler("dummy") ;
+    	
+    	Assert.assertNotNull("Third plugin passwordHandler", passwordHandler) ;
+    	Assert.assertEquals("Third plugin password", "password2", passwordHandler.getPassword()) ;
+    	Assert.assertTrue("First plugin checked", BasePasswordPlugin.checked[0]) ;
+    	Assert.assertTrue("Second plugin called", BasePasswordPlugin.checked[1]) ;
+    	Assert.assertTrue("Third plugin called", BasePasswordPlugin.checked[2]) ;
+    	
+    	BasePasswordPlugin.checked = new boolean[3] ;
+    	BasePasswordPlugin.currentId = -1 ;
+    	
+    	Assert.assertFalse("No plugin isPasswordSupported", manager.isPasswordSupported("dummy")) ;
+    	Assert.assertTrue("First plugin checked", BasePasswordPlugin.checked[0]) ;
+    	Assert.assertTrue("Second plugin called", BasePasswordPlugin.checked[1]) ;
+    	Assert.assertTrue("Third plugin called", BasePasswordPlugin.checked[2]) ;
+    	
+    	BasePasswordPlugin.checked = new boolean[3] ;
+    	BasePasswordPlugin.currentId = -1 ;
+    	
+    	passwordHandler = manager.passwordHandler("dummy") ;
+    	
+    	Assert.assertNull("No plugin passwordHandler", passwordHandler) ;
+    	Assert.assertTrue("First plugin checked", BasePasswordPlugin.checked[0]) ;
+    	Assert.assertTrue("Second plugin called", BasePasswordPlugin.checked[1]) ;
+    	Assert.assertTrue("Third plugin called", BasePasswordPlugin.checked[2]) ;
+    }
+    
+	@Before
+    public void setup() throws ConfigurationException
+    {
+		System.setProperty(Environment.SECURITY_SERVICE_PASSWORD_PLUGINS + "1", FirstPasswordPlugin.class.getName()) ;
+		System.setProperty(Environment.SECURITY_SERVICE_PASSWORD_PLUGINS + "2", SecondPasswordPlugin.class.getName()) ;
+		System.setProperty(Environment.SECURITY_SERVICE_PASSWORD_PLUGINS + "3", ThirdPasswordPlugin.class.getName()) ;
+    }
+
+    @After
+    public void tearDown()
+    {
+		System.clearProperty(Environment.SECURITY_SERVICE_PASSWORD_PLUGINS + "1") ;
+		System.clearProperty(Environment.SECURITY_SERVICE_PASSWORD_PLUGINS + "2") ;
+		System.clearProperty(Environment.SECURITY_SERVICE_PASSWORD_PLUGINS + "3") ;
+    }
+    
+    public static junit.framework.Test suite()
+    {
+        return new JUnit4TestAdapter(PasswordPluginManagerUnitTest.class);
+    }
+
+    static class BasePasswordPlugin implements PasswordPlugin
+    {
+    	static int currentId ;
+    	static boolean[] checked ;
+    	
+    	private final int id ;
+    	
+    	protected BasePasswordPlugin(final int id)
+    	{
+    		this.id = id ;
+    	}
+
+		@Override
+		public boolean supportsPasswordFile(final URI uri)
+		{
+			checked[id] = true ;
+			return (currentId == id) ;
+		}
+
+		@Override
+		public PasswordHandler pluginHandler(final URI uri)
+		{
+			return new PasswordHandler() {
+				public String getPassword() throws IOException {
+					return "password" + id ;
+				}
+			} ;
+		}
+    }
+
+    public static class FirstPasswordPlugin extends BasePasswordPlugin
+    {
+    	FirstPasswordPlugin()
+    	{
+    		super(0) ;
+    	}
+    }
+    
+    public static class SecondPasswordPlugin extends BasePasswordPlugin
+    {
+    	SecondPasswordPlugin()
+    	{
+    		super(1) ;
+    	}
+    }
+
+    public static class ThirdPasswordPlugin extends BasePasswordPlugin
+    {
+    	ThirdPasswordPlugin()
+    	{
+    		super(2) ;
+    	}
+    }
+}



More information about the jboss-svn-commits mailing list