Path: blob/master/test/jdk/javax/management/mxbean/MerlinMXBean.java
41152 views
/*1* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.Arrays;24import javax.management.openmbean.ArrayType;25import javax.management.openmbean.CompositeData;26import javax.management.openmbean.CompositeDataSupport;27import javax.management.openmbean.CompositeDataView;28import javax.management.openmbean.CompositeType;29import javax.management.openmbean.OpenDataException;30import javax.management.openmbean.OpenType;31import javax.management.openmbean.SimpleType;3233public interface MerlinMXBean {3435int PInt = 59;36SimpleType PIntType = SimpleType.INTEGER;37int getPInt();38void setPInt(int x);39int opPInt(int x, int y);4041long PLong = Long.MAX_VALUE;42SimpleType PLongType = SimpleType.LONG;43long getPLong();44void setPLong(long x);45long opPLong(long x, long y);4647short PShort = 55;48SimpleType PShortType = SimpleType.SHORT;49short getPShort();50void setPShort(short x);51short opPShort(short x, short y);5253byte PByte = 13;54SimpleType PByteType = SimpleType.BYTE;55byte getPByte();56void setPByte(byte x);57byte opPByte(byte x, byte y);5859char PChar = 'x';60SimpleType PCharType = SimpleType.CHARACTER;61char getPChar();62void setPChar(char x);63char opPChar(char x, char y);6465float PFloat = 1.3f;66SimpleType PFloatType = SimpleType.FLOAT;67float getPFloat();68void setPFloat(float x);69float opPFloat(float x, float y);7071double PDouble = Double.MAX_VALUE;72SimpleType PDoubleType = SimpleType.DOUBLE;73double getPDouble();74void setPDouble(double x);75double opPDouble(double x, double y);7677boolean PBoolean = true;78SimpleType PBooleanType = SimpleType.BOOLEAN;79boolean getPBoolean();80void setPBoolean(boolean x);81boolean opPBoolean(boolean x, boolean y);8283Integer WInteger = new Integer(59);84SimpleType WIntegerType = SimpleType.INTEGER;85Integer getWInteger();86void setWInteger(Integer x);87Integer opWInteger(Integer x, Integer y);8889Long WLong = new Long(Long.MAX_VALUE);90SimpleType WLongType = SimpleType.LONG;91Long getWLong();92void setWLong(Long x);93Long opWLong(Long x, Long y);9495Short WShort = new Short(Short.MAX_VALUE);96SimpleType WShortType = SimpleType.SHORT;97Short getWShort();98void setWShort(Short x);99Short opWShort(Short x, Short y);100101Byte WByte = new Byte(Byte.MAX_VALUE);102SimpleType WByteType = SimpleType.BYTE;103Byte getWByte();104void setWByte(Byte x);105Byte opWByte(Byte x, Byte y);106107Character WCharacter = new Character('x');108SimpleType WCharacterType = SimpleType.CHARACTER;109Character getWCharacter();110void setWCharacter(Character x);111Character opWCharacter(Character x, Character y);112113Float WFloat = new Float(1.3f);114SimpleType WFloatType = SimpleType.FLOAT;115Float getWFloat();116void setWFloat(Float x);117Float opWFloat(Float x, Float y);118119Double WDouble = new Double(Double.MAX_VALUE);120SimpleType WDoubleType = SimpleType.DOUBLE;121Double getWDouble();122void setWDouble(Double x);123Double opWDouble(Double x, Double y);124125Boolean WBoolean = Boolean.TRUE;126SimpleType WBooleanType = SimpleType.BOOLEAN;127Boolean getWBoolean();128void setWBoolean(Boolean x);129Boolean opWBoolean(Boolean x, Boolean y);130131int[] PIntA = {2, 3, 5, 7, 11, 13};132ArrayType PIntAType = ArrayTypeMaker.make(SimpleType.INTEGER, true);133int[] getPIntA();134void setPIntA(int[] x);135int[] opPIntA(int[] x, int[] y);136137int[][] PInt2D = {{1, 2}, {3, 4}};138ArrayType PInt2DType = ArrayTypeMaker.make(1, PIntAType);139int[][] getPInt2D();140void setPInt2D(int[][] x);141int[][] opPInt2D(int[][] x, int[][] y);142143Integer[] WIntA = {new Integer(3), new Integer(5)};144ArrayType WIntAType = ArrayTypeMaker.make(1, SimpleType.INTEGER);145Integer[] getWIntA();146void setWIntA(Integer[] x);147Integer[] opWIntA(Integer[] x, Integer[] y);148149Integer[][] WInt2D = {{new Integer(3)}, {new Integer(5)}};150ArrayType WInt2DType = ArrayTypeMaker.make(2, SimpleType.INTEGER);151Integer[][] getWInt2D();152void setWInt2D(Integer[][] x);153Integer[][] opWInt2D(Integer[][] x, Integer[][] y);154155String XString = "yo!";156SimpleType XStringType = SimpleType.STRING;157String getXString();158void setXString(String x);159String opXString(String x, String y);160161String[] XStringA = {"hello", "world"};162ArrayType XStringAType = ArrayTypeMaker.make(1, SimpleType.STRING);163String[] getXStringA();164void setXStringA(String[] x);165String[] opXStringA(String[] x, String[] y);166167int[] NoInts = {};168ArrayType NoIntsType = ArrayTypeMaker.make(SimpleType.INTEGER, true);169int[] getNoInts();170void setNoInts(int[] x);171int[] opNoInts(int[] x, int[] y);172173GetSetBean GetSet = GetSetBean.make(5, "x", new String[] {"a", "b"});174CompositeType GetSetType =175CompositeTypeMaker.make(GetSetBean.class.getName(),176GetSetBean.class.getName(),177new String[] {"int", "string", "stringArray"},178new String[] {"int", "string", "stringArray"},179new OpenType[] {180SimpleType.INTEGER,181SimpleType.STRING,182ArrayTypeMaker.make(1, SimpleType.STRING),183});184GetSetBean getGetSet();185void setGetSet(GetSetBean bean);186GetSetBean opGetSet(GetSetBean x, GetSetBean y);187188GetterInterface Interface = new GetterInterface() {189public boolean isWhatsit() {190return true;191}192193public int[] getInts() {194return new int[] {1};195}196197public String[] getStrings() {198return new String[] {"x"};199}200201public GetSetBean getGetSet() {202return GetSetBean.make(3, "a", new String[] {"b"});203}204205public boolean equals(Object o) {206if (!(o instanceof GetterInterface))207return false;208GetterInterface i = (GetterInterface) o;209return isWhatsit() == i.isWhatsit() &&210Arrays.equals(getInts(), i.getInts()) &&211Arrays.equals(getStrings(), i.getStrings()) &&212getGetSet().equals(i.getGetSet());213}214};215CompositeType InterfaceType =216CompositeTypeMaker.make(GetterInterface.class.getName(),217GetterInterface.class.getName(),218new String[] {219"ints", "getSet", "strings", "whatsit",220},221new String[] {222"ints", "getSet", "strings", "whatsit",223},224new OpenType[] {225ArrayTypeMaker.make(SimpleType.INTEGER, true),226GetSetType,227ArrayTypeMaker.make(1, SimpleType.STRING),228SimpleType.BOOLEAN,229});230GetterInterface getInterface();231void setInterface(GetterInterface i);232GetterInterface opInterface(GetterInterface x, GetterInterface y);233234/* Testing that we can use a public no-arg constructor plus a setter235* for every getter to reconstruct this object. Note that the236* constructor-guessing logic is no longer valid for this class,237* so if we can reconstruct it it must be because of the setters.238*/239public static class GetSetBean {240public GetSetBean() {241this(0, null, null);242}243244private GetSetBean(int Int, String string, String[] stringArray) {245this.Int = Int;246this.string = string;247this.stringArray = stringArray;248}249250public static GetSetBean251make(int Int, String string, String[] stringArray) {252GetSetBean b = new GetSetBean(Int, string, stringArray);253return b;254}255256public int getInt() {257return Int;258}259260public String getString() {261return this.string;262}263264public String[] getStringArray() {265return this.stringArray;266}267268public void setInt(int x) {269this.Int = x;270}271272public void setString(String string) {273this.string = string;274}275276public void setStringArray(String[] stringArray) {277this.stringArray = stringArray;278}279280public boolean equals(Object o) {281if (!(o instanceof GetSetBean))282return false;283GetSetBean b = (GetSetBean) o;284return (b.Int == Int &&285b.string.equals(string) &&286Arrays.equals(b.stringArray, stringArray));287}288289String string;290String[] stringArray;291int Int;292}293294public static interface GetterInterface {295public String[] getStrings();296public int[] getInts();297public boolean isWhatsit();298public GetSetBean getGetSet();299300// We uselessly mention the public methods inherited from Object because301// they should not prevent the interface from being translatable.302// We could consider encoding the result of hashCode() and toString()303// on the original object that implements this interface into the304// generated CompositeData and referencing that in the proxy, but305// that seems ambitious for now. Doing it only if hashCode() and/or306// toString() are mentioned in the interface is a possibility but307// a rather abstruse one.308public boolean equals(Object o);309public int hashCode();310public String toString();311}312313public static class ArrayTypeMaker {314static ArrayType make(int dims, OpenType baseType) {315try {316return new ArrayType(dims, baseType);317} catch (OpenDataException e) {318throw new Error(e);319}320}321322static ArrayType make(SimpleType baseType, boolean primitiveArray) {323try {324return new ArrayType(baseType, primitiveArray);325} catch (OpenDataException e) {326throw new Error(e);327}328}329}330331public static class CompositeTypeMaker {332static CompositeType make(String className,333String description,334String[] itemNames,335String[] itemDescriptions,336OpenType[] itemTypes) {337try {338return new CompositeType(className,339description,340itemNames,341itemDescriptions,342itemTypes);343} catch (OpenDataException e) {344throw new Error(e);345}346}347}348349public static interface GraphMXBean {350public NodeMXBean[] getNodes();351}352353public static class Graph implements GraphMXBean {354public Graph(Node... nodes) {355for (Node node : nodes)356node.setGraph(this);357this.nodes = nodes;358}359360public NodeMXBean[] getNodes() {361return nodes;362}363364private final Node[] nodes;365}366367public static interface NodeMXBean {368public String getName();369public GraphMXBean getGraph();370}371372public static class Node implements NodeMXBean {373public Node(String name) {374this.name = name;375}376377public String getName() {378return name;379}380381public GraphMXBean getGraph() {382return graph;383}384385public void setGraph(Graph graph) {386this.graph = graph;387}388389private final String name;390private Graph graph;391}392393SimpleType GraphType = SimpleType.OBJECTNAME;394GraphMXBean getGraph();395void setGraph(GraphMXBean g);396GraphMXBean opGraph(GraphMXBean x, GraphMXBean y);397String GraphObjectName = "test:type=GraphMXBean";398String NodeAObjectName = "test:type=NodeMXBean,name=a";399String NodeBObjectName = "test:type=NodeMXBean,name=b";400Node NodeA = new Node("a");401Node NodeB = new Node("b");402GraphMXBean Graph = new Graph(NodeA, NodeB);403404public static class ExoticCompositeData implements CompositeDataView {405private ExoticCompositeData(String whatsit) {406this.whatsit = whatsit;407}408409public static ExoticCompositeData from(CompositeData cd) {410String whatsit = (String) cd.get("whatsit");411if (!whatsit.startsWith("!"))412throw new IllegalArgumentException(whatsit);413return new ExoticCompositeData(whatsit.substring(1));414}415416public CompositeData toCompositeData(CompositeType ct) {417try {418return new CompositeDataSupport(ct, new String[] {"whatsit"},419new String[] {"!" + whatsit});420} catch (Exception e) {421throw new RuntimeException(e);422}423}424425public String getWhatsit() {426return whatsit;427}428429public boolean equals(Object o) {430return ((o instanceof ExoticCompositeData) &&431((ExoticCompositeData) o).whatsit.equals(whatsit));432}433434private final String whatsit;435436public static final CompositeType type;437static {438try {439type =440new CompositeType(ExoticCompositeData.class.getName(),441ExoticCompositeData.class.getName(),442new String[] {"whatsit"},443new String[] {"whatsit"},444new OpenType[] {SimpleType.STRING});445} catch (Exception e) {446throw new RuntimeException(e);447}448}449}450CompositeType ExoticType = ExoticCompositeData.type;451ExoticCompositeData getExotic();452void setExotic(ExoticCompositeData ecd);453ExoticCompositeData opExotic(ExoticCompositeData ecd1,454ExoticCompositeData ecd2);455ExoticCompositeData Exotic = new ExoticCompositeData("foo");456}457458459