1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.sourceforge.servletspy;
23
24 import java.io.IOException;
25 import java.io.PrintWriter;
26
27 import javax.servlet.jsp.PageContext;
28
29 import net.sourceforge.servletspy.config.ContextHandlerFactory;
30 import net.sourceforge.servletspy.ctx.Context;
31 import net.sourceforge.servletspy.writer.HtmlNodeWriter;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 /***
37 * @author arno schumacher
38 */
39 public final class Spy {
40
41 /*** The logger. */
42 private static final Log LOGGER = LogFactory.getLog(Spy.class);
43 /*** The singleton. */
44 private static Spy singleton = new Spy();
45 /*** Constructor. */
46 private Spy() {
47 }
48
49 /***
50 * Returns the singleton.
51 *
52 * @return the singleton.
53 */
54 public static Spy getInstance() {
55 return singleton;
56 }
57
58 /***
59 * @param pageContext The page context.
60 * @throws IOException signaling IO problems while generating the spy output.
61 */
62 public void process(final PageContext pageContext) throws IOException {
63 final long serializationStart = System.currentTimeMillis();
64 final Node state = serialize(pageContext);
65 LOGGER.info("Data serialized in " + (System.currentTimeMillis()-serializationStart) + "ms.");
66 final PrintWriter writer = new PrintWriter(pageContext.getOut());
67 final HtmlNodeWriter nodeWriter = new HtmlNodeWriter(writer);
68 final long htmlGenerationStart = System.currentTimeMillis();
69 nodeWriter.writeNode(state);
70 LOGGER.info("HTML generated in " + (System.currentTimeMillis()-htmlGenerationStart) + "ms.");
71 writer.flush();
72 }
73
74 private Node serialize(final PageContext pageContext) {
75 final IContextHandler[] handlers = ContextHandlerFactory
76 .getContextHandlers();
77 final Context context = new Context("ServletSpy", pageContext,
78 handlers);
79 context.getNode().setValue(" provided by http://arno-schumacher.de ");
80 context.proceed();
81 context.add("About", new About());
82 final Node node = context.getNode();
83 return node;
84 }
85 }