1 /* Reattore HTTP Server
2
3 Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 $Id: VelocityReporter.java,v 1.3 2003/03/05 04:31:56 michaelh Exp $
20 */
21
22 package juju.reattore.perfcap.reporter.impl;
23
24 import java.util.*;
25 import java.io.*;
26
27 import org.apache.commons.beanutils.BeanUtils;
28 import org.apache.velocity.app.VelocityEngine;
29 import org.apache.velocity.VelocityContext;
30 import org.apache.velocity.exception.*;
31
32 import juju.reattore.perfcap.reporter.Reporter;
33 import juju.reattore.perfcap.var.Variable;
34 import juju.reattore.perfcap.tester.Results;
35
36 /*** Generates an arbitrary report using a user defined Velocity
37 template. The available variables are:
38
39 <ul>
40 <li><tt>root</tt> - Top level of a tree of Maps containing all of
41 the results. The map is indexed by variable name.</li>
42 <li><tt>names</tt> - List of all of the variable names in order.</li>
43 <li><tt>values</tt> - List of Lists of all of the values a
44 variable took in order received.</li>
45 </ul>
46
47 @tag velocityreporter
48 @group Reporter
49 */
50 public class VelocityReporter
51 implements Reporter {
52
53 private String filter;
54 private String template = "report.vm";
55 private String out;
56
57 private Map root = new LinkedHashMap();
58 private List names = new ArrayList();
59 private List values = new ArrayList();
60
61 /*** The property from the test results. If unset, the leaf is the
62 whole Results object.
63
64 @param filter The Bean name of the property.
65 */
66 public void setFilter(String filter) {
67 this.filter = filter;
68 }
69
70 /*** The template file name to process.
71
72 @param file File name.
73 @default report.vm
74 */
75 public void setTemplate(String file) {
76 this.template = file;
77 }
78
79 /*** Name of the file to write to. If unset, writes to stdout.
80
81 @param out Name of the file.
82 */
83 public void setOut(String out) {
84 this.out = out;
85 }
86
87 /*** @see Reporter */
88 public void add(List ind, Results res)
89 throws Exception {
90
91 if (names.size() == 0) {
92 for (Iterator i = ind.iterator(); i.hasNext();) {
93 Variable var = (Variable)i.next();
94
95 values.add(new ArrayList());
96 names.add(var.getName());
97 }
98 }
99
100 for (int i = 0; i < ind.size(); i++) {
101 Variable var = (Variable)ind.get(i);
102 List row = (List)values.get(i);
103
104 Object value = var.getValue();
105 if (row.contains(value) == false) {
106 row.add(value);
107 }
108 }
109
110 Map m = root;
111
112 for (Iterator i = ind.iterator(); i.hasNext();) {
113 Variable var = (Variable)i.next();
114
115 if (i.hasNext()) {
116 Map next = (Map)m.get(var.getValue());
117
118 if (next == null) {
119 next = new LinkedHashMap();
120
121 m.put(var.getValue(), next);
122 }
123 m = next;
124 }
125 else {
126 if (filter != null) {
127 m.put(var.getValue(), BeanUtils.getProperty(res, filter));
128 }
129 else {
130 m.put(var.getValue(), res);
131 }
132 }
133 }
134 }
135
136 /*** @see Reporter */
137 public void end()
138 throws Exception {
139
140 VelocityEngine engine = new VelocityEngine();
141 engine.init();
142
143 VelocityContext ctx = new VelocityContext();
144
145 ctx.put("root", root);
146 ctx.put("names", names);
147 ctx.put("values", values);
148
149 StringWriter writer = new StringWriter();
150
151 if (engine.mergeTemplate(template, ctx, writer) != true) {
152 throw new Exception("Error processing template");
153 }
154
155 writer.flush();
156
157 if (out != null) {
158 PrintWriter pw = new PrintWriter(new FileWriter(out));
159 pw.print(writer.toString());
160 pw.close();
161 }
162 else {
163 System.out.println(writer.toString());
164 }
165 }
166 }
This page was automatically generated by Maven