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 if (isRunning())
72 throw new IllegalStateException(RUNNING);
73
74 Handler old_handler = _handler;
75
76 if (getServer()!=null)
77 getServer().getContainer().update(this, old_handler, handler, "handler");
78
79 if (handler!=null)
80 {
81 handler.setServer(getServer());
82 if (isStarted())
83 handler.start();
84 }
85
86 _handler = handler;
87
88 if (isStarted())
89 old_handler.stop();
90
91 }
92 catch(RuntimeException e)
93 {
94 throw e;
95 }
96 catch(Exception e)
97 {
98 throw new RuntimeException(e);
99 }
100 }
101
102
103
104
105
106 protected void doStart() throws Exception
107 {
108 if (_handler!=null)
109 _handler.start();
110 super.doStart();
111 }
112
113
114
115
116
117 protected void doStop() throws Exception
118 {
119 super.doStop();
120 if (_handler!=null)
121 _handler.stop();
122 }
123
124
125
126
127
128 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
129 {
130 if (_handler!=null && isStarted())
131 {
132 _handler.handle(target,baseRequest, request, response);
133 }
134 }
135
136
137
138 public void setServer(Server server)
139 {
140 if (isRunning())
141 throw new IllegalStateException(RUNNING);
142
143 Server old_server=getServer();
144
145 super.setServer(server);
146
147 Handler h=getHandler();
148 if (h!=null)
149 h.setServer(server);
150
151 if (server!=null && server!=old_server)
152 server.getContainer().update(this, null,_handler, "handler");
153 }
154
155
156
157 protected Object expandChildren(Object list, Class byClass)
158 {
159 return expandHandler(_handler,list,byClass);
160 }
161
162
163 }