View Javadoc

1   
2   /*
3    * Copyright (C) 2005 by Arno Schumacher
4    *
5    * This file is part of net.sourceforge.servletspy
6    *
7    * net.sourceforge.servletspy is free software; you can redistribute
8    * it and/or modify it under the terms of the GNU General Public 
9    * License as published by the Free Software Foundation; either 
10   * version 2, or (at your option) any later version.
11   *
12   * net.sourceforge.servletspy is distributed in the hope that it will
13   * be useful, but WITHOUT ANY WARRANTY; without even the implied 
14   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
15   * See the GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA 
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  }