1
- /*
2
- * Copyright 2012-2014 the original author or authors.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- package org .springframework .boot .context .embedded .undertow ;
18
-
19
- import io .undertow .Handlers ;
20
- import io .undertow .Undertow ;
21
- import io .undertow .Undertow .Builder ;
22
- import io .undertow .server .HttpHandler ;
23
- import io .undertow .server .handlers .PathHandler ;
24
- import io .undertow .servlet .api .DeploymentManager ;
25
-
26
- import javax .servlet .ServletException ;
27
-
28
- import org .springframework .boot .context .embedded .EmbeddedServletContainer ;
29
- import org .springframework .boot .context .embedded .EmbeddedServletContainerException ;
30
- import org .springframework .util .StringUtils ;
31
-
32
- /**
33
- * {@link EmbeddedServletContainer} that can be used to control an embedded Undertow
34
- * server. Typically this class should be created using
35
- * {@link UndertowEmbeddedServletContainerFactory} and not directly.
36
- *
37
- * @author Ivan Sopov
38
- * @author Andy Wilkinson
39
- * @since 1.2.0
40
- * @see UndertowEmbeddedServletContainer
41
- */
42
- public class UndertowEmbeddedServletContainer implements EmbeddedServletContainer {
43
-
44
- private final DeploymentManager manager ;
45
-
46
- private final Builder builder ;
47
-
48
- private final String contextPath ;
49
-
50
- private final int port ;
51
-
52
- private final boolean autoStart ;
53
-
54
- private Undertow undertow ;
55
-
56
- private boolean started = false ;
57
-
58
- public UndertowEmbeddedServletContainer (Builder builder , DeploymentManager manager ,
59
- String contextPath , int port , boolean autoStart ) {
60
- this .builder = builder ;
61
- this .manager = manager ;
62
- this .contextPath = contextPath ;
63
- this .port = port ;
64
- this .autoStart = autoStart ;
65
- }
66
-
67
- @ Override
68
- public synchronized void start () throws EmbeddedServletContainerException {
69
- if (!this .autoStart ) {
70
- return ;
71
- }
72
- if (this .undertow == null ) {
73
- try {
74
- HttpHandler servletHandler = this .manager .start ();
75
- if (StringUtils .isEmpty (this .contextPath )) {
76
- this .builder .setHandler (servletHandler );
77
- }
78
- else {
79
- PathHandler pathHandler = Handlers .path ().addPrefixPath (
80
- this .contextPath , servletHandler );
81
- this .builder .setHandler (pathHandler );
82
- }
83
- this .undertow = this .builder .build ();
84
- }
85
- catch (ServletException ex ) {
86
- throw new EmbeddedServletContainerException (
87
- "Unable to start embdedded Undertow" , ex );
88
- }
89
- }
90
- this .undertow .start ();
91
- this .started = true ;
92
- }
93
-
94
- @ Override
95
- public synchronized void stop () throws EmbeddedServletContainerException {
96
- if (this .started ) {
97
- this .started = false ;
98
- this .undertow .stop ();
99
- }
100
- }
101
-
102
- @ Override
103
- public int getPort () {
104
- return this .port ;
105
- }
106
-
1
+ /*
2
+ * Copyright 2012-2014 the original author or authors.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package org .springframework .boot .context .embedded .undertow ;
18
+
19
+ import io .undertow .Handlers ;
20
+ import io .undertow .Undertow ;
21
+ import io .undertow .Undertow .Builder ;
22
+ import io .undertow .server .HttpHandler ;
23
+ import io .undertow .server .handlers .PathHandler ;
24
+ import io .undertow .servlet .api .DeploymentManager ;
25
+
26
+ import javax .servlet .ServletException ;
27
+
28
+ import org .springframework .boot .context .embedded .EmbeddedServletContainer ;
29
+ import org .springframework .boot .context .embedded .EmbeddedServletContainerException ;
30
+ import org .springframework .util .StringUtils ;
31
+
32
+ /**
33
+ * {@link EmbeddedServletContainer} that can be used to control an embedded Undertow
34
+ * server. Typically this class should be created using
35
+ * {@link UndertowEmbeddedServletContainerFactory} and not directly.
36
+ *
37
+ * @author Ivan Sopov
38
+ * @author Andy Wilkinson
39
+ * @since 1.2.0
40
+ * @see UndertowEmbeddedServletContainer
41
+ */
42
+ public class UndertowEmbeddedServletContainer implements EmbeddedServletContainer {
43
+
44
+ private final DeploymentManager manager ;
45
+
46
+ private final Builder builder ;
47
+
48
+ private final String contextPath ;
49
+
50
+ private final int port ;
51
+
52
+ private final boolean autoStart ;
53
+
54
+ private Undertow undertow ;
55
+
56
+ private boolean started = false ;
57
+
58
+ public UndertowEmbeddedServletContainer (Builder builder , DeploymentManager manager ,
59
+ String contextPath , int port , boolean autoStart ) {
60
+ this .builder = builder ;
61
+ this .manager = manager ;
62
+ this .contextPath = contextPath ;
63
+ this .port = port ;
64
+ this .autoStart = autoStart ;
65
+ }
66
+
67
+ @ Override
68
+ public synchronized void start () throws EmbeddedServletContainerException {
69
+ if (!this .autoStart ) {
70
+ return ;
71
+ }
72
+ if (this .undertow == null ) {
73
+ try {
74
+ HttpHandler servletHandler = this .manager .start ();
75
+ if (StringUtils .isEmpty (this .contextPath )) {
76
+ this .builder .setHandler (servletHandler );
77
+ }
78
+ else {
79
+ PathHandler pathHandler = Handlers .path ().addPrefixPath (
80
+ this .contextPath , servletHandler );
81
+ this .builder .setHandler (pathHandler );
82
+ }
83
+ this .undertow = this .builder .build ();
84
+ }
85
+ catch (ServletException ex ) {
86
+ throw new EmbeddedServletContainerException (
87
+ "Unable to start embdedded Undertow" , ex );
88
+ }
89
+ }
90
+ this .undertow .start ();
91
+ this .started = true ;
92
+ }
93
+
94
+ @ Override
95
+ public synchronized void stop () throws EmbeddedServletContainerException {
96
+ if (this .started ) {
97
+ this .started = false ;
98
+ this .undertow .stop ();
99
+ }
100
+ }
101
+
102
+ @ Override
103
+ public int getPort () {
104
+ return this .port ;
105
+ }
106
+
107
107
}
0 commit comments