[embjopr-commits] EMBJOPR SVN: r566 - in trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit: as5 and 4 other directories.

embjopr-commits at lists.jboss.org embjopr-commits at lists.jboss.org
Mon Jul 20 11:05:38 EDT 2009


Author: ozizka at redhat.com
Date: 2009-07-20 11:05:38 -0400 (Mon, 20 Jul 2009)
New Revision: 566

Added:
   trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/jaas/
   trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/jaas/JBossCallbackHandler.java
   trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/jaas/JBossConfiguration.java
   trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/jaas/JaasAuthenticationInvocationHandler.java
   trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/jaas/JaasUtils.java
Modified:
   trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/EmbjoprTestCase.java
   trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/as5/ResourceTestBase.java
   trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/as5/connfactories/ConnFactoryConfigurationTest.java
   trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/as5/jmsDestinations/JMSConfigurationTest.java
   trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/as5/jmsDestinations/JMSMetricsTest.java
Log:
 * JAAS security added to ConnFactoryTest

Modified: trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/EmbjoprTestCase.java
===================================================================
--- trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/EmbjoprTestCase.java	2009-07-20 11:27:25 UTC (rev 565)
+++ trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/EmbjoprTestCase.java	2009-07-20 15:05:38 UTC (rev 566)
@@ -26,6 +26,7 @@
 import com.gargoylesoftware.htmlunit.html.*;
 import java.io.IOException;
 import java.util.*;
+import java.util.logging.Level;
 import org.jboss.logging.*;
 import org.apache.cactus.ServletTestCase;
 import org.jboss.jsfunit.framework.WebClientSpec;
@@ -48,6 +49,7 @@
 import org.jboss.jopr.jsfunit.exceptions.EmbJoprTestException;
 import org.jboss.jopr.jsfunit.exceptions.HtmlElementNotFoundException;
 import org.jboss.jopr.jsfunit.util.EmbJoprTestToolkit;
+import org.jboss.jopr.jsfunit.util.jaas.JaasUtils;
 
 
 /**
@@ -869,7 +871,8 @@
         
         return formattedPropertiesMap;
     }
-     
+
+
     /**
      * Check the properties for a particular managed component.
      * 
@@ -877,11 +880,22 @@
      * @param componentName - the name of the ManagedComponent
      * @param type - the component type
      */
