下面列出了怎么用javax.management.IntrospectionException的API类实例代码及写法,或者点击链接到github查看源代码。
private MBeanInfo testMBeanForDatasource() throws Exception {
Map<String, String[]> env = new HashMap<>();
String[] credentials = { "admin", "admin" };
env.put(JMXConnector.CREDENTIALS, credentials);
try {
String url = "service:jmx:rmi://localhost:12311/jndi/rmi://localhost:11199/jmxrmi";
JMXServiceURL jmxUrl = new JMXServiceURL(url);
JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxUrl, env);
MBeanServerConnection mBeanServer = jmxConnector.getMBeanServerConnection();
ObjectName mbeanObject = new ObjectName(dataSourceName + ",-1234:type=DataSource");
MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(mbeanObject);
return mBeanInfo;
} catch (MalformedURLException | MalformedObjectNameException | IntrospectionException | ReflectionException e) {
throw new AxisFault("Error while connecting to MBean Server " + e.getMessage(), e);
}
}
public MBeanInfo getMBeanInfo(ObjectName name)
throws InstanceNotFoundException,
IntrospectionException,
ReflectionException,
IOException {
if (logger.debugOn()) logger.debug("getMBeanInfo", "name=" + name);
final ClassLoader old = pushDefaultClassLoader();
try {
return connection.getMBeanInfo(name, delegationSubject);
} catch (IOException ioe) {
communicatorAdmin.gotIOException(ioe);
return connection.getMBeanInfo(name, delegationSubject);
} finally {
popDefaultClassLoader(old);
}
}
public MBeanInfo getMBeanInfo(ObjectName name)
throws InstanceNotFoundException,
IntrospectionException,
ReflectionException,
IOException {
if (logger.debugOn()) logger.debug("getMBeanInfo", "name=" + name);
final ClassLoader old = pushDefaultClassLoader();
try {
return connection.getMBeanInfo(name, delegationSubject);
} catch (IOException ioe) {
communicatorAdmin.gotIOException(ioe);
return connection.getMBeanInfo(name, delegationSubject);
} finally {
popDefaultClassLoader(old);
}
}
public MBeanInfo getMBeanInfo(ObjectName name)
throws InstanceNotFoundException,
IntrospectionException,
ReflectionException,
IOException {
if (logger.debugOn()) logger.debug("getMBeanInfo", "name=" + name);
final ClassLoader old = pushDefaultClassLoader();
try {
return connection.getMBeanInfo(name, delegationSubject);
} catch (IOException ioe) {
communicatorAdmin.gotIOException(ioe);
return connection.getMBeanInfo(name, delegationSubject);
} finally {
popDefaultClassLoader(old);
}
}
/**
* @see javax.management.MBeanServerConnection#getMBeanInfo(javax.management.ObjectName)
*/
public MBeanInfo getMBeanInfo(final ObjectName name)
throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException {
if (this.subject == null) {
return this.mbs.getMBeanInfo(name);
}
try {
return (MBeanInfo) Subject.doAsPrivileged(this.subject, new PrivilegedExceptionAction<MBeanInfo>() {
public final MBeanInfo run() throws Exception {
return mbs.getMBeanInfo(name);
}
}, this.context);
} catch (final PrivilegedActionException pe) {
final Exception e = JMXProviderUtils.extractException(pe);
if (e instanceof InstanceNotFoundException)
throw (InstanceNotFoundException) e;
if (e instanceof IntrospectionException)
throw (IntrospectionException) e;
if (e instanceof ReflectionException)
throw (ReflectionException) e;
if (e instanceof IOException)
throw (IOException) e;
throw JMXProviderUtils.newIOException("Got unexpected server exception: " + e, e);
}
}
private MBeanInfo testMBeanForDatasource() throws Exception {
Map<String, String[]> env = new HashMap<>();
String[] credentials = { "admin", "admin" };
env.put(JMXConnector.CREDENTIALS, credentials);
try {
String url = "service:jmx:rmi://localhost:12311/jndi/rmi://localhost:11199/jmxrmi";
JMXServiceURL jmxUrl = new JMXServiceURL(url);
JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxUrl, env);
MBeanServerConnection mBeanServer = jmxConnector.getMBeanServerConnection();
ObjectName mbeanObject = new ObjectName(dataSourceName + ",-1234:type=DataSource");
MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(mbeanObject);
return mBeanInfo;
} catch (MalformedURLException | MalformedObjectNameException | IntrospectionException |
ReflectionException e) {
throw new AxisFault("Error while connecting to MBean Server " + e.getMessage(), e);
}
}
public Object getNodeMBeansHistory(String nodeJmxUrl, String objectNames, List<String> attrs, String range)
throws IOException, MalformedObjectNameException, IntrospectionException, InstanceNotFoundException,
ReflectionException, MBeanException {
initNodeConnector(nodeJmxUrl);
Set<ObjectName> beans = nodeConnector.getMBeanServerConnection().queryNames(new ObjectName(objectNames), null);
HashMap<String, Object> results = new HashMap<>();
for (ObjectName bean : beans) {
results.put(bean.getCanonicalName(),
getNodeMBeanHistory(nodeJmxUrl, bean.getCanonicalName(), attrs, range));
}
return results;
}
@Override
public String getStatHistory(String sessionId, String range1, String function)
throws ReflectionException, InterruptedException, IntrospectionException, NotConnectedException,
InstanceNotFoundException, MalformedObjectNameException, IOException {
String newRange = MBeanInfoViewer.possibleModifyRange(range1, dataSources, 'a');
StatHistoryCacheEntry entry = StatHistoryCaching.getInstance().getEntryOrCompute(newRange, () -> {
RMProxyUserInterface rm = checkAccess(sessionId);
AttributeList attrs = rm.getMBeanAttributes(new ObjectName(RMJMXBeans.RUNTIMEDATA_MBEAN_NAME),
new String[] { "StatisticHistory" });
Attribute attr = (Attribute) attrs.get(0);
// content of the RRD4J database backing file
byte[] rrd4j = (byte[]) attr.getValue();
return MBeanInfoViewer.rrdContent(rrd4j, newRange, dataSources, function);
});
return entry.getValue();
}
public MBeanInfo getMBeanInfo(ObjectName name)
throws InstanceNotFoundException,
IntrospectionException,
ReflectionException,
IOException {
if (logger.debugOn()) logger.debug("getMBeanInfo", "name=" + name);
final ClassLoader old = pushDefaultClassLoader();
try {
return connection.getMBeanInfo(name, delegationSubject);
} catch (IOException ioe) {
communicatorAdmin.gotIOException(ioe);
return connection.getMBeanInfo(name, delegationSubject);
} finally {
popDefaultClassLoader(old);
}
}
@Override
MBeanAttributeInfo getMBeanAttributeInfo(String attributeName,
Method getter, Method setter) {
final String description = "Attribute exposed for management";
try {
return new MBeanAttributeInfo(attributeName, description,
getter, setter);
} catch (IntrospectionException e) {
throw new RuntimeException(e); // should not happen
}
}
/**
* Call <code>checkRead()</code>, then forward this method to the
* wrapped object.
*/
public MBeanInfo getMBeanInfo(ObjectName name)
throws
InstanceNotFoundException,
IntrospectionException,
ReflectionException {
checkRead();
return getMBeanServer().getMBeanInfo(name);
}
@Override
public Object getNodeMBeansInfo(String sessionId, String nodeJmxUrl, String objectNames, List<String> attrs)
throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException,
NotConnectedException, MalformedObjectNameException, NullPointerException, PermissionRestException {
// checking that still connected to the RM
RMProxyUserInterface rmProxy = checkAccess(sessionId);
return orThrowRpe(rmProxy.getNodeMBeansInfo(nodeJmxUrl, objectNames, attrs));
}
@Override
MBeanAttributeInfo getMBeanAttributeInfo(String attributeName,
Method getter, Method setter) {
final String description = "Attribute exposed for management";
try {
return new MBeanAttributeInfo(attributeName, description,
getter, setter);
} catch (IntrospectionException e) {
throw new RuntimeException(e); // should not happen
}
}
@Override
MBeanAttributeInfo getMBeanAttributeInfo(String attributeName,
Method getter, Method setter) {
final String description = "Attribute exposed for management";
try {
return new MBeanAttributeInfo(attributeName, description,
getter, setter);
} catch (IntrospectionException e) {
throw new RuntimeException(e); // should not happen
}
}
/**
* Call <code>checkRead()</code>, then forward this method to the
* wrapped object.
*/
public MBeanInfo getMBeanInfo(ObjectName name)
throws
InstanceNotFoundException,
IntrospectionException,
ReflectionException {
checkRead();
return getMBeanServer().getMBeanInfo(name);
}
@Override
public MBeanInfo getMBeanInfo(ObjectName name)
throws InstanceNotFoundException, IntrospectionException, ReflectionException {
Boolean originalValue = AUTHORIZING.get();
try {
AUTHORIZING.set(Boolean.TRUE);
return delegate.getMBeanInfo(name);
} finally {
AUTHORIZING.set(originalValue);
}
}
/**
* Call <code>checkRead()</code>, then forward this method to the
* wrapped object.
*/
public MBeanInfo getMBeanInfo(ObjectName name)
throws
InstanceNotFoundException,
IntrospectionException,
ReflectionException {
checkRead();
return getMBeanServer().getMBeanInfo(name);
}
@Override
MBeanAttributeInfo getMBeanAttributeInfo(String attributeName,
Method getter, Method setter) {
final String description = "Attribute exposed for management";
try {
return new MBeanAttributeInfo(attributeName, description,
getter, setter);
} catch (IntrospectionException e) {
throw new RuntimeException(e); // should not happen
}
}
protected static String listOptions(MBeanServerConnection mbsc, ObjectInstance instance)
throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException {
StringBuffer result = new StringBuffer();
MBeanInfo info = mbsc.getMBeanInfo(instance.getObjectName());
MBeanAttributeInfo[] attributes = info.getAttributes();
if (attributes.length > 0) {
result.append("Attributes:");
result.append("\n");
for (int i = 0; i < attributes.length; i++) {
result.append(' ' + attributes[i].getName() + ": " + attributes[i].getDescription() + " (type="
+ attributes[i].getType() + ")");
result.append("\n");
}
}
MBeanOperationInfo[] operations = info.getOperations();
if (operations.length > 0) {
result.append("Operations:");
result.append("\n");
for (int i = 0; i < operations.length; i++) {
MBeanParameterInfo[] params = operations[i].getSignature();
StringBuffer paramsStrBuffer = new StringBuffer();
if (params != null) {
for (int j = 0; j < params.length; j++) {
paramsStrBuffer.append("\n name=");
paramsStrBuffer.append(params[j].getName());
paramsStrBuffer.append(" type=");
paramsStrBuffer.append(params[j].getType());
paramsStrBuffer.append(" ");
paramsStrBuffer.append(params[j].getDescription());
}
}
result.append(' ' + operations[i].getName() + ": " + operations[i].getDescription() + "\n Parameters "
+ params != null ? params.length
: 0 + ", return type=" + operations[i].getReturnType() + paramsStrBuffer.toString());
result.append("\n");
}
}
return result.toString();
}
/**
* Call <code>checkRead()</code>, then forward this method to the
* wrapped object.
*/
public MBeanInfo getMBeanInfo(ObjectName name)
throws
InstanceNotFoundException,
IntrospectionException,
ReflectionException {
checkRead();
return getMBeanServer().getMBeanInfo(name);
}
/**
* Call <code>checkRead()</code>, then forward this method to the
* wrapped object.
*/
public MBeanInfo getMBeanInfo(ObjectName name)
throws
InstanceNotFoundException,
IntrospectionException,
ReflectionException {
checkRead();
return getMBeanServer().getMBeanInfo(name);
}
/**
* Call <code>checkRead()</code>, then forward this method to the
* wrapped object.
*/
public MBeanInfo getMBeanInfo(ObjectName name)
throws
InstanceNotFoundException,
IntrospectionException,
ReflectionException {
checkRead();
return getMBeanServer().getMBeanInfo(name);
}
@Override
MBeanAttributeInfo getMBeanAttributeInfo(String attributeName,
Method getter, Method setter) {
final String description = "Attribute exposed for management";
try {
return new MBeanAttributeInfo(attributeName, description,
getter, setter);
} catch (IntrospectionException e) {
throw new RuntimeException(e); // should not happen
}
}
@Override
MBeanAttributeInfo getMBeanAttributeInfo(String attributeName,
Method getter, Method setter) {
final String description = "Attribute exposed for management";
try {
return new MBeanAttributeInfo(attributeName, description,
getter, setter);
} catch (IntrospectionException e) {
throw new RuntimeException(e); // should not happen
}
}
private static void doExploit ( final Object payloadObject, MBeanServerConnection mbc )
throws IOException, InstanceNotFoundException, IntrospectionException, ReflectionException {
Object[] params = new Object[1];
params[ 0 ] = payloadObject;
System.err.println("Querying MBeans");
Set<ObjectInstance> testMBeans = mbc.queryMBeans(null, null);
System.err.println("Found " + testMBeans.size() + " MBeans");
for ( ObjectInstance oi : testMBeans ) {
MBeanInfo mBeanInfo = mbc.getMBeanInfo(oi.getObjectName());
for ( MBeanOperationInfo opInfo : mBeanInfo.getOperations() ) {
try {
mbc.invoke(oi.getObjectName(), opInfo.getName(), params, new String[] {});
System.err.println(oi.getObjectName() + ":" + opInfo.getName() + " -> SUCCESS");
return;
}
catch ( Throwable e ) {
String msg = e.getMessage();
if ( msg.startsWith("java.lang.ClassNotFoundException:") ) {
int start = msg.indexOf('"');
int stop = msg.indexOf('"', start + 1);
String module = ( start >= 0 && stop > 0 ) ? msg.substring(start + 1, stop) : "<unknown>";
if ( !"<unknown>".equals(module) && !"org.jboss.as.jmx:main".equals(module) ) {
int cstart = msg.indexOf(':');
int cend = msg.indexOf(' ', cstart + 2);
String cls = msg.substring(cstart + 2, cend);
System.err.println(oi.getObjectName() + ":" + opInfo.getName() + " -> FAIL CNFE " + cls + " (" + module + ")");
}
}
else {
System.err.println(oi.getObjectName() + ":" + opInfo.getName() + " -> SUCCESS|ERROR " + msg);
return;
}
}
}
}
}
/**
* Call <code>checkRead()</code>, then forward this method to the
* wrapped object.
*/
public MBeanInfo getMBeanInfo(ObjectName name)
throws
InstanceNotFoundException,
IntrospectionException,
ReflectionException {
checkRead();
return getMBeanServer().getMBeanInfo(name);
}
@Override
public MBeanInfo getMBeanInfo(ObjectName name)
throws InstanceNotFoundException, IntrospectionException, ReflectionException
{
return mbeanServer.getMBeanInfo(name);
}
/**
* Retrieves attributes of the specified mbean.
*
* @param nodeJmxUrl mbean server url
* @param objectName name of mbean
* @param attrs set of mbean attributes
*
* @return mbean attributes values
*/
public Object getNodeMBeanInfo(String nodeJmxUrl, String objectName, List<String> attrs)
throws IOException, InstanceNotFoundException, IntrospectionException, MalformedObjectNameException,
ReflectionException, NullPointerException {
initNodeConnector(nodeJmxUrl);
if ((attrs == null) || (attrs.size() == 0)) {
// no attribute is requested, we return
// the description of the mbean
return nodeConnector.getMBeanServerConnection().getMBeanInfo(new ObjectName(objectName));
} else {
List<Object> result = new LinkedList<>();
AttributeList attributes = nodeConnector.getMBeanServerConnection()
.getAttributes(new ObjectName(objectName),
attrs.toArray(new String[attrs.size()]));
for (Object attrObj : attributes) {
if (attrObj instanceof Attribute) {
// when data from MXBean is requested
// convert the composite data (see MXBean spec) into
// readable values
Attribute attr = (Attribute) attrObj;
Object value = attr.getValue();
if (value instanceof CompositeData[]) {
CompositeData[] valueArr = (CompositeData[]) value;
Object[] converted = new Object[valueArr.length];
for (int i = 0; i < valueArr.length; i++) {
converted[i] = convertCompositeData(valueArr[i]);
}
attr = new Attribute(attr.getName(), converted);
}
if (value instanceof CompositeData) {
CompositeData compositeData = (CompositeData) value;
attr = new Attribute(attr.getName(), convertCompositeData(compositeData));
}
result.add(attr);
}
}
return result;
}
}
public Map<String, Object> getOSJMXStats(JMXDeamons jmxDaemon, String host, String port, boolean jmxPluginEnabled) throws IOException,
AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException, IntrospectionException {
if (jmxPluginEnabled) {
JumbuneJMXClient client = new JumbuneJMXClient();
try{
return client.getOSStats(host, jmxDaemon.toString()+Constants.OS_IDENTIFIER);
}catch(ClassNotFoundException e){
LOGGER.error("error fetching jmx stats through plugin - " + e.getMessage());
}
}
List<String> jmxAttributeList = new ArrayList<String>();
JMXConnector connector = null;
MBeanServerConnection connection = null;
Map<String, Object> serviceStats = null;
JMXServiceURL url = new JMXServiceURL(JMX_URL_PREFIX + host + ":" + port + JMX_URL_POSTFIX);
String serviceUrl = ProfilerConstants.OS_URL;
connector = JMXConnectorInstance.getJMXConnectorInstance(url);
connection = connector.getMBeanServerConnection();
Set<ObjectName> names = connection.queryNames(null, null);
String objectName;
MBeanInfo info;
MBeanAttributeInfo[] mbi;
for (ObjectName objName : names) {
objectName = objName.toString();
if(objectName.indexOf(serviceUrl) > -1) {
if (serviceStats == null) {
serviceStats = new HashMap<String, Object>();
}
info = connection.getMBeanInfo(objName);
mbi = info.getAttributes();
String name = null;
for (int i = 0; i < info.getAttributes().length; i++) {
name = mbi[i].getName();
jmxAttributeList.add(name);
Object attributeValue = connection.getAttribute(objName, name);
if (attributeValue != null) {
serviceStats.put(name, String.valueOf(attributeValue));
}
if("".equals(attributeValue) || "[]".equals(attributeValue)){
serviceStats.put(getKeyName(objName, name), "-");
}
}
}
}
return serviceStats;
}
/**
* Test that all MBeans registered by Derby have:
* <UL>
* <LI> A type key property correct set.
* <LI> Expose a class name in com.pivotal.gemfirexd.internal.mbeans.
* </UL>
* @throws Exception
*/
public void testDerbyRegisteredMBeansSimpleInfo() throws Exception
{
Set<ObjectName> derbyMBeans = getDerbyDomainMBeans();
// We expect Derby to have registered MBeans
// including a management MBean and the one registered
// by our setUp method.
assertTrue("Derby MBEan count:" + derbyMBeans.size(),
derbyMBeans.size() >= 2);
final MBeanServerConnection jmx = getMBeanServerConnection();
for (final ObjectName name : derbyMBeans)
{
String type = name.getKeyProperty("type");
// Every Derby MBean has a type.
assertNotNull(type);
MBeanInfo mbeanInfo = AccessController.doPrivileged(
new PrivilegedExceptionAction<MBeanInfo>() {
public MBeanInfo run() throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException {
return jmx.getMBeanInfo(name);
}
}
);
String mbeanClassName = mbeanInfo.getClassName();
// Is the class name in the public api
assertTrue(mbeanClassName.startsWith("com.pivotal.gemfirexd.internal.mbeans."));
// See if it was the application created ManagementMBean
// This will have the implementation class registered
// as the class name since it is not registered by Derby.
if ("Management".equals(type)
&& "com.pivotal.gemfirexd.internal.mbeans.Management".equals(mbeanClassName))
{
continue;
}
// and is a Derby specific MBean.
assertTrue(mbeanClassName.endsWith("MBean"));
// Check the type is the class name of the MBean without
// the MBean and the package.
String scn = mbeanClassName.substring(mbeanClassName.lastIndexOf('.') + 1);
scn = scn.substring(0, scn.length() - "MBean".length());
assertEquals(scn, type);
}
}