org.springframework.jmx.support.JmxUtils Java Examples

The following examples show how to use org.springframework.jmx.support.JmxUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConnectorDelegate.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Connects to the remote {@code MBeanServer} using the configured {@code JMXServiceURL}:
 * to the specified JMX service, or to a local MBeanServer if no service URL specified.
 * @param serviceUrl the JMX service URL to connect to (may be {@code null})
 * @param environment the JMX environment for the connector (may be {@code null})
 * @param agentId the local JMX MBeanServer's agent id (may be {@code null})
 */
public MBeanServerConnection connect(JMXServiceURL serviceUrl, Map<String, ?> environment, String agentId)
		throws MBeanServerNotFoundException {

	if (serviceUrl != null) {
		if (logger.isDebugEnabled()) {
			logger.debug("Connecting to remote MBeanServer at URL [" + serviceUrl + "]");
		}
		try {
			this.connector = JMXConnectorFactory.connect(serviceUrl, environment);
			return this.connector.getMBeanServerConnection();
		}
		catch (IOException ex) {
			throw new MBeanServerNotFoundException("Could not connect to remote MBeanServer [" + serviceUrl + "]", ex);
		}
	}
	else {
		logger.debug("Attempting to locate local MBeanServer");
		return JmxUtils.locateMBeanServer(agentId);
	}
}
 
Example #2
Source File: MBeanExporter.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public ObjectName registerManagedResource(Object managedResource) throws MBeanExportException {
	Assert.notNull(managedResource, "Managed resource must not be null");
	ObjectName objectName;
	try {
		objectName = getObjectName(managedResource, null);
		if (this.ensureUniqueRuntimeObjectNames) {
			objectName = JmxUtils.appendIdentityToObjectName(objectName, managedResource);
		}
	}
	catch (Throwable ex) {
		throw new MBeanExportException("Unable to generate ObjectName for MBean [" + managedResource + "]", ex);
	}
	registerManagedResource(managedResource, objectName);
	return objectName;
}
 
Example #3
Source File: ConnectorDelegate.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Connects to the remote {@code MBeanServer} using the configured {@code JMXServiceURL}:
 * to the specified JMX service, or to a local MBeanServer if no service URL specified.
 * @param serviceUrl the JMX service URL to connect to (may be {@code null})
 * @param environment the JMX environment for the connector (may be {@code null})
 * @param agentId the local JMX MBeanServer's agent id (may be {@code null})
 */
public MBeanServerConnection connect(JMXServiceURL serviceUrl, Map<String, ?> environment, String agentId)
		throws MBeanServerNotFoundException {

	if (serviceUrl != null) {
		if (logger.isDebugEnabled()) {
			logger.debug("Connecting to remote MBeanServer at URL [" + serviceUrl + "]");
		}
		try {
			this.connector = JMXConnectorFactory.connect(serviceUrl, environment);
			return this.connector.getMBeanServerConnection();
		}
		catch (IOException ex) {
			throw new MBeanServerNotFoundException("Could not connect to remote MBeanServer [" + serviceUrl + "]", ex);
		}
	}
	else {
		logger.debug("Attempting to locate local MBeanServer");
		return JmxUtils.locateMBeanServer(agentId);
	}
}
 
Example #4
Source File: ConnectorDelegate.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Connects to the remote {@code MBeanServer} using the configured {@code JMXServiceURL}:
 * to the specified JMX service, or to a local MBeanServer if no service URL specified.
 * @param serviceUrl the JMX service URL to connect to (may be {@code null})
 * @param environment the JMX environment for the connector (may be {@code null})
 * @param agentId the local JMX MBeanServer's agent id (may be {@code null})
 */
public MBeanServerConnection connect(@Nullable JMXServiceURL serviceUrl, @Nullable Map<String, ?> environment, @Nullable String agentId)
		throws MBeanServerNotFoundException {

	if (serviceUrl != null) {
		if (logger.isDebugEnabled()) {
			logger.debug("Connecting to remote MBeanServer at URL [" + serviceUrl + "]");
		}
		try {
			this.connector = JMXConnectorFactory.connect(serviceUrl, environment);
			return this.connector.getMBeanServerConnection();
		}
		catch (IOException ex) {
			throw new MBeanServerNotFoundException("Could not connect to remote MBeanServer [" + serviceUrl + "]", ex);
		}
	}
	else {
		logger.debug("Attempting to locate local MBeanServer");
		return JmxUtils.locateMBeanServer(agentId);
	}
}
 
