View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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.providers.WebAppProvider;
28  import org.eclipse.jetty.http.HttpVersion;
29  import org.eclipse.jetty.jmx.MBeanContainer;
30  import org.eclipse.jetty.security.HashLoginService;
31  import org.eclipse.jetty.server.Handler;
32  import org.eclipse.jetty.server.HttpConfiguration;
33  import org.eclipse.jetty.server.HttpConnectionFactory;
34  import org.eclipse.jetty.server.LowResourceMonitor;
35  import org.eclipse.jetty.server.NCSARequestLog;
36  import org.eclipse.jetty.server.SecureRequestCustomizer;
37  import org.eclipse.jetty.server.Server;
38  import org.eclipse.jetty.server.ServerConnector;
39  import org.eclipse.jetty.server.SslConnectionFactory;
40  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
41  import org.eclipse.jetty.server.handler.DefaultHandler;
42  import org.eclipse.jetty.server.handler.HandlerCollection;
43  import org.eclipse.jetty.server.handler.RequestLogHandler;
44  import org.eclipse.jetty.server.handler.StatisticsHandler;
45  import org.eclipse.jetty.util.ssl.SslContextFactory;
46  import org.eclipse.jetty.util.thread.QueuedThreadPool;
47  import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler;
48  import org.eclipse.jetty.webapp.Configuration;
49  
50  /**
51   * Starts the Jetty Distribution's demo-base directory using entirely
52   * embedded jetty techniques.
53   */
54  public class LikeJettyXml
55  {
56      public static void main( String[] args ) throws Exception
57      {
58          // Path to as-built jetty-distribution directory
59          String jettyHomeBuild = "../../jetty-distribution/target/distribution";
60          
61          // Find jetty home and base directories
62          String homePath = System.getProperty("jetty.home", jettyHomeBuild);
63          File homeDir = new File(homePath);
64          if (!homeDir.exists())
65          {
66              throw new FileNotFoundException(homeDir.getAbsolutePath());
67          }
68          String basePath = System.getProperty("jetty.base", homeDir + "/demo-base");
69          File baseDir = new File(basePath);
70          if(!baseDir.exists())
71          {
72              throw new FileNotFoundException(baseDir.getAbsolutePath());
73          }
74          
75          // Configure jetty.home and jetty.base system properties
76          String jetty_home = homeDir.getAbsolutePath();
77          String jetty_base = baseDir.getAbsolutePath();
78          System.setProperty("jetty.home", jetty_home);
79          System.setProperty("jetty.base", jetty_base);
80  
81  
82          // === jetty.xml ===
83          // Setup Threadpool
84          QueuedThreadPool threadPool = new QueuedThreadPool();
85          threadPool.setMaxThreads(500);
86  
87          // Server
88          Server server = new Server(threadPool);
89  
90          // Scheduler
91          server.addBean(new ScheduledExecutorScheduler());
92  
93          // HTTP Configuration
94          HttpConfiguration http_config = new HttpConfiguration();
95          http_config.setSecureScheme("https");
96          http_config.setSecurePort(8443);
97          http_config.setOutputBufferSize(32768);
98          http_config.setRequestHeaderSize(8192);
99          http_config.setResponseHeaderSize(8192);
100         http_config.setSendServerVersion(true);
101         http_config.setSendDateHeader(false);
102         // httpConfig.addCustomizer(new ForwardedRequestCustomizer());
103 
104         // Handler Structure
105         HandlerCollection handlers = new HandlerCollection();
106         ContextHandlerCollection contexts = new ContextHandlerCollection();
107         handlers.setHandlers(new Handler[] { contexts, new DefaultHandler() });
108         server.setHandler(handlers);
109 
110         // Extra options
111         server.setDumpAfterStart(false);
112         server.setDumpBeforeStop(false);
113         server.setStopAtShutdown(true);
114 
115         // === jetty-jmx.xml ===
116         MBeanContainer mbContainer = new MBeanContainer(
117                 ManagementFactory.getPlatformMBeanServer());
118         server.addBean(mbContainer);
119 
120 
121         // === jetty-http.xml ===
122         ServerConnector http = new ServerConnector(server,
123                 new HttpConnectionFactory(http_config));
124         http.setPort(8080);
125         http.setIdleTimeout(30000);
126         server.addConnector(http);
127 
128 
129         // === jetty-https.xml ===
130         // SSL Context Factory
131         SslContextFactory sslContextFactory = new SslContextFactory();
132         sslContextFactory.setKeyStorePath(jetty_home + "/../../../jetty-server/src/test/config/etc/keystore");
133         sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
134         sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
135         sslContextFactory.setTrustStorePath(jetty_home + "/../../../jetty-server/src/test/config/etc/keystore");
136         sslContextFactory.setTrustStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
137         sslContextFactory.setExcludeCipherSuites("SSL_RSA_WITH_DES_CBC_SHA",
138                 "SSL_DHE_RSA_WITH_DES_CBC_SHA", "SSL_DHE_DSS_WITH_DES_CBC_SHA",
139                 "SSL_RSA_EXPORT_WITH_RC4_40_MD5",
140                 "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
141                 "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
142                 "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA");
143 
144         // SSL HTTP Configuration
145         HttpConfiguration https_config = new HttpConfiguration(http_config);
146         https_config.addCustomizer(new SecureRequestCustomizer());
147 
148         // SSL Connector
149         ServerConnector sslConnector = new ServerConnector(server,
150             new SslConnectionFactory(sslContextFactory,HttpVersion.HTTP_1_1.asString()),
151             new HttpConnectionFactory(https_config));
152         sslConnector.setPort(8443);
153         server.addConnector(sslConnector);
154 
155 
156         // === jetty-deploy.xml ===
157         DeploymentManager deployer = new DeploymentManager();
158         deployer.setContexts(contexts);
159         deployer.setContextAttribute(
160                 "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
161                 ".*/servlet-api-[^/]*\\.jar$");
162 
163         WebAppProvider webapp_provider = new WebAppProvider();
164         webapp_provider.setMonitoredDirName(jetty_base + "/webapps");
165         webapp_provider.setDefaultsDescriptor(jetty_home + "/etc/webdefault.xml");
166         webapp_provider.setScanInterval(1);
167         webapp_provider.setExtractWars(true);
168         webapp_provider.setConfigurationManager(new PropertiesConfigurationManager());
169 
170         deployer.addAppProvider(webapp_provider);
171         server.addBean(deployer);
172         
173         // === setup jetty plus ==
174         Configuration.ClassList.setServerDefault(server).addAfter(
175                 "org.eclipse.jetty.webapp.FragmentConfiguration",
176                 "org.eclipse.jetty.plus.webapp.EnvConfiguration",
177                 "org.eclipse.jetty.plus.webapp.PlusConfiguration");
178 
179         // === jetty-stats.xml ===
180         StatisticsHandler stats = new StatisticsHandler();
181         stats.setHandler(server.getHandler());
182         server.setHandler(stats);
183 
184 
185         // === jetty-requestlog.xml ===
186         NCSARequestLog requestLog = new NCSARequestLog();
187         requestLog.setFilename(jetty_home + "/logs/yyyy_mm_dd.request.log");
188         requestLog.setFilenameDateFormat("yyyy_MM_dd");
189         requestLog.setRetainDays(90);
190         requestLog.setAppend(true);
191         requestLog.setExtended(true);
192         requestLog.setLogCookies(false);
193         requestLog.setLogTimeZone("GMT");
194         RequestLogHandler requestLogHandler = new RequestLogHandler();
195         requestLogHandler.setRequestLog(requestLog);
196         handlers.addHandler(requestLogHandler);
197 
198 
199         // === jetty-lowresources.xml ===
200         LowResourceMonitor lowResourcesMonitor=new LowResourceMonitor(server);
201         lowResourcesMonitor.setPeriod(1000);
202         lowResourcesMonitor.setLowResourcesIdleTimeout(200);
203         lowResourcesMonitor.setMonitorThreads(true);
204         lowResourcesMonitor.setMaxConnections(0);
205         lowResourcesMonitor.setMaxMemory(0);
206         lowResourcesMonitor.setMaxLowResourcesTime(5000);
207         server.addBean(lowResourcesMonitor);
208 
209 
210         // === test-realm.xml ===
211         HashLoginService login = new HashLoginService();
212         login.setName("Test Realm");
213         login.setConfig(jetty_base + "/etc/realm.properties");
214         login.setRefreshInterval(0);
215         server.addBean(login);
216 
217         
218         // Start the server
219         server.start();
220         server.join();
221     }
222 }