下面列出了怎么用javax.management.openmbean.CompositeData的API类实例代码及写法,或者点击链接到github查看源代码。
protected CompositeData getCompositeData() {
// CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
// vmOptionItemNames!
final Object[] vmOptionItemValues = {
option.getName(),
option.getValue(),
new Boolean(option.isWriteable()),
option.getOrigin().toString(),
};
try {
return new CompositeDataSupport(vmOptionCompositeType,
vmOptionItemNames,
vmOptionItemValues);
} catch (OpenDataException e) {
// Should never reach here
throw new AssertionError(e);
}
}
private static boolean compositeDataEqual(CompositeData cd1,
CompositeData cd2,
NamedMXBeans namedMXBeans) {
if (cd1 == cd2)
return true;
if (!cd1.getCompositeType().equals(cd2.getCompositeType()))
return false;
Collection v1 = cd1.values();
Collection v2 = cd2.values();
if (v1.size() != v2.size())
return false; // should not happen
for (Iterator i1 = v1.iterator(), i2 = v2.iterator();
i1.hasNext(); ) {
if (!equal(i1.next(), i2.next(), namedMXBeans))
return false;
}
return true;
}
Object fromCompositeData(CompositeData cd,
String[] itemNames,
MXBeanMapping[] converters)
throws InvalidObjectException {
Object o;
try {
final Class<?> targetClass = getTargetClass();
ReflectUtil.checkPackageAccess(targetClass);
o = targetClass.newInstance();
for (int i = 0; i < itemNames.length; i++) {
if (cd.containsKey(itemNames[i])) {
Object openItem = cd.get(itemNames[i]);
Object javaItem =
converters[i].fromOpenValue(openItem);
MethodUtil.invoke(setters[i], o, new Object[] {javaItem});
}
}
} catch (Exception e) {
throw invalidObjectException(e);
}
return o;
}
static void createNotification(String notifType,
String poolName,
MemoryUsage usage,
long count) {
MemoryImpl mbean = (MemoryImpl) ManagementFactory.getMemoryMXBean();
if (!mbean.hasListeners()) {
// if no listener is registered.
return;
}
long timestamp = System.currentTimeMillis();
String msg = getNotifMsg(notifType);
Notification notif = new Notification(notifType,
mbean.getObjectName(),
getNextSeqNumber(),
timestamp,
msg);
MemoryNotificationInfo info =
new MemoryNotificationInfo(poolName,
usage,
count);
CompositeData cd =
MemoryNotifInfoCompositeData.toCompositeData(info);
notif.setUserData(cd);
mbean.sendNotification(notif);
}
protected CompositeData getCompositeData() {
// CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
// memoryNotifInfoItemNames!
final Object[] memoryNotifInfoItemValues = {
memoryNotifInfo.getPoolName(),
MemoryUsageCompositeData.toCompositeData(memoryNotifInfo.getUsage()),
new Long(memoryNotifInfo.getCount()),
};
try {
return new CompositeDataSupport(memoryNotifInfoCompositeType,
memoryNotifInfoItemNames,
memoryNotifInfoItemValues);
} catch (OpenDataException e) {
// Should never reach here
throw new AssertionError(e);
}
}
Object fromCompositeData(CompositeData cd,
String[] itemNames,
MXBeanMapping[] converters)
throws InvalidObjectException {
Object o;
try {
final Class<?> targetClass = getTargetClass();
ReflectUtil.checkPackageAccess(targetClass);
o = targetClass.newInstance();
for (int i = 0; i < itemNames.length; i++) {
if (cd.containsKey(itemNames[i])) {
Object openItem = cd.get(itemNames[i]);
Object javaItem =
converters[i].fromOpenValue(openItem);
MethodUtil.invoke(setters[i], o, new Object[] {javaItem});
}
}
} catch (Exception e) {
throw invalidObjectException(e);
}
return o;
}
void createGCNotification(long timestamp,
String gcName,
String gcAction,
String gcCause,
GcInfo gcInfo) {
if (!hasListeners()) {
return;
}
Notification notif = new Notification(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION,
getObjectName(),
getNextSeqNumber(),
timestamp,
gcName);
GarbageCollectionNotificationInfo info =
new GarbageCollectionNotificationInfo(gcName,
gcAction,
gcCause,
gcInfo);
CompositeData cd =
GarbageCollectionNotifInfoCompositeData.toCompositeData(info);
notif.setUserData(cd);
sendNotification(notif);
}
@Override
final Object fromNonNullOpenValue(Object openValue)
throws InvalidObjectException {
final TabularData table = (TabularData) openValue;
final Collection<CompositeData> rows = cast(table.values());
final Map<Object, Object> valueMap =
sortedMap ? newSortedMap() : newInsertionOrderMap();
for (CompositeData row : rows) {
final Object key =
keyMapping.fromOpenValue(row.get("key"));
final Object value =
valueMapping.fromOpenValue(row.get("value"));
if (valueMap.put(key, value) != null) {
final String msg =
"Duplicate entry in TabularData: key=" + key;
throw new InvalidObjectException(msg);
}
}
return valueMap;
}
@Override
public String toString() {
if (userData == null) {
return null;
}
if (userData.getClass().isArray()) {
String name =
Utils.getArrayClassName(userData.getClass().getName());
int length = Array.getLength(userData);
return name + "[" + length + "]";
}
if (userData instanceof CompositeData ||
userData instanceof TabularData) {
return userData.getClass().getName();
}
return userData.toString();
}
protected CompositeData getCompositeData() {
// CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
// memoryUsageItemNames!
final Object[] memoryUsageItemValues = {
new Long(usage.getInit()),
new Long(usage.getUsed()),
new Long(usage.getCommitted()),
new Long(usage.getMax()),
};
try {
return new CompositeDataSupport(memoryUsageCompositeType,
memoryUsageItemNames,
memoryUsageItemValues);
} catch (OpenDataException e) {
// Should never reach here
throw new AssertionError(e);
}
}
protected CompositeData getCompositeData() {
// CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
// stackTraceElementItemNames!
final Object[] stackTraceElementItemValues = {
ste.getClassName(),
ste.getMethodName(),
ste.getFileName(),
new Integer(ste.getLineNumber()),
new Boolean(ste.isNativeMethod()),
};
try {
return new CompositeDataSupport(stackTraceElementCompositeType,
stackTraceElementItemNames,
stackTraceElementItemValues);
} catch (OpenDataException e) {
// Should never reach here
throw new AssertionError(e);
}
}
protected CompositeData getCompositeData() {
// CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
// memoryUsageItemNames!
final Object[] memoryUsageItemValues = {
new Long(usage.getInit()),
new Long(usage.getUsed()),
new Long(usage.getCommitted()),
new Long(usage.getMax()),
};
try {
return new CompositeDataSupport(memoryUsageCompositeType,
memoryUsageItemNames,
memoryUsageItemValues);
} catch (OpenDataException e) {
// Should never reach here
throw new AssertionError(e);
}
}
/** */
private void checkContinuousQueryView(IgniteEx origNode, IgniteEx checkNode) {
TabularDataSupport qrys = systemView(checkNode, CQ_SYS_VIEW);
assertEquals(1, qrys.size());
for (int i = 0; i < qrys.size(); i++) {
CompositeData cq = qrys.get(new Object[] {i});
assertEquals("cache-1", cq.get("cacheName"));
assertEquals(100, cq.get("bufferSize"));
assertEquals(1000L, cq.get("interval"));
assertEquals(origNode.localNode().id().toString(), cq.get("nodeId"));
if (origNode.localNode().id().equals(checkNode.localNode().id()))
assertTrue(cq.get("localListener").toString().startsWith(getClass().getName()));
else
assertNull(cq.get("localListener"));
assertTrue(cq.get("remoteFilter").toString().startsWith(getClass().getName()));
assertNull(cq.get("localTransformedListener"));
assertNull(cq.get("remoteTransformer"));
}
}
private static String getProperty(TabularData td, String propName) {
CompositeData cd = td.get(new Object[] { propName});
if (cd != null) {
String key = (String) cd.get("key");
if (!propName.equals(key)) {
throw new RuntimeException("TEST FAILED: " +
key + " property found" +
" but expected to be " + propName);
}
return (String) cd.get("value");
}
return null;
}
public static LockInfo toLockInfo(CompositeData cd) {
if (cd == null) {
throw new NullPointerException("Null CompositeData");
}
if (!isTypeMatched(lockInfoCompositeType, cd.getCompositeType())) {
throw new IllegalArgumentException(
"Unexpected composite type for LockInfo");
}
String className = getString(cd, CLASS_NAME);
int identityHashCode = getInt(cd, IDENTITY_HASH_CODE);
return new LockInfo(className, identityHashCode);
}
public static String getGcCause(CompositeData cd) {
String gccause = getString(cd, GC_CAUSE);
if (gccause == null) {
throw new IllegalArgumentException("Invalid composite data: " +
"Attribute " + GC_CAUSE + " has null value");
}
return gccause;
}
public static Map<String, MemoryUsage>
getMemoryUsageAfterGc(CompositeData cd) {
try {
TabularData td = (TabularData) cd.get(MEMORY_USAGE_AFTER_GC);
//return (Map<String,MemoryUsage>)
return cast(memoryUsageMapType.toJavaTypeData(td));
} catch (InvalidObjectException | OpenDataException e) {
// Should never reach here
throw new AssertionError(e);
}
}
public static LockInfo toLockInfo(CompositeData cd) {
if (cd == null) {
throw new NullPointerException("Null CompositeData");
}
if (!isTypeMatched(lockInfoCompositeType, cd.getCompositeType())) {
throw new IllegalArgumentException(
"Unexpected composite type for LockInfo");
}
String className = getString(cd, CLASS_NAME);
int identityHashCode = getInt(cd, IDENTITY_HASH_CODE);
return new LockInfo(className, identityHashCode);
}
/**
* Constructs a <tt>MemoryUsage</tt> object from a
* {@link CompositeData CompositeData}.
*/
private MemoryUsage(CompositeData cd) {
// validate the input composite data
MemoryUsageCompositeData.validateCompositeData(cd);
this.init = MemoryUsageCompositeData.getInit(cd);
this.used = MemoryUsageCompositeData.getUsed(cd);
this.committed = MemoryUsageCompositeData.getCommitted(cd);
this.max = MemoryUsageCompositeData.getMax(cd);
}
private ConfigurationInfo(CompositeData cd) {
this.settings = createMap(cd.get("settings"));
this.name = (String) cd.get("name");
this.label = (String) cd.get("label");
this.description = (String) cd.get("description");
this.provider = (String) cd.get("provider");
this.contents = (String) cd.get("contents");
}
public MonitorInfo[] lockedMonitors() {
CompositeData[] lockedMonitorsData =
(CompositeData[]) cdata.get(LOCKED_MONITORS);
// The LockedMonitors item cannot be null, but if it is we will get
// a NullPointerException when we ask for its length.
MonitorInfo[] monitors =
new MonitorInfo[lockedMonitorsData.length];
for (int i = 0; i < lockedMonitorsData.length; i++) {
CompositeData cdi = lockedMonitorsData[i];
monitors[i] = MonitorInfo.from(cdi);
}
return monitors;
}
public static String getGcName(CompositeData cd) {
String gcname = getString(cd, GC_NAME);
if (gcname == null) {
throw new IllegalArgumentException("Invalid composite data: " +
"Attribute " + GC_NAME + " has null value");
}
return gcname;
}
private static void checkSunGC() throws Exception {
// Test com.sun.management proxy
List<GarbageCollectorMXBean> gcs = getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean gc : gcs) {
ObjectName sunGc =
new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE +
",name=" + gc.getName());
CompositeData cd = (CompositeData) server.getAttribute(sunGc, "LastGcInfo");
if (cd != null) {
System.out.println("GC statistic for : " + gc.getName());
printGcInfo(cd);
}
}
}
public static String getPoolName(CompositeData cd) {
String poolname = getString(cd, POOL_NAME);
if (poolname == null) {
throw new IllegalArgumentException("Invalid composite data: " +
"Attribute " + POOL_NAME + " has null value");
}
return poolname;
}
public CompositeData getCompositeDataAttribute()
throws OpenDataException {
CompositeType ct = new CompositeType("CompositeDataAttribute",
"Composite Data Attribute",
itemNames,
itemDescriptions,
itemTypes);
Object itemValues[] = { ia, da, sa };
return new CompositeDataSupport(ct, itemNames, itemValues);
}
public long getCodeCacheFree()
throws JMException
{
CompositeData data
= (CompositeData) _mbeanServer.getAttribute(getCodeCacheName(), "Usage");
MemoryUsage usage = MemoryUsage.from(data);
return usage.getMax() - usage.getUsed();
}
private static CompositeData convert(MessageAndAddresses maa) {
if (maa == null) {
return null;
}
try {
TabularData addrData = new TabularDataSupport(TAB_ADDR_TYPE);
for(Address addr : maa.getAddresses()) {
addrData.put(new CompositeDataSupport(ROW_ADDR_TYPE, new String[]{"addressType", "address"}, new Object[]{addr.getType(), addr.toString()}));
}
TabularData headerData = new TabularDataSupport(TAB_HEADER_TYPE);
Enumeration en = maa.getMessage().getAllHeaders();
while (en.hasMoreElements()) {
Header header = (Header) en.nextElement();
headerData.put(new CompositeDataSupport(ROW_HEADER_TYPE, new String[]{"header-name", "header-value"}, new Object[]{header.getName(), header.getValue()}));
}
String error = null;
if (maa.getException() != null) {
StringWriter sw = new StringWriter();
sw.append(maa.getException().toString());
maa.getException().printStackTrace(new PrintWriter(sw));
sw.flush();
error = sw.toString();
}
return new CompositeDataSupport(MAIL_INFO_TYPE,
new String[] {"messageId", "date", "subject", "toAddresses", "headers", "errorDescription"},
new Object[]{maa.getMessage().getMessageID(), new Date(maa.getTimestamp()), maa.getMessage().getSubject(), addrData, headerData, error}
);
} catch (OpenDataException | MessagingException e) {
throw new IllegalArgumentException("cannot convert MessageAndAddresses to CompositeData", e);
}
}
public void getAttribute(PrintWriter writer, String onameStr, String att, String key) {
try {
ObjectName oname = new ObjectName(onameStr);
Object value = mBeanServer.getAttribute(oname, att);
if(null != key && value instanceof CompositeData)
value = ((CompositeData)value).get(key);
String valueStr;
if (value != null) {
valueStr = value.toString();
} else {
valueStr = "<null>";
}
writer.print("OK - Attribute get '");
writer.print(onameStr);
writer.print("' - ");
writer.print(att);
if(null != key) {
writer.print(" - key '");
writer.print(key);
writer.print("'");
}
writer.print(" = ");
writer.println(MBeanDumper.escape(valueStr));
} catch (Exception ex) {
writer.println("Error - " + ex.toString());
ex.printStackTrace(writer);
}
}
static boolean equal(Object o1, Object o2, NamedMXBeans namedMXBeans) {
if (o1 == o2)
return true;
if (o1 == null || o2 == null)
return false;
if (o1.getClass().isArray()) {
if (!o2.getClass().isArray())
return false;
return deepEqual(o1, o2, namedMXBeans);
}
if (o1 instanceof Map) {
if (!(o2 instanceof Map))
return false;
return equalMap((Map) o1, (Map) o2, namedMXBeans);
}
if (o1 instanceof CompositeData && o2 instanceof CompositeData) {
return compositeDataEqual((CompositeData) o1, (CompositeData) o2,
namedMXBeans);
}
if (Proxy.isProxyClass(o1.getClass())) {
if (Proxy.isProxyClass(o2.getClass()))
return proxyEqual(o1, o2, namedMXBeans);
InvocationHandler ih = Proxy.getInvocationHandler(o1);
// if (ih instanceof MXBeanInvocationHandler) {
// return proxyEqualsObject((MXBeanInvocationHandler) ih,
// o2, namedMXBeans);
if (ih instanceof MBeanServerInvocationHandler) {
return true;
} else if (ih instanceof CompositeDataInvocationHandler) {
return o2.equals(o1);
// We assume the other object has a reasonable equals method
}
} else if (Proxy.isProxyClass(o2.getClass()))
return equal(o2, o1, namedMXBeans);
return o1.equals(o2);
}
public void handleNotification(Notification notif, Object handback) {
String type = notif.getType();
if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) ||
type.equals(MemoryNotificationInfo.
MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) {
listenerInvoked = true;
MemoryNotificationInfo minfo = MemoryNotificationInfo.
from((CompositeData) notif.getUserData());
System.out.print("Notification for " + minfo.getPoolName());
System.out.print(" [type = " + type);
System.out.println(" count = " + minfo.getCount() + "]");
System.out.println(INDENT + "usage = " + minfo.getUsage());
}
}