HomeIndex > FAQ > GBean Development > Is it possible to get a reference to the MBeanServer from a GBean?

Yes. Simply add a reference of type MBeanServerReference to your GBeanInfo, and call the getMBeanServer() on the reference to obtain the MBeanServer. For example:

public class Example {
    public Example(MBeanServerReference mbeanServerReference) {
        MBeanServer mbeanServer = mbeanServerReference.getMBeanServer());
    }

    public static final GBeanInfo GBEAN_INFO;
    static {
        GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(Example.class);
        infoBuilder.addReference("MBeanServerReference", MBeanServerReference.class);

        infoBuilder.setConstructor(new String[]{"MBeanServerReference"});

        GBEAN_INFO = infoBuilder.getBeanInfo();
    }

    public static GBeanInfo getGBeanInfo() {
        return GBEAN_INFO;
    }
}

Here is the xml configuration for the example above:

<module xmlns="http://geronimo.apache.org/xml/ns/deployment-1.1">
  <environment>

    <moduleId>
      <groupId>myco</groupId>
      <artifactId>myco</artifactId>
      <version>1.0</version>
      <type>car</type>
    </moduleId>

    <dependencies>
      <dependency>
        <groupId>org.apache.geronimo.framework</groupId>
        <artifactId>j2ee-system</artifactId>
        <type>car</type>
      </dependency>
    </dependencies>

  </environment>

  <gbean name="Example" class="com.myco.Example">
      <reference name="MBeanServerReference">
          <name>MBeanServerReference</name>
      </reference>
  </gbean>
</module>