package org.glassfish.jersey.server.internal.monitoring.jmx;
import org.glassfish.jersey.message.internal.MediaTypes;
import org.glassfish.jersey.server.model.ResourceMethod;
import org.glassfish.jersey.server.monitoring.ResourceMethodMXBean;
import org.glassfish.jersey.server.monitoring.ResourceMethodStatistics;
public class ResourceMethodMXBeanImpl implements ResourceMethodMXBean {
private volatile ExecutionStatisticsDynamicBean methodExecutionStatisticsMxBean;
private volatile ExecutionStatisticsDynamicBean requestExecutionStatisticsMxBean;
private final String path;
private final String name;
private final ResourceMethod resourceMethod;
private final String methodBeanName;
public ResourceMethodMXBeanImpl(ResourceMethodStatistics methodStatistics, boolean uriResource,
MBeanExposer mBeanExposer, String parentName, String methodUniqueId) {
this.resourceMethod = methodStatistics.getResourceMethod();
final Class<?> handlerClass = resourceMethod.getInvocable().getHandler().getHandlerClass();
final Class<?>[] paramTypes = resourceMethod.getInvocable().getHandlingMethod().getParameterTypes();
this.name = resourceMethod.getInvocable().getHandlingMethod().getName();
StringBuilder params = new StringBuilder();
for (Class<?> type : paramTypes) {
params.append(type.getSimpleName()).append(";");
}
if (params.length() > 0) {
params.setLength(params.length() - 1);
}
if (uriResource) {
path = "N/A";
} else {
path = resourceMethod.getParent().getParent() == null ? "" : resourceMethod.getParent().getPath();
}
final String hash = Integer.toHexString(methodUniqueId.hashCode());
String beanName = resourceMethod.getHttpMethod() + "->";
if (uriResource) {
beanName += handlerClass.getSimpleName()
+ "." + name + "(" + params.toString() + ")#" + hash;
} else {
beanName += name + "(" + params.toString() + ")#"
+ hash;
}
this.methodBeanName = parentName + ",detail=methods,method=" + beanName;
mBeanExposer.registerMBean(this, methodBeanName);
methodExecutionStatisticsMxBean = new ExecutionStatisticsDynamicBean(methodStatistics.getMethodStatistics(),
mBeanExposer, methodBeanName, MBeanExposer.PROPERTY_EXECUTION_TIMES_METHODS);
requestExecutionStatisticsMxBean = new ExecutionStatisticsDynamicBean(methodStatistics.getRequestStatistics(),
mBeanExposer, methodBeanName, MBeanExposer.PROPERTY_EXECUTION_TIMES_REQUESTS);
}
public void updateResourceMethodStatistics(ResourceMethodStatistics resourceMethodStatisticsImpl) {
this.methodExecutionStatisticsMxBean.updateExecutionStatistics(resourceMethodStatisticsImpl.getMethodStatistics());
this.requestExecutionStatisticsMxBean.updateExecutionStatistics(resourceMethodStatisticsImpl.getRequestStatistics());
}
@Override
public String getPath() {
return path;
}
@Override
public String getHttpMethod() {
return resourceMethod.getHttpMethod();
}
@Override
public String getDeclaringClassName() {
return this.resourceMethod.getInvocable().getHandlingMethod().getDeclaringClass().getName();
}
@Override
public String getConsumesMediaType() {
return MediaTypes.convertToString(resourceMethod.getConsumedTypes());
}
@Override
public String getProducesMediaType() {
return MediaTypes.convertToString(resourceMethod.getProducedTypes());
}
@Override
public String getMethodName() {
return name;
}
}