usr/src/java/rad/org/opensolaris/os/rad/jmx/JMXEnumMapper.java
author David Powell <david.e.powell@oracle.com>
Thu, 11 Aug 2011 15:41:42 -0700
changeset 764 ebb25c1dac73
parent 681 e3ebf98ba2fd
permissions -rw-r--r--
18812 Support building with alternate java implementations

/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 */

package org.opensolaris.os.rad.jmx;

import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenDataException;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;
import org.opensolaris.os.adr.Type.EnumType;
import org.opensolaris.os.adr.Type.EnumValue;
import org.opensolaris.os.rad.PlainEnumMapper;

/**
 * The JMX EnumMapper.  Maps enumeration values to CompositeData consisting
 * of a "value" set to the string value of the enumeration value.
 */
public class JMXEnumMapper extends PlainEnumMapper {
    @Override
    public OpenType<?> toOpenType(EnumType type) {
	if (type.getFallback() == null)
	    return super.toOpenType(type);

	String name = "Flex" + type.getName();
	String fnames[] = new String[] { FlexEnum.VALUE };
	OpenType<?> ftypes[] = new OpenType<?>[] { SimpleType.STRING };
	try {
	    return new CompositeType(name, name, fnames, fnames, ftypes);
	} catch (OpenDataException ex) {
	    System.out.println("ODE caught: " + ex);
	    ex.printStackTrace();
	    return null;
	}
    }

    @Override
    public Object fromWire(EnumType type, EnumValue value)
	throws OpenDataException {

	if (type.getFallback() == null)
	    return super.fromWire(type, value);

	CompositeType ct = (CompositeType)client_.getOpenType(type);
	return new CompositeDataSupport(ct,
	    new String[] { FlexEnum.VALUE }, new Object[] { value.getName() });
    }

    @Override
    public String toWire(EnumType type, Object data) {
	if (type.getFallback() == null)
	    return super.toWire(type, data);
	return (String)((CompositeData)data).get(FlexEnum.VALUE);
    }
}