public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) { Set<ObjectInstance> instances = newSet(); if (name == null) name = ObjectName.WILDCARD; if (query == null) query = trueQuery; MBeanServer oldMBS = QueryEval.getMBeanServer(); try { query.setMBeanServer(this); for (ObjectName n : mbeans.keySet()) { if (name.apply(n)) { try { if (query.apply(n)) instances.add(getObjectInstance(n)); } catch (Exception e) { // OK: Ignore this MBean in the result } } } } finally { query.setMBeanServer(oldMBS); } return instances; }
/** * Applies the specified queries to the set of ObjectInstances. */ private Set<ObjectInstance> filterListOfObjectInstances(Set<ObjectInstance> list, QueryExp query) { // Null query. // if (query == null) { return list; } else { Set<ObjectInstance> result = new HashSet<ObjectInstance>(); // Access the filter. // for (ObjectInstance oi : list) { boolean res = false; MBeanServer oldServer = QueryEval.getMBeanServer(); query.setMBeanServer(server); try { res = query.apply(oi.getObjectName()); } catch (Exception e) { res = false; } finally { /* * query.setMBeanServer is probably * QueryEval.setMBeanServer so put back the old * value. Since that method uses a ThreadLocal * variable, this code is only needed for the * unusual case where the user creates a custom * QueryExp that calls a nested query on another * MBeanServer. */ query.setMBeanServer(oldServer); } if (res) { result.add(oi); } } return result; } }
/** Set the mbean server on the QueryExp and try and pass back any previously set one */ private static MBeanServer setQueryExpServer(QueryExp query, MBeanServer toSet) { // We assume the QueryExp is a QueryEval subclass or uses the QueryEval thread local // mechanism to store any existing MBeanServer. If that's not the case we have no // way to access the old mbeanserver to let us restore it MBeanServer result = QueryEval.getMBeanServer(); query.setMBeanServer(toSet); return result; }
public boolean apply(final ObjectName name) { final MBeanServer mbs = QueryEval.getMBeanServer(); return isInstanceOf(mbs, name, broadcasterClass); }
/** * Create MBean server, register MBeans. */ public final void setUp() throws Exception { // Get the Platform MBean Server mbs = MBeanServerFactory.createMBeanServer(); // Sets the MBean server on which the query is to be performed. new QueryEval() { private static final long serialVersionUID = 3832902139541599280L; }.setMBeanServer(mbs); instances = new Hello[10]; names = new ObjectName[instances.length]; instances[0] = new Hello(); instances[1] = new Hello(); instances[2] = new Hello(); instances[1].setNumber(new Integer(123)); instances[1].setNumber1(new BigDecimal((double) 123123123)); instances[2].setIntNumber1(10); instances[2].setIntNumber2(100); instances[2].setLongNumber1(10); instances[2].setLongNumber2(100); instances[2].setDoubleNumber1(10); instances[2].setDoubleNumber2(100); instances[2].setFloatNumber1(10); instances[2].setFloatNumber2(100); instances[2].setBoolean1(true); instances[2].setBoolean2(false); instances[2].setNumber1(new BigDecimal((double) 10)); instances[2].setNumber2(new BigDecimal((double) 100)); // Register the Hello MBean for (int i = 0; i < instances.length; i++) { if (instances[i] != null) { names[i] = new ObjectName( "org.apache.harmony.test.func.api.javax.management:type=Hello" + i); mbs.registerMBean(instances[i], names[i]); } } }