9
9
package org .cryptomator .frontend .webdav ;
10
10
11
11
import com .google .common .base .Preconditions ;
12
- import dagger .Module ;
13
- import dagger .Provides ;
14
- import org .cryptomator .frontend .webdav .mount .MounterModule ;
15
12
import org .eclipse .jetty .http .UriCompliance ;
16
- import org .eclipse .jetty .server .Connector ;
17
- import org .eclipse .jetty .server .HttpConfiguration ;
18
- import org .eclipse .jetty .server .HttpConnectionFactory ;
19
- import org .eclipse .jetty .server .Server ;
20
- import org .eclipse .jetty .server .ServerConnector ;
13
+ import org .eclipse .jetty .server .*;
21
14
import org .eclipse .jetty .server .handler .ContextHandlerCollection ;
22
15
import org .eclipse .jetty .servlet .ServletContextHandler ;
23
16
import org .eclipse .jetty .servlet .ServletHolder ;
24
17
import org .eclipse .jetty .util .thread .ExecutorThreadPool ;
25
18
26
- import javax .inject .Qualifier ;
27
- import javax .inject .Singleton ;
28
- import java .lang .annotation .Documented ;
29
- import java .lang .annotation .Retention ;
30
- import java .lang .annotation .RetentionPolicy ;
31
- import java .util .Collection ;
32
19
import java .util .HashSet ;
33
20
import java .util .concurrent .BlockingQueue ;
34
- import java .util .concurrent .ExecutorService ;
35
21
import java .util .concurrent .LinkedBlockingQueue ;
36
22
import java .util .concurrent .ThreadPoolExecutor ;
37
23
import java .util .concurrent .TimeUnit ;
38
24
import java .util .concurrent .atomic .AtomicInteger ;
39
25
40
- @ Module (includes = {MounterModule .class })
41
- class WebDavServerModule {
26
+ class WebDavServerFactory {
42
27
43
28
private static final int MAX_PENDING_REQUESTS = 400 ;
44
29
private static final int MAX_THREADS = 100 ;
45
30
private static final int THREAD_IDLE_SECONDS = 60 ;
46
31
private static final String ROOT_PATH = "/" ;
32
+ private static final AtomicInteger THREAD_NUM = new AtomicInteger ();
47
33
48
- private AtomicInteger threadNum = new AtomicInteger ( 1 );
34
+ private WebDavServerFactory (){}
49
35
50
- @ Provides
51
- @ Singleton
52
- ThreadPoolExecutor provideThreadPoolExecutor () {
36
+ private static ThreadPoolExecutor createThreadPoolExecutor () {
53
37
// set core pool size = MAX_THREADS and allow coreThreadTimeOut to enforce spawning threads till the maximum even if the queue is not full
54
38
BlockingQueue <Runnable > queue = new LinkedBlockingQueue <>(MAX_PENDING_REQUESTS );
55
39
ThreadPoolExecutor executor = new ThreadPoolExecutor (MAX_THREADS , MAX_THREADS , THREAD_IDLE_SECONDS , TimeUnit .SECONDS , queue );
56
40
executor .allowCoreThreadTimeOut (true );
57
41
return executor ;
58
42
}
59
43
60
- @ Provides
61
- @ Singleton
62
- ExecutorService provideExecutorService (ThreadPoolExecutor executorService ) {
63
- return executorService ;
64
- }
65
-
66
- @ Provides
67
- @ Singleton
68
- ExecutorThreadPool provideThreadPool (ThreadPoolExecutor executorService ) {
44
+ private static ExecutorThreadPool createThreadPool (ThreadPoolExecutor executorService ) {
69
45
ExecutorThreadPool threadPool = new ExecutorThreadPool (executorService );
70
- executorService .setThreadFactory (this ::createServerThread );
46
+ executorService .setThreadFactory (WebDavServerFactory ::createServerThread );
71
47
try {
72
48
threadPool .start ();
73
49
return threadPool ;
@@ -76,67 +52,51 @@ ExecutorThreadPool provideThreadPool(ThreadPoolExecutor executorService) {
76
52
}
77
53
}
78
54
79
- private Thread createServerThread (Runnable runnable ) {
80
- Thread t = new Thread (runnable , String .format ("webdav-%03d" , threadNum . getAndIncrement ()));
55
+ private static Thread createServerThread (Runnable runnable ) {
56
+ Thread t = new Thread (runnable , String .format ("webdav-%03d" , THREAD_NUM . incrementAndGet ()));
81
57
t .setDaemon (true );
82
58
return t ;
83
59
}
84
60
85
61
86
- @ Provides
87
- @ Singleton
88
- Server provideServer (ExecutorThreadPool threadPool , ContextHandlerCollection servletCollection ) {
62
+ private static Server createServer (ExecutorThreadPool threadPool , ContextHandlerCollection servletCollection ) {
89
63
Preconditions .checkState (threadPool .isStarted ()); // otherwise addBean() will make the threadpool managed, i.e. it will be shut down when the server is stopped
90
64
Server server = new Server (threadPool );
91
65
server .setHandler (servletCollection );
92
66
return server ;
93
67
}
94
68
95
- @ Provides
96
- @ Singleton
97
- ServerConnector provideServerConnector (Server server ) {
69
+ private static ServerConnector createServerConnector (Server server ) {
98
70
HttpConfiguration config = new HttpConfiguration ();
99
71
config .setUriCompliance (UriCompliance .from ("0,AMBIGUOUS_PATH_SEPARATOR,AMBIGUOUS_PATH_ENCODING" ));
100
72
ServerConnector connector = new ServerConnector (server , new HttpConnectionFactory (config ));
101
73
server .setConnectors (new Connector []{connector });
102
74
return connector ;
103
75
}
104
76
105
- @ Provides
106
- @ Singleton
107
- ContextHandlerCollection provideContextHandlerCollection (@ CatchAll ServletContextHandler catchAllServletHandler ) {
77
+ private static ContextHandlerCollection createContextHandlerCollection (ServletContextHandler catchAllServletHandler ) {
108
78
ContextHandlerCollection collection = new ContextHandlerCollection ();
109
79
collection .addHandler (catchAllServletHandler );
110
80
return collection ;
111
81
}
112
82
113
- @ Provides
114
- @ Singleton
115
- @ CatchAll
116
- ServletContextHandler provideServletContextHandler (DefaultServlet servlet ) {
83
+ private static ServletContextHandler createDefaultServletContext (DefaultServlet servlet ) {
117
84
final ServletContextHandler servletContext = new ServletContextHandler (null , ROOT_PATH , ServletContextHandler .NO_SESSIONS );
118
85
final ServletHolder servletHolder = new ServletHolder (ROOT_PATH , servlet );
119
86
servletContext .addServlet (servletHolder , ROOT_PATH );
120
87
return servletContext ;
121
88
}
122
89
123
- @ Provides
124
- @ Singleton
125
- @ ContextPaths
126
- Collection <String > provideContextPaths () {
127
- return new HashSet <>();
128
- }
129
-
130
- @ Qualifier
131
- @ Documented
132
- @ Retention (RetentionPolicy .RUNTIME )
133
- @interface CatchAll {
134
- }
135
-
136
- @ Qualifier
137
- @ Documented
138
- @ Retention (RetentionPolicy .RUNTIME )
139
- @interface ContextPaths {
90
+ public static WebDavServer createWebDavServer () {
91
+ var contextPaths = new HashSet <String >();
92
+ var executorService = createThreadPoolExecutor ();
93
+ var threadPool = createThreadPool (executorService );
94
+ var defaultServlet = new DefaultServlet (contextPaths );
95
+ var defaultServletCtx = createDefaultServletContext (defaultServlet );
96
+ var servletCollectionCtx = createContextHandlerCollection (defaultServletCtx );
97
+ var server = createServer (threadPool , servletCollectionCtx );
98
+ var serverConnector = createServerConnector (server );
99
+ return new WebDavServer (server , executorService , serverConnector , servletCollectionCtx );
140
100
}
141
101
142
102
}
0 commit comments