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