View Javadoc

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