5
5
import org .testcontainers .utility .MountableFile ;
6
6
import org .testcontainers .applicationserver .ApplicationServerContainer ;
7
7
8
+ import java .io .IOException ;
9
+ import java .nio .charset .StandardCharsets ;
10
+ import java .nio .file .Files ;
11
+ import java .nio .file .Path ;
12
+ import java .nio .file .Paths ;
13
+ import java .nio .file .StandardOpenOption ;
8
14
import java .time .Duration ;
15
+ import java .util .ArrayList ;
16
+ import java .util .Arrays ;
17
+ import java .util .List ;
9
18
import java .util .Objects ;
10
19
11
20
/**
@@ -18,8 +27,6 @@ public class LibertyServerContainer extends ApplicationServerContainer {
18
27
// About the image
19
28
public static final String IMAGE = "open-liberty" ;
20
29
21
- public static final String DEFAULT_TAG = "23.0.0.3-full-java17-openj9" ;
22
-
23
30
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName .parse (IMAGE );
24
31
25
32
// Container defaults
@@ -33,9 +40,12 @@ public class LibertyServerContainer extends ApplicationServerContainer {
33
40
34
41
private static final String APPLICATION_DROPIN_DIR = "/config/dropins/" ;
35
42
43
+ private static final List <String > DEFAULT_FEATURES = Arrays .asList ("webProfile-10.0" );
44
+
36
45
// Container fields
37
- @ NonNull
38
- private MountableFile serverConfiguration = MountableFile .forClasspathResource ("default/config/defaultServer.xml" );
46
+ private MountableFile serverConfiguration ;
47
+
48
+ private List <String > features = new ArrayList <>();
39
49
40
50
// Constructors
41
51
public LibertyServerContainer (String imageName ) {
@@ -62,9 +72,19 @@ private void preconfigure() {
62
72
public void configure () {
63
73
super .configure ();
64
74
65
- // Copy default server configuration
66
- Objects .requireNonNull (serverConfiguration );
67
- withCopyFileToContainer (serverConfiguration , SERVER_CONFIG_DIR + "server.xml" );
75
+ // Copy server configuration
76
+ if ( Objects .nonNull (serverConfiguration ) ) {
77
+ withCopyFileToContainer (serverConfiguration , SERVER_CONFIG_DIR + "server.xml" );
78
+ return ;
79
+ }
80
+
81
+ if ( ! features .isEmpty () ) {
82
+ withCopyFileToContainer (generateServerConfiguration (features ), SERVER_CONFIG_DIR + "server.xml" );
83
+ return ;
84
+ }
85
+
86
+ withCopyFileToContainer (generateServerConfiguration (DEFAULT_FEATURES ), SERVER_CONFIG_DIR + "server.xml" );
87
+
68
88
}
69
89
70
90
@ Override
@@ -75,14 +95,51 @@ protected String getApplicationInstallDirectory() {
75
95
// Configuration
76
96
77
97
/**
78
- * The server configuration file that will be copied to the Liberty container
98
+ * The server configuration file that will be copied to the Liberty container.
99
+ *
100
+ * Calling this method more than once will replace the existing serverConfig if set.
79
101
*
80
102
* @param serverConfig - server.xml
81
103
* @return self
82
104
*/
83
105
public LibertyServerContainer withServerConfiguration (@ NonNull MountableFile serverConfig ) {
84
- System .out .println ("KJA1017 serverConfig called: " + serverConfig .getFilesystemPath ());
85
106
this .serverConfiguration = serverConfig ;
86
107
return this ;
87
108
}
109
+
110
+ /**
111
+ * A list of Liberty features to configure on the Liberty container.
112
+ *
113
+ * These features will be ignored if a serverConfig file is set.
114
+ *
115
+ * @param features - The list of features
116
+ * @return self
117
+ */
118
+ public LibertyServerContainer withFeatures (String ... features ) {
119
+ this .features .addAll (Arrays .asList (features ));
120
+ return this ;
121
+ }
122
+
123
+ // Helpers
124
+
125
+ private static final MountableFile generateServerConfiguration (List <String > features ) {
126
+ String configContents = "" ;
127
+ configContents += "<server><featureManager>" ;
128
+ for (String feature : features ) {
129
+ configContents += "<feature>" + feature + "</feature>" ;
130
+ }
131
+ configContents += "</featureManager></server>" ;
132
+ configContents += System .lineSeparator ();
133
+
134
+ Path generatedConfigPath = Paths .get (getTempDirectory ().toString (), "generatedServer.xml" );
135
+
136
+ try {
137
+ Files .write (generatedConfigPath , configContents .getBytes (StandardCharsets .UTF_8 ),
138
+ StandardOpenOption .CREATE , StandardOpenOption .WRITE );
139
+ } catch (IOException ioe ) {
140
+ throw new RuntimeException ("Unable to generate server configuration at runtime" , ioe );
141
+ }
142
+
143
+ return MountableFile .forHostPath (generatedConfigPath );
144
+ }
88
145
}
0 commit comments