Example #5
Source File: MBeanClientInterceptor.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Routes a method invocation (not a property get/set) to the corresponding
 * operation on the managed resource.
 * @param method the method corresponding to operation on the managed resource.
 * @param args the invocation arguments
 * @return the value returned by the method invocation.
 */
private Object invokeOperation(Method method, Object[] args) throws JMException, IOException {
	Assert.state(this.serverToUse != null, "No MBeanServerConnection available");

	MethodCacheKey key = new MethodCacheKey(method.getName(), method.getParameterTypes());
	MBeanOperationInfo info = this.allowedOperations.get(key);
	if (info == null) {
		throw new InvalidInvocationException("Operation '" + method.getName() +
				"' is not exposed on the management interface");
	}

	String[] signature;
	synchronized (this.signatureCache) {
		signature = this.signatureCache.get(method);
		if (signature == null) {
			signature = JmxUtils.getMethodSignature(method);
			this.signatureCache.put(method, signature);
		}
	}

	return this.serverToUse.invoke(this.objectName, method.getName(), args, signature);
}
 
Example #6
Source File: MBeanExporter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public ObjectName registerManagedResource(Object managedResource) throws MBeanExportException {
	Assert.notNull(managedResource, "Managed resource must not be null");
	ObjectName objectName;
	try {
		objectName = getObjectName(managedResource, null);
		if (this.ensureUniqueRuntimeObjectNames) {
			objectName = JmxUtils.appendIdentityToObjectName(objectName, managedResource);
		}
	}
	catch (Throwable ex) {
		throw new MBeanExportException("Unable to generate ObjectName for MBean [" + managedResource + "]", ex);
	}
	registerManagedResource(managedResource, objectName);
	return objectName;
}
 
Example #7
Source File: MBeanExporter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Build an adapted MBean for the given bean instance, if possible.
 * <p>The default implementation builds a JMX 1.2 StandardMBean
 * for the target's MBean/MXBean interface in case of an AOP proxy,
 * delegating the interface's management operations to the proxy.
 * @param bean the original bean instance
 * @return the adapted MBean, or {@code null} if not possible
 */
@SuppressWarnings("unchecked")
protected DynamicMBean adaptMBeanIfPossible(Object bean) throws JMException {
	Class<?> targetClass = AopUtils.getTargetClass(bean);
	if (targetClass != bean.getClass()) {
		Class<?> ifc = JmxUtils.getMXBeanInterface(targetClass);
		if (ifc != null) {
			if (!ifc.isInstance(bean)) {
				throw new NotCompliantMBeanException("Managed bean [" + bean +
						"] has a target class with an MXBean interface but does not expose it in the proxy");
			}
			return new StandardMBean(bean, ((Class<Object>) ifc), true);
		}
		else {
			ifc = JmxUtils.getMBeanInterface(targetClass);
			if (ifc != null) {
				if (!ifc.isInstance(bean)) {
					throw new NotCompliantMBeanException("Managed bean [" + bean +
							"] has a target class with an MBean interface but does not expose it in the proxy");
				}
				return new StandardMBean(bean, ((Class<Object>) ifc));
			}
		}
	}
	return null;
}
 
Example #8
Source File: MBeanClientInterceptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Routes a method invocation (not a property get/set) to the corresponding
 * operation on the managed resource.
 * @param method the method corresponding to operation on the managed resource.
 * @param args the invocation arguments
 * @return the value returned by the method invocation.
 */
private Object invokeOperation(Method method, Object[] args) throws JMException, IOException {
	MethodCacheKey key = new MethodCacheKey(method.getName(), method.getParameterTypes());
	MBeanOperationInfo info = this.allowedOperations.get(key);
	if (info == null) {
		throw new InvalidInvocationException("Operation '" + method.getName() +
				"' is not exposed on the management interface");
	}
	String[] signature = null;
	synchronized (this.signatureCache) {
		signature = this.signatureCache.get(method);
		if (signature == null) {
			signature = JmxUtils.getMethodSignature(method);
			this.signatureCache.put(method, signature);
		}
	}
	return this.serverToUse.invoke(this.objectName, method.getName(), args, signature);
}
 
Example #9
Source File: MBeanClientInterceptor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Routes a method invocation (not a property get/set) to the corresponding
 * operation on the managed resource.
 * @param method the method corresponding to operation on the managed resource.
 * @param args the invocation arguments
 * @return the value returned by the method invocation.
 */
