1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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 }