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