-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
JettyServer.java
156 lines (132 loc) · 4.94 KB
/
JettyServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// ========================================================================
// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.maven.plugin;
import java.io.File;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.RequestLog;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.RequestLogHandler;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.xml.XmlConfiguration;
/**
* JettyServer
*
* Maven jetty plugin version of a wrapper for the Server class.
*
*/
public class JettyServer extends org.eclipse.jetty.server.Server
{
private RequestLog requestLog;
private ContextHandlerCollection contexts;
/**
*
*/
public JettyServer()
{
super();
//make sure Jetty does not use URLConnection caches with the plugin
Resource.setDefaultUseCaches(false);
}
public void setRequestLog (RequestLog requestLog)
{
this.requestLog = requestLog;
}
/**
* @see org.eclipse.jetty.server.Server#doStart()
*/
public void doStart() throws Exception
{
super.doStart();
}
/**
* @see org.eclipse.jetty.server.handler.HandlerCollection#addHandler(org.eclipse.jetty.server.Handler)
*/
public void addWebApplication(WebAppContext webapp) throws Exception
{
contexts.addHandler (webapp);
}
/**
* Set up the handler structure to receive a webapp.
* Also put in a DefaultHandler so we get a nice page
* than a 404 if we hit the root and the webapp's
* context isn't at root.
* @throws Exception
*/
public void configureHandlers () throws Exception
{
DefaultHandler defaultHandler = new DefaultHandler();
RequestLogHandler requestLogHandler = new RequestLogHandler();
if (this.requestLog != null)
requestLogHandler.setRequestLog(this.requestLog);
contexts = (ContextHandlerCollection)super.getChildHandlerByClass(ContextHandlerCollection.class);
if (contexts==null)
{
contexts = new ContextHandlerCollection();
HandlerCollection handlers = (HandlerCollection)super.getChildHandlerByClass(HandlerCollection.class);
if (handlers==null)
{
handlers = new HandlerCollection();
super.setHandler(handlers);
handlers.setHandlers(new Handler[]{contexts, defaultHandler, requestLogHandler});
}
else
{
handlers.addHandler(contexts);
}
}
}
/**
* Apply xml files to server startup, passing in ourselves as the
* "Server" instance.
*
* @param files
* @throws Exception
*/
public void applyXmlConfigurations (List<File> files)
throws Exception
{
if (files == null || files.isEmpty())
return;
Map<String,Object> lastMap = Collections.singletonMap("Server", (Object)this);
for ( File xmlFile : files )
{
if (PluginLog.getLog() != null)
PluginLog.getLog().info( "Configuring Jetty from xml configuration file = " + xmlFile.getCanonicalPath() );
XmlConfiguration xmlConfiguration = new XmlConfiguration(Resource.toURL(xmlFile));
//chain ids from one config file to another
if (lastMap != null)
xmlConfiguration.getIdMap().putAll(lastMap);
//Set the system properties each time in case the config file set a new one
Enumeration<?> ensysprop = System.getProperties().propertyNames();
while (ensysprop.hasMoreElements())
{
String name = (String)ensysprop.nextElement();
xmlConfiguration.getProperties().put(name,System.getProperty(name));
}
xmlConfiguration.configure();
lastMap = xmlConfiguration.getIdMap();
}
}
}