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          // SSL configurations
86          SslContextFactory sslContextFactory = new SslContextFactory();
87          sslContextFactory.setKeyStorePath(jetty_home + "/etc/keystore");
88          sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
89          sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
90          sslContextFactory.setTrustStorePath(jetty_home + "/etc/keystore");
91          sslContextFactory.setTrustStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
92          sslContextFactory.setExcludeCipherSuites(
93                  "SSL_RSA_WITH_DES_CBC_SHA",
94                  "SSL_DHE_RSA_WITH_DES_CBC_SHA",
95                  "SSL_DHE_DSS_WITH_DES_CBC_SHA",
96                  "SSL_RSA_EXPORT_WITH_RC4_40_MD5",
97                  "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
98                  "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
99                  "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA");
100 
101 
102         // Spdy Connector
103         SPDYServerConnectionFactory.checkNPNAvailable();
104 
105         PushStrategy push = new ReferrerPushStrategy();
106         HTTPSPDYServerConnectionFactory spdy2 = new HTTPSPDYServerConnectionFactory(2,config,push);
107         spdy2.setInputBufferSize(8192);
108         spdy2.setInitialWindowSize(32768);
109 
110         HTTPSPDYServerConnectionFactory spdy3 = new HTTPSPDYServerConnectionFactory(3,config,push);
111         spdy2.setInputBufferSize(8192);
112 
113         NPNServerConnectionFactory npn = new NPNServerConnectionFactory(spdy3.getProtocol(),spdy2.getProtocol(),http.getProtocol());
114         npn.setDefaultProtocol(http.getProtocol());
115         npn.setInputBufferSize(1024);
116         
117         SslConnectionFactory ssl = new SslConnectionFactory(sslContextFactory,npn.getProtocol());
118         
119         ServerConnector spdyConnector = new ServerConnector(server,ssl,npn,spdy3,spdy2,http);
120         spdyConnector.setPort(8443);
121 
122         server.addConnector(spdyConnector);
123         
124         
125         // Setup handlers
126         HandlerCollection handlers = new HandlerCollection();
127         ContextHandlerCollection contexts = new ContextHandlerCollection();
128         RequestLogHandler requestLogHandler = new RequestLogHandler();
129 
130         handlers.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler });
131 
132         StatisticsHandler stats = new StatisticsHandler();
133         stats.setHandler(handlers);
134 
135         server.setHandler(stats);
136 
137         // Setup deployers
138         DeploymentManager deployer = new DeploymentManager();
139         deployer.setContexts(contexts);
140         server.addBean(deployer);
141 
142         WebAppProvider webapp_provider = new WebAppProvider();
143         webapp_provider.setMonitoredDirName(jetty_home + "/webapps");
144         webapp_provider.setParentLoaderPriority(false);
145         webapp_provider.setExtractWars(true);
146         webapp_provider.setScanInterval(2);
147         webapp_provider.setDefaultsDescriptor(jetty_home + "/etc/webdefault.xml");
148         deployer.addAppProvider(webapp_provider);
149 
150         HashLoginService login = new HashLoginService();
151         login.setName("Test Realm");
152         login.setConfig(jetty_home + "/etc/realm.properties");
153         server.addBean(login);
154 
155         NCSARequestLog requestLog = new NCSARequestLog(jetty_home + "/logs/jetty-yyyy_mm_dd.log");
156         requestLog.setExtended(false);
157         requestLogHandler.setRequestLog(requestLog);
158 
159         server.setStopAtShutdown(true);
160 
161         server.start();
162         server.dumpStdErr();
163         server.join();
164     }
165 }