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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
681
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
     1
/*
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
     2
 * CDDL HEADER START
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
     3
 *
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
     5
 * Common Development and Distribution License (the "License").
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
     6
 * You may not use this file except in compliance with the License.
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
     7
 *
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
     8
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
     9
 * or http://www.opensolaris.org/os/licensing.
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    10
 * See the License for the specific language governing permissions
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    11
 * and limitations under the License.
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    12
 *
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    13
 * When distributing Covered Code, include this CDDL HEADER in each
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    14
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    15
 * If applicable, add the following below this CDDL HEADER, with the
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    16
 * fields enclosed by brackets "[]" replaced with your own identifying
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    17
 * information: Portions Copyright [yyyy] [name of copyright owner]
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    18
 *
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    19
 * CDDL HEADER END
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    20
 */
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    21
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    22
/*
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    23
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    24
 */
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    25
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    26
package org.opensolaris.os.rad.jmx;
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    27
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    28
import javax.management.openmbean.CompositeData;
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    29
import javax.management.openmbean.CompositeDataSupport;
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    30
import javax.management.openmbean.CompositeType;
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    31
import javax.management.openmbean.OpenDataException;
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    32
import javax.management.openmbean.OpenType;
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    33
import javax.management.openmbean.SimpleType;
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    34
import org.opensolaris.os.adr.Type.EnumType;
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    35
import org.opensolaris.os.adr.Type.EnumValue;
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    36
import org.opensolaris.os.rad.PlainEnumMapper;
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    37
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    38
/**
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    39
 * The JMX EnumMapper.  Maps enumeration values to CompositeData consisting
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    40
 * of a "value" set to the string value of the enumeration value.
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    41
 */
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    42
public class JMXEnumMapper extends PlainEnumMapper {
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    43
    @Override
764
ebb25c1dac73 18812 Support building with alternate java implementations
David Powell <david.e.powell@oracle.com>
parents: 681
diff changeset
    44
    public OpenType<?> toOpenType(EnumType type) {
681
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    45
	if (type.getFallback() == null)
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    46
	    return super.toOpenType(type);
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    47
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    48
	String name = "Flex" + type.getName();
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    49
	String fnames[] = new String[] { FlexEnum.VALUE };
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    50
	OpenType<?> ftypes[] = new OpenType<?>[] { SimpleType.STRING };
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    51
	try {
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    52
	    return new CompositeType(name, name, fnames, fnames, ftypes);
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    53
	} catch (OpenDataException ex) {
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    54
	    System.out.println("ODE caught: " + ex);
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    55
	    ex.printStackTrace();
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    56
	    return null;
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    57
	}
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    58
    }
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    59
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    60
    @Override
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    61
    public Object fromWire(EnumType type, EnumValue value)
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    62
	throws OpenDataException {
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    63
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    64
	if (type.getFallback() == null)
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    65
	    return super.fromWire(type, value);
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    66
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    67
	CompositeType ct = (CompositeType)client_.getOpenType(type);
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    68
	return new CompositeDataSupport(ct,
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    69
	    new String[] { FlexEnum.VALUE }, new Object[] { value.getName() });
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    70
    }
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    71
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    72
    @Override
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    73
    public String toWire(EnumType type, Object data) {
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    74
	if (type.getFallback() == null)
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    75
	    return super.toWire(type, data);
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    76
	return (String)((CompositeData)data).get(FlexEnum.VALUE);
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    77
    }
e3ebf98ba2fd 18035 enumeration fallback values
David Powell <david.e.powell@oracle.com>
parents:
diff changeset
    78
}