View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.providers.WebAppProvider;
25  import org.eclipse.jetty.jmx.MBeanContainer;
26  import org.eclipse.jetty.security.HashLoginService;
27  import org.eclipse.jetty.server.ForwardedRequestCustomizer;
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.NCSARequestLog;
32  import org.eclipse.jetty.server.SecureRequestCustomizer;
33  import org.eclipse.jetty.server.Server;
34  import org.eclipse.jetty.server.ServerConnector;
35  import org.eclipse.jetty.server.SslConnectionFactory;
36  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
37  import org.eclipse.jetty.server.handler.DefaultHandler;
38  import org.eclipse.jetty.server.handler.HandlerCollection;
39  import org.eclipse.jetty.server.handler.RequestLogHandler;
40  import org.eclipse.jetty.server.handler.StatisticsHandler;
41  import org.eclipse.jetty.spdy.server.NPNServerConnectionFactory;
42  import org.eclipse.jetty.spdy.server.SPDYServerConnectionFactory;
43  import org.eclipse.jetty.spdy.server.http.HTTPSPDYServerConnectionFactory;
44  import org.eclipse.jetty.spdy.server.http.PushStrategy;
45  import org.eclipse.jetty.spdy.server.http.ReferrerPushStrategy;
46  import org.eclipse.jetty.util.ssl.SslContextFactory;
47  import org.eclipse.jetty.util.thread.QueuedThreadPool;
48  
49  public class SpdyServer
50  {
51      public static void main(String[] args) throws Exception
52      {
53          String jetty_home = System.getProperty("jetty.home","../../jetty-distribution/target/distribution");
54          System.setProperty("jetty.home",jetty_home);
55  
56          // Setup Threadpool
57          QueuedThreadPool threadPool = new QueuedThreadPool();
58          threadPool.setMaxThreads(500);
59  
60          Server server = new Server(threadPool);
61          server.manage(threadPool);
62          server.setDumpAfterStart(false);
63          server.setDumpBeforeStop(false);
64  
65          // Setup JMX
66          MBeanContainer mbContainer=new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
67          server.addBean(mbContainer);
68  
69  
70          // Common HTTP configuration
71          HttpConfiguration config = new HttpConfiguration();
72          config.setSecurePort(8443);
73          config.addCustomizer(new ForwardedRequestCustomizer());
74          config.addCustomizer(new SecureRequestCustomizer());
75          config.setSendServerVersion(true);
76          
77          
78          // Http Connector
79          HttpConnectionFactory http = new HttpConnectionFactory(config);
80          ServerConnector httpConnector = new ServerConnector(server,http);
81          httpConnector.setPort(8080);
82          httpConnector.setIdleTimeout(10000);
83          server.addConnector(httpConnector);
84  
85          
86          
87          // SSL configurations
88          SslContextFactory sslContextFactory = new SslContextFactory();
89          sslContextFactory.setKeyStorePath(jetty_home + "/etc/keystore");
90          sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
91          sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
92          sslContextFactory.setTrustStorePath(jetty_home + "/etc/keystore");
93          sslContextFactory.setTrustStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
94          sslContextFactory.setExcludeCipherSuites(
95                  "SSL_RSA_WITH_DES_CBC_SHA",
96                  "SSL_DHE_RSA_WITH_DES_CBC_SHA",
97                  "SSL_DHE_DSS_WITH_DES_CBC_SHA",
98                  "SSL_RSA_EXPORT_WITH_RC4_40_MD5",
99                  "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
100                 "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
101                 "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA");
102         
103         
104 
105 
106         // Spdy Connector
107         SPDYServerConnectionFactory.checkNPNAvailable();
108 
109         PushStrategy push = new ReferrerPushStrategy();
110         HTTPSPDYServerConnectionFactory spdy2 = new HTTPSPDYServerConnectionFactory(2,config,push);
111         spdy2.setInputBufferSize(8192);
112         spdy2.setInitialWindowSize(32768);
113 
114         HTTPSPDYServerConnectionFactory spdy3 = new HTTPSPDYServerConnectionFactory(3,config,push);
115         spdy2.setInputBufferSize(8192);
116 
117         NPNServerConnectionFactory npn = new NPNServerConnectionFactory(spdy3.getProtocol(),spdy2.getProtocol(),http.getProtocol());
118         npn.setDefaultProtocol(http.getProtocol());
119         npn.setInputBufferSize(1024);
120         
121         SslConnectionFactory ssl = new SslConnectionFactory(sslContextFactory,npn.getProtocol());
122         
123         ServerConnector spdyConnector = new ServerConnector(server,ssl,npn,spdy3,spdy2,http);
124         spdyConnector.setPort(8443);
125 
126         server.addConnector(spdyConnector);
127         
128         
129         // Setup handlers
130         HandlerCollection handlers = new HandlerCollection();
131         ContextHandlerCollection contexts = new ContextHandlerCollection();
132         RequestLogHandler requestLogHandler = new RequestLogHandler();
133 
134         handlers.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler });
135 
136         StatisticsHandler stats = new StatisticsHandler();
137         stats.setHandler(handlers);
138 
139         server.setHandler(stats);
140 
141         // Setup deployers
142         DeploymentManager deployer = new DeploymentManager();
143         deployer.setContexts(contexts);
144         server.addBean(deployer);
145 
146         WebAppProvider webapp_provider = new WebAppProvider();
147         webapp_provider.setMonitoredDirName(jetty_home + "/webapps");
148         webapp_provider.setParentLoaderPriority(false);
149         webapp_provider.setExtractWars(true);
150         webapp_provider.setScanInterval(2);
151         webapp_provider.setDefaultsDescriptor(jetty_home + "/etc/webdefault.xml");
152         deployer.addAppProvider(webapp_provider);
153 
154         HashLoginService login = new HashLoginService();
155         login.setName("Test Realm");
156         login.setConfig(jetty_home + "/etc/realm.properties");
157         server.addBean(login);
158 
159         NCSARequestLog requestLog = new NCSARequestLog(jetty_home + "/logs/jetty-yyyy_mm_dd.log");
160         requestLog.setExtended(false);
161         requestLogHandler.setRequestLog(requestLog);
162 
163         server.setStopAtShutdown(true);
164 
165         server.start();
166         server.dumpStdErr();
167         server.join();
168     }
169 }