View Javadoc

1   
2   
3   /*
4    * Copyright (C) 2005 by Arno Schumacher
5    *
6    * This file is part of net.sourceforge.servletspy
7    *
8    * net.sourceforge.servletspy is free software; you can redistribute
9    * it and/or modify it under the terms of the GNU General Public 
10   * License as published by the Free Software Foundation; either 
11   * version 2, or (at your option) any later version.
12   *
13   * net.sourceforge.servletspy is distributed in the hope that it will
14   * be useful, but WITHOUT ANY WARRANTY; without even the implied 
15   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
16   * See the GNU General Public License for more details.
17   *
18   * You should have received a copy of the GNU General Public License
19   * along with this program; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA 
21   */
22  
23  package net.sourceforge.servletspy.config;
24  
25  import java.io.Serializable;
26  import java.util.ArrayList;
27  import java.util.Iterator;
28  import java.util.List;
29  
30  import net.sourceforge.servletspy.IContextHandler;
31  
32  /***
33   * @author arno schumacher
34   */
35  public final class ContextHandlerFactory implements Serializable {
36      private static final long serialVersionUID = 559092271215295038L;
37  
38      private static ContextHandlerFactory instance;
39  
40      private static class Problem extends RuntimeException {
41          private static final long serialVersionUID = -7832286622466882882L;
42  
43          Problem(final Throwable t) {
44              super(t);
45          }
46      }
47  
48      private final List contextHandlers = new ArrayList();
49  
50      ContextHandlerFactory(final ServletSpy servletSpy) {
51          if (servletSpy == null) {
52              throw new NullPointerException();
53          }
54          instance = this;
55          init(servletSpy);
56      }
57  
58      public static IContextHandler[] getContextHandlers() {
59      	if ( instance == null ) {
60      		throw new IllegalStateException( "Factory not configured." );
61      	}
62      	
63          return (IContextHandler[]) instance.contextHandlers
64                  .toArray(new IContextHandler[instance.contextHandlers.size()]);
65      }
66  
67      private void init(final ServletSpy servletSpy) {
68          final Handler[] handlers = servletSpy.getHandlers();
69          for (int i = 0; i < handlers.length; i++) {
70              final IContextHandler contextHandler = createHandler(handlers[i]);
71              contextHandlers.add(contextHandler);
72          }
73      }
74  
75      public void destroy() {
76          final Iterator it = contextHandlers.iterator();
77          while (it.hasNext()) {
78              try {
79                  ((IContextHandler) it.next()).destroy();
80              } catch (Exception e) {
81                  // ignore
82              }
83          }
84      }
85  
86      private IContextHandler createHandler(final Handler contextHandlerConfig) {
87          try {
88              final Object handlerInstance = Thread.currentThread()
89                      .getContextClassLoader().loadClass(
90                              contextHandlerConfig.getClassName()).newInstance();
91              final IContextHandler contextHandler = 
92                  (IContextHandler) handlerInstance;
93              contextHandler.init(contextHandlerConfig.getConfigParam());
94              return contextHandler;
95          } catch (Exception e) {
96              throw new Problem(e);
97          }
98      }
99  }