private Object invokeOperation(Method method, Object[] args) throws JMException, IOException {
	MethodCacheKey key = new MethodCacheKey(method.getName(), method.getParameterTypes());
	MBeanOperationInfo info = this.allowedOperations.get(key);
	if (info == null) {
		throw new InvalidInvocationException("Operation '" + method.getName() +
				"' is not exposed on the management interface");
	}
	String[] signature = null;
	synchronized (this.signatureCache) {
		signature = this.signatureCache.get(method);
		if (signature == null) {
			signature = JmxUtils.getMethodSignature(method);
			this.signatureCache.put(method, signature);
		}
	}
	return this.serverToUse.invoke(this.objectName, method.getName(), args, signature);
}
 
Example #10
Source File: MBeanExporter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public ObjectName registerManagedResource(Object managedResource) throws MBeanExportException {
	Assert.notNull(managedResource, "Managed resource must not be null");
	ObjectName objectName;
	try {
		objectName = getObjectName(managedResource, null);
		if (this.ensureUniqueRuntimeObjectNames) {
			objectName = JmxUtils.appendIdentityToObjectName(objectName, managedResource);
		}
	}
	catch (Exception ex) {
		throw new MBeanExportException("Unable to generate ObjectName for MBean [" + managedResource + "]", ex);
	}
	registerManagedResource(managedResource, objectName);
	return objectName;
}
 
Example #11
Source File: MBeanExporter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Build an adapted MBean for the given bean instance, if possible.
 * <p>The default implementation builds a JMX 1.2 StandardMBean
 * for the target's MBean/MXBean interface in case of an AOP proxy,
 * delegating the interface's management operations to the proxy.
 * @param bean the original bean instance
 * @return the adapted MBean, or {@code null} if not possible
 */
@SuppressWarnings("unchecked")
protected DynamicMBean adaptMBeanIfPossible(Object bean) throws JMException {
	Class<?> targetClass = AopUtils.getTargetClass(bean);
	if (targetClass != bean.getClass()) {
		Class<?> ifc = JmxUtils.getMXBeanInterface(targetClass);
		if (ifc != null) {
			if (!ifc.isInstance(bean)) {
				throw new NotCompliantMBeanException("Managed bean [" + bean +
						"] has a target class with an MXBean interface but does not expose it in the proxy");
			}
			return new StandardMBean(bean, ((Class<Object>) ifc), true);
		}
		else {
			ifc = JmxUtils.getMBeanInterface(targetClass);
			if (ifc != null) {
				if (!ifc.isInstance(bean)) {
					throw new NotCompliantMBeanException("Managed bean [" + bean +
							"] has a target class with an MBean interface but does not expose it in the proxy");
				}
				return new StandardMBean(bean, ((Class<Object>) ifc));
			}
		}
	}
	return null;
}
 
Example #12
Source File: MBeanExporter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public ObjectName registerManagedResource(Object managedResource) throws MBeanExportException {
	Assert.notNull(managedResource, "Managed resource must not be null");
	ObjectName objectName;
	try {
		objectName = getObjectName(managedResource, null);
		if (this.ensureUniqueRuntimeObjectNames) {
			objectName = JmxUtils.appendIdentityToObjectName(objectName, managedResource);
		}
	}
	catch (Throwable ex) {
		throw new MBeanExportException("Unable to generate ObjectName for MBean [" + managedResource + "]", ex);
	}
	registerManagedResource(managedResource, objectName);
	return objectName;
}
 
Example #13
Source File: ConnectorDelegate.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Connects to the remote {@code MBeanServer} using the configured {@code JMXServiceURL}:
 * to the specified JMX service, or to a local MBeanServer if no service URL specified.
 * @param serviceUrl the JMX service URL to connect to (may be {@code null})
 * @param environment the JMX environment for the connector (may be {@code null})
 * @param agentId the local JMX MBeanServer's agent id (may be {@code null})
 */
