[jboss-cvs] JBossAS SVN: r80171 - in trunk: security/src/main/org/jboss/security/integration and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Oct 28 21:39:26 EDT 2008


Author: scott.stark at jboss.org
Date: 2008-10-28 21:39:25 -0400 (Tue, 28 Oct 2008)
New Revision: 80171

Added:
   trunk/system/src/main/org/jboss/deployers/plugins/managed/BeanMetaDataICF.java
   trunk/system/src/main/org/jboss/deployers/plugins/managed/SecurityActions.java
   trunk/testsuite/src/main/org/jboss/test/profileservice/test/SecurityManagedObjectsTestCase.java
Modified:
   trunk/security/src/etc/deploy/security-jboss-beans.xml
   trunk/security/src/main/org/jboss/security/integration/JNDIBasedSecurityRegistration.java
   trunk/security/src/main/org/jboss/security/plugins/SecurityConfig.java
Log:
JBAS-5286, Add a BeanMetaDataICF for mc deployment managed object creation

Modified: trunk/security/src/etc/deploy/security-jboss-beans.xml
===================================================================
--- trunk/security/src/etc/deploy/security-jboss-beans.xml	2008-10-29 01:26:19 UTC (rev 80170)
+++ trunk/security/src/etc/deploy/security-jboss-beans.xml	2008-10-29 01:39:25 UTC (rev 80171)
@@ -40,6 +40,8 @@
    and SecurityManagement under java:/securityManagement -->
    <bean name="JNDIBasedSecurityRegistration" 
          class="org.jboss.security.integration.JNDIBasedSecurityRegistration">
+      <!-- This has to be on one line, see JBMDR-49 -->
+      <annotation>@org.jboss.managed.api.annotation.ManagementObject(name="JNDIBasedSecurityRegistration",componentType=@org.jboss.managed.api.annotation.ManagementComponent(type="MCBean", subtype="Security"))</annotation>
       <property name="policyRegistration"><inject bean="JBossSecurityPolicyRegistration" /></property>
       <property name="securityManagement"><inject bean="JNDIBasedSecurityManagement" /></property>
    </bean>

Modified: trunk/security/src/main/org/jboss/security/integration/JNDIBasedSecurityRegistration.java
===================================================================
--- trunk/security/src/main/org/jboss/security/integration/JNDIBasedSecurityRegistration.java	2008-10-29 01:26:19 UTC (rev 80170)
+++ trunk/security/src/main/org/jboss/security/integration/JNDIBasedSecurityRegistration.java	2008-10-29 01:39:25 UTC (rev 80171)
@@ -78,8 +78,29 @@
          throw new RuntimeException(e);
       }
    }
