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

<body>

<summary/>

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

<s1 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>

<s2 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' %&gt;
&lt;%
BasicMBean basic = (BasicMBean) Jmx.find("example:name=test");

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

</s2>

</s1>

<s1 title="Compatibility">

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

</s1>

  </body>
</document>
