All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.javasimon.console.action.AbstractTableAction Maven / Gradle / Ivy
package org.javasimon.console.action;
import org.javasimon.Sample;
import org.javasimon.Simon;
import org.javasimon.console.Action;
import org.javasimon.console.ActionContext;
import org.javasimon.console.ActionException;
import org.javasimon.console.SimonType;
import org.javasimon.console.SimonTypeFactory;
import org.javasimon.console.SimonVisitor;
import org.javasimon.console.SimonVisitors;
import org.javasimon.console.TimeFormatType;
import org.javasimon.console.reflect.Getter;
import org.javasimon.console.reflect.GetterFactory;
import org.javasimon.console.text.Stringifier;
import org.javasimon.console.text.StringifierFactory;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.servlet.ServletException;
/**
* Base class for exporting Simons as tabular data.
* The following methods are called during rendering and can be overridden:
*
* {@link #printTable }:
* {@link #printHeaderRow}:
* {@link #printHeaderCell} : {@link #printCell}
*
* {@link #printBody}:
* {@link #printBodyRow}:
* {@link #printBodyCell} : {@link #printCell}
*
*
*
*
*
* @author gquintana
*/
public class AbstractTableAction extends Action {
/** Value formatter. */
protected StringifierFactory stringifierFactory;
/** Pattern for Simon name filtering. */
private String pattern;
/** Types for Simon type filtering. */
private Set types;
/** Column list in response. */
private List columns = new ArrayList();
/** Content type of response. */
protected final String contentType;
/** Decimal format pattern used for printing doubles. */
protected String numberPattern = StringifierFactory.READABLE_NUMBER_PATTERN;
/** Flag indicating if simons should be reset during sampling. */
protected boolean reset;
/** Base constructor initializes columns list. */
protected AbstractTableAction(ActionContext context, String contentType) {
super(context);
this.contentType = contentType;
columns.add(new Column("Name", "name"));
columns.add(new Column("Type", "type") {
private Stringifier stringifier;
@Override
public SimonType getValue(Object object) {
return SimonTypeFactory.getValueFromInstance((Sample) object);
}
@Override
public String getFormattedValue(Object object) {
return getStringifier(object).toString(getValue(object));
}
@Override
public Stringifier getStringifier(Object object) {
if (stringifier == null) {
stringifier = stringifierFactory.getStringifier(SimonType.class);
}
return stringifier;
}
});
columns.add(new Column("Counter", "counter"));
columns.add(new Column("Total", "total"));
columns.add(new Column("Min", "min"));
columns.add(new Column("Mean", "mean"));
columns.add(new Column("Last", "last"));
columns.add(new Column("Max", "max"));
columns.add(new Column("Std Dev", "standardDeviation"));
columns.add(new Column("First Use", "firstUsage"));
columns.add(new Column("Last Use", "lastUsage"));
columns.add(new Column("Note", "note"));
}
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
@Override
public void readParameters() {
TimeFormatType timeFormat = getContext().getParameterAsEnum("timeFormat", TimeFormatType.class, TimeFormatType.MILLISECOND);
stringifierFactory.init(timeFormat,
StringifierFactory.READABLE_DATE_PATTERN,
numberPattern);
pattern = getContext().getParameterAsString("pattern", null);
types = getContext().getParametersAsEnums("type", SimonType.class, null);
reset = getContext().getParameterAsBoolean("reset", Boolean.FALSE);
}
protected void printTable(PrintWriter writer) throws IOException {
printHeaderRow(writer);
printBody(writer);
}
protected void printHeaderRow(PrintWriter writer) throws IOException {
for (Column column : columns) {
printHeaderCell(column, writer);
}
}
protected void printHeaderCell(Column c, PrintWriter writer) throws IOException {
printCell(c, c.getTitle(), writer);
}
protected void printBody(PrintWriter writer) throws IOException {
SimonVisitors.visitList(getContext().getManager(), pattern, types, new SimonVisitorImpl(writer));
}
protected void printBodyRow(Simon simon, PrintWriter writer) throws IOException {
final Sample sample = reset ? simon.sampleAndReset() : simon.sample();
printBodyRow(sample, writer);
}
protected void printBodyRow(Sample sample, PrintWriter writer) throws IOException {
for (Column column : columns) {
printBodyCell(column, sample, writer);
}
}
protected void printBodyCell(Column c, Sample sample, PrintWriter writer) throws IOException {
printCell(c, c.getFormattedValue(sample), writer);
}
protected void printCell(Column c, String s, PrintWriter writer) throws IOException {
if (s != null) {
writer.write(s);
}
}
public void execute() throws ServletException, IOException, ActionException {
dontCache();
PrintWriter writer = null;
try {
getContext().setContentType(contentType);
writer = getContext().getWriter();
printTable(writer);
} finally {
if (writer != null) {
writer.flush();
}
}
}
private class SimonVisitorImpl implements SimonVisitor {
private final PrintWriter writer;
public SimonVisitorImpl(PrintWriter writer) {
this.writer = writer;
}
public void visit(Simon simon) throws IOException {
printBodyRow(simon, writer);
}
}
protected class Column {
/** Column title/header. */
private final String title;
/** Column property name. */
private final String name;
public Column(String title, String name) {
this.title = title;
this.name = name;
}
/** Get column property name. */
public String getName() {
return name;
}
/** Get column property title/header. */
public String getTitle() {
return title;
}
/**
* Returns getter for given column and row (simon).
*
* @param object Simon row
* @return getter
*/
@SuppressWarnings("unchecked")
private Getter getGetter(Object object) {
return GetterFactory.getGetter(object.getClass(), name);
}
/** Returns raw column value. */
public T getValue(Object object) {
Getter getter = getGetter(object);
return getter == null ? null : getter.get(object);
}
/**
* Get stringier used for given column and row.
*
* @param object Row (simon)
* @return Stringifier
*/
public Stringifier getStringifier(Object object) {
Getter getter = getGetter(object);
if (getter == null) {
return stringifierFactory.getNullStringifier();
} else {
return stringifierFactory.getStringifier(getter.getType(), getter.getSubType());
}
}
/** Get formatted column value. */
public String getFormattedValue(Object object) {
Getter getter = getGetter(object);
if (getter == null) {
return stringifierFactory.toString(null);
} else {
return stringifierFactory.toString(getter.get(object), getter.getSubType());
}
}
}
}