1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * 
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * The contents of this file are subject to the terms of either the Universal Permissive License
   7  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   8  *
   9  * or the following license:
  10  *
  11  * Redistribution and use in source and binary forms, with or without modification, are permitted
  12  * provided that the following conditions are met:
  13  * 
  14  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  15  * and the following disclaimer.
  16  * 
  17  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  18  * conditions and the following disclaimer in the documentation and/or other materials provided with
  19  * the distribution.
  20  * 
  21  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  22  * endorse or promote products derived from this software without specific prior written permission.
  23  * 
  24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  31  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  */
  33 package org.openjdk.jmc.rjmx.test.internal;
  34 
  35 import java.lang.management.ManagementFactory;
  36 
  37 import javax.management.ObjectName;
  38 import javax.management.openmbean.ArrayType;
  39 import javax.management.openmbean.CompositeData;
  40 import javax.management.openmbean.CompositeDataSupport;
  41 import javax.management.openmbean.CompositeType;
  42 import javax.management.openmbean.OpenDataException;
  43 import javax.management.openmbean.OpenType;
  44 import javax.management.openmbean.SimpleType;
  45 import javax.management.openmbean.TabularData;
  46 import javax.management.openmbean.TabularDataSupport;
  47 import javax.management.openmbean.TabularType;
  48 
  49 /**
  50  * When this bean is added to the management server of a JRockit, it provides a bean that exposes
  51  * nested TabularData and CompositeData structures through attributes and operations. This class is
  52  * meant to be used to test GUI components, such as the MBeanBrowser of the ManagementConsole, that
  53  * inspects such structures.
  54  */
  55 public class TabularDataBeanTestMBean implements ITabularDataBeanTestMBean {
  56 
  57         TabularDataSupport tabTest;
  58         CompositeData compTest;
  59 
  60         public TabularDataBeanTestMBean() {
  61                 CompositeType simpleCompositeType;
  62                 try {
  63                         simpleCompositeType = new CompositeType("simpleCompositeType", "compdescription", //$NON-NLS-1$ //$NON-NLS-2$
  64                                         new String[] {"djur", "bil", "apa", "båt"}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  65                                         new String[] {"ett djur", "en bil", "en apa", "en båt"}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  66                                         new OpenType[] {SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING,});
  67 
  68                         TabularType simpleTabularType = new TabularType("simpleTabularTypeName", "tabdescription", //$NON-NLS-1$ //$NON-NLS-2$
  69                                         simpleCompositeType, new String[] {"djur", "bil", "apa", "båt"}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  70 
  71                         OpenType<?>[] compositeContentsTypes = new OpenType[5];
  72                         String[] compositeKeys = new String[5];
  73                         String[] compositeDescriptions = new String[5];
  74                         compositeContentsTypes[0] = simpleTabularType;
  75                         compositeKeys[0] = "Tab Data"; //$NON-NLS-1$
  76                         compositeDescriptions[0] = "tab data desc"; //$NON-NLS-1$
  77                         compositeContentsTypes[4] = simpleCompositeType;
  78                         compositeKeys[4] = "Comp Data"; //$NON-NLS-1$
  79                         compositeDescriptions[4] = "comp data desc"; //$NON-NLS-1$
  80                         compositeContentsTypes[3] = new ArrayType<String>(2, SimpleType.STRING);
  81                         compositeKeys[3] = "Array Data"; //$NON-NLS-1$
  82                         compositeDescriptions[3] = "comp data desc"; //$NON-NLS-1$
  83                         for (int i = 1; i < 3; i++) {
  84                                 compositeContentsTypes[i] = SimpleType.STRING;
  85                                 compositeKeys[i] = "StringIndex" + i; //$NON-NLS-1$
  86                                 compositeDescriptions[i] = "description" + i; //$NON-NLS-1$
  87                         }
  88                         CompositeType complexCompositeType = new CompositeType("ComplexCompositeTypeName", //$NON-NLS-1$
  89                                         "complex composite type desc", compositeKeys, compositeDescriptions, compositeContentsTypes); //$NON-NLS-1$
  90                         TabularType complexTabularType = new TabularType("ComplexTabularTypeName", "complex tabular type", //$NON-NLS-1$ //$NON-NLS-2$
  91                                         complexCompositeType, compositeKeys);
  92 
  93                         CompositeData simpleCompositeData = new CompositeDataSupport(simpleCompositeType,
  94                                         new String[] {"djur", "bil", "apa", "båt"}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  95                                         new String[] {"häst", "corvette", "lemur", "nautilus"}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  96                         TabularDataSupport simpleTabularData = new TabularDataSupport(simpleTabularType);
  97                         simpleTabularData.put(simpleCompositeData);
  98                         /*
  99                          * simpleTabularData.put(simpleCompositeData);
 100                          * simpleTabularData.put(simpleCompositeData);
 101                          * simpleTabularData.put(simpleCompositeData);
 102                          */
 103 
 104                         compTest = new CompositeDataSupport(complexCompositeType, compositeKeys,
 105                                         new Object[] {simpleTabularData, "string2", "string3", //$NON-NLS-1$ //$NON-NLS-2$
 106                                                         new String[][] {{"string1_1", "string1_2"}, {"string2_1", "string2_2"}}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
 107                                                         simpleCompositeData});
 108                         tabTest = new TabularDataSupport(complexTabularType);
 109                         tabTest.put(compTest);
 110                         tabTest.put(new CompositeDataSupport(complexCompositeType, compositeKeys,
 111                                         new Object[] {simpleTabularData, "secondcomp2", "secondcomp3", //$NON-NLS-1$ //$NON-NLS-2$
 112                                                         new String[][] {{"array1_1", "array1_2"}, {"array2_1", "array2_2"}}, simpleCompositeData})); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
 113                         /*
 114                          * tabTest.put(complexCompositeData); tabTest.put(complexCompositeData);
 115                          * tabTest.put(complexCompositeData); tabTest.put(complexCompositeData);
 116                          */
 117 
 118                 } catch (OpenDataException e) {
 119                         // TODO: Add proper logging
 120                         e.printStackTrace();
 121                 }
 122         }
 123 
 124         /*
 125          * (non-Javadoc)
 126          *
 127          * @see com.jrockit.console.rjmx.TestMBean#getTabTest()
 128          */
 129         @Override
 130         public TabularData getTabTest() {
 131                 return tabTest;
 132         }
 133 
 134         /*
 135          * (non-Javadoc)
 136          *
 137          * @see com.jrockit.console.rjmx.TestMBean#operationThatReturnsTabularData()
 138          */
 139         @Override
 140         public TabularData operationThatReturnsTabularData() {
 141                 return tabTest;
 142         }
 143 
 144         /*
 145          * (non-Javadoc)
 146          *
 147          * @see com.jrockit.console.rjmx.TestMBean#operationThatReturnsComposite()
 148          */
 149         @Override
 150         public CompositeData operationThatReturnsComposite() {
 151                 return compTest;
 152         }
 153 
 154         public static void main(String[] args) {
 155                 try {
 156                         try {
 157                                 ManagementFactory.getPlatformMBeanServer().createMBean(TabularDataBeanTestMBean.class.getName(),
 158                                                 new ObjectName("com.jrockit", "name", "TestMBean")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 159                         } catch (Exception e) {
 160                                 e.printStackTrace();
 161                         }
 162                         while (true) {
 163                                 Thread.sleep(1000);
 164                         }
 165                 } catch (InterruptedException e) {
 166                         // TODO: Add proper logging
 167                         e.printStackTrace();
 168                 }
 169         }
 170 }