Tải bản đầy đủ - 0 (trang)
Table?5-2. Rows of tabular data for our example

Table?5-2. Rows of tabular data for our example

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

}



// . . .

} catch (Exception e) {

// . . .

}



The index array returned by this method contains two elements: at index 0 is a String

containing "Building A", and at index 1 is an Integer containing the value 3. This

method is handy if the caller is aware of the structure of the CompositeType of which

each row is constituted but wants to retrieve key values of a particular row without

necessarily having to know what the key structure looks like. If the CompositeData

parameter is null, an IllegalArgumentException is thrown. If the CompositeData

parameter does not conform to this TabularData object's CompositeType structure, an

InvalidOpenTypeException is thrown.

toString() returns a string representation of this TabularData object. The format of the

representation is implementation-dependent.

Now let's look at each of the Map-like methods of TabularData.

clear() removes all rows from this TabularData object.

containsKey() takes an Object array that contains values that correspond to the key type

of the CompositeData object and returns true if there is a row containing that key.

Using our example, suppose we pass an Object array that contains "Building A" (at

element 0) and 3 (at element 1):

public someMethod(TabularData buildingData) {

try {

Object[] key = {

"Building A",

new Short(3)

};

if (buildingData.containsKey(key))

doSomething(); // gets executed

key = new Object[2];

key[0] = "No Such Building";

key[1] = new Short(3);

if (buildingData.containsKey(key))

doSomethingElse(); // does NOT get executed

} catch (Exception e) {

// . . .

}

}



As we can see from Table 5-2, the first call to containsKey() in this example will return

true, because the key specified corresponds to a row that exists in the TabularData

object. The second call will return false, because no row contains a name of "No Such

Building." In addition, if the key does not conform to the structure of the key specified

when the TabularType object was created, this method returns false. It does not throw

163



an exception. This method is useful when the caller needs to know whether a particular

row exists in the table but doesn't necessarily have to retrieve and process that row.

containsValue() functions in exactly the same manner as containsKey(), except it takes a

CompositeData object as a parameter. If all fields of the row specified by the

CompositeData object passed as a parameter match the contents of one row of this

TabularData object exactly, this method returns true. If not, or if the CompositeData

object has a different structure than the rows of this TabularData object, this method

returns false. It does not throw an exception.

equals() takes an Object parameter and returns true if the specified Object is a

TabularData object and each row is a match for at least one other row in this

TabularData object. If the specified Object parameter is null, is not a TabularType

object, or has at least one row that is different from this TabularData object, this method

returns false. It does not throw an exception.

get() takes an Object array that contains a key to this TabularData object and returns

the corresponding CompositeData object for the row that matches the specified key. If

the specified key is null, a NullPointerException is thrown. If the specified key does

not conform to this TabularData object's TabularType definition, an

InvalidKeyException is thrown.

hashcode() returns the hashcode for this TabularData object.

isEmpty() returns a boolean indicating whether or not this TabularData object contains

any rows. If it contains at least one CompositeData object (i.e., a row), this method

returns true. Otherwise, it returns false.

keySet() returns a Set that contains all of the keys in the underlying Map-like

implementation. The returned Set can then be iterated over by calling the iterator()

method of the Set. Each item in the Set is a List object that contains the objects that

constitute the key, which in the current implementation are Strings.

public void someMethod(TabularData buildingData) {

try {

Set set = buildingData.keySet();

Iterator iter = set.iterator();

while (iter.hasNext()) {

List key = (List)iter.next();

String name = (String)key.get(0);

int height = ((Integer)key.get(1)).intValue();

// now do something with these values. . .

}

} catch (Exception e) {

// . . .

}

}



164



Because a List is used to maintain the keys internally, the order of the items follows

exactly the order in which the keys were specified when the TabularType object was

created for this TabularData object.

put() takes a CompositeData object and stores it in its internal Map-like implementation.

No key needs to be specified (as is the case with the put() method of the Map interface, for

example), because the key is calculated based on the index names specified in the

TabularType object that describes this TabularData object. However, TabularData

does not allow duplicate keys. In our example, the index names are the Name and Height

fields of the BuildingCompositeType object. If the CompositeData object we want to

store in this TabularType object looks like the second row from Table 5-2, the key is

calculated to be "Building B" and 90. Consider the following example:

public void addCompositeDataObject(TabularData buildingData)

try {

String[] itemNames = {

"Name",

"NumberOfFloors",

"Height",

"UndergroundParking",

"NumberOfElevators",

"OfficeSpace"

};

Object[] itemValues = {

"Building B",

new Short(7),

new Integer(90),

new Boolean(false),

new Short(3),

new Long(70000)

};

CompositeData building = new CompositeDataSupport(

buildingType, // See Example 5-1

itemNames,

itemValues

);

buildingData.put(building);

} catch (Exception e) {

// . . .

}



The emphasized line shows the method call to add a CompositeData object. As in the

previous examples, we have assumed that the TabularData object with which we're

working has already been created (we haven't seen how to do this yet—if you suspect that

we use TabularDataSupport to create the object, you're correct; we'll look at this in the

next section). The following code shows how to retrieve the row we added in the

previous example:

public void addCompositeDataObject(TabularData buildingData)

try {

// see above example. . .

buildingData.put(building);



165



}



// now retrieve the row just added:

Object[] key = {

"Building B",

new Integer(90)

};

CompositeData newRow = buildingData.get(key);

// . . .

} catch (Exception e) {

// . . .

}



If the CompositeData object reference passed to this method is null, a

NullPointerException is thrown. If the CompositeData object doesn't conform to the

CompositeType object that dictates the row structure of this TabularData object, an

InvalidOpenTypeException is thrown. As we mentioned earlier, the key for the

