<document>
  <header>
    <product>resin</product>
    <title>Using MBeanRegistration</title>
    <type>tutorial</type>
    <description>MBeans can implement the MBeanRegistration interface to find the ObjectName and MBeanServer they're registered with.</description>
 <tutorial-startpage>index.jsp</tutorial-startpage>
  </header>

<body>

<summary/>

<section title="Files in this tutorial">
<deftable>
<tr><td><viewfile-link file="WEB-INF/web.xml"/>
    <td>Configures the JMX-managed bean
<tr><td><viewfile-link file="WEB-INF/classes/example/Test.java"/>
    <td>The resource bean implementation.
<tr><td><viewfile-link file="WEB-INF/classes/example/TestMBean.java"/>
    <td>The management interface for the bean.
<tr><td><viewfile-link file="index.jsp"/>
    <td>Using the managed bean.
</deftable>
</section>

<section title="MBeanRegistration">

<p>Frequently, a managed bean will either need
its <code>ObjectName</code> or its <code>MBeanServer</code>.  When the
bean implements the <code>MBeanRegistration</code> interface, the
JMX server tells the bean its <code>ObjectName</code> on registration.</p>

<p>The bean can verify the <code>ObjectName</code> or even
returning a different name, although returning a different
<code>ObjectName</code> is generally a bad idea in most cases since it
makes the to configure.</p>

<example title="Test.java">
package example;

import javax.management.ObjectName;
import javax.management.MBeanServer;
import javax.management.MBeanRegistration;

public class Test implements TestMBean, MBeanRegistration {
  private ObjectName _name;

  public ObjectName getObjectName()
  {
    return _name;
  }
  
  public ObjectName preRegister(MBeanServer server, ObjectName name)
    throws Exception
  {
    _name = name;

    return name;
  }
  
  public void postRegister(Boolean registrationDone)
  {
  }
  
  public void preDeregister()
    throws Exception
  {
  }
  
  public void postDeregister()
  {
  }
}
</example>

<section title="Client">

<p>The client JSP asks for the object's ObjectName to see the ObjectName
passed in the <code>preRegistration</code> call.</p>

<example title="index.jsp">
&lt;%@ page import='com.caucho.jmx.Jmx, example.BasicMBean' %>
&lt;%
BasicMBean basic = (BasicMBean) Jmx.find("example:name=test");

out.println("ObjectName: " + test.getObjectName());
%>
</example>
<results>
ObjectName: example:name=test
</results>

</section>

</section>

<section title="Compatibility">

<p>MBeanRegistration is part of the JMX specification.</p>

</section>

  </body>
</document>