public MBeanServerConnection connect(@Nullable JMXServiceURL serviceUrl, @Nullable Map<String, ?> environment, @Nullable String agentId)
		throws MBeanServerNotFoundException {

	if (serviceUrl != null) {
		if (logger.isDebugEnabled()) {
			logger.debug("Connecting to remote MBeanServer at URL [" + serviceUrl + "]");
		}
		try {
			this.connector = JMXConnectorFactory.connect(serviceUrl, environment);
			return this.connector.getMBeanServerConnection();
		}
		catch (IOException ex) {
			throw new MBeanServerNotFoundException("Could not connect to remote MBeanServer [" + serviceUrl + "]", ex);
		}
	}
	else {
		logger.debug("Attempting to locate local MBeanServer");
		return JmxUtils.locateMBeanServer(agentId);
	}
}
 
Example #14
Source File: MBeanClientInterceptor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Routes a method invocation (not a property get/set) to the corresponding
 * operation on the managed resource.
 * @param method the method corresponding to operation on the managed resource.
 * @param args the invocation arguments
 * @return the value returned by the method invocation.
 */
private Object invokeOperation(Method method, Object[] args) throws JMException, IOException {
	Assert.state(this.serverToUse != null, "No MBeanServerConnection available");

	MethodCacheKey key = new MethodCacheKey(method.getName(), method.getParameterTypes());
	MBeanOperationInfo info = this.allowedOperations.get(key);
	if (info == null) {
		throw new InvalidInvocationException("Operation '" + method.getName() +
				"' is not exposed on the management interface");
	}

	String[] signature;
	synchronized (this.signatureCache) {
		signature = this.signatureCache.get(method);
		if (signature == null) {
			signature = JmxUtils.getMethodSignature(method);
			this.signatureCache.put(method, signature);
		}
	}

	return this.serverToUse.invoke(this.objectName, method.getName(), args, signature);
}
 
Example #15
Source File: CamelContextMetadataMBeanTest.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuilder() throws Exception {
    final MBeanServer mBeanServer = JmxUtils.locateMBeanServer();
    final Set<ObjectInstance> mBeans = mBeanServer.queryMBeans(ObjectName.getInstance("io.syndesis.camel:*"), null);
    assertThat(mBeans).hasSize(1);

    final ObjectName objectName = mBeans.iterator().next().getObjectName();
    final AttributeList attributes = mBeanServer.getAttributes(objectName, ATTRIBUTES);
    assertThat(attributes.asList()).hasSize(ATTRIBUTES.length);
}
 
Example #16
Source File: MBeanClientInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Object invokeAttribute(PropertyDescriptor pd, MethodInvocation invocation)
		throws JMException, IOException {

	String attributeName = JmxUtils.getAttributeName(pd, this.useStrictCasing);
	MBeanAttributeInfo inf = this.allowedAttributes.get(attributeName);
	// If no attribute is returned, we know that it is not defined in the
	// management interface.
	if (inf == null) {
		throw new InvalidInvocationException(
				"Attribute '" + pd.getName() + "' is not exposed on the management interface");
	}
	if (invocation.getMethod().equals(pd.getReadMethod())) {
		if (inf.isReadable()) {
			return this.serverToUse.getAttribute(this.objectName, attributeName);
		}
		else {
			throw new InvalidInvocationException("Attribute '" + attributeName + "' is not readable");
		}
	}
	else if (invocation.getMethod().equals(pd.getWriteMethod())) {
		if (inf.isWritable()) {
			this.serverToUse.setAttribute(this.objectName, new Attribute(attributeName, invocation.getArguments()[0]));
			return null;
		}
		else {
			throw new InvalidInvocationException("Attribute '" + attributeName + "' is not writable");
		}
	}
	else {
		throw new IllegalStateException(
				"Method [" + invocation.getMethod() + "] is neither a bean property getter nor a setter");
	}
}
 
Example #17
Source File: AnnotationMBeanInfoAssembler.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Nonnull
private Class findJmxInterface(String beanKey, Class<?> beanClass) {
    Class cachedInterface = interfaceCache.get(beanKey);
    if (cachedInterface != null) {
        return cachedInterface;
    }

    Class mbeanInterface = JmxUtils.getMBeanInterface(beanClass);
    if (mbeanInterface != null) { // found with MBean ending
        interfaceCache.put(beanKey, mbeanInterface);
        return mbeanInterface;
    }

    Class[] ifaces = ClassUtils.getAllInterfacesForClass(beanClass);

    for (Class ifc : ifaces) {
        ManagedResource metadata = attributeSource.getManagedResource(ifc);
        if (metadata != null) { // found with @ManagedResource annotation
            interfaceCache.put(beanKey, ifc);
            return ifc;
        }
    }

    throw new IllegalArgumentException(String.format(
            "Bean %s doesn't implement management interfaces. Management interface should either follow naming scheme or be annotated by @ManagedResource",
            beanKey));
}
 
