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.config;
23  
24  import java.io.InputStream;
25  import java.net.URL;
26  
27  import javax.servlet.ServletContextEvent;
28  import javax.servlet.ServletContextListener;
29  
30  /***
31   * @author arno schumacher
32   */
33  public final class Loader implements ServletContextListener {
34      private static final String CONFIG_RESOURCE = "servlet-spy.xml";
35  
36      private static class Problem extends RuntimeException {
37          private static final long serialVersionUID = 3046326989811674823L;
38  
39          private Problem(final Throwable t) {
40              super(t);
41          }
42  
43          public Problem(final String string) {
44              super(string);
45          }
46      }
47  
48      private ContextHandlerFactory contextHandlerFactory = null;
49  
50      public synchronized void contextInitialized(final ServletContextEvent arg0) {
51          if (contextHandlerFactory != null) {
52              contextHandlerFactory.destroy();
53              contextHandlerFactory = null;
54          }
55  
56          URL url = Thread.currentThread().getContextClassLoader().getResource(
57                  CONFIG_RESOURCE);
58          if (url == null) {
59              throw new Problem("Could not find configuration resource '"
60                      + CONFIG_RESOURCE + "'");
61          }
62  
63          try {
64              InputStream is = url.openStream();
65              ServletSpy servletSpy = ServletSpy.fromXML(is, "UTF-8");
66              contextHandlerFactory = new ContextHandlerFactory(servletSpy);
67              is.close();
68              arg0.getServletContext().setAttribute("SpyConfiguration",
69                      servletSpy);
70          } catch (Exception e) {
71              throw new Problem(e);
72          }
73      }
74  
75      public synchronized void contextDestroyed(final ServletContextEvent arg0) {
76          if (contextHandlerFactory == null) {
77              return;
78          }
79          contextHandlerFactory.destroy();
80          contextHandlerFactory = null;
81      }
82  }