+
    
    
+   public String getPolicyRegistrationContextName()
+   {
+      return policyRegistrationContextName;
+   }
+
+   public void setPolicyRegistrationContextName(String policyRegistrationContextName)
+   {
+      this.policyRegistrationContextName = policyRegistrationContextName;
+   }
+
+   public String getSecurityManagementContextName()
+   {
+      return securityManagementContextName;
+   }
+
+   public void setSecurityManagementContextName(String securityManagementContextName)
+   {
+      this.securityManagementContextName = securityManagementContextName;
+   }
+
    private void establishPolicyRegistration() throws Exception
    {
       Context ctx = new InitialContext();

Modified: trunk/security/src/main/org/jboss/security/plugins/SecurityConfig.java
===================================================================
--- trunk/security/src/main/org/jboss/security/plugins/SecurityConfig.java	2008-10-29 01:26:19 UTC (rev 80170)
+++ trunk/security/src/main/org/jboss/security/plugins/SecurityConfig.java	2008-10-29 01:39:25 UTC (rev 80171)
@@ -44,7 +44,7 @@
  * @version $Revision: 58710 $
  */
 @JMX(name = "jboss.security:service=SecurityConfig", exposedInterface = SecurityConfigMBean.class)
- at ManagementObject(componentType=@ManagementComponent(type="MCBean", subtype="SecurityConfig"))
+ at ManagementObject(name="SecurityConfig", componentType=@ManagementComponent(type="MCBean", subtype="Security"))
 public class SecurityConfig extends ServiceMBeanSupport implements SecurityConfigMBean
 {
    /** The default Configuration mbean name */

Added: trunk/system/src/main/org/jboss/deployers/plugins/managed/BeanMetaDataICF.java
===================================================================
--- trunk/system/src/main/org/jboss/deployers/plugins/managed/BeanMetaDataICF.java	                        (rev 0)
+++ trunk/system/src/main/org/jboss/deployers/plugins/managed/BeanMetaDataICF.java	2008-10-29 01:39:25 UTC (rev 80171)
@@ -0,0 +1,195 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.deployers.plugins.managed;
+
+import java.lang.annotation.Annotation;
+import java.util.Set;
+
+import org.jboss.beans.info.spi.BeanInfo;
+import org.jboss.beans.info.spi.PropertyInfo;
+import org.jboss.beans.metadata.spi.AnnotationMetaData;
+import org.jboss.beans.metadata.spi.BeanMetaData;
+import org.jboss.dependency.spi.ControllerContext;
+import org.jboss.kernel.spi.dependency.KernelController;
+import org.jboss.logging.Logger;
+import org.jboss.managed.api.ManagedProperty;
+import org.jboss.managed.api.annotation.ManagementObjectClass;
+import org.jboss.managed.spi.factory.InstanceClassFactory;
+import org.jboss.metatype.api.values.MetaValue;
+import org.jboss.metatype.api.values.MetaValueFactory;
+
+/**
+ * An InstanceClassFactory for BeanMetaData
+ * 
+ * @author Scott.Stark at jboss.org
+ * @version $Revision:$
+ */
+public class BeanMetaDataICF
+   implements InstanceClassFactory<BeanMetaData>
+{
+   private static final Logger log = Logger.getLogger(BeanMetaDataICF.class);
+   private KernelController controller;
+   /** The meta value factory */
+   private MetaValueFactory metaValueFactory = MetaValueFactory.getInstance(); 
+
+   public KernelController getController()
+   {
+      return controller;
+   }
+   public void setController(KernelController controller)
+   {
+      this.controller = controller;
+   }
+
+   public MetaValueFactory getMetaValueFactory()
+   {
+      return metaValueFactory;
+   }
+   public void setMetaValueFactory(MetaValueFactory metaValueFactory)
+   {
+      this.metaValueFactory = metaValueFactory;
+   }
+
+   public Object locateBean(String beanName)
+   {
+      ControllerContext context = getController().getInstalledContext(beanName);
+      if (context == null)
+      {
+         return null;
+      }
+      return context.getTarget();
+   }
+
+   public Object getComponentName(BeanInfo beanInfo, ManagedProperty property,
+         BeanMetaData attachment, MetaValue value)
+   {
+      return attachment.getName();
+   }
+
+   public Class<?> getManagedObjectClass(BeanMetaData attachment)
+      throws ClassNotFoundException
+   {
+      Class<?> mocClass = null;
+
+      // Look for a ManagementObjectClass annotation
+      Set<AnnotationMetaData> annotations = attachment.getAnnotations();
+      if(annotations != null)
+      {
+         for(AnnotationMetaData amd : annotations)
+         {
+            Annotation ann = amd.getAnnotationInstance();
+            if(ann instanceof ManagementObjectClass)
+            {
+               ManagementObjectClass moc = (ManagementObjectClass) ann;
+               mocClass = moc.code();
+               log.debug("Saw ManagementObjectClass, "+mocClass+" for bean: "+attachment);
+               break;
+            }
+         }
+      }
+      // Use the bean from the metadata
+      if(mocClass == null)
+      {
+         String beanClassName = attachment.getBean();
+         if(beanClassName != null && beanClassName.length() > 0)
+         {
+            // TODO: TCL may not be correct
+            ClassLoader loader = getClassLoader(attachment);
+            mocClass = loader.loadClass(beanClassName);
+            log.debug("Using bean class:, "+mocClass+" for bean: "+attachment);
+         }
+      }
+      return mocClass;
+   }
+
+   public MetaValue getValue(BeanInfo beanInfo, ManagedProperty property,
+         BeanMetaData attachment)
+   {
+      // Get the property from the bean
+      // First look to the mapped name
+      String name = property.getMappedName();
+      if (name == null)
+         property.getName();
+      PropertyInfo propertyInfo = beanInfo.getProperty(name);
+      Object bean = locateBean(attachment.getName());
+      Object value = null;
+      MetaValue mvalue = null;
+
+      try
+      {
+         value = propertyInfo.get(bean);
+         mvalue = metaValueFactory.create(value, propertyInfo.getType());
+      }
+      catch(Throwable e)
+      {
+         log.debug("Failed to get property value for bean: "+beanInfo.getName()
+               +", property: "+propertyInfo.getName(), e);
+         mvalue = metaValueFactory.create(null, propertyInfo.getType());
+         return mvalue;
+      }
+
+      return mvalue;
+   }
+
+   public void setValue(BeanInfo beanInfo, ManagedProperty property,
+         BeanMetaData attachment, MetaValue value)
+   {
+      ClassLoader prevLoader = SecurityActions.getContextClassLoader();
+      String beanName = attachment.getName();
+      // First look to the mapped name
+      String name = property.getMappedName();
+      if (name == null)
+         property.getName();
+      try
+      {
+         ClassLoader loader = getClassLoader(attachment);
+         // Set the mbean class loader as the TCL
+         SecurityActions.setContextClassLoader(loader);
+
+         PropertyInfo propertyInfo = beanInfo.getProperty(name);
+         if(propertyInfo == null)
+            throw new IllegalArgumentException("No matching property found: " + name + "/" + beanName);
+
+         Object plainValue = metaValueFactory.unwrap(value, propertyInfo.getType());
+         Object bean = locateBean(beanName);
+         propertyInfo.set(bean, plainValue);
+      }
+      catch(Throwable e)
+      {
+         throw new IllegalStateException("Failed to set property value: "+name + "/" + beanName, e);
+      }
+      finally
+      {
+         SecurityActions.setContextClassLoader(prevLoader);
+      }
+   }
+
+   protected ClassLoader getClassLoader(BeanMetaData deployment)
+   {
+      ClassLoader loader = null;
+      // TODO...
+      // Fallback to TCL if there is no
+      if(loader == null)
+         loader = SecurityActions.getContextClassLoader();
+      return loader;
+   }
+}

Added: trunk/system/src/main/org/jboss/deployers/plugins/managed/SecurityActions.java
===================================================================
--- trunk/system/src/main/org/jboss/deployers/plugins/managed/SecurityActions.java	                        (rev 0)
+++ trunk/system/src/main/org/jboss/deployers/plugins/managed/SecurityActions.java	2008-10-29 01:39:25 UTC (rev 80171)
@@ -0,0 +1,160 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.deployers.plugins.managed;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+/**
+ * Package privileged actions
+ * 
+ * @author Scott.Stark at jboss.org
+ * @version $Revision: 71039 $
+ */
+class SecurityActions
+{
+   interface TCLAction
+   {
+      class UTIL
+      {
+         static TCLAction getTCLAction()
+         {
+            return System.getSecurityManager() == null ? NON_PRIVILEGED : PRIVILEGED;
+         }
+
+         static ClassLoader getContextClassLoader()
+         {
+            return getTCLAction().getContextClassLoader();
+         }
+
+         static ClassLoader getContextClassLoader(Thread thread)
+         {
+            return getTCLAction().getContextClassLoader(thread);
+         }
+
+         static void setContextClassLoader(ClassLoader cl)
+         {
+            getTCLAction().setContextClassLoader(cl);
+         }
+
+         static void setContextClassLoader(Thread thread, ClassLoader cl)
+         {
+            getTCLAction().setContextClassLoader(thread, cl);
+         }
+      }
+
+      TCLAction NON_PRIVILEGED = new TCLAction()
+      {
+         public ClassLoader getContextClassLoader()
+         {
+            return Thread.currentThread().getContextClassLoader();
+         }
+
+         public ClassLoader getContextClassLoader(Thread thread)
+         {
+            return thread.getContextClassLoader();
+         }
+
+         public void setContextClassLoader(ClassLoader cl)
+         {
+            Thread.currentThread().setContextClassLoader(cl);
+         }
+
+         public void setContextClassLoader(Thread thread, ClassLoader cl)
+         {
+            thread.setContextClassLoader(cl);
+         }
+      };
+
+      TCLAction PRIVILEGED = new TCLAction()
+      {
+         private final PrivilegedAction<ClassLoader> getTCLPrivilegedAction = new PrivilegedAction<ClassLoader>()
+         {
+            public ClassLoader run()
+            {
+               return Thread.currentThread().getContextClassLoader();
+            }
+         };
+
+         public ClassLoader getContextClassLoader()
+         {
+            return AccessController.doPrivileged(getTCLPrivilegedAction);
+         }
+
+         public ClassLoader getContextClassLoader(final Thread thread)
+         {
+            return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
+            {
+               public ClassLoader run()
+               {
+                  return thread.getContextClassLoader();
+               }
+            });
+         }
+
+         public void setContextClassLoader(final ClassLoader cl)
+         {
+            AccessController.doPrivileged(
+               new PrivilegedAction<ClassLoader>()
+               {
+                  public ClassLoader run()
+                  {
+                     Thread.currentThread().setContextClassLoader(cl);
+                     return null;
+                  }
+               }
+            );
+         }
+
+         public void setContextClassLoader(final Thread thread, final ClassLoader cl)
+         {
+            AccessController.doPrivileged(
+               new PrivilegedAction<ClassLoader>()
+               {
+                  public ClassLoader run()
+                  {
+                     thread.setContextClassLoader(cl);
+                     return null;
+                  }
+               }
+            );
+         }
+      };
+
+      ClassLoader getContextClassLoader();
+
+      ClassLoader getContextClassLoader(Thread thread);
+
+      void setContextClassLoader(ClassLoader cl);
+
+      void setContextClassLoader(Thread thread, ClassLoader cl);
+   }
+
+   static ClassLoader getContextClassLoader()
+   {
+      return TCLAction.UTIL.getContextClassLoader();
+   }
+   static void setContextClassLoader(ClassLoader loader)
+   {
+      TCLAction.UTIL.setContextClassLoader(loader);
+   }
+}
\ No newline at end of file

Added: trunk/testsuite/src/main/org/jboss/test/profileservice/test/SecurityManagedObjectsTestCase.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/profileservice/test/SecurityManagedObjectsTestCase.java	                        (rev 0)
+++ trunk/testsuite/src/main/org/jboss/test/profileservice/test/SecurityManagedObjectsTestCase.java	2008-10-29 01:39:25 UTC (rev 80171)
@@ -0,0 +1,90 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.profileservice.test;
+
+import java.util.Set;
+
+import org.jboss.deployers.spi.management.ManagementView;
+import org.jboss.managed.api.ComponentType;
+import org.jboss.managed.api.ManagedComponent;
+
+/**
+ * TODO: validate the expected management interfaces
+ * 
+ * @author Scott.Stark at jboss.org
+ * @version $Revision:$
+ */
+public class SecurityManagedObjectsTestCase
+   extends AbstractProfileServiceTest
+{
+   protected ManagementView activeView;
+
+   public SecurityManagedObjectsTestCase(String name)
+   {
+      super(name);
+   }
+
+   /**
+    * Looks for ComponentType("MCBean", "Security") type and logs them.
+    * @exception thrown if there are no matching components
+    */
+   public void testSecurityMCBeans()
+      throws Exception
+   {
+      ManagementView mgtMview = getManagementView();
+      ComponentType type = new ComponentType("MCBean", "Security");
+      Set<ManagedComponent> mcs = mgtMview.getComponentsForType(type);
+      assertTrue("There are MCBean,Security components", mcs.size() > 0);
+      getLog().debug("MCBeans: "+mcs);
+   }
+   /**
+    * Look at the SecurityConfig ManagedComponent
+    * @throws Exception
+    */
+   public void testSecurityConfig()
+      throws Exception
+   {
+      ManagementView mgtMview = getManagementView();
+      ComponentType type = new ComponentType("MCBean", "Security");
+      ManagedComponent mc = mgtMview.getComponent("SecurityConfig", type);
+      assertNotNull(mc);
+   }
+   /**
+    * Look at the JNDIBasedSecurityRegistration ManagedComponent
+    * @throws Exception
+    */
+   public void testJNDIBasedSecurityRegistration()
+      throws Exception
+   {
+      ManagementView mgtMview = getManagementView();
+      ComponentType type = new ComponentType("MCBean", "Security");
+      ManagedComponent mc = mgtMview.getComponent("JNDIBasedSecurityRegistration", type);
+      // TODO: assertNotNull(mc);
+   }
+
+   @Override
+   protected String getProfileName()
+   {
+      return "profileservice";
+   }
+
+}




More information about the jboss-cvs-commits mailing list