1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mortbay.jetty.plugin;
17
18
19 import org.mortbay.jetty.Connector;
20 import org.mortbay.jetty.Handler;
21 import org.mortbay.jetty.RequestLog;
22 import org.mortbay.jetty.Server;
23 import org.mortbay.jetty.handler.ContextHandlerCollection;
24 import org.mortbay.jetty.handler.DefaultHandler;
25 import org.mortbay.jetty.handler.HandlerCollection;
26 import org.mortbay.jetty.handler.RequestLogHandler;
27 import org.mortbay.jetty.nio.SelectChannelConnector;
28 import org.mortbay.jetty.plugin.util.JettyPluginServer;
29 import org.mortbay.jetty.plugin.util.PluginLog;
30 import org.mortbay.jetty.security.UserRealm;
31 import org.mortbay.jetty.webapp.WebAppContext;
32 import org.mortbay.resource.Resource;
33
34
35
36
37
38
39
40 public class Jetty6PluginServer implements JettyPluginServer
41 {
42 public static int DEFAULT_PORT = 8080;
43 public static int DEFAULT_MAX_IDLE_TIME = 30000;
44 private Server server;
45 private ContextHandlerCollection contexts;
46 HandlerCollection handlers;
47 private RequestLogHandler requestLogHandler;
48 private DefaultHandler defaultHandler;
49
50 private RequestLog requestLog;
51
52
53
54
55
56 public Jetty6PluginServer()
57 {
58 this.server = new Server();
59 this.server.setStopAtShutdown(true);
60
61 Resource.setDefaultUseCaches(false);
62 }
63
64
65
66
67 public void setConnectors(Object[] connectors)
68 {
69 if (connectors==null || connectors.length==0)
70 return;
71
72 for (int i=0; i<connectors.length;i++)
73 {
74 Connector connector = (Connector)connectors[i];
75 PluginLog.getLog().debug("Setting Connector: "+connector.getClass().getName()+" on port "+connector.getPort());
76 this.server.addConnector(connector);
77 }
78 }
79
80
81
82
83
84
85
86
87 public Object[] getConnectors()
88 {
89 return this.server.getConnectors();
90 }
91
92
93
94
95
96
97 public void setUserRealms(Object[] realms) throws Exception
98 {
99 if (realms == null)
100 return;
101
102 for (int i=0; i<realms.length;i++)
103 this.server.addUserRealm((UserRealm)realms[i]);
104 }
105
106
107
108
109
110 public Object[] getUserRealms()
111 {
112 return this.server.getUserRealms();
113 }
114
115
116 public void setRequestLog (Object requestLog)
117 {
118 this.requestLog = (RequestLog)requestLog;
119 }
120
121 public Object getRequestLog ()
122 {
123 return this.requestLog;
124 }
125
126
127
128
129 public void start() throws Exception
130 {
131 PluginLog.getLog().info("Starting jetty "+this.server.getClass().getPackage().getImplementationVersion()+" ...");
132 this.server.start();
133 }
134
135
136
137
138 public Object getProxiedObject()
139 {
140 return this.server;
141 }
142
143
144
145
146 public void addWebApplication(WebAppContext webapp) throws Exception
147 {
148 contexts.addHandler (webapp);
149 }
150
151
152
153
154
155
156
157
158
159 public void configureHandlers () throws Exception
160 {
161 this.defaultHandler = new DefaultHandler();
162 this.requestLogHandler = new RequestLogHandler();
163 if (this.requestLog != null)
164 this.requestLogHandler.setRequestLog(this.requestLog);
165
166 this.contexts = (ContextHandlerCollection)server.getChildHandlerByClass(ContextHandlerCollection.class);
167 if (this.contexts==null)
168 {
169 this.contexts = new ContextHandlerCollection();
170 this.handlers = (HandlerCollection)server.getChildHandlerByClass(HandlerCollection.class);
171 if (this.handlers==null)
172 {
173 this.handlers = new HandlerCollection();
174 this.server.setHandler(handlers);
175 this.handlers.setHandlers(new Handler[]{this.contexts, this.defaultHandler, this.requestLogHandler});
176 }
177 else
178 {
179 this.handlers.addHandler(this.contexts);
180 }
181 }
182 }
183
184
185
186
187
188
189
190 public Object createDefaultConnector(String portnum) throws Exception
191 {
192 SelectChannelConnector connector = new SelectChannelConnector();
193 connector = new SelectChannelConnector();
194 int port = ((portnum==null||portnum.equals(""))?DEFAULT_PORT:Integer.parseInt(portnum.trim()));
195 connector.setPort(port);
196 connector.setMaxIdleTime(DEFAULT_MAX_IDLE_TIME);
197
198 return connector;
199 }
200
201
202
203
204 public void join () throws Exception
205 {
206 this.server.getThreadPool().join();
207 }
208 }