-    protected void checkComponentProperties(Map<String, MetaValue> expectedProperties, 
-                                            String componentName, 
-                                            ComponentType type) throws Exception {
+    protected void checkComponentProperties(Map<String, MetaValue> expectedProperties, String componentName,
+						ComponentType type) throws Exception {
+			checkComponentProperties(expectedProperties, componentName, type, false);
+		}
+		
+    /**
+     * Check the properties for a particular managed component.
+     *
+     * @param expectedProperties maps property names to expected values
+     * @param componentName - the name of the ManagedComponent
+     * @param type - the component type
+     */
+    protected void checkComponentProperties(Map<String, MetaValue> expectedProperties, String componentName,
+						ComponentType type, boolean secured) throws Exception {
         
-        Map<String, MetaValue> actualProperties = getComponentProperties(componentName, type);
+        Map<String, MetaValue> actualProperties = getComponentProperties(componentName, type, secured);
         
         // Verify that the property values are correct
         for(String propertyName : expectedProperties.keySet()) {
@@ -891,13 +905,22 @@
         }
     }
 
+		
     /**
-     * Create a map of property names to property values for a particular
-     * component.
+     * Create a map of property names to property values for a particular component. Unsecured.
      */
     protected Map<String, MetaValue> getComponentProperties(String componentName,
                                                             ComponentType type) throws Exception {
-        ManagedComponent component = getManagedComponent(componentName, type);
+			return getComponentProperties(componentName, type, false);
+		}
+
+    /**
+     * Create a map of property names to property values for a particular component.
+     */
+    protected Map<String, MetaValue> getComponentProperties(String componentName,
+                                                            ComponentType type, boolean secured) throws Exception
+		{
+        ManagedComponent component = getManagedComponent(componentName, type, secured);
         assertNotNull("The returned component was null; Name: "+componentName, component);
         assertEquals(componentName, component.getName());
         
@@ -913,7 +936,8 @@
         
         return propertiesMap;
     }
-    
+
+		
     /**
      * Create a map of property names to property values for a particular
      * component. The desired property names are given by specificProperties.
@@ -946,9 +970,14 @@
      */
     protected ManagedComponent getManagedComponent(String componentName,
                                                    ComponentType type) throws Exception {
+			return getManagedComponent(componentName, type, false);
+		}
         
+    protected ManagedComponent getManagedComponent(String componentName,
+                                                   ComponentType type, boolean secured) throws Exception {
+
         // Get the ManagedComponent
-        ManagementView mgtView = getCurrentProfileView();
+        ManagementView mgtView = getCurrentProfileView( secured );
         ManagedComponent component = mgtView.getComponent(componentName, type);
         
         return component;
@@ -960,7 +989,16 @@
      * @param deployment - the deployment we are interested in
      */
     protected boolean isDeployed(String deployment) throws Exception {
-        ManagementView currentProfileView = getCurrentProfileView();
+			return isDeployed(deployment, false);
+		}
+    /**
+     * Return whether or not the given deployment is deployed.
+     *
+     * @param deployment - the deployment we are interested in
+		 * @param secured   If true, JAAS secured ManagementView is used.
+     */
+    protected boolean isDeployed(String deployment, boolean secured) throws Exception {
+        ManagementView currentProfileView = getCurrentProfileView(secured);
         ManagedDeployment managedDeployment = null;
         
         try {
@@ -975,6 +1013,21 @@
     /**
      * Get the profile service.
      */
+    protected ProfileService getProfileService( boolean secured ){
+			return secured ? getProfileServiceSecured() : getProfileService();
+		}
+
+    protected ProfileService getProfileServiceSecured()
+    {
+			try {
+				return JaasUtils.getInstance().getProfileService();
+			} catch (NamingException ex) {
+				// Farah's methods don't propagate exceptions -> report and swallow.
+				log.error("Can't get secured ProfileService: " + ex.toString(), ex);
+				return null;
+			}
+		}
+		
     protected ProfileService getProfileService()
     {
         ProfileService profileService = null;
@@ -1000,8 +1053,25 @@
 
     /**
      * Get the current profile view.
+		 * @param secured  If true, returns JAAS profile view of secured profile service.
      */
-    protected ManagementView getCurrentProfileView()
+    protected ManagementView getCurrentProfileView( boolean secured ){ return secured ? getCurrentProfileViewSecured() : getCurrentProfileView(); }
+
+    /**
+     * Get the current profile view.
+     */
+		protected ManagementView getCurrentProfileViewSecured(){
+			try {
+				ManagementView view = JaasUtils.getInstance().getManagementView();
+				view.load();
+				return view;
+			} catch (NamingException ex) {
+				log.error("Can't get ManagementView: "+ex);
+				return null;
+			}
+		}
+
+		protected ManagementView getCurrentProfileView()
     {
         ProfileService profileService = getProfileService();
         ManagementView currentProfileView = profileService.getViewManager();

Modified: trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/as5/ResourceTestBase.java
===================================================================
--- trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/as5/ResourceTestBase.java	2009-07-20 11:27:25 UTC (rev 565)
+++ trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/as5/ResourceTestBase.java	2009-07-20 15:05:38 UTC (rev 566)
@@ -180,17 +180,30 @@
     /**
      * Configure the properties associated with this resource
      * and verify that the properties were updated appropriately.
-     * 
+     *
      * Assumes the resource tree node (eg. "JMS Destinations", "Datasources",
      * etc.) is already expanded.
      */
-    protected void configureResource(String resourceCategory, 
+    protected void configureResource(String category, String subCategory, String name, Map<String, MetaValue> propsMap,
+        Map<String, MetaValue> propsMapChanges, ComponentType componentType, String expectedMessage) throws Exception {
+			configureResource(category, subCategory, name, propsMap, propsMapChanges, componentType, expectedMessage, false);
+		}
+		
+    /**
+     * Configure the properties associated with this resource
+     * and verify that the properties were updated appropriately.
+     *
+     * Assumes the resource tree node (eg. "JMS Destinations", "Datasources",
+     * etc.) is already expanded.
+     */
+    protected void configureResource(String resourceCategory,
                                      String resourceSubCategory,
                                      String resourceName,
                                      Map<String, MetaValue> propertiesMap,
                                      Map<String, MetaValue> propertiesMapChanges,
                                      ComponentType componentType,
-                                     String expectedMessage) throws Exception {
+                                     String expectedMessage,
+																		 boolean secured) throws Exception {
         
         // Navigate to the configuration page for the resource
         navigateToPage(resourceCategory, resourceSubCategory, 
@@ -202,7 +215,7 @@
         checkClientAndServerMessages(expectedMessage, expectedMessage, false);
         
         // Verify that the properties were set correctly
-        checkComponentProperties(propertiesMapChanges, resourceName, componentType);
+        checkComponentProperties(propertiesMapChanges, resourceName, componentType, secured);
     }
     
     /**

Modified: trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/as5/connfactories/ConnFactoryConfigurationTest.java
===================================================================
--- trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/as5/connfactories/ConnFactoryConfigurationTest.java	2009-07-20 11:27:25 UTC (rev 565)
+++ trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/as5/connfactories/ConnFactoryConfigurationTest.java	2009-07-20 15:05:38 UTC (rev 566)
@@ -22,13 +22,8 @@
 
 package org.jboss.jopr.jsfunit.as5.connfactories;
 
-import org.jboss.jopr.jsfunit.*;
-import com.gargoylesoftware.htmlunit.html.*;
-import java.io.*;
 import junit.framework.Test;
 import junit.framework.TestSuite;
-import org.jboss.jopr.jsfunit.exceptions.*;
-import org.jboss.jopr.jsfunit.as5.ResourceTestBase;
 import org.jboss.managed.api.ComponentType;
 import org.jboss.metatype.api.values.SimpleValueSupport;
 import org.jboss.metatype.api.values.MetaValue;
@@ -45,6 +40,10 @@
 
 public class ConnFactoryConfigurationTest extends ConnFactoryTest {
 
+		private static final boolean SECURED = true;
+		private static final boolean UNSECURED = false;
+
+
     /*
      * CONFIGURATION TESTS
      */
@@ -75,7 +74,7 @@
      */
     public void testConfigureTxConnectionFactoryChangePropertiesUsingExistingDSFile() throws Exception {
         String jndiName = "ChangePropertiesExistingTxCF";
-        Map<String, MetaValue> propertiesMap = getComponentProperties(jndiName, LOCAL_TX_COMPONENT_TYPE);
+        Map<String, MetaValue> propertiesMap = getComponentProperties(jndiName, LOCAL_TX_COMPONENT_TYPE, SECURED);
         
         // Change some property values that are already set 
         Map<String, MetaValue> propertiesMapChanges = new LinkedHashMap<String, MetaValue>();
@@ -131,24 +130,22 @@
     /**
      * Common code for changing the property values of a connection factory.
      */
-    private void changeConnFactoryProperties(String jndiName, 
+    private void changeConnFactoryProperties(String jndiName,
                                              CFType cfType,
                                              ComponentType componentType,
                                              Map<String, MetaValue> propertiesMapChanges,
                                              Map<String, MetaValue> propertiesMap) throws Exception {
         
         // The success message we should see
-        String expectedMessage = UPDATE_MESSAGE + cfType.getName() + " '" 
-                                 + jndiName + "'";
+        String expectedMessage = UPDATE_MESSAGE + cfType.getName() + " '"+jndiName+"'";
         
         // Change some property values and check that the properties of
         // the ManagedComponent were updated appropriately
         configureResource(CF_NAV_LABEL, cfType.getLabel(), jndiName, 
                           propertiesMap, propertiesMapChanges, componentType,
-                          expectedMessage);
+                          expectedMessage, SECURED);
   
-        assertTrue("The connection factory is not deployed ",
-                   isDeployed(jndiName + "-ds.xml"));
+        assertTrue("The connection factory is not deployed ", isDeployed(jndiName + "-ds.xml", SECURED));
         
         // Clean up
         deleteConnFactory(cfType, jndiName); 
@@ -228,15 +225,13 @@
         propertiesMap.remove("min-pool-size");
         
         // The success message we should see
-        String expectedMessage = UPDATE_MESSAGE + cfType.getName() + " '" 
-                                 + jndiName + "'";
+        String expectedMessage = UPDATE_MESSAGE + cfType.getName() + " '"+jndiName+"'";
         
         unsetResourceProperties(CF_NAV_LABEL, cfType.getLabel(), 
                                 jndiName, propertiesMap, propertiesToUnset, 
                                 componentType, expectedMessage);
         
-        assertTrue("The connection factory is not deployed ",
-                   isDeployed(jndiName + "-ds.xml"));
+        assertTrue("The connection factory is not deployed ", isDeployed(jndiName + "-ds.xml", SECURED));
      
         // Clean up
         deleteConnFactory(cfType, jndiName);   

Modified: trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/as5/jmsDestinations/JMSConfigurationTest.java
===================================================================
--- trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/as5/jmsDestinations/JMSConfigurationTest.java	2009-07-20 11:27:25 UTC (rev 565)
+++ trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/as5/jmsDestinations/JMSConfigurationTest.java	2009-07-20 15:05:38 UTC (rev 566)
@@ -22,13 +22,8 @@
 
 package org.jboss.jopr.jsfunit.as5.jmsDestinations;
 
-import com.gargoylesoftware.htmlunit.html.*;
-import junit.framework.Test;
 import java.util.Map;
-import java.util.HashMap;
 import java.util.LinkedHashMap;
-import org.jboss.jopr.jsfunit.*;
-import org.jboss.jopr.jsfunit.exceptions.EmbJoprTestException;
 import org.jboss.metatype.api.values.MetaValue;
 import org.jboss.metatype.api.values.SimpleValueSupport;
 import org.jboss.managed.api.ComponentType;

Modified: trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/as5/jmsDestinations/JMSMetricsTest.java
===================================================================
--- trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/as5/jmsDestinations/JMSMetricsTest.java	2009-07-20 11:27:25 UTC (rev 565)
+++ trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/as5/jmsDestinations/JMSMetricsTest.java	2009-07-20 15:05:38 UTC (rev 566)
@@ -399,8 +399,10 @@
         expectedMetrics.put(COUNT_DELTA, "0");
         expectedMetrics.put(DEPTH, "0");
         expectedMetrics.put(DEPTH_DELTA, "0");
-        expectedMetrics.put(TIME_LAST_UPDATE, "0");
-          
+        //expectedMetrics.put(TIME_LAST_UPDATE, "0"); // This currently changes
+				//(09:27:08) ozizka: There's a metric, Time Last Update, which has a description: The timestamp of the last message add
+				//(09:27:31) ozizka: But it changes upon queue receiver creation... so, is the description wrong or the behavior?
+				
         checkDestinationMetrics(jndiName, expectedMetrics, getQueueSummaryMetrics(),
                                 DestinationType.QUEUE);
     }

Added: trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/jaas/JBossCallbackHandler.java
===================================================================
--- trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/jaas/JBossCallbackHandler.java	                        (rev 0)
+++ trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/jaas/JBossCallbackHandler.java	2009-07-20 15:05:38 UTC (rev 566)
@@ -0,0 +1,69 @@
+/*
+ * Jopr Management Platform
+ * Copyright (C) 2005-2009 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation, and/or the GNU Lesser
+ * General Public License, version 2.1, also as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License and the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * and the GNU Lesser General Public License along with this program;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.jboss.jopr.jsfunit.util.jaas;
+
+import java.io.IOException;
+
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.auth.callback.NameCallback;
+import javax.security.auth.callback.PasswordCallback;
+
+/**
+ * @author Ian Springer
+ */
+public class JBossCallbackHandler implements CallbackHandler {
+    private String username;
+    private char[] password;
+
+    public JBossCallbackHandler(String username, String password)
+    {
+        this.username = username;
+        this.password = password.toCharArray();
+    }
+
+    public void handle(Callback[] callbacks) throws
+            IOException, UnsupportedCallbackException
+    {
+        for (Callback callback : callbacks)
+        {
+            //System.out.println("Handling Callback [" + callback + "]...");
+            if (callback instanceof NameCallback)
+            {
+
+                NameCallback nameCallback = (NameCallback)callback;
+                nameCallback.setName(this.username);
+            }
+            else if (callback instanceof PasswordCallback)
+            {
+                PasswordCallback passwordCallback = (PasswordCallback)callback;
+                passwordCallback.setPassword(this.password);
+            }
+            else
+            {
+                throw new UnsupportedCallbackException(callback, "Unrecognized Callback: " + callback);
+            }
+        }
+    }
+}

Added: trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/jaas/JBossConfiguration.java
===================================================================
--- trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/jaas/JBossConfiguration.java	                        (rev 0)
+++ trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/jaas/JBossConfiguration.java	2009-07-20 15:05:38 UTC (rev 566)
@@ -0,0 +1,66 @@
+/*
+ * Jopr Management Platform
+ * Copyright (C) 2005-2009 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation, and/or the GNU Lesser
+ * General Public License, version 2.1, also as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License and the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * and the GNU Lesser General Public License along with this program;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.jboss.jopr.jsfunit.util.jaas;
+
+import java.util.Map;
+import java.util.HashMap;
+
+import javax.security.auth.login.Configuration;
+import javax.security.auth.login.AppConfigurationEntry;
+
+/**
+ * A JAAS configuration for a JBoss client. This is the programmatic equivalent of the following auth.conf file:
+ *
+ * <code>
+ * jboss
+ * {
+ *   org.jboss.security.ClientLoginModule required
+ *     multi-threaded=true;
+ * };
+ * </code>
+ *
+ * @author Ian Springer
+ */
+public class JBossConfiguration extends Configuration {
+    public static final String JBOSS_ENTRY_NAME = "jboss";
+
+    private static final String JBOSS_LOGIN_MODULE_CLASS_NAME = "org.jboss.security.ClientLoginModule";
+    private static final String MULTI_THREADED_OPTION = "multi-threaded";
+
+    public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
+        if (JBOSS_ENTRY_NAME.equals(name)) {
+            Map options = new HashMap(1);
+            options.put(MULTI_THREADED_OPTION, Boolean.TRUE.toString());
+            AppConfigurationEntry appConfigurationEntry =
+                new AppConfigurationEntry(JBOSS_LOGIN_MODULE_CLASS_NAME,
+                    AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options);
+            return new AppConfigurationEntry[] {appConfigurationEntry};
+        } else {
+            throw new IllegalArgumentException("Unknown entry name: " + name);
+        }
+    }
+
+    public void refresh() {
+        return;
+    }
+}

Added: trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/jaas/JaasAuthenticationInvocationHandler.java
===================================================================
--- trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/jaas/JaasAuthenticationInvocationHandler.java	                        (rev 0)
+++ trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/jaas/JaasAuthenticationInvocationHandler.java	2009-07-20 15:05:38 UTC (rev 566)
@@ -0,0 +1,58 @@
+/*
+ * Jopr Management Platform
+ * Copyright (C) 2005-2009 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation, and/or the GNU Lesser
+ * General Public License, version 2.1, also as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License and the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * and the GNU Lesser General Public License along with this program;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.jboss.jopr.jsfunit.util.jaas;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+
+import javax.security.auth.login.LoginException;
+import javax.security.auth.login.LoginContext;
+import javax.security.auth.login.Configuration;
+
+/**
+ * @author Ian Springer
+ */
+public class JaasAuthenticationInvocationHandler implements InvocationHandler {    
+    private Object target;
+    private LoginContext loginContext;
+
+    public JaasAuthenticationInvocationHandler(Object target, String username, String password) {
+        this.target = target;                
+        JBossCallbackHandler jaasCallbackHandler = new JBossCallbackHandler(username, password);
+        Configuration jaasConfig = new JBossConfiguration();
+        try {
+            this.loginContext = new LoginContext(JBossConfiguration.JBOSS_ENTRY_NAME, null, jaasCallbackHandler,
+                    jaasConfig);
+        }
+        catch (LoginException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+        this.loginContext.login();
+        Object returnValue = method.invoke(this.target, args);
+        this.loginContext.logout();
+        return returnValue;
+    }
+}

Added: trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/jaas/JaasUtils.java
===================================================================
--- trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/jaas/JaasUtils.java	                        (rev 0)
+++ trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/util/jaas/JaasUtils.java	2009-07-20 15:05:38 UTC (rev 566)
@@ -0,0 +1,114 @@
+package org.jboss.jopr.jsfunit.util.jaas;
+
+import java.lang.reflect.Proxy;
+
+import java.util.Properties;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import org.jboss.deployers.spi.management.ManagementView;
+import org.jboss.deployers.spi.management.deploy.DeploymentManager;
+import org.jboss.logging.Logger;
+import org.jboss.profileservice.spi.ProfileService;
+
+
+
+/**
+ * Singleton class to keep JAAS secured ProfileService, ManagementView and DeploymentManager.
+ * Partially copied from Jopr's test.EjbClient.
+ * @author Ondrej Zizka
+ */
+public class JaasUtils {
+
+	private static final Logger log = Logger.getLogger(JaasUtils.class);
+
+	// Singleton.
+	private static JaasUtils INSTANCE = null;
+	public static JaasUtils getInstance() throws NamingException {
+		if( null == INSTANCE ){
+			INSTANCE = new JaasUtils();
+			INSTANCE.init( true );
+		}
+		return INSTANCE;
+	}
+
+	private static final String JNDI_LOGIN_INITIAL_CONTEXT_FACTORY = "org.jboss.security.jndi.JndiLoginInitialContextFactory";
+	private static final String NAMING_CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";
+	private static final String SECURE_PROFILE_SERVICE_JNDI_NAME = "SecureProfileService/remote";
+	private static final String SECURE_MANAGEMENT_VIEW_JNDI_NAME = "SecureManagementView/remote";
+	private static final String SECURE_DEPLOYMENT_MANAGER_JNDI_NAME = "SecureDeploymentManager/remote";
+	private static final String PROFILE_SERVICE_JNDI_NAME = "ProfileService";
+	private static final String MANAGEMENT_VIEW_JNDI_NAME = "ManagementView";
+	private static final String DEPLOYMENT_MANAGER_JNDI_NAME = "DeploymentManager";
+	private static final String PROFILE_SERVICE_PRINCIPAL = "admin";
+	private static final String PROFILE_SERVICE_CREDENTIALS = "admin";
+
+	
+	ProfileService profileService;
+	ManagementView managementView;
+	DeploymentManager deploymentManager;
+
+	public DeploymentManager getDeploymentManager() {		return deploymentManager;	}
+	public ManagementView getManagementView() {		return managementView;	}
+	public ProfileService getProfileService() {		return profileService;	}
+
+
+	/**
+	 * Obtains ProfileService, ManagementView and DeploymentManager.
+	 * @param useJaas  If true, all those three are secured with JAAS.
+	 */
+	private void init(boolean useJaas) throws NamingException {
+
+		Properties env = new Properties();
+		env.setProperty(Context.PROVIDER_URL, "jnp://127.0.0.1:1099/");
+		env.setProperty(Context.INITIAL_CONTEXT_FACTORY, NAMING_CONTEXT_FACTORY);
+
+		InitialContext initialContext = createInitialContext(env);
+
+
+		if (!useJaas) {
+			this.profileService = (ProfileService) lookup(initialContext, PROFILE_SERVICE_JNDI_NAME);
+			this.managementView = this.profileService.getViewManager();
+			this.deploymentManager = this.profileService.getDeploymentManager();
+		}
+		else {
+			this.profileService = (ProfileService) lookup(initialContext, PROFILE_SERVICE_JNDI_NAME);
+			this.managementView = (ManagementView) lookup(initialContext, MANAGEMENT_VIEW_JNDI_NAME);
+			this.deploymentManager = (DeploymentManager) lookup(initialContext, DEPLOYMENT_MANAGER_JNDI_NAME);
+
+			//apply the jaas security
+			ClassLoader classLoader = JaasUtils.class.getClassLoader();
+			this.profileService = (ProfileService) Proxy.newProxyInstance(classLoader,
+							new Class<?>[]{ProfileService.class},
+							new JaasAuthenticationInvocationHandler( this.profileService, PROFILE_SERVICE_PRINCIPAL, PROFILE_SERVICE_CREDENTIALS ));
+
+			this.managementView = (ManagementView) Proxy.newProxyInstance(classLoader,
+							new Class<?>[]{ManagementView.class},
+							new JaasAuthenticationInvocationHandler( this.managementView, PROFILE_SERVICE_PRINCIPAL, PROFILE_SERVICE_CREDENTIALS ));
+
+			this.deploymentManager = (DeploymentManager) Proxy.newProxyInstance(classLoader,
+							new Class<?>[]{DeploymentManager.class},
+							new JaasAuthenticationInvocationHandler( this.deploymentManager, PROFILE_SERVICE_PRINCIPAL, PROFILE_SERVICE_CREDENTIALS ));
+		}
+
+		//  This must be called before the other ManagementView operations may be used.
+		this.managementView.load();
+	}
+
+
+	private static InitialContext createInitialContext(Properties env) throws NamingException {
+		log.info("Creating JNDI InitialContext with env [" + env + "]...");
+		InitialContext initialContext = new InitialContext(env);
+		log.info("Created JNDI InitialContext [" + initialContext + "].");
+		return initialContext;
+	}
+
+	private static Object lookup(InitialContext initialContext, String name) throws NamingException {
+		log.info("Looking up name '" + name + "' from InitialContext...");
+		Object obj = initialContext.lookup(name);
+		log.info("Found Object: " + obj);
+		return obj;
+	}
+}// class



More information about the embjopr-commits mailing list