001: /*****************************************************************************
002: * Copyright (C) NanoContainer Organization. All rights reserved. *
003: * ------------------------------------------------------------------------- *
004: * The software in this package is published under the terms of the BSD *
005: * style license a copy of which has been included with this distribution in *
006: * the LICENSE.txt file. *
007: * *
008: * Original code by Joerg Schaible *
009: *****************************************************************************/package org.picocontainer.gems.jmx;
010:
011: import javax.management.MBeanServer;
012:
013: import org.picocontainer.ComponentAdapter;
014: import org.picocontainer.Parameter;
015: import org.picocontainer.PicoCompositionException;
016: import org.picocontainer.Characteristics;
017: import org.picocontainer.ComponentMonitor;
018: import org.picocontainer.LifecycleStrategy;
019: import org.picocontainer.behaviors.AbstractBehaviorFactory;
020:
021: import java.util.Properties;
022:
023: /**
024: * {@link org.picocontainer.ComponentFactory} that instantiates {@link JMXExposed} instances.
025: * @author Jörg Schaible
026: */
027: public class JMXExposing extends AbstractBehaviorFactory {
028:
029: private final MBeanServer mBeanServer;
030: private final DynamicMBeanProvider[] providers;
031:
032: /**
033: * Construct a JMXExposingComponentFactory.
034: * @param mBeanServer The {@link MBeanServer} used for registering the MBean.
035: * @param providers An array with providers for converting the component instance into a
036: * {@link javax.management.DynamicMBean}.
037: * @throws NullPointerException Thrown if the {@link MBeanServer} or the array with the {@link DynamicMBeanProvider}
038: * instances is null.
039: */
040: public JMXExposing(final MBeanServer mBeanServer,
041: final DynamicMBeanProvider[] providers)
042: throws NullPointerException {
043: if (mBeanServer == null || providers == null) {
044: throw new NullPointerException();
045: }
046: this .mBeanServer = mBeanServer;
047: this .providers = providers;
048: }
049:
050: /**
051: * Construct a JMXExposingComponentFactory. This instance uses a {@link DynamicMBeanComponentProvider} as
052: * default to register any component instance in the {@link MBeanServer}, that is already a
053: * {@link javax.management.DynamicMBean}.
054: * @param mBeanServer The {@link MBeanServer} used for registering the MBean.
055: * @throws NullPointerException Thrown if the {@link MBeanServer} or the array with the {@link DynamicMBeanProvider}
056: * instances is null.
057: */
058: public JMXExposing(final MBeanServer mBeanServer)
059: throws NullPointerException {
060: this (
061: mBeanServer,
062: new DynamicMBeanProvider[] { new DynamicMBeanComponentProvider() });
063: }
064:
065: /**
066: * Retrieve a {@link ComponentAdapter}. Wrap the instance retrieved by the delegate with an instance of a
067: * {@link JMXExposed}.
068: * @see org.picocontainer.ComponentFactory#createComponentAdapter(ComponentMonitor,LifecycleStrategy,Properties,Object,Class,Parameter...)
069: */
070: public ComponentAdapter createComponentAdapter(
071: ComponentMonitor componentMonitor,
072: LifecycleStrategy lifecycleStrategy,
073: Properties componentProperties, Object componentKey,
074: Class componentImplementation, Parameter... parameters)
075: throws PicoCompositionException {
076: final ComponentAdapter componentAdapter = super
077: .createComponentAdapter(componentMonitor,
078: lifecycleStrategy, componentProperties,
079: componentKey, componentImplementation,
080: parameters);
081: if (AbstractBehaviorFactory.removePropertiesIfPresent(
082: componentProperties, Characteristics.NO_JMX)) {
083: return componentAdapter;
084: } else {
085: return new JMXExposed(componentAdapter, mBeanServer,
086: providers);
087: }
088: }
089:
090: public ComponentAdapter addComponentAdapter(
091: ComponentMonitor componentMonitor,
092: LifecycleStrategy lifecycleStrategy,
093: Properties componentProperties, ComponentAdapter adapter) {
094: if (AbstractBehaviorFactory.removePropertiesIfPresent(
095: componentProperties, Characteristics.NO_JMX)) {
096: return super .addComponentAdapter(componentMonitor,
097: lifecycleStrategy, componentProperties, adapter);
098: } else {
099: return new JMXExposed(super.addComponentAdapter(
100: componentMonitor, lifecycleStrategy,
101: componentProperties, adapter), mBeanServer,
102: providers);
103: }
104:
105: }
106: }
|