Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Inheritance Patterns

In the case of Standard MBeans, the management interface can be inherited from the superclass. It is also possible for the subclass to override the management interface of its ancestor by declaring its own management interface. You will now see how the agent determines the actual management interface of a Standard MBean and then investigate the inheritance patterns.

The most common case, which you have seen with the two Standard MBean examples so far, is to have the MBean class directly implement its own management interface. However, it is also possible for an MBean to inherit its management interface from a superclass. For example, you could create a subclass of the User class—implemented in the previous chapter—and call it Guest. In this case, the Guest class inherits the methods and fields of its superclass as per the usual Java language inheritance rules, but it also inherits the management interface of its superclass. When the Guest component is registered to the agent, the agent will first look for a management interface matching the naming conventions for the Guest class. If the agent does not find the GuestMBean interface, it will traverse the inheritance tree of the Guest class to find the ancestor User. For the User class, the agent will find the matching management interface declaration. Therefore, the Guest class is a compliant MBean and exposes the same attributes and operations as its ancestor User. The management applications will be able to manipulate the Guest instances by using the same interface as for the User instances.

Figure 3.2 shows the inheritance pattern for the Guest class. When the Guest component instance is registered to the agent, it will expose the UserMBean management interface.

Figure 3.2 Class structure of the Guest MBean.

It's also possible for the Guest class to override the management interface of its ancestor class. If the Guest class implements a management interface GuestMBean, the agent only exposes the attributes and operations specified in that specific interface. The management interface of all the ancestor classes will be overridden, and nothing of the ancestor classes is exposed to the management. You could use this feature to turn the Guest component instances to strictly read-only in terms of management by defining the interface in Listing 3.5.

Listing 3.5 GuestMBean.java

public interface GuestMBean {

  // read-only attributes
  public long getID();
  public String getName();
  public String getAddress();
  public String[] getPhoneNumbers();
  // operations
  public String printInfo();

When implementing the Guest resource, you would declare the class as follows:

public class Guest extends User implements GuestMBean {
  // implementation

This declaration would hide the UserMBean management interface completely and only allow the management application to read the Guest object's state.

In case the Guest should inherit the management interface of its ancestor User and expose additional attributes and operations, you must have the GuestMBean extend the management interface of the superclass, in this case the UserMBean interface.

As Figure 3.3 shows, this leads to two parallel inheritance trees—one of the implementation and another one of the management interface. Of course, it is also possible for the Guest in this case to implement the entire management interface, instead of inheriting the implementation from the User superclass.

Figure 3.3 Management interface

  • + Share This
  • 🔖 Save To Your Account