Example #18
Source File: MBeanExporter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() {
	// If no server was provided then try to find one. This is useful in an environment
	// where there is already an MBeanServer loaded.
	if (this.server == null) {
		this.server = JmxUtils.locateMBeanServer();
	}
}
 
Example #19
Source File: MBeanClientInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Object invokeAttribute(PropertyDescriptor pd, MethodInvocation invocation)
		throws JMException, IOException {

	String attributeName = JmxUtils.getAttributeName(pd, this.useStrictCasing);
	MBeanAttributeInfo inf = this.allowedAttributes.get(attributeName);
	// If no attribute is returned, we know that it is not defined in the
	// management interface.
	if (inf == null) {
		throw new InvalidInvocationException(
				"Attribute '" + pd.getName() + "' is not exposed on the management interface");
	}
	if (invocation.getMethod().equals(pd.getReadMethod())) {
		if (inf.isReadable()) {
			return this.serverToUse.getAttribute(this.objectName, attributeName);
		}
		else {
			throw new InvalidInvocationException("Attribute '" + attributeName + "' is not readable");
		}
	}
	else if (invocation.getMethod().equals(pd.getWriteMethod())) {
		if (inf.isWritable()) {
			this.serverToUse.setAttribute(this.objectName, new Attribute(attributeName, invocation.getArguments()[0]));
			return null;
		}
		else {
			throw new InvalidInvocationException("Attribute '" + attributeName + "' is not writable");
		}
	}
	else {
		throw new IllegalStateException(
				"Method [" + invocation.getMethod() + "] is neither a bean property getter nor a setter");
	}
}
 
Example #20
Source File: MBeanExporter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() {
	// If no server was provided then try to find one. This is useful in an environment
	// where there is already an MBeanServer loaded.
	if (this.server == null) {
		this.server = JmxUtils.locateMBeanServer();
	}
}
 
Example #21
Source File: MBeanClientInterceptor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
private Object invokeAttribute(PropertyDescriptor pd, MethodInvocation invocation)
		throws JMException, IOException {

	Assert.state(this.serverToUse != null, "No MBeanServerConnection available");

	String attributeName = JmxUtils.getAttributeName(pd, this.useStrictCasing);
	MBeanAttributeInfo inf = this.allowedAttributes.get(attributeName);
	// If no attribute is returned, we know that it is not defined in the
	// management interface.
	if (inf == null) {
		throw new InvalidInvocationException(
				"Attribute '" + pd.getName() + "' is not exposed on the management interface");
	}

	if (invocation.getMethod().equals(pd.getReadMethod())) {
		if (inf.isReadable()) {
			return this.serverToUse.getAttribute(this.objectName, attributeName);
		}
		else {
			throw new InvalidInvocationException("Attribute '" + attributeName + "' is not readable");
		}
	}
	else if (invocation.getMethod().equals(pd.getWriteMethod())) {
		if (inf.isWritable()) {
			this.serverToUse.setAttribute(this.objectName, new Attribute(attributeName, invocation.getArguments()[0]));
			return null;
		}
		else {
			throw new InvalidInvocationException("Attribute '" + attributeName + "' is not writable");
		}
	}
	else {
		throw new IllegalStateException(
				"Method [" + invocation.getMethod() + "] is neither a bean property getter nor a setter");
	}
}
 
Example #22
Source File: MBeanExporter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void afterPropertiesSet() {
	// If no server was provided then try to find one. This is useful in an environment
	// where there is already an MBeanServer loaded.
	if (this.server == null) {
		this.server = JmxUtils.locateMBeanServer();
	}
}
 
Example #23
Source File: MBeanExporter.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Build an adapted MBean for the given bean instance, if possible.
 * <p>The default implementation builds a JMX 1.2 StandardMBean
 * for the target's MBean/MXBean interface in case of an AOP proxy,
 * delegating the interface's management operations to the proxy.
 * @param bean the original bean instance
 * @return the adapted MBean, or {@code null} if not possible
 */
