Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
328bbaf1
Commit
328bbaf1
authored
May 03, 2017
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update port file writer to support reactive servers
Closes gh-8531
parent
bdd8cb34
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
88 additions
and
17 deletions
+88
-17
EmbeddedServerPortFileWriter.java
...ngframework/boot/system/EmbeddedServerPortFileWriter.java
+20
-7
GenericReactiveWebApplicationContext.java
...eactive/context/GenericReactiveWebApplicationContext.java
+12
-0
ReactiveWebApplicationContext.java
...t/web/reactive/context/ReactiveWebApplicationContext.java
+12
-0
EmbeddedServerPortFileWriterTests.java
...mework/boot/system/EmbeddedServerPortFileWriterTests.java
+44
-10
No files found.
spring-boot/src/main/java/org/springframework/boot/system/EmbeddedServerPortFileWriter.java
View file @
328bbaf1
...
@@ -21,12 +21,14 @@ import java.io.File;
...
@@ -21,12 +21,14 @@ import java.io.File;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext
;
import
org.springframework.boot.web.context.WebServerInitializedEvent
;
import
org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent
;
import
org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationListener
;
import
org.springframework.context.ApplicationListener
;
import
org.springframework.util.Assert
;
import
org.springframework.util.Assert
;
import
org.springframework.util.FileCopyUtils
;
import
org.springframework.util.FileCopyUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.context.ConfigurableWebApplicationContext
;
/**
/**
* An {@link ApplicationListener} that saves embedded server port and management port into
* An {@link ApplicationListener} that saves embedded server port and management port into
...
@@ -40,7 +42,7 @@ import org.springframework.util.StringUtils;
...
@@ -40,7 +42,7 @@ import org.springframework.util.StringUtils;
* @since 1.4.0
* @since 1.4.0
*/
*/
public
class
EmbeddedServerPortFileWriter
public
class
EmbeddedServerPortFileWriter
implements
ApplicationListener
<
Servlet
WebServerInitializedEvent
>
{
implements
ApplicationListener
<
WebServerInitializedEvent
>
{
private
static
final
String
DEFAULT_FILE_NAME
=
"application.port"
;
private
static
final
String
DEFAULT_FILE_NAME
=
"application.port"
;
...
@@ -84,7 +86,7 @@ public class EmbeddedServerPortFileWriter
...
@@ -84,7 +86,7 @@ public class EmbeddedServerPortFileWriter
}
}
@Override
@Override
public
void
onApplicationEvent
(
Servlet
WebServerInitializedEvent
event
)
{
public
void
onApplicationEvent
(
WebServerInitializedEvent
event
)
{
File
portFile
=
getPortFile
(
event
.
getApplicationContext
());
File
portFile
=
getPortFile
(
event
.
getApplicationContext
());
try
{
try
{
String
port
=
String
.
valueOf
(
event
.
getWebServer
().
getPort
());
String
port
=
String
.
valueOf
(
event
.
getWebServer
().
getPort
());
...
@@ -100,12 +102,12 @@ public class EmbeddedServerPortFileWriter
...
@@ -100,12 +102,12 @@ public class EmbeddedServerPortFileWriter
/**
/**
* Return the actual port file that should be written for the given application
* Return the actual port file that should be written for the given application
* context. The default implementation builds a file from the source file and the
* context. The default implementation builds a file from the source file and the
* application context namespace.
* application context namespace
if available
.
* @param applicationContext the source application context
* @param applicationContext the source application context
* @return the file that should be written
* @return the file that should be written
*/
*/
protected
File
getPortFile
(
ServletWebServer
ApplicationContext
applicationContext
)
{
protected
File
getPortFile
(
ApplicationContext
applicationContext
)
{
String
contextName
=
applicationContext
.
getNamespace
(
);
String
contextName
=
getContextName
(
applicationContext
);
if
(
StringUtils
.
isEmpty
(
contextName
))
{
if
(
StringUtils
.
isEmpty
(
contextName
))
{
return
this
.
file
;
return
this
.
file
;
}
}
...
@@ -124,6 +126,17 @@ public class EmbeddedServerPortFileWriter
...
@@ -124,6 +126,17 @@ public class EmbeddedServerPortFileWriter
return
new
File
(
this
.
file
.
getParentFile
(),
name
);
return
new
File
(
this
.
file
.
getParentFile
(),
name
);
}
}
private
String
getContextName
(
ApplicationContext
applicationContext
)
{
if
(
applicationContext
instanceof
ConfigurableWebApplicationContext
)
{
return
((
ConfigurableWebApplicationContext
)
applicationContext
)
.
getNamespace
();
}
if
(
applicationContext
instanceof
ReactiveWebApplicationContext
)
{
return
((
ReactiveWebApplicationContext
)
applicationContext
).
getNamespace
();
}
return
null
;
}
private
boolean
isUpperCase
(
String
name
)
{
private
boolean
isUpperCase
(
String
name
)
{
for
(
int
i
=
0
;
i
<
name
.
length
();
i
++)
{
for
(
int
i
=
0
;
i
<
name
.
length
();
i
++)
{
if
(
Character
.
isLetter
(
name
.
charAt
(
i
))
if
(
Character
.
isLetter
(
name
.
charAt
(
i
))
...
...
spring-boot/src/main/java/org/springframework/boot/web/reactive/context/GenericReactiveWebApplicationContext.java
View file @
328bbaf1
...
@@ -28,6 +28,8 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
...
@@ -28,6 +28,8 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
public
class
GenericReactiveWebApplicationContext
extends
public
class
GenericReactiveWebApplicationContext
extends
AnnotationConfigApplicationContext
implements
ReactiveWebApplicationContext
{
AnnotationConfigApplicationContext
implements
ReactiveWebApplicationContext
{
private
String
namespace
;
public
GenericReactiveWebApplicationContext
()
{
public
GenericReactiveWebApplicationContext
()
{
super
();
super
();
}
}
...
@@ -36,4 +38,14 @@ public class GenericReactiveWebApplicationContext extends
...
@@ -36,4 +38,14 @@ public class GenericReactiveWebApplicationContext extends
super
(
annotatedClasses
);
super
(
annotatedClasses
);
}
}
@Override
public
void
setNamespace
(
String
namespace
)
{
this
.
namespace
=
namespace
;
}
@Override
public
String
getNamespace
()
{
return
this
.
namespace
;
}
}
}
spring-boot/src/main/java/org/springframework/boot/web/reactive/context/ReactiveWebApplicationContext.java
View file @
328bbaf1
...
@@ -26,4 +26,16 @@ import org.springframework.context.ApplicationContext;
...
@@ -26,4 +26,16 @@ import org.springframework.context.ApplicationContext;
*/
*/
public
interface
ReactiveWebApplicationContext
extends
ApplicationContext
{
public
interface
ReactiveWebApplicationContext
extends
ApplicationContext
{
/**
* Set the namespace for this reactive web application context.
* @param namespace the namespace for the context
*/
void
setNamespace
(
String
namespace
);
/**
* Return the namespace for this reactive web application context, if any.
* @return the namespace or {@code null}
*/
String
getNamespace
();
}
}
spring-boot/src/test/java/org/springframework/boot/system/EmbeddedServerPortFileWriterTests.java
View file @
328bbaf1
...
@@ -20,13 +20,20 @@ import java.io.File;
...
@@ -20,13 +20,20 @@ import java.io.File;
import
java.io.FileReader
;
import
java.io.FileReader
;
import
java.util.HashSet
;
import
java.util.HashSet
;
import
java.util.Set
;
import
java.util.Set
;
import
java.util.function.BiFunction
;
import
org.junit.After
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.junit.rules.TemporaryFolder
;
import
org.junit.rules.TemporaryFolder
;
import
org.junit.runner.RunWith
;
import
org.junit.runners.Parameterized
;
import
org.junit.runners.Parameterized.Parameters
;
import
org.springframework.boot.web.context.WebServerInitializedEvent
;
import
org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext
;
import
org.springframework.boot.web.reactive.context.ReactiveWebServerInitializedEvent
;
import
org.springframework.boot.web.server.WebServer
;
import
org.springframework.boot.web.server.WebServer
;
import
org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext
;
import
org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext
;
import
org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent
;
import
org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent
;
...
@@ -44,10 +51,44 @@ import static org.mockito.Mockito.mock;
...
@@ -44,10 +51,44 @@ import static org.mockito.Mockito.mock;
* @author Phillip Webb
* @author Phillip Webb
* @author Andy Wilkinson
* @author Andy Wilkinson
*/
*/
@RunWith
(
Parameterized
.
class
)
public
class
EmbeddedServerPortFileWriterTests
{
public
class
EmbeddedServerPortFileWriterTests
{
@Rule
@Rule
public
TemporaryFolder
temporaryFolder
=
new
TemporaryFolder
();
public
final
TemporaryFolder
temporaryFolder
=
new
TemporaryFolder
();
@Parameters
(
name
=
"{0}"
)
public
static
Object
[]
parameters
()
{
BiFunction
<
String
,
Integer
,
?
extends
WebServerInitializedEvent
>
servletEvent
=
(
String
name
,
Integer
port
)
->
{
ServletWebServerApplicationContext
applicationContext
=
mock
(
ServletWebServerApplicationContext
.
class
);
given
(
applicationContext
.
getNamespace
()).
willReturn
(
name
);
WebServer
source
=
mock
(
WebServer
.
class
);
given
(
source
.
getPort
()).
willReturn
(
port
);
ServletWebServerInitializedEvent
event
=
new
ServletWebServerInitializedEvent
(
applicationContext
,
source
);
return
event
;
};
BiFunction
<
String
,
Integer
,
?
extends
WebServerInitializedEvent
>
reactiveEvent
=
(
String
name
,
Integer
port
)
->
{
ReactiveWebServerApplicationContext
applicationContext
=
mock
(
ReactiveWebServerApplicationContext
.
class
);
given
(
applicationContext
.
getNamespace
()).
willReturn
(
name
);
WebServer
source
=
mock
(
WebServer
.
class
);
given
(
source
.
getPort
()).
willReturn
(
port
);
return
new
ReactiveWebServerInitializedEvent
(
source
,
applicationContext
);
};
return
new
Object
[]
{
new
Object
[]
{
"Servlet"
,
servletEvent
},
new
Object
[]
{
"Reactive"
,
reactiveEvent
}
};
}
private
final
BiFunction
<
String
,
Integer
,
?
extends
WebServerInitializedEvent
>
eventFactory
;
public
EmbeddedServerPortFileWriterTests
(
String
name
,
BiFunction
<
String
,
Integer
,
?
extends
WebServerInitializedEvent
>
eventFactory
)
{
this
.
eventFactory
=
eventFactory
;
}
@Before
@Before
@After
@After
...
@@ -117,15 +158,8 @@ public class EmbeddedServerPortFileWriterTests {
...
@@ -117,15 +158,8 @@ public class EmbeddedServerPortFileWriterTests {
assertThat
(
collectFileNames
(
file
.
getParentFile
())).
contains
(
managementFile
);
assertThat
(
collectFileNames
(
file
.
getParentFile
())).
contains
(
managementFile
);
}
}
private
ServletWebServerInitializedEvent
mockEvent
(
String
name
,
int
port
)
{
private
WebServerInitializedEvent
mockEvent
(
String
name
,
int
port
)
{
ServletWebServerApplicationContext
applicationContext
=
mock
(
return
this
.
eventFactory
.
apply
(
name
,
port
);
ServletWebServerApplicationContext
.
class
);
WebServer
source
=
mock
(
WebServer
.
class
);
given
(
applicationContext
.
getNamespace
()).
willReturn
(
name
);
given
(
source
.
getPort
()).
willReturn
(
port
);
ServletWebServerInitializedEvent
event
=
new
ServletWebServerInitializedEvent
(
applicationContext
,
source
);
return
event
;
}
}
private
Set
<
String
>
collectFileNames
(
File
directory
)
{
private
Set
<
String
>
collectFileNames
(
File
directory
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment