1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.maven.plugin;
20
21
22 import java.io.File;
23 import java.util.Collections;
24 import java.util.Enumeration;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.eclipse.jetty.server.Handler;
29 import org.eclipse.jetty.server.RequestLog;
30 import org.eclipse.jetty.server.handler.ContextHandlerCollection;
31 import org.eclipse.jetty.server.handler.DefaultHandler;
32 import org.eclipse.jetty.server.handler.HandlerCollection;
33 import org.eclipse.jetty.server.handler.RequestLogHandler;
34 import org.eclipse.jetty.util.resource.Resource;
35 import org.eclipse.jetty.webapp.WebAppContext;
36 import org.eclipse.jetty.xml.XmlConfiguration;
37
38
39
40
41
42
43
44
45 public class JettyServer extends org.eclipse.jetty.server.Server
46 {
47 private RequestLog requestLog;
48 private ContextHandlerCollection contexts;
49
50
51
52
53
54
55 public JettyServer()
56 {
57 super();
58
59 Resource.setDefaultUseCaches(false);
60 }
61
62
63 public void setRequestLog (RequestLog requestLog)
64 {
65 this.requestLog = requestLog;
66 }
67
68
69
70
71 public void doStart() throws Exception
72 {
73 super.doStart();
74 }
75
76
77
78
79
80 public void addWebApplication(WebAppContext webapp) throws Exception
81 {
82 contexts.addHandler (webapp);
83 }
84
85
86
87
88
89
90
91
92
93 public void configureHandlers () throws Exception
94 {
95 DefaultHandler defaultHandler = new DefaultHandler();
96 RequestLogHandler requestLogHandler = new RequestLogHandler();
97 if (this.requestLog != null)
98 requestLogHandler.setRequestLog(this.requestLog);
99
100 contexts = (ContextHandlerCollection)super.getChildHandlerByClass(ContextHandlerCollection.class);
101 if (contexts==null)
102 {
103 contexts = new ContextHandlerCollection();
104 HandlerCollection handlers = (HandlerCollection)super.getChildHandlerByClass(HandlerCollection.class);
105 if (handlers==null)
106 {
107 handlers = new HandlerCollection();
108 super.setHandler(handlers);
109 handlers.setHandlers(new Handler[]{contexts, defaultHandler, requestLogHandler});
110 }
111 else
112 {
113 handlers.addHandler(contexts);
114 }
115 }
116 }
117
118
119
120
121
122
123
124
125 public void applyXmlConfigurations (List<File> files)
126 throws Exception
127 {
128 if (files == null || files.isEmpty())
129 return;
130
131 Map<String,Object> lastMap = Collections.singletonMap("Server", (Object)this);
132
133 for ( File xmlFile : files )
134 {
135 if (PluginLog.getLog() != null)
136 PluginLog.getLog().info( "Configuring Jetty from xml configuration file = " + xmlFile.getCanonicalPath() );
137
138
139 XmlConfiguration xmlConfiguration = new XmlConfiguration(Resource.toURL(xmlFile));
140
141
142 if (lastMap != null)
143 xmlConfiguration.getIdMap().putAll(lastMap);
144
145
146 Enumeration<?> ensysprop = System.getProperties().propertyNames();
147 while (ensysprop.hasMoreElements())
148 {
149 String name = (String)ensysprop.nextElement();
150 xmlConfiguration.getProperties().put(name,System.getProperty(name));
151 }
152 xmlConfiguration.configure();
153 lastMap = xmlConfiguration.getIdMap();
154 }
155 }
156 }