@SuppressWarnings("unchecked")
@Nullable
protected DynamicMBean adaptMBeanIfPossible(Object bean) throws JMException {
	Class<?> targetClass = AopUtils.getTargetClass(bean);
	if (targetClass != bean.getClass()) {
		Class<?> ifc = JmxUtils.getMXBeanInterface(targetClass);
		if (ifc != null) {
			if (!ifc.isInstance(bean)) {
				throw new NotCompliantMBeanException("Managed bean [" + bean +
						"] has a target class with an MXBean interface but does not expose it in the proxy");
			}
			return new StandardMBean(bean, ((Class<Object>) ifc), true);
		}
		else {
			ifc = JmxUtils.getMBeanInterface(targetClass);
			if (ifc != null) {
				if (!ifc.isInstance(bean)) {
					throw new NotCompliantMBeanException("Managed bean [" + bean +
							"] has a target class with an MBean interface but does not expose it in the proxy");
				}
				return new StandardMBean(bean, ((Class<Object>) ifc));
			}
		}
	}
	return null;
}
 
Example #24
Source File: MBeanExporter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Build an adapted MBean for the given bean instance, if possible.
 * <p>The default implementation builds a JMX 1.2 StandardMBean
 * for the target's MBean/MXBean interface in case of an AOP proxy,
 * delegating the interface's management operations to the proxy.
 * @param bean the original bean instance
 * @return the adapted MBean, or {@code null} if not possible
 */
@SuppressWarnings("unchecked")
@Nullable
protected DynamicMBean adaptMBeanIfPossible(Object bean) throws JMException {
	Class<?> targetClass = AopUtils.getTargetClass(bean);
	if (targetClass != bean.getClass()) {
		Class<?> ifc = JmxUtils.getMXBeanInterface(targetClass);
		if (ifc != null) {
			if (!ifc.isInstance(bean)) {
				throw new NotCompliantMBeanException("Managed bean [" + bean +
						"] has a target class with an MXBean interface but does not expose it in the proxy");
			}
			return new StandardMBean(bean, ((Class<Object>) ifc), true);
		}
		else {
			ifc = JmxUtils.getMBeanInterface(targetClass);
			if (ifc != null) {
				if (!ifc.isInstance(bean)) {
					throw new NotCompliantMBeanException("Managed bean [" + bean +
							"] has a target class with an MBean interface but does not expose it in the proxy");
				}
				return new StandardMBean(bean, ((Class<Object>) ifc));
			}
		}
	}
	return null;
}
 
Example #25
Source File: MBeanExporter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void afterPropertiesSet() {
	// If no server was provided then try to find one. This is useful in an environment
	// where there is already an MBeanServer loaded.
	if (this.server == null) {
		this.server = JmxUtils.locateMBeanServer();
	}
}
 
Example #26
Source File: MBeanClientInterceptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
private Object invokeAttribute(PropertyDescriptor pd, MethodInvocation invocation)
		throws JMException, IOException {

	Assert.state(this.serverToUse != null, "No MBeanServerConnection available");

	String attributeName = JmxUtils.getAttributeName(pd, this.useStrictCasing);
	MBeanAttributeInfo inf = this.allowedAttributes.get(attributeName);
	// If no attribute is returned, we know that it is not defined in the
	// management interface.
	if (inf == null) {
		throw new InvalidInvocationException(
				"Attribute '" + pd.getName() + "' is not exposed on the management interface");
	}

	if (invocation.getMethod().equals(pd.getReadMethod())) {
		if (inf.isReadable()) {
			return this.serverToUse.getAttribute(this.objectName, attributeName);
		}
		else {
			throw new InvalidInvocationException("Attribute '" + attributeName + "' is not readable");
		}
	}
	else if (invocation.getMethod().equals(pd.getWriteMethod())) {
		if (inf.isWritable()) {
			this.serverToUse.setAttribute(this.objectName, new Attribute(attributeName, invocation.getArguments()[0]));
			return null;
		}
		else {
			throw new InvalidInvocationException("Attribute '" + attributeName + "' is not writable");
		}
	}
	else {
		throw new IllegalStateException(
				"Method [" + invocation.getMethod() + "] is neither a bean property getter nor a setter");
	}
}
 
Example #27
Source File: AbstractReflectiveMBeanInfoAssembler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Iterate through all properties on the MBean class and gives subclasses
 * the chance to vote on the inclusion of both the accessor and mutator.
 * If a particular accessor or mutator is voted for inclusion, the appropriate
 * metadata is assembled and passed to the subclass for descriptor population.
 * @param managedBean the bean instance (might be an AOP proxy)
 * @param beanKey the key associated with the MBean in the beans map
 * of the {@code MBeanExporter}
 * @return the attribute metadata
 * @throws JMException in case of errors
 * @see #populateAttributeDescriptor
 */
