1 /*
2  * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import java.util.Arrays;
25 import javax.management.openmbean.ArrayType;
26 import javax.management.openmbean.CompositeData;
27 import javax.management.openmbean.CompositeDataSupport;
28 import javax.management.openmbean.CompositeDataView;
29 import javax.management.openmbean.CompositeType;
30 import javax.management.openmbean.OpenDataException;
31 import javax.management.openmbean.OpenType;
32 import javax.management.openmbean.SimpleType;
33 
34 public interface MerlinMXBean {
35 
36     int PInt = 59;
37     SimpleType PIntType = SimpleType.INTEGER;
getPInt()38     int getPInt();
setPInt(int x)39     void setPInt(int x);
opPInt(int x, int y)40     int opPInt(int x, int y);
41 
42     long PLong = Long.MAX_VALUE;
43     SimpleType PLongType = SimpleType.LONG;
getPLong()44     long getPLong();
setPLong(long x)45     void setPLong(long x);
opPLong(long x, long y)46     long opPLong(long x, long y);
47 
48     short PShort = 55;
49     SimpleType PShortType = SimpleType.SHORT;
getPShort()50     short getPShort();
setPShort(short x)51     void setPShort(short x);
opPShort(short x, short y)52     short opPShort(short x, short y);
53 
54     byte PByte = 13;
55     SimpleType PByteType = SimpleType.BYTE;
getPByte()56     byte getPByte();
setPByte(byte x)57     void setPByte(byte x);
opPByte(byte x, byte y)58     byte opPByte(byte x, byte y);
59 
60     char PChar = 'x';
61     SimpleType PCharType = SimpleType.CHARACTER;
getPChar()62     char getPChar();
setPChar(char x)63     void setPChar(char x);
opPChar(char x, char y)64     char opPChar(char x, char y);
65 
66     float PFloat = 1.3f;
67     SimpleType PFloatType = SimpleType.FLOAT;
getPFloat()68     float getPFloat();
setPFloat(float x)69     void setPFloat(float x);
opPFloat(float x, float y)70     float opPFloat(float x, float y);
71 
72     double PDouble = Double.MAX_VALUE;
73     SimpleType PDoubleType = SimpleType.DOUBLE;
getPDouble()74     double getPDouble();
setPDouble(double x)75     void setPDouble(double x);
opPDouble(double x, double y)76     double opPDouble(double x, double y);
77 
78     boolean PBoolean = true;
79     SimpleType PBooleanType = SimpleType.BOOLEAN;
getPBoolean()80     boolean getPBoolean();
setPBoolean(boolean x)81     void setPBoolean(boolean x);
opPBoolean(boolean x, boolean y)82     boolean opPBoolean(boolean x, boolean y);
83 
84     Integer WInteger = new Integer(59);
85     SimpleType WIntegerType = SimpleType.INTEGER;
getWInteger()86     Integer getWInteger();
setWInteger(Integer x)87     void setWInteger(Integer x);
opWInteger(Integer x, Integer y)88     Integer opWInteger(Integer x, Integer y);
89 
90     Long WLong = new Long(Long.MAX_VALUE);
91     SimpleType WLongType = SimpleType.LONG;
getWLong()92     Long getWLong();
setWLong(Long x)93     void setWLong(Long x);
opWLong(Long x, Long y)94     Long opWLong(Long x, Long y);
95 
96     Short WShort = new Short(Short.MAX_VALUE);
97     SimpleType WShortType = SimpleType.SHORT;
getWShort()98     Short getWShort();
setWShort(Short x)99     void setWShort(Short x);
opWShort(Short x, Short y)100     Short opWShort(Short x, Short y);
101 
102     Byte WByte = new Byte(Byte.MAX_VALUE);
103     SimpleType WByteType = SimpleType.BYTE;
getWByte()104     Byte getWByte();
setWByte(Byte x)105     void setWByte(Byte x);
opWByte(Byte x, Byte y)106     Byte opWByte(Byte x, Byte y);
107 
108     Character WCharacter = new Character('x');
109     SimpleType WCharacterType = SimpleType.CHARACTER;
getWCharacter()110     Character getWCharacter();
setWCharacter(Character x)111     void setWCharacter(Character x);
opWCharacter(Character x, Character y)112     Character opWCharacter(Character x, Character y);
113 
114     Float WFloat = new Float(1.3f);
115     SimpleType WFloatType = SimpleType.FLOAT;
getWFloat()116     Float getWFloat();
setWFloat(Float x)117     void setWFloat(Float x);
opWFloat(Float x, Float y)118     Float opWFloat(Float x, Float y);
119 
120     Double WDouble = new Double(Double.MAX_VALUE);
121     SimpleType WDoubleType = SimpleType.DOUBLE;
getWDouble()122     Double getWDouble();
setWDouble(Double x)123     void setWDouble(Double x);
opWDouble(Double x, Double y)124     Double opWDouble(Double x, Double y);
125 
126     Boolean WBoolean = Boolean.TRUE;
127     SimpleType WBooleanType = SimpleType.BOOLEAN;
getWBoolean()128     Boolean getWBoolean();
setWBoolean(Boolean x)129     void setWBoolean(Boolean x);
opWBoolean(Boolean x, Boolean y)130     Boolean opWBoolean(Boolean x, Boolean y);
131 
132     int[] PIntA = {2, 3, 5, 7, 11, 13};
133     ArrayType PIntAType = ArrayTypeMaker.make(SimpleType.INTEGER, true);
getPIntA()134     int[] getPIntA();
setPIntA(int[] x)135     void setPIntA(int[] x);
opPIntA(int[] x, int[] y)136     int[] opPIntA(int[] x, int[] y);
137 
138     int[][] PInt2D = {{1, 2}, {3, 4}};
139     ArrayType PInt2DType = ArrayTypeMaker.make(1, PIntAType);
getPInt2D()140     int[][] getPInt2D();
setPInt2D(int[][] x)141     void setPInt2D(int[][] x);
opPInt2D(int[][] x, int[][] y)142     int[][] opPInt2D(int[][] x, int[][] y);
143 
144     Integer[] WIntA = {new Integer(3), new Integer(5)};
145     ArrayType WIntAType = ArrayTypeMaker.make(1, SimpleType.INTEGER);
getWIntA()146     Integer[] getWIntA();
setWIntA(Integer[] x)147     void setWIntA(Integer[] x);
opWIntA(Integer[] x, Integer[] y)148     Integer[] opWIntA(Integer[] x, Integer[] y);
149 
150     Integer[][] WInt2D = {{new Integer(3)}, {new Integer(5)}};
151     ArrayType WInt2DType = ArrayTypeMaker.make(2, SimpleType.INTEGER);
getWInt2D()152     Integer[][] getWInt2D();
setWInt2D(Integer[][] x)153     void setWInt2D(Integer[][] x);
opWInt2D(Integer[][] x, Integer[][] y)154     Integer[][] opWInt2D(Integer[][] x, Integer[][] y);
155 
156     String XString = "yo!";
157     SimpleType XStringType = SimpleType.STRING;
getXString()158     String getXString();
setXString(String x)159     void setXString(String x);
opXString(String x, String y)160     String opXString(String x, String y);
161 
162     String[] XStringA = {"hello", "world"};
163     ArrayType XStringAType = ArrayTypeMaker.make(1, SimpleType.STRING);
getXStringA()164     String[] getXStringA();
setXStringA(String[] x)165     void setXStringA(String[] x);
opXStringA(String[] x, String[] y)166     String[] opXStringA(String[] x, String[] y);
167 
168     int[] NoInts = {};
169     ArrayType NoIntsType = ArrayTypeMaker.make(SimpleType.INTEGER, true);
getNoInts()170     int[] getNoInts();
setNoInts(int[] x)171     void setNoInts(int[] x);
opNoInts(int[] x, int[] y)172     int[] opNoInts(int[] x, int[] y);
173 
174     GetSetBean GetSet = GetSetBean.make(5, "x", new String[] {"a", "b"});
175     CompositeType GetSetType =
176         CompositeTypeMaker.make(GetSetBean.class.getName(),
177                                 GetSetBean.class.getName(),
178                                 new String[] {"int", "string", "stringArray"},
179                                 new String[] {"int", "string", "stringArray"},
180                                 new OpenType[] {
181                                     SimpleType.INTEGER,
182                                     SimpleType.STRING,
183                                     ArrayTypeMaker.make(1, SimpleType.STRING),
184                                 });
getGetSet()185     GetSetBean getGetSet();
setGetSet(GetSetBean bean)186     void setGetSet(GetSetBean bean);
opGetSet(GetSetBean x, GetSetBean y)187     GetSetBean opGetSet(GetSetBean x, GetSetBean y);
188 
189     GetterInterface Interface = new GetterInterface() {
190         public boolean isWhatsit() {
191             return true;
192         }
193 
194         public int[] getInts() {
195             return new int[] {1};
196         }
197 
198         public String[] getStrings() {
199             return new String[] {"x"};
200         }
201 
202         public GetSetBean getGetSet() {
203             return GetSetBean.make(3, "a", new String[] {"b"});
204         }
205 
206         public boolean equals(Object o) {
207             if (!(o instanceof GetterInterface))
208                 return false;
209             GetterInterface i = (GetterInterface) o;
210             return isWhatsit() == i.isWhatsit() &&
211                    Arrays.equals(getInts(), i.getInts()) &&
212                    Arrays.equals(getStrings(), i.getStrings()) &&
213                    getGetSet().equals(i.getGetSet());
214         }
215      };
216      CompositeType InterfaceType =
217         CompositeTypeMaker.make(GetterInterface.class.getName(),
218                                 GetterInterface.class.getName(),
219                                 new String[] {
220                                     "ints", "getSet", "strings", "whatsit",
221                                 },
222                                 new String[] {
223                                     "ints", "getSet", "strings", "whatsit",
224                                 },
225                                 new OpenType[] {
226                                     ArrayTypeMaker.make(SimpleType.INTEGER, true),
227                                     GetSetType,
228                                     ArrayTypeMaker.make(1, SimpleType.STRING),
229                                     SimpleType.BOOLEAN,
230                                 });
getInterface()231      GetterInterface getInterface();
setInterface(GetterInterface i)232      void setInterface(GetterInterface i);
opInterface(GetterInterface x, GetterInterface y)233      GetterInterface opInterface(GetterInterface x, GetterInterface y);
234 
235     /* Testing that we can use a public no-arg constructor plus a setter
236      * for every getter to reconstruct this object.  Note that the
237      * constructor-guessing logic is no longer valid for this class,
238      * so if we can reconstruct it it must be because of the setters.
239      */
240     public static class GetSetBean {
GetSetBean()241         public GetSetBean() {
242             this(0, null, null);
243         }
244 
GetSetBean(int Int, String string, String[] stringArray)245         private GetSetBean(int Int, String string, String[] stringArray) {
246             this.Int = Int;
247             this.string = string;
248             this.stringArray = stringArray;
249         }
250 
251         public static GetSetBean
make(int Int, String string, String[] stringArray)252                 make(int Int, String string, String[] stringArray) {
253             GetSetBean b = new GetSetBean(Int, string, stringArray);
254             return b;
255         }
256 
getInt()257         public int getInt() {
258             return Int;
259         }
260 
getString()261         public String getString() {
262             return this.string;
263         }
264 
getStringArray()265         public String[] getStringArray() {
266             return this.stringArray;
267         }
268 
setInt(int x)269         public void setInt(int x) {
270             this.Int = x;
271         }
272 
setString(String string)273         public void setString(String string) {
274             this.string = string;
275         }
276 
setStringArray(String[] stringArray)277         public void setStringArray(String[] stringArray) {
278             this.stringArray = stringArray;
279         }
280 
equals(Object o)281         public boolean equals(Object o) {
282             if (!(o instanceof GetSetBean))
283                 return false;
284             GetSetBean b = (GetSetBean) o;
285             return (b.Int == Int &&
286                     b.string.equals(string) &&
287                     Arrays.equals(b.stringArray, stringArray));
288         }
289 
290         String string;
291         String[] stringArray;
292         int Int;
293     }
294 
295     public static interface GetterInterface {
getStrings()296         public String[] getStrings();
getInts()297         public int[] getInts();
isWhatsit()298         public boolean isWhatsit();
getGetSet()299         public GetSetBean getGetSet();
300 
301         // We uselessly mention the public methods inherited from Object because
302         // they should not prevent the interface from being translatable.
303         // We could consider encoding the result of hashCode() and toString()
304         // on the original object that implements this interface into the
305         // generated CompositeData and referencing that in the proxy, but
306         // that seems ambitious for now.  Doing it only if hashCode() and/or
307         // toString() are mentioned in the interface is a possibility but
308         // a rather abstruse one.
equals(Object o)309         public boolean equals(Object o);
hashCode()310         public int hashCode();
toString()311         public String toString();
312     }
313 
314     public static class ArrayTypeMaker {
make(int dims, OpenType baseType)315         static ArrayType make(int dims, OpenType baseType) {
316             try {
317                 return new ArrayType(dims, baseType);
318             } catch (OpenDataException e) {
319                 throw new Error(e);
320             }
321         }
322 
make(SimpleType baseType, boolean primitiveArray)323         static ArrayType make(SimpleType baseType, boolean primitiveArray) {
324             try {
325                 return new ArrayType(baseType, primitiveArray);
326             } catch (OpenDataException e) {
327                 throw new Error(e);
328             }
329         }
330     }
331 
332     public static class CompositeTypeMaker {
make(String className, String description, String[] itemNames, String[] itemDescriptions, OpenType[] itemTypes)333         static CompositeType make(String className,
334                                   String description,
335                                   String[] itemNames,
336                                   String[] itemDescriptions,
337                                   OpenType[] itemTypes) {
338             try {
339                 return new CompositeType(className,
340                                          description,
341                                          itemNames,
342                                          itemDescriptions,
343                                          itemTypes);
344             } catch (OpenDataException e) {
345                 throw new Error(e);
346             }
347         }
348     }
349 
350     public static interface GraphMXBean {
getNodes()351         public NodeMXBean[] getNodes();
352     }
353 
354     public static class Graph implements GraphMXBean {
Graph(Node... nodes)355         public Graph(Node... nodes) {
356             for (Node node : nodes)
357                 node.setGraph(this);
358             this.nodes = nodes;
359         }
360 
getNodes()361         public NodeMXBean[] getNodes() {
362             return nodes;
363         }
364 
365         private final Node[] nodes;
366     }
367 
368     public static interface NodeMXBean {
getName()369         public String getName();
getGraph()370         public GraphMXBean getGraph();
371     }
372 
373     public static class Node implements NodeMXBean {
Node(String name)374         public Node(String name) {
375             this.name = name;
376         }
377 
getName()378         public String getName() {
379             return name;
380         }
381 
getGraph()382         public GraphMXBean getGraph() {
383             return graph;
384         }
385 
setGraph(Graph graph)386         public void setGraph(Graph graph) {
387             this.graph = graph;
388         }
389 
390         private final String name;
391         private Graph graph;
392     }
393 
394     SimpleType GraphType = SimpleType.OBJECTNAME;
getGraph()395     GraphMXBean getGraph();
setGraph(GraphMXBean g)396     void setGraph(GraphMXBean g);
opGraph(GraphMXBean x, GraphMXBean y)397     GraphMXBean opGraph(GraphMXBean x, GraphMXBean y);
398     String GraphObjectName = "test:type=GraphMXBean";
399     String NodeAObjectName = "test:type=NodeMXBean,name=a";
400     String NodeBObjectName = "test:type=NodeMXBean,name=b";
401     Node NodeA = new Node("a");
402     Node NodeB = new Node("b");
403     GraphMXBean Graph = new Graph(NodeA, NodeB);
404 
405     public static class ExoticCompositeData implements CompositeDataView {
ExoticCompositeData(String whatsit)406         private ExoticCompositeData(String whatsit) {
407             this.whatsit = whatsit;
408         }
409 
from(CompositeData cd)410         public static ExoticCompositeData from(CompositeData cd) {
411             String whatsit = (String) cd.get("whatsit");
412             if (!whatsit.startsWith("!"))
413                 throw new IllegalArgumentException(whatsit);
414             return new ExoticCompositeData(whatsit.substring(1));
415         }
416 
toCompositeData(CompositeType ct)417         public CompositeData toCompositeData(CompositeType ct) {
418             try {
419                 return new CompositeDataSupport(ct, new String[] {"whatsit"},
420                                                 new String[] {"!" + whatsit});
421             } catch (Exception e) {
422                 throw new RuntimeException(e);
423             }
424         }
425 
getWhatsit()426         public String getWhatsit() {
427             return whatsit;
428         }
429 
equals(Object o)430         public boolean equals(Object o) {
431             return ((o instanceof ExoticCompositeData) &&
432                     ((ExoticCompositeData) o).whatsit.equals(whatsit));
433         }
434 
435         private final String whatsit;
436 
437         public static final CompositeType type;
438         static {
439             try {
440                 type =
441                     new CompositeType(ExoticCompositeData.class.getName(),
442                                       ExoticCompositeData.class.getName(),
443                                       new String[] {"whatsit"},
444                                       new String[] {"whatsit"},
445                                       new OpenType[] {SimpleType.STRING});
446             } catch (Exception e) {
447                 throw new RuntimeException(e);
448             }
449         }
450     }
451     CompositeType ExoticType = ExoticCompositeData.type;
getExotic()452     ExoticCompositeData getExotic();
setExotic(ExoticCompositeData ecd)453     void setExotic(ExoticCompositeData ecd);
opExotic(ExoticCompositeData ecd1, ExoticCompositeData ecd2)454     ExoticCompositeData opExotic(ExoticCompositeData ecd1,
455                                  ExoticCompositeData ecd2);
456     ExoticCompositeData Exotic = new ExoticCompositeData("foo");
457 }
458