|
1 | 1 | package pl.wavesoftware.gasper; |
2 | 2 |
|
3 | 3 | import org.slf4j.event.Level; |
4 | | -import pl.wavesoftware.eid.utils.EidPreconditions; |
5 | | -import pl.wavesoftware.gasper.internal.Executor; |
6 | 4 | import pl.wavesoftware.gasper.internal.HttpEndpoint; |
7 | | -import pl.wavesoftware.gasper.internal.Settings; |
8 | | -import pl.wavesoftware.gasper.internal.maven.MavenResolver; |
9 | 5 |
|
10 | | -import java.net.ServerSocket; |
11 | 6 | import java.nio.file.Path; |
12 | | -import java.nio.file.Paths; |
13 | | -import java.util.ArrayList; |
14 | | -import java.util.Collections; |
15 | | -import java.util.LinkedHashMap; |
16 | | -import java.util.List; |
17 | | -import java.util.Map; |
18 | 7 | import java.util.function.Function; |
19 | 8 |
|
20 | | -import static pl.wavesoftware.eid.utils.EidPreconditions.tryToExecute; |
21 | | - |
22 | 9 | /** |
23 | 10 | * @author Krzysztof Suszyński <krzysztof.suszynski@wavesoftware.pl> |
24 | | - * @since 2016-03-05 |
| 11 | + * @since 2016-03-06 |
25 | 12 | */ |
26 | | -public final class GasperBuilder extends Gasper.RunnerCreator { |
27 | | - |
28 | | - private String packaging = MavenResolver.DEFAULT_PACKAGING; |
29 | | - private String classifier = ""; |
30 | | - private Map<String, String> systemProperties = new LinkedHashMap<>(); |
31 | | - private List<String> jvmOptions = new ArrayList<>(); |
32 | | - private Map<String, String> environment = new LinkedHashMap<>(); |
33 | | - private String systemPropertyForPort; |
34 | | - private Integer port; |
35 | | - private boolean inheritIO = false; |
36 | | - private String context = Gasper.DEFAULT_CONTEXT; |
37 | | - private int portAvailableMaxTime = Gasper.DEFAULT_PORT_AVAILABLE_MAX_SECONDS; |
38 | | - private int deploymentMaxTime = Gasper.DEFAULT_DEPLOYMENT_MAX_SECONDS; |
39 | | - private Function<HttpEndpoint, Boolean> contextChecker = Executor.DEFAULT_CONTEXT_CHECKER; |
40 | | - private Path pomfile = Paths.get(MavenResolver.DEFAULT_POM); |
41 | | - private Level level = Level.INFO; |
42 | | - |
43 | | - protected GasperBuilder() {} |
44 | | - |
45 | | - public GasperBuilder withArtifactPackaging(String packaging) { |
46 | | - this.packaging = packaging; |
47 | | - return this; |
48 | | - } |
49 | | - |
50 | | - public GasperBuilder withArtifactClassifier(String classifier) { |
51 | | - this.classifier = classifier; |
52 | | - return this; |
53 | | - } |
54 | | - |
55 | | - public GasperBuilder withEnvironmentVariable(String key, String value) { |
56 | | - environment.put(key, value); |
57 | | - return this; |
58 | | - } |
59 | | - |
60 | | - public GasperBuilder withSystemProperty(String key, String value) { |
61 | | - systemProperties.put(key, value); |
62 | | - return this; |
63 | | - } |
64 | | - |
65 | | - public GasperBuilder withJVMOptions(String... options) { |
66 | | - Collections.addAll(jvmOptions, options); |
67 | | - return this; |
68 | | - } |
69 | | - |
70 | | - public GasperBuilder withPort(int port) { |
71 | | - this.port = port; |
72 | | - return this; |
73 | | - } |
74 | | - |
75 | | - public GasperBuilder usingSystemPropertyForPort(String systemPropertyForPort) { |
76 | | - this.systemPropertyForPort = systemPropertyForPort; |
77 | | - return this; |
78 | | - } |
79 | | - |
80 | | - public GasperBuilder usingPomFile(Path pomfile) { |
81 | | - this.pomfile = pomfile; |
82 | | - return this; |
83 | | - } |
84 | | - |
85 | | - public GasperBuilder withServerLoggingOnConsole() { |
86 | | - return withServerLoggingOnConsole(true); |
87 | | - } |
88 | | - |
89 | | - public GasperBuilder withServerLoggingOnConsole(boolean inheritIO) { |
90 | | - this.inheritIO = inheritIO; |
91 | | - return this; |
92 | | - } |
93 | | - |
94 | | - public GasperBuilder withMaxStartupTime(int seconds) { |
95 | | - this.portAvailableMaxTime = seconds; |
96 | | - return this; |
97 | | - } |
98 | | - |
99 | | - public GasperBuilder withMaxDeploymentTime(int seconds) { |
100 | | - this.deploymentMaxTime = seconds; |
101 | | - return this; |
102 | | - } |
103 | | - |
104 | | - public GasperBuilder waitForWebContext(String context) { |
105 | | - this.context = context; |
106 | | - return this; |
107 | | - } |
108 | | - |
109 | | - public GasperBuilder usingWebContextChecker(Function<HttpEndpoint, Boolean> contextChecker) { |
110 | | - this.contextChecker = contextChecker; |
111 | | - return this; |
112 | | - } |
113 | | - |
114 | | - public GasperBuilder silentGasperMessages() { |
115 | | - usingLogLevel(Level.WARN); |
116 | | - return this; |
117 | | - } |
118 | | - |
119 | | - public GasperBuilder usingLogLevel(Level level) { |
120 | | - this.level = level; |
121 | | - return this; |
122 | | - } |
123 | | - |
124 | | - public Gasper build() { |
125 | | - if (port == null) { |
126 | | - port = findNotBindedPort(); |
127 | | - } |
128 | | - if (systemPropertyForPort != null) { |
129 | | - withSystemProperty(systemPropertyForPort, port.toString()); |
130 | | - } |
131 | | - Settings settings = new Settings( |
132 | | - packaging, classifier, port, |
133 | | - systemProperties, jvmOptions, environment, |
134 | | - inheritIO, context, contextChecker, |
135 | | - portAvailableMaxTime, deploymentMaxTime, |
136 | | - pomfile, level |
137 | | - ); |
138 | | - return create(settings); |
139 | | - } |
140 | | - |
141 | | - private static Integer findNotBindedPort() { |
142 | | - return tryToExecute((EidPreconditions.UnsafeSupplier<Integer>) () -> { |
143 | | - try (ServerSocket socket = new ServerSocket(0)) { |
144 | | - return socket.getLocalPort(); |
145 | | - } |
146 | | - }, "20160305:202934"); |
147 | | - |
148 | | - } |
| 13 | +public interface GasperBuilder extends Gasper.RunnerCreator { |
| 14 | + GasperBuilder withArtifactPackaging(String packaging); |
| 15 | + |
| 16 | + GasperBuilder withArtifactClassifier(String classifier); |
| 17 | + |
| 18 | + GasperBuilder withEnvironmentVariable(String key, String value); |
| 19 | + |
| 20 | + GasperBuilder withSystemProperty(String key, String value); |
| 21 | + |
| 22 | + GasperBuilder withJVMOptions(String... options); |
| 23 | + |
| 24 | + GasperBuilder withPort(int port); |
| 25 | + |
| 26 | + GasperBuilder usingSystemPropertyForPort(String systemPropertyForPort); |
| 27 | + |
| 28 | + GasperBuilder usingPomFile(Path pomfile); |
| 29 | + |
| 30 | + GasperBuilder withServerLoggingOnConsole(); |
| 31 | + |
| 32 | + GasperBuilder withServerLoggingOnConsole(boolean inheritIO); |
| 33 | + |
| 34 | + GasperBuilder withMaxStartupTime(int seconds); |
| 35 | + |
| 36 | + GasperBuilder withMaxDeploymentTime(int seconds); |
| 37 | + |
| 38 | + GasperBuilder waitForWebContext(String context); |
| 39 | + |
| 40 | + GasperBuilder usingWebContextChecker(Function<HttpEndpoint, Boolean> contextChecker); |
| 41 | + |
| 42 | + GasperBuilder silentGasperMessages(); |
| 43 | + |
| 44 | + GasperBuilder usingLogLevel(Level level); |
| 45 | + |
| 46 | + Gasper build(); |
149 | 47 | } |
0 commit comments