@Override
protected ModelMBeanAttributeInfo[] getAttributeInfo(Object managedBean, String beanKey) throws JMException {
	PropertyDescriptor[] props = BeanUtils.getPropertyDescriptors(getClassToExpose(managedBean));
	List<ModelMBeanAttributeInfo> infos = new ArrayList<ModelMBeanAttributeInfo>();

	for (PropertyDescriptor prop : props) {
		Method getter = prop.getReadMethod();
		if (getter != null && getter.getDeclaringClass() == Object.class) {
			continue;
		}
		if (getter != null && !includeReadAttribute(getter, beanKey)) {
			getter = null;
		}

		Method setter = prop.getWriteMethod();
		if (setter != null && !includeWriteAttribute(setter, beanKey)) {
			setter = null;
		}

		if (getter != null || setter != null) {
			// If both getter and setter are null, then this does not need exposing.
			String attrName = JmxUtils.getAttributeName(prop, isUseStrictCasing());
			String description = getAttributeDescription(prop, beanKey);
			ModelMBeanAttributeInfo info = new ModelMBeanAttributeInfo(attrName, description, getter, setter);

			Descriptor desc = info.getDescriptor();
			if (getter != null) {
				desc.setField(FIELD_GET_METHOD, getter.getName());
			}
			if (setter != null) {
				desc.setField(FIELD_SET_METHOD, setter.getName());
			}

			populateAttributeDescriptor(desc, getter, setter, beanKey);
			info.setDescriptor(desc);
			infos.add(info);
		}
	}

	return infos.toArray(new ModelMBeanAttributeInfo[infos.size()]);
}
 
Example #28
Source File: JmxUtilsAnnotationTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void annotatedMXBean() throws Exception {
	assertTrue("MXBean annotation not detected correctly", JmxUtils.isMBean(FooX.class));
}
 
Example #29
Source File: JmxUtilsAnnotationTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void notMXBean() throws Exception {
	assertFalse("MXBean annotation not detected correctly", JmxUtils.isMBean(FooNotX.class));
}
 
Example #30
Source File: AbstractReflectiveMBeanInfoAssembler.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Iterate through all properties on the MBean class and gives subclasses
 * the chance to vote on the inclusion of both the accessor and mutator.
 * If a particular accessor or mutator is voted for inclusion, the appropriate
 * metadata is assembled and passed to the subclass for descriptor population.
 * @param managedBean the bean instance (might be an AOP proxy)
 * @param beanKey the key associated with the MBean in the beans map
 * of the {@code MBeanExporter}
 * @return the attribute metadata
 * @throws JMException in case of errors
 * @see #populateAttributeDescriptor
 */
@Override
protected ModelMBeanAttributeInfo[] getAttributeInfo(Object managedBean, String beanKey) throws JMException {
	PropertyDescriptor[] props = BeanUtils.getPropertyDescriptors(getClassToExpose(managedBean));
	List<ModelMBeanAttributeInfo> infos = new ArrayList<ModelMBeanAttributeInfo>();

	for (PropertyDescriptor prop : props) {
		Method getter = prop.getReadMethod();
		if (getter != null && getter.getDeclaringClass() == Object.class) {
			continue;
		}
		if (getter != null && !includeReadAttribute(getter, beanKey)) {
			getter = null;
		}

		Method setter = prop.getWriteMethod();
		if (setter != null && !includeWriteAttribute(setter, beanKey)) {
			setter = null;
		}

		if (getter != null || setter != null) {
			// If both getter and setter are null, then this does not need exposing.
			String attrName = JmxUtils.getAttributeName(prop, isUseStrictCasing());
			String description = getAttributeDescription(prop, beanKey);
			ModelMBeanAttributeInfo info = new ModelMBeanAttributeInfo(attrName, description, getter, setter);

			Descriptor desc = info.getDescriptor();
			if (getter != null) {
				desc.setField(FIELD_GET_METHOD, getter.getName());
			}
			if (setter != null) {
				desc.setField(FIELD_SET_METHOD, setter.getName());
			}

			populateAttributeDescriptor(desc, getter, setter, beanKey);
			info.setDescriptor(desc);
			infos.add(info);
		}
	}

	return infos.toArray(new ModelMBeanAttributeInfo[infos.size()]);
}