1 /* 2 * Copyright (c) 2008, 2015, 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 /* 25 * @test 26 * @bug 6692027 27 * @summary Check that custom subclasses of QueryEval can be serialized. 28 * @author Eamonn McManus 29 * @modules java.management 30 */ 31 32 import java.io.ByteArrayInputStream; 33 import java.io.ByteArrayOutputStream; 34 import java.io.ObjectInputStream; 35 import java.io.ObjectOutputStream; 36 import java.lang.management.ManagementFactory; 37 import java.util.Set; 38 import java.util.concurrent.atomic.AtomicInteger; 39 import javax.management.MBeanServer; 40 import javax.management.MalformedObjectNameException; 41 import javax.management.ObjectName; 42 import javax.management.QueryEval; 43 import javax.management.QueryExp; 44 45 public class CustomQueryTest { 46 public static interface CountMBean { 47 public int getCount(); 48 public void increment(); 49 } 50 51 public static class Count implements CountMBean { 52 private AtomicInteger count = new AtomicInteger(); 53 54 public int getCount() { 55 return count.get(); 56 } 57 58 public void increment() { 59 count.incrementAndGet(); 60 } 61 62 } 63 64 public static final ObjectName countName; 65 static { 66 try { 67 countName = new ObjectName("d:type=Count"); 68 } catch (MalformedObjectNameException e) { 69 throw new AssertionError(e); 70 } 71 } 72 73 /* A query that calls the increment method of the Count MBean every time 74 * it is evaluated. If there is no ObjectName filter, the query will be 75 * evaluated for every MBean in the MBean Server, so the count will be 76 * incremented by the number of MBeans. 77 */ 78 public static class IncrQuery extends QueryEval implements QueryExp { 79 public boolean apply(ObjectName name) { 80 try { 81 getMBeanServer().invoke(countName, "increment", null, null); 82 return true; 83 } catch (Throwable t) { 84 t.printStackTrace(); 85 System.exit(1); 86 throw new AssertionError(); // not reached 87 } 88 } 89 } 90 91 public static void main(String[] args) throws Exception { 92 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 93 mbs.registerMBean(new Count(), countName); 94 int mbeanCount = mbs.getMBeanCount(); 95 QueryExp query = new IncrQuery(); 96 Set<ObjectName> names = mbs.queryNames(null, query); 97 assertEquals(mbeanCount, names.size()); 98 assertEquals(mbeanCount, mbs.getAttribute(countName, "Count")); 99 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 100 ObjectOutputStream oout = new ObjectOutputStream(bout); 101 oout.writeObject(query); 102 oout.close(); 103 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); 104 ObjectInputStream oin = new ObjectInputStream(bin); 105 query = (QueryExp) oin.readObject(); 106 names = mbs.queryNames(null, query); 107 assertEquals(mbeanCount * 2, mbs.getAttribute(countName, "Count")); 108 } 109 110 private static void assertEquals(Object expected, Object actual) 111 throws Exception { 112 if (!expected.equals(actual)) { 113 String failure = "FAILED: expected " + expected + ", got " + actual; 114 throw new Exception(failure); 115 } 116 } 117 }