CompositeData row to be added is calculated internally, based on the contents of the

CompositeData object and the key structure specified when the TabularType object for

this TabularData object was created. If the key already exists in the TabularData object,

a KeyAlreadyExistsException will be thrown. The key for each row must be unique.

putAll() functions in exactly the same manner as put(), except that putAll() allows us to

store more than one CompositeData object in a single method call. putAll() takes an

array of CompositeData objects and stores them all inside the internal Map-like

implementation. When an attempt is made to put each CompositeData element into the

array that is passed to this method, that putAll() attempt is subject to the same conditions

as a put() attempt. This means that the same exceptions can be thrown.

remove() takes an Object array containing the key of a CompositeData row to be

removed. If the key does not exist, this method returns null; otherwise, it returns a

reference to the CompositeData object that was removed. If the Object array reference

is null, a NullPointerException will be thrown. If the reference is to an object that

does not conform to the CompositeType that specifies the structure of rows in this

TabularData object, an InvalidKeyException will be thrown.

size() returns the number of CompositeData elements, or rows, that are contained within

this TabularData object.

values() returns a Collection that contains all of the CompositeData elements that are

contained in the underlying Map-like implementation. The returned Collection can then

be used to iterate through the CompositeData objects.

5.1.2.6 TabularDataSupport



This class implements the TabularData interface. If we need to create a TabularData

object, we use one of this class's two constructors:

public TabularDataSupport(TabularType tabularType) {



166



// . . .

}

public TabularDataSupport(TabularType tabularType,

int initialCapacity,

float loadFactor) {

// . . .

}



The first parameter to both constructors is a TabularType object that describes the key

structure for the TabularData object to be created, as well as the structure of the

CompositeData elements that make up each row in this TabularData object. As we've

already seen, the underlying implementation TabularData is a Map (in fact, it's a

HashMap). The initialCapacity and loadFactor parameters of the second constructor

allow us to set the initial size of the Map and the load factor, which must be a floatingpoint number between zero and one in other words, it's a percentage). Once the number

of entries in the Map exceeds this percentage, the capacity of the Map is increased.

Both constructors verify the validity of the tabularType parameter. If it is null, an

IllegalArgumentException is thrown. When using the second constructor, an

IllegalArgumentException is also thrown if the initialCapacity or the loadFactor is

less than zero.

Unlike CompositeDataSupport, for which we must specify the constituents when we

create an instance, TabularDataSupport can be created empty. Once we create an

instance of TabularDataSupport, we can use the put() or putAll() method to add

CompositeData rows.



5.2 Open MBean Metadata Classes

Now that we've seen how open MBean types work and how they are defined, let's take a

look at creating the metadata classes that describe resources that are instrumented as open

MBeans. We will start by seeing how to describe parameters, attributes, constructors,

operations, notifications, and finally OpenMBeanInfo. Each of these is described by an

interface and implemented by a support class. This is similar to model MBeans—the

focus of the previous chapter—and the open MBean types we looked at earlier in this

chapter.

As with the open MBean type classes, when we talk about an object that implements the

interface (e.g., an OpenMBeanParameterInfo object) we have presupposed the creation

of the object using a support class. This is because we generally work with a particular

open MBean metadata type through its interface and use the corresponding support class

only to create instances of those objects.

A common theme for all of the OpenMBean*Info objects (where * is Parameter,

Attribute, etc.) is that the support classes provided extend their

javax.management.MBean*Info counterparts—for example,



167



OpenMBeanParameterInfoSupport extends MBeanParameterInfo. The relationships



between the open MBean classes are shown in Figure 5-1.

Figure 5-1. Relationships between the open MBean metadata classes



As we saw with dynamic MBeans in Figure 3-1, a single instance of

OpenMBeanInfoSupport completely describes an MBean. The relationship between

OpenMBeanInfoSupport and the other open MBean interfaces is shown in Figure 5-2.

Figure 5-2. Relationships between OpenMBeanInfoSupport and the other open MBean

metadata interfaces



In the rest of this section we will look at each of the open MBean metadata interfaces and

classes, starting with OpenMBeanParameterInfo.

5.2.1 OpenMBeanParameterInfo

This interface deals with open MBean parameter information. There are several read-only

attributes of an open MBean parameter:

Description



168



A human-readable description of the parameter described by this

OpenMBeanParameterInfo object.

Name

The name of the parameter; usually the variable name assigned to the parameter

in the signature of the method, of which this parameter is a constituent.

OpenType

The OpenType subclass that is this parameter's type.

DefaultValue

A default value for the parameter represented by this OpenMBeanParamaterInfo

object, to be used if one is not provided for the parameter. This attribute is null if

it has not been set.

LegalValues

A Set of values that specifies the only values the parameter represented by this

OpenMBeanParameterInfo object may have. This attribute is null if it has not

been set. If this attribute has been set, MinValue and MaxValue will be null, as

this attribute is mutually exclusive with the use of minimum and maximum values.

MinValue

The minimum value that a parameter represented by this

OpenMBeanParameterInfo object may have. This attribute is an object that

implements the Comparable interface, such as String, Short, Integer, Long, or

Float. This attribute is null if it has not been set. MinValue and LegalValues

are mutually exclusive.

MaxValue

The maximum value that a parameter represented by this

OpenMBeanParameterInfo object may have. This attribute is an object that

implements the Comparable interface. This attribute is null if it has not been set.

MaxValue and LegalValues are mutually exclusive.

In addition to these read-only attributes, the OpenMBeanParameterInfo interface

provides some other utility methods, which we will look at in this section.

OpenMBeanParameterInfo is defined as:

public interface OpenMBeanParameterInfo {

public String getDescription();



169



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

Table?5-2. Rows of tabular data for our example

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

×