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

Example 4-3. The significant constructors of ModelMBeanConstructorInfo

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

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



desc.setField("displayName", "The Default Constructor");

ModelMBeanConstructorInfo[] constructorInfo = new

ModelMBeanConstructorInfo[1];

constructorInfo[0] = new ModelMBeanConstructorInfo(

"DefaultConstructor",

"The default constructor",

new MBeanParameterInfo[0],

desc

);



Notice that in this example the name and displayName fields do not have the same value,

as is the case when a default descriptor is created for a ModelMBeanConstructorInfo

object. This is one advantage of creating your own descriptor.

What if you simply want to expose all of the public constructors for your MBean? This

method was covered thoroughly in the previous chapter; however, it bears repeating here,

because this is by far the simplest way to create ModelMBeanConstructorInfo objects.

In the following example, we create a descriptor and pass it explicitly to the

ModelMBeanConstructorInfo's second constructor:

// . . .

Constructor[] constructors = this.getClass().getConstructors();

ModelMBeanConstructorInfo[] constructorInfo = new

ModelMBeanConstructorInfo[constructors.length];

for (int aa = 0; aa < constructors.length; aa++) {

Descriptor desc = new DescriptorSupport(

new String[] {

("name=" + constructors[aa].getName()),

"descriptorType=operation",

"role=constructor"

};

);

constructorInfo[aa] = new ModelMBeanConstructorInfo(

"Constructs a Basic MBean.", // description

constructors[aa]

// java.lang.reflect.Constructor

desc

);

}

// . . .



Notice that the displayName field has been omitted from this example. Recall from our

discussion of the required descriptor fields that only name and descriptorType are

required, so we are free to omit displayName.

4.2.2.3 ModelMBeanOperationInfo



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

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

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

MBean operation. Recall from the previous chapter that there are two constructors that

136



are used to set the essential properties for an MBeanOperationInfo 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

ModelMBeanOperationInfo, which inherits from ModelMBeanOperationInfo. There are

four constructors of interest on ModelMBeanOperationInfo, as shown in Example 4-4.

Example 4-4. The significant constructors of ModelMBeanOperationInfo

public class ModelMBeanOperationInfo extends MBeanOperationInfo

implements DescriptorAccess {

// . . .

public ModelMBeanOperationInfo(String description,

Method operationMethod) {

// . . .

}

public ModelMBeanOperationInfo (String description,

Method operationMethod,

Descriptor descriptor) }

// . . .

}

public ModelMBeanOperationInfo(String name,

String description,

MBeanParameterInfo[] signature,

String type,

int impact) {

// . . .

}

public ModelMBeanOperationInfo(String name,

String description,

MBeanParameterInfo[] signature,

String type,

int impact,

Descriptor descriptor) {

// . . .

}

// . . .

}



Notice the emphasized lines in Example 4-4. 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-4, a default descriptor containing the

following predefined fields is created by ModelMBeanOperationInfo:











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

137



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

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

// . . .

ModelMBeanOperationInfo[] operationInfo = new

ModelMBeanOperationInfo[1];

operationInfo[0] = new ModelMBeanOperationInfo(

"reset",

"Resets the state of this MBean.",

new MBeanParameterInfo[0],

Void.TYPE.getName(),

MBeanOperationInfo.ACTION

);

// . . .



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

ModelMBeanOperationInfo object created here results in a descriptor whose name and

displayName fields are set to "reset".

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

constructors shown in Example 4-4. The second constructor uses Constructor 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 constructor of

MBeanOperationInfo—covered in the previous chapter—and will not be presented here.

The following example shows how to create a ModelMBeanOperationInfo object using

the fourth constructor:

// . . .

Descriptor desc = new DescriptorSupport(

new String[] {

"name=reset",

"descriptorType=operation",

"role=operation",

("class=" + this.getClass().getName())

}

);

ModelMBeanOperationInfo[] operationInfo = new

ModelMBeanOperationInfo[1];

operationInfo[0] = new ModelMBeanOperationInfo(

"reset",

"Resets the state of this MBean.",

new MBeanParameterInfo[0],

Void.TYPE.getName(),

MBeanOperationInfo.ACTION,

desc

);



The only difference between the previous two examples is that we created our own

descriptor in the second. There are several advantages to creating your own descriptors,

not the least of which is improved code readability. Notice that the class field was set to



138



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

Example 4-3. The significant constructors of ModelMBeanConstructorInfo

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

×