Skip to content

Commit

Permalink
ISPN-1473 Upgrade to RHQ to reduce RHQ annotation retention scope
Browse files Browse the repository at this point in the history
  • Loading branch information
galderz authored and maniksurtani committed Nov 3, 2011
1 parent 1f3354c commit eb1ba59
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 27 deletions.
3 changes: 3 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@
<version>${version.jboss.marshalling}</version>
</dependency>

<!-- As of v3.0.4, the RHQ annotations have a retention policy of COMPILE,
which means they are no longer required at runtime. -->
<dependency>
<groupId>org.rhq.helpers</groupId>
<artifactId>rhq-pluginAnnotations</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
Expand Down
4 changes: 2 additions & 2 deletions parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@
<version.netty>3.2.4.Final</version.netty>
<version.org.jboss.naming>5.0.6.CR1</version.org.jboss.naming>
<version.resteasy>2.2.1.GA</version.resteasy>
<version.rhq.pluginAnnotations>3.0.1</version.rhq.pluginAnnotations>
<version.rhq.plugingen>3.0.1</version.rhq.plugingen>
<version.rhq.pluginAnnotations>3.0.4</version.rhq.pluginAnnotations>
<version.rhq.plugingen>3.0.4</version.rhq.plugingen>
<version.rhq>3.0.0</version.rhq>
<version.scala>2.9.1</version.scala>
<version.slf4j>1.6.1</version.slf4j>
Expand Down
1 change: 1 addition & 0 deletions server/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<dependency>
<groupId>org.rhq.helpers</groupId>
<artifactId>rhq-pluginAnnotations</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

Expand Down
13 changes: 12 additions & 1 deletion tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,18 @@
<groupId>org.rhq.helpers</groupId>
<artifactId>rhq-pluginGen</artifactId>
<version>${version.rhq.plugingen}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.rhq.helpers</groupId>
<artifactId>rhq-pluginAnnotations</artifactId>
<version>${version.rhq.pluginAnnotations}</version>
</dependency>

<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>${version.javassist}</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
import java.util.List;
import java.util.Set;

import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtMethod;

