Tải bản đầy đủ - 0 (trang)
Example 4-2. The significant constructors of ModelMBeanAttributeInfo

Example 4-2. The significant constructors of ModelMBeanAttributeInfo

Tải bản đầy đủ - 0trang

boolean isWritable,

boolean isIs) {



}



// . . .

}

public ModelMBeanAttributeInfo(String name,

String type,

String description,

boolean isReadable,

boolean isWritable,

boolean isIs,

Descriptor descriptor) {

// . . .

}

// . . .



Notice the emphasized lines in Example 4-2. The second and fourth constructors are each

passed a reference to a Descriptor object. The first and second constructors are

otherwise identical, as are the third and fourth. As mentioned earlier, every model MBean

attribute contains a Descriptor object that provides a richer set of metadata than that of

the metadata class.

In the first and third constructors in Example 4-2, a default descriptor is created by

ModelMBeanAttributeInfo. The default descriptor contains the following predefined

fields:











descriptorType

displayName

iterable

name



Each of these fields (with the exception of iterable) was discussed in a previous section.

For two of these fields, the values are literal strings: the value for descriptorType is

"attribute", and the value for iterable is "F". The values for both displayName and

name are set to the name parameter that was passed to the constructor. Consider the

following example, which results in a descriptor whose name and displayName fields are

set to "WorkFactor":

// . . .

ModelMBeanAttributeInfo[] attributeInfo = new

ModelMBeanAttributeInfo[1];

attributeInfo[0] = new ModelMBeanAttributeInfo(

"WorkFactor",

"java.lang.Integer",

"Amount of work performed per work unit.",

true,

false,

false

);

// . . .



132



This example uses the third constructor from Example 4-2 (use of the first constructor

requires more code). For more information on the use of the reflection API for this

constructor, see Section 3.2.1.1 in the previous chapter.

Notice the second parameter to the MBeanAttributeInfo constructor in this example.

Even though the attribute type for WorkFactor is the fundamental type int, we pass

"java.lang.Integer" to the ModelMBeanAttributeInfo constructor. There appears to

be an oversight in the JMX 1.0 RI: when the getter for an attribute whose return value is a

fundamental type is invoked, the return value is wrapped in its corresponding JDK

wrapper class. Thus, even though the getter for WorkFactor returns an int, the reflection

API (which is what the JMX RI uses under the hood to perform the invocation) wraps the

return value in a java.lang.Integer. However, the RI does not take this into account,

and upon returning from the getter, the RI checks to see if the return value matches what

was expected.

Say we create the metadata using the proper value (notice parameter #2):

// . . .

attributeInfo[0] =

new ModelMBeanAttributeInfo(

"WorkFactor",

Integer.TYPE.getName(),

"Amount of work performed per work unit.",

true,

false,

false

);

// . . .



The RI looks at the return value from the getter (which has been wrapped in a

java.lang.Integer), compares it to what is in the metadata (which is the string

representation of a fundamental int), declares the return value to be bad, and throws an

exception. This is clearly not what we intended—to work around the problem, we have to

specify the JDK wrapper class name when we create the metadata class.

If you would like to create your own descriptor instead of using the default descriptor,

you can use the second and fourth constructors shown in Example 4-2. The second

constructor uses Method objects and the Java reflection API and differs from the fourth

constructor only in that regard. Use of this constructor is otherwise identical to that of the

corresponding MBeanAttributeInfo constructor, which was covered in the previous

chapter, and will not be presented here. The following example shows how to create a

ModelMBeanAttributeInfo object using the fourth constructor for the read-only

attribute WorkFactor:

// . . .

Descriptor desc = new DescriptorSupport();

desc.setField("name", "WorkFactor");

desc.setField("descriptorType", "attribute");

desc.setField("getMethod", "getWorkFactor");



133



ModelMBeanAttributeInfo[] attributeInfo = new

ModelMBeanAttributeInfo[1];

attributeInfo[0] = new ModelMBeanAttributeInfo(

"WorkFactor",

"java.lang.Integer",

"Amount of work performed per work unit.",

true,

false,

false,

desc

);

// . . .



Notice that we create the Descriptor object by instantiating the DescriptorSupport

class, as we discussed earlier in this chapter. We used the setField() method in this

example, but there are many ways to set the fields of the descriptor. The use of the other

DescriptorSupport constructors was discussed at length earlier in this chapter.

4.2.2.2 ModelMBeanConstructorInfo



All but one of the essential properties that must be set for a model MBean constructor are

the same as those that must be set for a dynamic MBean constructor, so they will not be

discussed here. The lone exception is the descriptor that must be set for a model MBean

constructor. Recall from the previous chapter that there are two constructors that are used

to set the essential properties for an MBeanConstructorInfo instance. One of these

constructors uses the Java reflection API, and the other explicitly sets all of the essential

properties. These same properties (in addition to the descriptor) exist on

ModelMBeanConstructorInfo, which inherits from MBeanConstructorInfo. Four

constructors are available to you on ModelMBeanConstructorInfo, as shown in

Example 4-3.

Example 4-3. The significant constructors of ModelMBeanConstructorInfo

public class ModelMBeanConstructorInfo extends MBeanConstructorInfo

implements DescriptorAccess, Cloneable {

// . . .

public ModelMBeanConstructorInfo(String description,

Constructor constructorMethod) {

// . . .

}

public ModelMBeanConstructorInfo(String description,

Constructor constructorMethod,

Descriptor descriptor) {

// . . .

}

public ModelMBeanConstructorInfo(String name,

String description,

MBeanParameterInfo[] signature) {

// . . .

}

public ModelMBeanConstructorInfo(String name,



134



String description,

MBeanParameterInfo[] signature,

Descriptor descriptor) {

// . . .

}

// . . .

}



Notice the emphasized lines in Example 4-3. The second and fourth constructors are each

passed a reference to a Descriptor object. The first and second constructors are

otherwise identical, as are the third and fourth.

In the first and third constructors in Example 4-3, a default descriptor containing the

following predefined fields is created by ModelMBeanConstructorInfo:











descriptorType

displayName

name

role



Each of these fields was discussed in a previous section. For two of these fields, the

values are literal strings: the value for descriptorType is "operation", and the value

for role is "constructor". The values for both displayName and name are set to the

name parameter that was passed to the constructor. Consider the following example:

// . . .

ModelMBeanConstructorInfo[] constructorInfo = new

ModelMBeanConstructorInfo[1];

constructorInfo[0] = new ModelMBeanConstructorInfo(

"DefaultConstructor",

"The default constructor",

new MBeanParameterInfo[0]

);

// . . .



This example uses the third constructor from Example 4-3. The

ModelMBeanConstructorInfo object created here results in a descriptor whose name and

displayName fields are set to "DefaultConstructor".

If you would like to create your own descriptor, you can use the second and fourth

constructors shown in Example 4-3. The second constructor uses Constructor objects

and the Java reflection API and differs from the fourth constructor only in that regard.

The following example shows how to create a ModelMBeanConstructorInfo object

using the fourth constructor for an MBean's default constructor:

// . . .

Descriptor desc = new DescriptorSupport();

desc.setField("name", "DefaultConstructor");

desc.setField("descriptorType", "operation");

desc.setField("role", "constructor");



135



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Example 4-2. The significant constructors of ModelMBeanAttributeInfo

Tải bản đầy đủ ngay(0 tr)

×