[jboss-cvs] JBossAS SVN: r71039 - trunk/system-jmx/src/main/org/jboss/system/deployers/managed.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Mar 19 22:37:00 EDT 2008
Author: scott.stark at jboss.org
Date: 2008-03-19 22:36:59 -0400 (Wed, 19 Mar 2008)
New Revision: 71039
Added:
trunk/system-jmx/src/main/org/jboss/system/deployers/managed/SecurityActions.java
Modified:
trunk/system-jmx/src/main/org/jboss/system/deployers/managed/ServiceMetaDataICF.java
Log:
JBAS-5330, use the mbean class loader as the ICF callout TCL.
Added: trunk/system-jmx/src/main/org/jboss/system/deployers/managed/SecurityActions.java
===================================================================
--- trunk/system-jmx/src/main/org/jboss/system/deployers/managed/SecurityActions.java (rev 0)
+++ trunk/system-jmx/src/main/org/jboss/system/deployers/managed/SecurityActions.java 2008-03-20 02:36:59 UTC (rev 71039)
@@ -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.system.deployers.managed;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+/**
+ * Package privileged actions
+ *
+ * @author Scott.Stark at jboss.org
+ * @version $Revision$
+ */
+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);
+ }
+}
Property changes on: trunk/system-jmx/src/main/org/jboss/system/deployers/managed/SecurityActions.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ native
Modified: trunk/system-jmx/src/main/org/jboss/system/deployers/managed/ServiceMetaDataICF.java
===================================================================
--- trunk/system-jmx/src/main/org/jboss/system/deployers/managed/ServiceMetaDataICF.java 2008-03-20 02:24:30 UTC (rev 71038)
+++ trunk/system-jmx/src/main/org/jboss/system/deployers/managed/ServiceMetaDataICF.java 2008-03-20 02:36:59 UTC (rev 71039)
@@ -26,10 +26,8 @@
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
-import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
-import javax.management.ReflectionException;
import org.jboss.beans.info.spi.BeanInfo;
import org.jboss.beans.info.spi.PropertyInfo;
@@ -76,15 +74,13 @@
public Class<? extends Serializable> getManagedObjectClass(ServiceMetaData md)
throws ClassNotFoundException
{
+ ClassLoader prevLoader = SecurityActions.getContextClassLoader();
try
{
- // Fallback to TCL if there is no mbeanServer
- ClassLoader loader = null;
- if(mbeanServer != null)
- loader = mbeanServer.getClassLoader(md.getClassLoaderName());
- if(loader == null)
- loader = Thread.currentThread().getContextClassLoader();
+ ClassLoader loader = getServiceMetaDataCL(md);
Class moClass = loader.loadClass(md.getCode());
+ // Set the mbean class loader as the TCL
+ SecurityActions.setContextClassLoader(loader);
// Looks for a ManagementObjectClass annotation that defines
// an alternate class to scan for management annotations
@@ -109,6 +105,10 @@
{
throw new ClassNotFoundException("Failed to obtain mbean class loader", e);
}
+ finally
+ {
+ SecurityActions.setContextClassLoader(prevLoader);
+ }
}
public MetaValue getValue(BeanInfo beanInfo, ManagedProperty property, ServiceMetaData md)
@@ -118,127 +118,164 @@
if (name == null)
property.getName();
- // Get the attribute value from the metadata
+ ClassLoader prevLoader = SecurityActions.getContextClassLoader();
Object value = null;
- for (ServiceAttributeMetaData amd : md.getAttributes())
+ MetaValue mvalue = null;
+ try
{
- // The compare is case-insensitve due to the attribute/javabean case mismatch
- if (amd.getName().equalsIgnoreCase(name))
+ ClassLoader loader = getServiceMetaDataCL(md);
+ // Set the mbean class loader as the TCL
+ SecurityActions.setContextClassLoader(loader);
+
+ // Get the attribute value from the metadata
+ for (ServiceAttributeMetaData amd : md.getAttributes())
{
- value = amd.getValue();
- break;
+ // The compare is case-insensitve due to the attribute/javabean case mismatch
+ if (amd.getName().equalsIgnoreCase(name))
+ {
+ value = amd.getValue();
+ break;
+ }
}
- }
- // If the value is null, look to mbean for the value
- if (value == null)
- {
- ObjectName mbean = md.getObjectName();
- try
+ // If the value is null, look to mbean for the value
+ if (value == null)
{
- value = getMbeanServer().getAttribute(mbean, name);
- }
- catch (AttributeNotFoundException e)
- {
- // Try the alternate name
- String attribute = name;
- if(Character.isUpperCase(name.charAt(0)))
- attribute = Character.toLowerCase(name.charAt(0)) + name.substring(1);
- else
- attribute = Character.toUpperCase(name.charAt(0)) + name.substring(1);
+ ObjectName mbean = md.getObjectName();
try
{
- value = getMbeanServer().getAttribute(mbean, attribute);
+ value = getMbeanServer().getAttribute(mbean, name);
}
- catch(Exception e2)
+ catch (AttributeNotFoundException e)
{
- log.debug("Failed to get value from mbean for: "+attribute, e2);
- }
+ // Try the alternate name
+ String attribute = name;
+ if(Character.isUpperCase(name.charAt(0)))
+ attribute = Character.toLowerCase(name.charAt(0)) + name.substring(1);
+ else
+ attribute = Character.toUpperCase(name.charAt(0)) + name.substring(1);
+ try
+ {
+ value = getMbeanServer().getAttribute(mbean, attribute);
+ }
+ catch(Exception e2)
+ {
+ log.debug("Failed to get value from mbean for: "+attribute, e2);
+ }
+ }
+ catch(Exception e)
+ {
+ log.debug("Failed to get value from mbean for: "+name, e);
+ }
}
+
+ // Unwrap the ServiceValueMetaData types
+ if (value instanceof ServiceTextValueMetaData)
+ {
+ ServiceTextValueMetaData text = (ServiceTextValueMetaData) value;
+ value = text.getText();
+ }
+ else if (value instanceof ServiceDependencyValueMetaData)
+ {
+ ServiceDependencyValueMetaData depends = (ServiceDependencyValueMetaData) value;
+ value = depends.getDependency();
+ }
+ // TODO: unwrap other ServiceValueMetaData types
+
+ PropertyInfo propertyInfo = beanInfo.getProperty(name);
+ try
+ {
+ mvalue = metaValueFactory.create(value, propertyInfo.getType());
+ }
catch(Exception e)
{
- log.debug("Failed to get value from mbean for: "+name, e);
+ log.debug("Failed to get property value for bean: "+beanInfo.getName()
+ +", property: "+propertyInfo.getName(), e);
+ mvalue = metaValueFactory.create(null, propertyInfo.getType());
+ return mvalue;
}
}
-
- // Unwrap the ServiceValueMetaData types
- if (value instanceof ServiceTextValueMetaData)
+ catch(InstanceNotFoundException e)
{
- ServiceTextValueMetaData text = (ServiceTextValueMetaData) value;
- value = text.getText();
+ throw new IllegalStateException("Failed to obtain mbean class loader", e);
}
- else if (value instanceof ServiceDependencyValueMetaData)
+ finally
{
- ServiceDependencyValueMetaData depends = (ServiceDependencyValueMetaData) value;
- value = depends.getDependency();
+ SecurityActions.setContextClassLoader(prevLoader);
}
- // TODO: unwrap other ServiceValueMetaData types
-
- PropertyInfo propertyInfo = beanInfo.getProperty(name);
- MetaValue mvalue;
- try
- {
- mvalue = metaValueFactory.create(value, propertyInfo.getType());
- }
- catch(Exception e)
- {
- log.debug("Failed to get property value for bean: "+beanInfo.getName()
- +", property: "+propertyInfo.getName(), e);
- mvalue = metaValueFactory.create(null, propertyInfo.getType());
- }
return mvalue;
}
public void setValue(BeanInfo beanInfo, ManagedProperty property, ServiceMetaData md, MetaValue value)
{
- // First look to the mapped name
- String name = property.getMappedName();
- if (name == null)
- property.getName();
+ ClassLoader prevLoader = SecurityActions.getContextClassLoader();
+ try
+ {
+ ClassLoader loader = getServiceMetaDataCL(md);
+ // Set the mbean class loader as the TCL
+ SecurityActions.setContextClassLoader(loader);
- // Get the attribute value
- ServiceValueMetaData attributeValue = null;
- for (ServiceAttributeMetaData amd : md.getAttributes())
- {
- // The compare is case-insensitve due to the attribute/javabean case mismatch
- if (amd.getName().equalsIgnoreCase(name))
+ // First look to the mapped name
+ String name = property.getMappedName();
+ if (name == null)
+ property.getName();
+
+ // Get the attribute value
+ ServiceValueMetaData attributeValue = null;
+ for (ServiceAttributeMetaData amd : md.getAttributes())
{
- attributeValue = amd.getValue();
- break;
+ // The compare is case-insensitive due to the attribute/javabean case mismatch
+ if (amd.getName().equalsIgnoreCase(name))
+ {
+ attributeValue = amd.getValue();
+ break;
+ }
}
- }
- // There may not be an attribute value, see if there is a matching property
- if (attributeValue == null)
- {
- String aname = mapAttributeName(md, name);
- if(aname != null)
+ // There may not be an attribute value, see if there is a matching property
+ if (attributeValue == null)
{
- ServiceAttributeMetaData attr = new ServiceAttributeMetaData();
- attr.setName(aname);
- md.addAttribute(attr);
- attributeValue = new ServiceTextValueMetaData("");
- attr.setValue(attributeValue);
+ String aname = mapAttributeName(md, name);
+ if(aname != null)
+ {
+ ServiceAttributeMetaData attr = new ServiceAttributeMetaData();
+ attr.setName(aname);
+ md.addAttribute(attr);
+ attributeValue = new ServiceTextValueMetaData("");
+ attr.setValue(attributeValue);
+ }
}
- }
- if (attributeValue != null)
- {
- PropertyInfo propertyInfo = beanInfo.getProperty(name);
- Object plainValue = metaValueFactory.unwrap(value, propertyInfo.getType());
-
- // Unwrap the ServiceValueMetaData types
- if (attributeValue instanceof ServiceTextValueMetaData)
+ if (attributeValue != null)
{
- ServiceTextValueMetaData text = (ServiceTextValueMetaData) attributeValue;
- text.setText(String.valueOf(plainValue));
+ PropertyInfo propertyInfo = beanInfo.getProperty(name);
+ Object plainValue = metaValueFactory.unwrap(value, propertyInfo.getType());
+
+ // Unwrap the ServiceValueMetaData types
+ if (attributeValue instanceof ServiceTextValueMetaData)
+ {
+ ServiceTextValueMetaData text = (ServiceTextValueMetaData) attributeValue;
+ text.setText(String.valueOf(plainValue));
+ }
+ else if (value instanceof ServiceDependencyValueMetaData)
+ {
+ ServiceDependencyValueMetaData depends = (ServiceDependencyValueMetaData) attributeValue;
+ depends.setDependency(String.valueOf(plainValue));
+ }
+ // TODO: unwrap other ServiceValueMetaData types
+ else
+ {
+ throw new IllegalArgumentException("Unhandled attribute value type: " + name + "/" + md+", class="+attributeValue.getClass());
+ }
}
- else if (value instanceof ServiceDependencyValueMetaData)
- {
- ServiceDependencyValueMetaData depends = (ServiceDependencyValueMetaData) attributeValue;
- depends.setDependency(String.valueOf(plainValue));
- }
- // TODO: unwrap other ServiceValueMetaData types
+ else
+ throw new IllegalArgumentException("No matching attribute found: " + name + "/" + md);
}
- else
- throw new IllegalArgumentException("No matching attribute found: " + name + "/" + md);
+ catch(InstanceNotFoundException e)
+ {
+ throw new IllegalStateException("Failed to obtain mbean class loader", e);
+ }
+ finally
+ {
+ SecurityActions.setContextClassLoader(prevLoader);
+ }
}
public Object getComponentName(BeanInfo beanInfo, ManagedProperty property, ServiceMetaData md, MetaValue value)
@@ -253,6 +290,28 @@
}
/**
+ * Obtains the ServiceMetaData class loader from the
+ * getClassLoaderName value if there is an mbeanServer.
+ *
+ * @param md - the mbean metadata
+ * @return the ServiceMetaData.ClassLoaderName class loader if
+ * the mbeanServer has been set, the current TCL otherwise.
+ * @throws InstanceNotFoundException if no mbean class loader can be
+ * found by the ServiceMetaData.ClassLoaderName
+ */
+ private ClassLoader getServiceMetaDataCL(ServiceMetaData md)
+ throws InstanceNotFoundException
+ {
+ ClassLoader loader = null;
+ if(mbeanServer != null)
+ loader = mbeanServer.getClassLoader(md.getClassLoaderName());
+ // Fallback to TCL if there is no mbeanServer
+ if(loader == null)
+ loader = Thread.currentThread().getContextClassLoader();
+ return loader;
+ }
+
+ /**
* Try to find a matching mbean attribute
* @param name
* @return
More information about the jboss-cvs-commits
mailing list