import org.infinispan.factories.scopes.Scope;
import org.infinispan.factories.scopes.Scopes;
import org.infinispan.jmx.annotations.MBean;
Expand Down Expand Up @@ -66,6 +72,7 @@
*/
public class RhqPluginXmlGenerator {
private static final Log log = LogFactory.getLog(RhqPluginXmlGenerator.class);
private static ClassPool classPool;
private static String cp;

public static void main(String[] args) throws Exception {
Expand All @@ -82,7 +89,7 @@ public static boolean validOptions(String options[][], DocErrorReporter reporter
return true;
}

public static boolean start(RootDoc rootDoc) throws IOException {
public static boolean start(RootDoc rootDoc) throws Exception {
List<Class<?>> mbeanIspnClasses = getMBeanClasses();
List<Class<?>> globalClasses = new ArrayList<Class<?>>();
List<Class<?>> namedCacheClasses = new ArrayList<Class<?>>();
Expand All @@ -97,6 +104,9 @@ public static boolean start(RootDoc rootDoc) throws IOException {
}
}

// Init the Javassist class pool.
classPool = ClassPool.getDefault();
classPool.insertClassPath(new ClassClassPath(RhqPluginXmlGenerator.class));
PluginGen pg = new PluginGen();

Props root = new Props();
Expand Down Expand Up @@ -162,24 +172,29 @@ private static List<Class<?>> getMBeanClasses() throws IOException {
}
}

private static void populateMetricsAndOperations(List<Class<?>> classes, Props props, boolean withNamePrefix) {
private static void populateMetricsAndOperations(List<Class<?>> classes,
Props props, boolean withNamePrefix) throws Exception {
props.setHasOperations(true);
props.setHasMetrics(true);
for (Class<?> clazz : classes) {
MBean mbean = clazz.getAnnotation(MBean.class);
String prefix = withNamePrefix ? mbean.objectName() + '.' : "";
Method[] methods = clazz.getMethods();
for (Method method : methods) {
Metric rhqMetric = method.getAnnotation(Metric.class);
ManagedAttribute managedAttr = method.getAnnotation(ManagedAttribute.class);
ManagedOperation managedOp = method.getAnnotation(ManagedOperation.class);
Operation rhqOperation = method.getAnnotation(Operation.class);
CtClass ctClass = classPool.get(clazz.getName());

CtMethod[] ctMethods = ctClass.getMethods();
for (CtMethod ctMethod : ctMethods) {
ManagedAttribute managedAttr = (ManagedAttribute)
ctMethod.getAnnotation(ManagedAttribute.class);
ManagedOperation managedOp = (ManagedOperation)
ctMethod.getAnnotation(ManagedOperation.class);

Metric rhqMetric = (Metric) ctMethod.getAnnotation(Metric.class);
if (rhqMetric != null) {
debug("Metric annotation found " + rhqMetric);
// Property and description resolution are the reason why annotation scanning is done here.
// These two fields are calculated from either the method name or the Managed* annotations,
// and so, only the infinispan side knows about that.
String property = prefix + BeanConventions.getPropertyFromBeanConvention(method);
String property = prefix + getPropertyFromBeanConvention(ctMethod);
if (!rhqMetric.property().isEmpty()) {
property = prefix + rhqMetric.property();
}
Expand All @@ -202,11 +217,14 @@ private static void populateMetricsAndOperations(List<Class<?>> classes, Props p
props.getMetrics().add(metric);
}

Operation rhqOperation = (Operation) ctMethod.getAnnotation(Operation.class);
if (rhqOperation != null) {
debug("Operation annotation found " + rhqOperation);
String name = prefix + method.getName();
String name;
if (!rhqOperation.name().isEmpty()) {
name = prefix + rhqOperation.name();
} else {
name = prefix + ctMethod.getName();
}
OperationProps operation = new OperationProps(name);
String displayName = withNamePrefix ? "[" + mbean.objectName() + "] " + rhqOperation.displayName() : rhqOperation.displayName();
Expand All @@ -222,11 +240,11 @@ private static void populateMetricsAndOperations(List<Class<?>> classes, Props p
operation.setDescription(rhqOperation.displayName());
}

Annotation[][] paramAnnotations = method.getParameterAnnotations();
Object[][] paramAnnotations = ctMethod.getParameterAnnotations();
int i = 0;
for (Annotation[] paramAnnotationsInEach : paramAnnotations) {
for (Object[] paramAnnotationsInEach : paramAnnotations) {
boolean hadParameter = false;
for (Annotation annot : paramAnnotationsInEach) {
for (Object annot : paramAnnotationsInEach) {
debug("Parameter annotation " + annot);
if (annot instanceof Parameter) {
Parameter param = (Parameter) annot;
Expand All @@ -240,31 +258,38 @@ private static void populateMetricsAndOperations(List<Class<?>> classes, Props p
operation.getParams().add(new SimpleProperty("p" + i++));
}
}
Class<?> returnType = method.getReturnType();
if (!returnType.equals(Void.TYPE)) {
SimpleProperty prop = new SimpleProperty("operationResult");
operation.setResult(prop);
CtClass returnType = ctMethod.getReturnType();
if (!returnType.equals(CtClass.voidType)) {
if (!returnType.equals(Void.TYPE)) {
SimpleProperty prop = new SimpleProperty("operationResult");
operation.setResult(prop);
}
}
props.getOperations().add(operation);
}
}
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
debug("Inspecting field " + field);
Metric rhqMetric = field.getAnnotation(Metric.class);

CtField[] ctFields = ctClass.getDeclaredFields();
for (CtField ctField : ctFields) {
debug("Inspecting field " + ctField);

Metric rhqMetric = (Metric)ctField.getAnnotation(Metric.class);
if (rhqMetric != null) {
debug("Field " + field + " contains Metric annotation " + rhqMetric);
String property = prefix + BeanConventions.getPropertyFromBeanConvention(field);
debug("Field " + ctField + " contains Metric annotation " + rhqMetric);
String property;
if (!rhqMetric.property().isEmpty()) {
property = prefix + rhqMetric.property();
} else {
property = prefix + getPropertyFromBeanConvention(ctField);
}
MetricProps metric = new MetricProps(property);
String displayName = withNamePrefix ? "[" + mbean.objectName() + "] " + rhqMetric.displayName() : rhqMetric.displayName();
metric.setDisplayName(displayName);
metric.setDisplayType(rhqMetric.displayType());
metric.setDataType(rhqMetric.dataType());
metric.setUnits(rhqMetric.units());
ManagedAttribute managedAttr = field.getAnnotation(ManagedAttribute.class);
ManagedAttribute managedAttr = (ManagedAttribute)
ctField.getAnnotation(ManagedAttribute.class);
if (managedAttr != null) {
debug("Metric has ManagedAttribute annotation " + managedAttr);
metric.setDescription(managedAttr.description());
Expand All @@ -279,6 +304,25 @@ private static void populateMetricsAndOperations(List<Class<?>> classes, Props p
}
}

private static String getPropertyFromBeanConvention(CtMethod ctMethod) {
String getterOrSetter = ctMethod.getName();
if (getterOrSetter.startsWith("get") || getterOrSetter.startsWith("set")) {
String withoutGet = getterOrSetter.substring(4);
// not specifically Bean convention, but this is what is bound in JMX.
return Character.toUpperCase(getterOrSetter.charAt(3)) + withoutGet;
} else if (getterOrSetter.startsWith("is")) {
String withoutIs = getterOrSetter.substring(3);
return Character.toUpperCase(getterOrSetter.charAt(2)) + withoutIs;
}
return getterOrSetter;
}

private static String getPropertyFromBeanConvention(CtField ctField) {
String fieldName = ctField.getName();
String withoutFirstChar = fieldName.substring(1);
return Character.toUpperCase(fieldName.charAt(0)) + withoutFirstChar;
}

private static void debug(Object o) {
// if (log.isDebugEnabled()) log.debug(o);
// System.out.println(o);
Expand Down
1 change: 1 addition & 0 deletions tree/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<dependency>
<groupId>org.rhq.helpers</groupId>
<artifactId>rhq-pluginAnnotations</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
Expand Down

0 comments on commit eb1ba59

Please sign in to comment.