use of javax.management.openmbean.CompositeType in project geode by apache.
the class OpenTypeConverter method makeCompositeConverter.
/**
* @return the open type converrter for a given type
*/
private static OpenTypeConverter makeCompositeConverter(Class c) throws OpenDataException {
final List<Method> methods = Arrays.asList(c.getMethods());
final SortedMap<String, Method> getterMap = OpenTypeUtil.newSortedMap();
for (Method method : methods) {
final String propertyName = propertyName(method);
if (propertyName == null)
continue;
Method old = getterMap.put(OpenTypeUtil.decapitalize(propertyName), method);
if (old != null) {
final String msg = "Class " + c.getName() + " has method name clash: " + old.getName() + ", " + method.getName();
throw new OpenDataException(msg);
}
}
final int nitems = getterMap.size();
if (nitems == 0) {
throw new OpenDataException("Can't map " + c.getName() + " to an open data type");
}
final Method[] getters = new Method[nitems];
final String[] itemNames = new String[nitems];
final OpenType[] openTypes = new OpenType[nitems];
int i = 0;
for (Map.Entry<String, Method> entry : getterMap.entrySet()) {
itemNames[i] = entry.getKey();
final Method getter = entry.getValue();
getters[i] = getter;
final Type retType = getter.getGenericReturnType();
openTypes[i] = toConverter(retType).getOpenType();
i++;
}
CompositeType compositeType = new // field
CompositeType(// field
c.getName(), // field
c.getName(), // field
itemNames, // field descriptions
itemNames, openTypes);
return new CompositeConverter(c, compositeType, itemNames, getters);
}
use of javax.management.openmbean.CompositeType in project geode by apache.
the class TableConverter method toNonNullOpenValue.
Object toNonNullOpenValue(Object value) throws OpenDataException {
final Map<Object, Object> valueMap = (Map<Object, Object>) value;
if (valueMap instanceof SortedMap) {
Comparator comparator = ((SortedMap) valueMap).comparator();
if (comparator != null) {
final String msg = "Cannot convert SortedMap with non-null comparator: " + comparator;
IllegalArgumentException iae = new IllegalArgumentException(msg);
OpenDataException ode = new OpenDataException(msg);
ode.initCause(iae);
throw ode;
}
}
final TabularType tabularType = (TabularType) getOpenType();
final TabularData table = new TabularDataSupport(tabularType);
final CompositeType rowType = tabularType.getRowType();
for (Map.Entry entry : valueMap.entrySet()) {
final Object openKey = keyConverter.toOpenValue(entry.getKey());
final Object openValue = valueConverter.toOpenValue(entry.getValue());
final CompositeData row;
row = new CompositeDataSupport(rowType, keyValueArray, new Object[] { openKey, openValue });
table.put(row);
}
return table;
}
use of javax.management.openmbean.CompositeType in project geode by apache.
the class CompositeConverter method toNonNullOpenValue.
/**
* Converts to open value
*/
Object toNonNullOpenValue(Object value) throws OpenDataException {
CompositeType ct = (CompositeType) getOpenType();
if (value instanceof CompositeDataView)
return ((CompositeDataView) value).toCompositeData(ct);
if (value == null)
return null;
Object[] values = new Object[getters.length];
for (int i = 0; i < getters.length; i++) {
try {
Object got = getters[i].invoke(value, (Object[]) null);
values[i] = getterConverters[i].toOpenValue(got);
} catch (Exception e) {
throw openDataException("Error calling getter for " + itemNames[i] + ": " + e, e);
}
}
return new CompositeDataSupport(ct, itemNames, values);
}
use of javax.management.openmbean.CompositeType in project karaf by apache.
the class PackagesMBeanImpl method getImports.
@Override
public TabularData getImports() {
try {
String[] names = new String[] { "PackageName", "Filter", "Optional", "ID", "Bundle Name", "Resolvable" };
CompositeType bundleType = new CompositeType("PackageImports", "Imported packages", names, names, new OpenType[] { SimpleType.STRING, SimpleType.STRING, SimpleType.BOOLEAN, SimpleType.LONG, SimpleType.STRING, SimpleType.BOOLEAN });
TabularType tableType = new TabularType("PackageImports", "Imported packages", bundleType, new String[] { "Filter", "ID" });
TabularData table = new TabularDataSupport(tableType);
List<PackageRequirement> imports = packageService.getImports();
for (PackageRequirement req : imports) {
Object[] data = new Object[] { req.getPackageName(), req.getFilter(), req.isOptional(), req.getBundle().getBundleId(), req.getBundle().getSymbolicName(), req.isResolveable() };
CompositeData comp = new CompositeDataSupport(bundleType, names, data);
try {
table.put(comp);
} catch (KeyAlreadyExistsException e) {
throw new RuntimeException("Id: " + req.getBundle().getBundleId() + ", filter: " + req.getFilter(), e);
}
}
return table;
} catch (RuntimeException e) {
// To avoid the exception gets swallowed by jmx
LOGGER.error(e.getMessage(), e);
throw e;
} catch (OpenDataException e) {
LOGGER.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
}
use of javax.management.openmbean.CompositeType in project karaf by apache.
the class HttpMBeanImpl method getServlets.
public TabularData getServlets() throws MBeanException {
try {
CompositeType servletType = new CompositeType("Servlet", "HTTP Servlet", new String[] { "Bundle-ID", "Servlet", "Servlet Name", "State", "Alias", "URL" }, new String[] { "ID of the bundle that registered the servlet", "Class name of the servlet", "Servlet Name", "Current state of the servlet", "Aliases of the servlet", "URL of the servlet" }, new OpenType[] { SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING });
TabularType tableType = new TabularType("Servlets", "Table of all HTTP servlets", servletType, new String[] { "Bundle-ID", "Servlet Name", "State" });
TabularData table = new TabularDataSupport(tableType);
List<ServletInfo> servletInfos = servletService.getServlets();
for (ServletInfo info : servletInfos) {
CompositeData data = new CompositeDataSupport(servletType, new String[] { "Bundle-ID", "Servlet", "Servlet Name", "State", "Alias", "URL" }, new Object[] { info.getBundleId(), info.getClassName(), info.getName(), info.getStateString(), info.getAlias(), Arrays.toString(info.getUrls()) });
table.put(data);
}
return table;
} catch (Exception e) {
throw new MBeanException(null, e.toString());
}
}
Aggregations