1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.server.handler;
15
16 import java.io.IOException;
17
18 import javax.servlet.ServletException;
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21
22 import org.eclipse.jetty.server.Handler;
23 import org.eclipse.jetty.server.HandlerContainer;
24 import org.eclipse.jetty.server.Request;
25 import org.eclipse.jetty.server.Server;
26 import org.eclipse.jetty.util.component.LifeCycle;
27
28
29
30
31
32
33 public class HotSwapHandler extends AbstractHandlerContainer
34 {
35 private volatile Handler _handler;
36
37
38
39
40
41 public HotSwapHandler()
42 {
43 }
44
45
46
47
48
49 public Handler getHandler()
50 {
51 return _handler;
52 }
53
54
55
56
57
58 public Handler[] getHandlers()
59 {
60 return new Handler[] {_handler};
61 }
62
63
64
65
66
67 public void setHandler(Handler handler)
68 {
69 try
70 {
71 Handler old_handler = _handler;
72
73 if (getServer()!=null)
74 getServer().getContainer().update(this, old_handler, handler, "handler");
75
76 if (handler!=null)
77 {
78 handler.setServer(getServer());
79 if (isStarted())
80 handler.start();
81 }
82
83 _handler = handler;
84
85 if (isStarted())
86 old_handler.stop();
87
88 }
89 catch(RuntimeException e)
90 {
91 throw e;
92 }
93 catch(Exception e)
94 {
95 throw new RuntimeException(e);
96 }
97 }
98
99
100
101
102
103 protected void doStart() throws Exception
104 {
105 if (_handler!=null)
106 _handler.start();
107 super.doStart();
108 }
109
110
111
112
113
114 protected void doStop() throws Exception
115 {
116 super.doStop();
117 if (_handler!=null)
118 _handler.stop();
119 }
120
121
122
123
124
125 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
126 {
127 if (_handler!=null && isStarted())
128 {
129 _handler.handle(target,baseRequest, request, response);
130 }
131 }
132
133
134
135 public void setServer(Server server)
136 {
137 Server old_server=getServer();
138 if (server==old_server)
139 return;
140
141 if (isRunning())
142 throw new IllegalStateException(RUNNING);
143
144 super.setServer(server);
145
146 Handler h=getHandler();
147 if (h!=null)
148 h.setServer(server);
149
150 if (server!=null && server!=old_server)
151 server.getContainer().update(this, null,_handler, "handler");
152 }
153
154
155
156 protected Object expandChildren(Object list, Class byClass)
157 {
158 return expandHandler(_handler,list,byClass);
159 }
160
161
162 }