下面列出了怎么用javax.management.QueryExp的API类实例代码及写法,或者点击链接到github查看源代码。
private Set<ObjectName> queryNames(final ObjectName name,
final QueryExp query) {
PrivilegedAction<Set<ObjectName>> act =
new PrivilegedAction<Set<ObjectName>>() {
public Set<ObjectName> run() {
return mBeanServer.queryNames(name, query);
}
};
try {
return AccessController.doPrivileged(act);
} catch (RuntimeException e) {
logger.fine("queryNames", "Failed to query names: " + e);
logger.debug("queryNames", e);
throw e;
}
}
public Set<ObjectName> queryNames(ObjectName name,
QueryExp query)
throws IOException {
if (logger.debugOn()) logger.debug("queryNames",
"name=" + name + ", query=" + query);
final MarshalledObject<QueryExp> sQuery =
new MarshalledObject<QueryExp>(query);
final ClassLoader old = pushDefaultClassLoader();
try {
return connection.queryNames(name, sQuery, delegationSubject);
} catch (IOException ioe) {
communicatorAdmin.gotIOException(ioe);
return connection.queryNames(name, sQuery, delegationSubject);
} finally {
popDefaultClassLoader(old);
}
}
public Set<ObjectName> queryNames(ObjectName name,
QueryExp query)
throws IOException {
if (logger.debugOn()) logger.debug("queryNames",
"name=" + name + ", query=" + query);
final MarshalledObject<QueryExp> sQuery =
new MarshalledObject<QueryExp>(query);
final ClassLoader old = pushDefaultClassLoader();
try {
return connection.queryNames(name, sQuery, delegationSubject);
} catch (IOException ioe) {
communicatorAdmin.gotIOException(ioe);
return connection.queryNames(name, sQuery, delegationSubject);
} finally {
popDefaultClassLoader(old);
}
}
private Set<ObjectName> queryNames(final ObjectName name,
final QueryExp query) {
PrivilegedAction<Set<ObjectName>> act =
new PrivilegedAction<Set<ObjectName>>() {
public Set<ObjectName> run() {
return mBeanServer.queryNames(name, query);
}
};
try {
return AccessController.doPrivileged(act);
} catch (RuntimeException e) {
logger.fine("queryNames", "Failed to query names: " + e);
logger.debug("queryNames", e);
throw e;
}
}
public Set<ObjectName> queryNames(ObjectName name,
QueryExp query)
throws IOException {
if (logger.debugOn()) logger.debug("queryNames",
"name=" + name + ", query=" + query);
final MarshalledObject<QueryExp> sQuery =
new MarshalledObject<QueryExp>(query);
final ClassLoader old = pushDefaultClassLoader();
try {
return connection.queryNames(name, sQuery, delegationSubject);
} catch (IOException ioe) {
communicatorAdmin.gotIOException(ioe);
return connection.queryNames(name, sQuery, delegationSubject);
} finally {
popDefaultClassLoader(old);
}
}
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;
}
public Set<ObjectName> queryNames(ObjectName name,
QueryExp query)
throws IOException {
if (logger.debugOn()) logger.debug("queryNames",
"name=" + name + ", query=" + query);
final MarshalledObject<QueryExp> sQuery =
new MarshalledObject<QueryExp>(query);
final ClassLoader old = pushDefaultClassLoader();
try {
return connection.queryNames(name, sQuery, delegationSubject);
} catch (IOException ioe) {
communicatorAdmin.gotIOException(ioe);
return connection.queryNames(name, sQuery, delegationSubject);
} finally {
popDefaultClassLoader(old);
}
}
private Set<ObjectName> queryNames(final ObjectName name,
final QueryExp query) {
PrivilegedAction<Set<ObjectName>> act =
new PrivilegedAction<Set<ObjectName>>() {
public Set<ObjectName> run() {
return mBeanServer.queryNames(name, query);
}
};
try {
return AccessController.doPrivileged(act);
} catch (RuntimeException e) {
logger.fine("queryNames", "Failed to query names: " + e);
logger.debug("queryNames", e);
throw e;
}
}
public Set<ObjectInstance> queryMBeans(ObjectName name,
QueryExp query)
throws IOException {
if (logger.debugOn()) logger.debug("queryMBeans",
"name=" + name + ", query=" + query);
final MarshalledObject<QueryExp> sQuery =
new MarshalledObject<QueryExp>(query);
final ClassLoader old = pushDefaultClassLoader();
try {
return connection.queryMBeans(name, sQuery, delegationSubject);
} catch (IOException ioe) {
communicatorAdmin.gotIOException(ioe);
return connection.queryMBeans(name, sQuery, delegationSubject);
} finally {
popDefaultClassLoader(old);
}
}
public Set<ObjectName> queryNames(ObjectName name,
QueryExp query)
throws IOException {
if (logger.debugOn()) logger.debug("queryNames",
"name=" + name + ", query=" + query);
final MarshalledObject<QueryExp> sQuery =
new MarshalledObject<QueryExp>(query);
final ClassLoader old = pushDefaultClassLoader();
try {
return connection.queryNames(name, sQuery, delegationSubject);
} catch (IOException ioe) {
communicatorAdmin.gotIOException(ioe);
return connection.queryNames(name, sQuery, delegationSubject);
} finally {
popDefaultClassLoader(old);
}
}
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;
}
private Set<ObjectName> queryNames(final ObjectName name,
final QueryExp query) {
PrivilegedAction<Set<ObjectName>> act =
new PrivilegedAction<Set<ObjectName>>() {
public Set<ObjectName> run() {
return mBeanServer.queryNames(name, query);
}
};
try {
return AccessController.doPrivileged(act);
} catch (RuntimeException e) {
logger.fine("queryNames", "Failed to query names: " + e);
logger.debug("queryNames", e);
throw e;
}
}
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;
}
public Set<ObjectName> queryNames(ObjectName name,
QueryExp query)
throws IOException {
if (logger.debugOn()) logger.debug("queryNames",
"name=" + name + ", query=" + query);
final MarshalledObject<QueryExp> sQuery =
new MarshalledObject<QueryExp>(query);
final ClassLoader old = pushDefaultClassLoader();
try {
return connection.queryNames(name, sQuery, delegationSubject);
} catch (IOException ioe) {
communicatorAdmin.gotIOException(ioe);
return connection.queryNames(name, sQuery, delegationSubject);
} finally {
popDefaultClassLoader(old);
}
}
/**
* Checks if the specified object is deployed in JBoss Application Server
*
* @return if specified object is deployed
*/
public static boolean isObjectDeployed(JBDeploymentManager dm, final ObjectName searchPattern) {
try {
dm.invokeRemoteAction(new JBRemoteAction<Boolean>() {
@Override
public Boolean action(MBeanServerConnection connection, JBoss5ProfileServiceProxy profileService) throws Exception {
// FIXME is this reflection really needed
Method method = connection.getClass().getMethod("queryMBeans", new Class[] {ObjectName.class, QueryExp.class});
method = fixJava4071957(method);
Set managedObj = (Set) method.invoke(connection, new Object[] {searchPattern, null});
return managedObj.size() > 0;
}
});
} catch (ExecutionException ex) {
LOGGER.log(Level.INFO, null, ex);
}
return false;
}
private Set<ObjectName> queryNames(final ObjectName name,
final QueryExp query) {
PrivilegedAction<Set<ObjectName>> act =
new PrivilegedAction<Set<ObjectName>>() {
public Set<ObjectName> run() {
return mBeanServer.queryNames(name, query);
}
};
try {
return AccessController.doPrivileged(act);
} catch (RuntimeException e) {
logger.fine("queryNames", "Failed to query names: " + e);
logger.debug("queryNames", e);
throw e;
}
}
public Set<ObjectName> queryNames(ObjectName name,
QueryExp query)
throws IOException {
if (logger.debugOn()) logger.debug("queryNames",
"name=" + name + ", query=" + query);
final MarshalledObject<QueryExp> sQuery =
new MarshalledObject<QueryExp>(query);
final ClassLoader old = pushDefaultClassLoader();
try {
return connection.queryNames(name, sQuery, delegationSubject);
} catch (IOException ioe) {
communicatorAdmin.gotIOException(ioe);
return connection.queryNames(name, sQuery, delegationSubject);
} finally {
popDefaultClassLoader(old);
}
}
public Set<ObjectName> queryNames(ObjectName name,
QueryExp query)
throws IOException {
if (logger.debugOn()) logger.debug("queryNames",
"name=" + name + ", query=" + query);
final MarshalledObject<QueryExp> sQuery =
new MarshalledObject<QueryExp>(query);
final ClassLoader old = pushDefaultClassLoader();
try {
return connection.queryNames(name, sQuery, delegationSubject);
} catch (IOException ioe) {
communicatorAdmin.gotIOException(ioe);
return connection.queryNames(name, sQuery, delegationSubject);
} finally {
popDefaultClassLoader(old);
}
}
/**
* Wait till all service requests have been serviced. This method will only wait for a maximum
* of {@link ServerManagement#TIMEOUT}
*
* @throws Exception If an error occurs while trying to connect to the Tomcat MBean
*/
public void waitForRequestCompletion() throws Exception {
SecurityManager secMan = System.getSecurityManager();
if (secMan != null) {
secMan.checkPermission(new ManagementPermission("control"));
}
log.info("Waiting for request service completion...");
/**
* Get all MBeans with names such as Catalina:type=RequestProcessor,worker=http-9762,name=HttpRequest<n>
* & Catalina:type=RequestProcessor,worker=http-9762,name=HttpsRequest<n>
*/
MBeanServer mbs = MbeanManagementFactory.getMBeanServer();
boolean areRequestsInService;
long start = System.currentTimeMillis();
do {
// Check whether there are any processors which are currently in the SERVICE stage (3)
QueryExp query = Query.eq(Query.attr("stage"), Query.value(3)); // 3 = org.apache.coyote.Constants.STAGE_SERVICE
Set set = mbs.queryNames(new ObjectName("Catalina:type=RequestProcessor,*"), query);
if (set.size() > 0) {
areRequestsInService = true;
if (System.currentTimeMillis() - start > TIMEOUT) {
log.warn("Timeout occurred even though there are active connections.");
break;
}
Thread.sleep(2000);
} else {
areRequestsInService = false;
}
} while (areRequestsInService);
log.info("All requests have been served.");
}
private Set<ObjectName> queryNamesImpl(ObjectName name, QueryExp query) {
// Query the MBeans on the repository
//
Set<NamedObject> list = repository.query(name, query);
return (objectNamesFromFilteredNamedObjects(list, query));
}
private int doQueryMBeans(QueryExp query, Set<ObjectInstance> referenceSet) {
int errorCount = 0;
System.out.println(" <*> Perform queryMBeans call ");
try {
// Call queryMBeans on the remote MBeanServer
Set<ObjectInstance> remoteSet = mbsc.queryMBeans(null, query);
// Compare the 2 Set<ObjectInstance>
errorCount += checkSet(remoteSet, referenceSet);
// Cleaning
remoteSet.clear();
} catch (Exception e) {
Utils.printThrowable(e, true);
errorCount++;
}
if ( errorCount == 0 ) {
System.out.println("\t(OK)");
} else {
System.out.println("\t(ERROR) Query failed");
}
return errorCount;
}
/**
* 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;
}
}
private static int query(MBeanServer mbs,
String pattern,
String[][] data) throws Exception {
int error = 0;
System.out.println("\nAttribute Value Pattern = " + pattern + "\n");
for (int i = 0; i < data.length; i++) {
ObjectName on = new ObjectName("domain:type=Simple,pattern=" +
ObjectName.quote(pattern) +
",name=" + i);
Simple s = new Simple(data[i][0]);
mbs.registerMBean(s, on);
QueryExp q =
Query.match(Query.attr("StringNumber"), Query.value(pattern));
q.setMBeanServer(mbs);
boolean r = q.apply(on);
System.out.print("Attribute Value = " +
mbs.getAttribute(on, "StringNumber"));
if (r && "OK".equals(data[i][1])) {
System.out.println(" OK");
} else if (!r && "KO".equals(data[i][1])) {
System.out.println(" KO");
} else {
System.out.println(" Error");
error++;
}
}
return error;
}
public Set<ObjectName> queryNames(ObjectName name, QueryExp query) {
Set<ObjectInstance> instances = queryMBeans(name, query);
Set<ObjectName> names = newSet();
for (ObjectInstance instance : instances)
names.add(instance.getObjectName());
return names;
}
private Set<ObjectInstance> queryMBeansImpl(ObjectName name,
QueryExp query) {
// Query the MBeans on the repository
//
Set<NamedObject> list = repository.query(name, query);
return (objectInstancesFromFilteredNamedObjects(list, query));
}
public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
// Check if the caller has the right to invoke 'queryMBeans'
//
checkMBeanPermission((String) null, null, null, "queryMBeans");
// Perform query without "query".
//
Set<ObjectInstance> list = queryMBeansImpl(name, null);
// Check if the caller has the right to invoke 'queryMBeans'
// on each specific classname/objectname in the list.
//
Set<ObjectInstance> allowedList =
new HashSet<ObjectInstance>(list.size());
for (ObjectInstance oi : list) {
try {
checkMBeanPermission(oi.getClassName(), null,
oi.getObjectName(), "queryMBeans");
allowedList.add(oi);
} catch (SecurityException e) {
// OK: Do not add this ObjectInstance to the list
}
}
// Apply query to allowed MBeans only.
//
return filterListOfObjectInstances(allowedList, query);
} else {
// Perform query.
//
return queryMBeansImpl(name, query);
}
}
private Set<ObjectName> queryNamesImpl(ObjectName name, QueryExp query) {
// Query the MBeans on the repository
//
Set<NamedObject> list = repository.query(name, query);
return (objectNamesFromFilteredNamedObjects(list, query));
}
/**
* 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;
}
}
/**
* 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;
}
}
public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
// Check if the caller has the right to invoke 'queryMBeans'
//
checkMBeanPermission((String) null, null, null, "queryMBeans");
// Perform query without "query".
//
Set<ObjectInstance> list = queryMBeansImpl(name, null);
// Check if the caller has the right to invoke 'queryMBeans'
// on each specific classname/objectname in the list.
//
Set<ObjectInstance> allowedList =
new HashSet<ObjectInstance>(list.size());
for (ObjectInstance oi : list) {
try {
checkMBeanPermission(oi.getClassName(), null,
oi.getObjectName(), "queryMBeans");
allowedList.add(oi);
} catch (SecurityException e) {
// OK: Do not add this ObjectInstance to the list
}
}
// Apply query to allowed MBeans only.
//
return filterListOfObjectInstances(allowedList, query);
} else {
// Perform query.
//
return queryMBeansImpl(name, query);
}
}