View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.embedded;
20  
21  import java.io.File;
22  import java.io.FileNotFoundException;
23  import java.lang.management.ManagementFactory;
24  
25  import org.eclipse.jetty.deploy.DeploymentManager;
26  import org.eclipse.jetty.deploy.PropertiesConfigurationManager;
27  import org.eclipse.jetty.deploy.bindings.DebugListenerBinding;
28  import org.eclipse.jetty.deploy.providers.WebAppProvider;
29  import org.eclipse.jetty.http.HttpVersion;
30  import org.eclipse.jetty.jmx.MBeanContainer;
31  import org.eclipse.jetty.rewrite.handler.RewriteHandler;
32  import org.eclipse.jetty.security.HashLoginService;
33  import org.eclipse.jetty.server.DebugListener;
34  import org.eclipse.jetty.server.Handler;
35  import org.eclipse.jetty.server.HttpConfiguration;
36  import org.eclipse.jetty.server.HttpConnectionFactory;
37  import org.eclipse.jetty.server.LowResourceMonitor;
38  import org.eclipse.jetty.server.NCSARequestLog;
39  import org.eclipse.jetty.server.SecureRequestCustomizer;
40  import org.eclipse.jetty.server.Server;
41  import org.eclipse.jetty.server.ServerConnectionStatistics;
42  import org.eclipse.jetty.server.ServerConnector;
43  import org.eclipse.jetty.server.SslConnectionFactory;
44  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
45  import org.eclipse.jetty.server.handler.DefaultHandler;
46  import org.eclipse.jetty.server.handler.HandlerCollection;
47  import org.eclipse.jetty.server.handler.RequestLogHandler;
48  import org.eclipse.jetty.server.handler.StatisticsHandler;
49  import org.eclipse.jetty.util.ssl.SslContextFactory;
50  import org.eclipse.jetty.util.thread.QueuedThreadPool;
51  import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler;
52  import org.eclipse.jetty.webapp.Configuration;
53  
54  /**
55   * Starts the Jetty Distribution's demo-base directory using entirely
56   * embedded jetty techniques.
57   */
58  public class LikeJettyXml
59  {
60      public static void main( String[] args ) throws Exception
61      {
62          // Path to as-built jetty-distribution directory
63          String jettyHomeBuild = "../../jetty-distribution/target/distribution";
64          
65          // Find jetty home and base directories
66          String homePath = System.getProperty("jetty.home", jettyHomeBuild);
67          File start_jar = new File(homePath,"start.jar");
68          if (!start_jar.exists())
69          {
70              homePath = jettyHomeBuild = "jetty-distribution/target/distribution";
71              start_jar = new File(homePath,"start.jar");
72              if (!start_jar.exists())
73                  throw new FileNotFoundException(start_jar.toString());
74          }
75  
76          File homeDir = new File(homePath);
77  
78          String basePath = System.getProperty("jetty.base", homeDir + "/demo-base");
79          File baseDir = new File(basePath);
80          if(!baseDir.exists())
81          {
82              throw new FileNotFoundException(baseDir.getAbsolutePath());
83          }
84          
85          // Configure jetty.home and jetty.base system properties
86          String jetty_home = homeDir.getAbsolutePath();
87          String jetty_base = baseDir.getAbsolutePath();
88          System.setProperty("jetty.home", jetty_home);
89          System.setProperty("jetty.base", jetty_base);
90  
91  
92          // === jetty.xml ===
93          // Setup Threadpool
94          QueuedThreadPool threadPool = new QueuedThreadPool();
95          threadPool.setMaxThreads(500);
96  
97          // Server
98          Server server = new Server(threadPool);
99  
100         // Scheduler
101         server.addBean(new ScheduledExecutorScheduler());
102 
103         // HTTP Configuration
104         HttpConfiguration http_config = new HttpConfiguration();
105         http_config.setSecureScheme("https");
106         http_config.setSecurePort(8443);
107         http_config.setOutputBufferSize(32768);
108         http_config.setRequestHeaderSize(8192);
109         http_config.setResponseHeaderSize(8192);
110         http_config.setSendServerVersion(true);
111         http_config.setSendDateHeader(false);
112         // httpConfig.addCustomizer(new ForwardedRequestCustomizer());
113 
114         // Handler Structure
115         HandlerCollection handlers = new HandlerCollection();
116         ContextHandlerCollection contexts = new ContextHandlerCollection();
117         handlers.setHandlers(new Handler[] { contexts, new DefaultHandler() });
118         server.setHandler(handlers);
119 
120         // Extra options
121         server.setDumpAfterStart(false);
122         server.setDumpBeforeStop(false);
123         server.setStopAtShutdown(true);
124 
125         // === jetty-jmx.xml ===
126         MBeanContainer mbContainer = new MBeanContainer(
127                 ManagementFactory.getPlatformMBeanServer());
128         server.addBean(mbContainer);
129 
130 
131         // === jetty-http.xml ===
132         ServerConnector http = new ServerConnector(server,
133                 new HttpConnectionFactory(http_config));
134         http.setPort(8080);
135         http.setIdleTimeout(30000);
136         server.addConnector(http);
137 
138 
139         // === jetty-https.xml ===
140         // SSL Context Factory
141         SslContextFactory sslContextFactory = new SslContextFactory();
142         sslContextFactory.setKeyStorePath(jetty_home + "/../../../jetty-server/src/test/config/etc/keystore");
143         sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
144         sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
145         sslContextFactory.setTrustStorePath(jetty_home + "/../../../jetty-server/src/test/config/etc/keystore");
146         sslContextFactory.setTrustStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
147         sslContextFactory.setExcludeCipherSuites("SSL_RSA_WITH_DES_CBC_SHA",
148                 "SSL_DHE_RSA_WITH_DES_CBC_SHA", "SSL_DHE_DSS_WITH_DES_CBC_SHA",
149                 "SSL_RSA_EXPORT_WITH_RC4_40_MD5",
150                 "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
151                 "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
152                 "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA");
153 
154         // SSL HTTP Configuration
155         HttpConfiguration https_config = new HttpConfiguration(http_config);
156         https_config.addCustomizer(new SecureRequestCustomizer());
157 
158         // SSL Connector
159         ServerConnector sslConnector = new ServerConnector(server,
160             new SslConnectionFactory(sslContextFactory,HttpVersion.HTTP_1_1.asString()),
161             new HttpConnectionFactory(https_config));
162         sslConnector.setPort(8443);
163         server.addConnector(sslConnector);
164 
165 
166         // === jetty-deploy.xml ===
167         DeploymentManager deployer = new DeploymentManager();
168         DebugListener debug = new DebugListener(System.err,true,true,true);
169         server.addBean(debug);        
170         deployer.addLifeCycleBinding(new DebugListenerBinding(debug));
171         deployer.setContexts(contexts);
172         deployer.setContextAttribute(
173                 "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
174                 ".*/servlet-api-[^/]*\\.jar$");
175 
176         WebAppProvider webapp_provider = new WebAppProvider();
177         webapp_provider.setMonitoredDirName(jetty_base + "/webapps");
178         webapp_provider.setDefaultsDescriptor(jetty_home + "/etc/webdefault.xml");
179         webapp_provider.setScanInterval(1);
180         webapp_provider.setExtractWars(true);
181         webapp_provider.setConfigurationManager(new PropertiesConfigurationManager());
182 
183         deployer.addAppProvider(webapp_provider);
184         server.addBean(deployer);
185         
186         // === setup jetty plus ==
187         Configuration.ClassList.setServerDefault(server).addAfter(
188                 "org.eclipse.jetty.webapp.FragmentConfiguration",
189                 "org.eclipse.jetty.plus.webapp.EnvConfiguration",
190                 "org.eclipse.jetty.plus.webapp.PlusConfiguration");
191 
192         // === jetty-stats.xml ===
193         StatisticsHandler stats = new StatisticsHandler();
194         stats.setHandler(server.getHandler());
195         server.setHandler(stats);
196         ServerConnectionStatistics.addToAllConnectors(server);
197 
198         // === Rewrite Handler
199         RewriteHandler rewrite = new RewriteHandler();
200         rewrite.setHandler(server.getHandler());
201         server.setHandler(rewrite);
202 
203         // === jetty-requestlog.xml ===
204         NCSARequestLog requestLog = new NCSARequestLog();
205         requestLog.setFilename(jetty_home + "/logs/yyyy_mm_dd.request.log");
206         requestLog.setFilenameDateFormat("yyyy_MM_dd");
207         requestLog.setRetainDays(90);
208         requestLog.setAppend(true);
209         requestLog.setExtended(true);
210         requestLog.setLogCookies(false);
211         requestLog.setLogTimeZone("GMT");
212         RequestLogHandler requestLogHandler = new RequestLogHandler();
213         requestLogHandler.setRequestLog(requestLog);
214         handlers.addHandler(requestLogHandler);
215 
216 
217         // === jetty-lowresources.xml ===
218         LowResourceMonitor lowResourcesMonitor=new LowResourceMonitor(server);
219         lowResourcesMonitor.setPeriod(1000);
220         lowResourcesMonitor.setLowResourcesIdleTimeout(200);
221         lowResourcesMonitor.setMonitorThreads(true);
222         lowResourcesMonitor.setMaxConnections(0);
223         lowResourcesMonitor.setMaxMemory(0);
224         lowResourcesMonitor.setMaxLowResourcesTime(5000);
225         server.addBean(lowResourcesMonitor);
226 
227 
228         // === test-realm.xml ===
229         HashLoginService login = new HashLoginService();
230         login.setName("Test Realm");
231         login.setConfig(jetty_base + "/etc/realm.properties");
232         login.setHotReload(false);
233         server.addBean(login);
234         
235         // Start the server
236         server.start();
237         server.join();
238     }
239 }