├── .github
└── workflows
│ ├── build.yml
│ └── maven-publish.yml
├── .gitignore
├── LICENSE
├── README.MD
├── pom.xml
└── src
├── main
└── java
│ └── np
│ └── com
│ └── madanpokharel
│ └── embed
│ └── nats
│ ├── EmbeddedNatsConfig.java
│ ├── EmbeddedNatsServer.java
│ ├── NatsPackageResolver.java
│ ├── NatsProcess.java
│ ├── NatsServerConfig.java
│ ├── NatsServerStarter.java
│ ├── NatsStarter.java
│ ├── NatsStreamingVersion.java
│ ├── NatsVersion.java
│ ├── RunningNatsProcess.java
│ └── ServerType.java
└── test
├── java
└── np
│ └── com
│ └── madanpokharel
│ └── embed
│ └── nats
│ ├── EmbeddedNatsStreamingTest.java
│ └── EmbeddedNatsTest.java
└── resources
└── logback.xml
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: build
2 |
3 | on:
4 | push:
5 | branches: [ master ]
6 | paths: [ '**.java', '.github/workflows/build.yml', 'pom.xml' ]
7 | pull_request:
8 | branches: [ master ]
9 | workflow_dispatch:
10 |
11 | jobs:
12 | build:
13 | runs-on: ${{ matrix.os }}
14 | strategy:
15 | matrix:
16 | java: [ '11' ,'17', '21' ]
17 | os: [ 'ubuntu-latest', 'macos-latest', 'windows-latest' ]
18 | permissions:
19 | contents: read
20 | packages: write
21 | name: Java ${{ matrix.Java }} (${{ matrix.os }})
22 | steps:
23 | - uses: actions/checkout@v4
24 | - name: Setup java
25 | uses: actions/setup-java@v4
26 | with:
27 | distribution: 'zulu'
28 | java-version: ${{ matrix.java }}
29 | - run: mvn test
30 |
--------------------------------------------------------------------------------
/.github/workflows/maven-publish.yml:
--------------------------------------------------------------------------------
1 | name: Manual Maven Deploy Package
2 |
3 | on:
4 | workflow_dispatch:
5 | inputs:
6 | VERSION:
7 | description: 'New Version number'
8 | required: true
9 |
10 | jobs:
11 | build:
12 | runs-on: ubuntu-latest
13 | permissions:
14 | contents: read
15 | packages: write
16 | env:
17 | artifact_name: nats-embedded
18 |
19 | steps:
20 | - uses: actions/checkout@v4
21 | - name: Set up JDK 8 for deploy to OSSRH
22 | uses: actions/setup-java@v4
23 | with:
24 | distribution: 'temurin'
25 | java-version: '8'
26 | server-id: ossrh
27 | server-username: MAVEN_USERNAME
28 | server-password: MAVEN_CENTRAL_TOKEN
29 | gpg-private-key: ${{ secrets.GPG_SIGNING_KEY }}
30 | gpg-passphrase: MAVEN_GPG_PASSPHRASE
31 |
32 | - name: Build with Maven
33 | run: mvn -B package --file pom.xml
34 |
35 | - name: Update package version
36 | run: mvn versions:set -DnewVersion=${{ github.event.inputs.VERSION }}
37 |
38 | - name: Publish to Apache Maven Central
39 | run: mvn deploy -PmavenRelease
40 | env:
41 | MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
42 | MAVEN_CENTRAL_TOKEN: ${{ secrets.OSSRH_PASSWORD }}
43 | MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
44 |
45 | - name: Set up JDK 8 for deploy to github packages
46 | uses: actions/setup-java@v4
47 | with:
48 | distribution: 'temurin'
49 | java-version: '8'
50 | server-id: github
51 |
52 | - name: Publish to GitHub Packages Apache Maven
53 | run: mvn deploy -PgithubRelease
54 | env:
55 | GITHUB_TOKEN: ${{ secrets.NATS_GITHUB_TOKEN }}
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | # Created by https://www.gitignore.io/api/java,maven,intellij+all
3 | # Edit at https://www.gitignore.io/?templates=java,maven,intellij+all
4 |
5 | ### Intellij+all ###
6 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
7 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
8 |
9 | # User-specific stuff
10 | .idea/**/workspace.xml
11 | .idea/**/tasks.xml
12 | .idea/**/usage.statistics.xml
13 | .idea/**/dictionaries
14 | .idea/**/shelf
15 |
16 | # Generated files
17 | .idea/**/contentModel.xml
18 |
19 | # Sensitive or high-churn files
20 | .idea/**/dataSources/
21 | .idea/**/dataSources.ids
22 | .idea/**/dataSources.local.xml
23 | .idea/**/sqlDataSources.xml
24 | .idea/**/dynamic.xml
25 | .idea/**/uiDesigner.xml
26 | .idea/**/dbnavigator.xml
27 |
28 | # Gradle
29 | .idea/**/gradle.xml
30 | .idea/**/libraries
31 |
32 | # Gradle and Maven with auto-import
33 | # When using Gradle or Maven with auto-import, you should exclude module files,
34 | # since they will be recreated, and may cause churn. Uncomment if using
35 | # auto-import.
36 | # .idea/modules.xml
37 | # .idea/*.iml
38 | # .idea/modules
39 | # *.iml
40 | # *.ipr
41 |
42 | # CMake
43 | cmake-build-*/
44 |
45 |
46 | # File-based project format
47 | *.iws
48 |
49 | # IntelliJ
50 | out/
51 |
52 | # mpeltonen/sbt-idea plugin
53 | .idea_modules/
54 |
55 | # JIRA plugin
56 | atlassian-ide-plugin.xml
57 |
58 | # Cursive Clojure plugin
59 | .idea/replstate.xml
60 |
61 | # Crashlytics plugin (for Android Studio and IntelliJ)
62 | com_crashlytics_export_strings.xml
63 | crashlytics.properties
64 | crashlytics-build.properties
65 | fabric.properties
66 |
67 | # Editor-based Rest Client
68 | .idea/httpRequests
69 |
70 | # Android studio 3.1+ serialized cache file
71 | .idea/caches/build_file_checksums.ser
72 |
73 | ### Intellij+all Patch ###
74 | # Ignores the whole .idea folder and all .iml files
75 | # See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360
76 |
77 | .idea/
78 |
79 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023
80 |
81 | *.iml
82 | modules.xml
83 | .idea/misc.xml
84 | *.ipr
85 |
86 | # Sonarlint plugin
87 | .idea/sonarlint
88 |
89 | ### Java ###
90 | # Compiled class file
91 | *.class
92 |
93 | # Log file
94 | *.log
95 |
96 | # BlueJ files
97 | *.ctxt
98 |
99 | # Mobile Tools for Java (J2ME)
100 | .mtj.tmp/
101 |
102 | # Package Files #
103 | *.jar
104 | *.war
105 | *.nar
106 | *.ear
107 | *.zip
108 | *.tar.gz
109 | *.rar
110 |
111 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
112 | hs_err_pid*
113 |
114 | ### Maven ###
115 | target/
116 | pom.xml.tag
117 | pom.xml.releaseBackup
118 | pom.xml.versionsBackup
119 | pom.xml.next
120 | release.properties
121 | dependency-reduced-pom.xml
122 | buildNumber.properties
123 | .mvn/timing.properties
124 | .mvn/wrapper/maven-wrapper.jar
125 | .flattened-pom.xml
126 |
127 | # End of https://www.gitignore.io/api/java,maven,intellij+all
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
--------------------------------------------------------------------------------
/README.MD:
--------------------------------------------------------------------------------
1 | 
2 | [](https://opensource.org/licenses/Apache-2.0)
3 | 
4 | ### Embedded Nats and Streaming Sever
5 |
6 | Embedded Nats and Nats Streaming server for integration tests.
7 | Tested with Windows, Mac OSX and Linux
8 |
9 | ## Usage
10 | Add the following dependency to project
11 | For Maven:
12 |
13 | ```xml
14 |
EmbeddedNatsConfig class.
23 | * 24 | * @author Madan Pokharel 25 | * @version $Id: $Id 26 | */ 27 | public final class EmbeddedNatsConfig { 28 | final String downloadPath; 29 | final String downloadUserAgent; 30 | final String artifactStorePath; 31 | final String extractDirectory; 32 | private final NatsServerConfig serverConfig; 33 | 34 | private EmbeddedNatsConfig(String downloadPath, 35 | String downloadUserAgent, 36 | String artifactStorePath, 37 | String extractDirectory, 38 | NatsServerConfig serverConfig) { 39 | this.downloadPath = downloadPath; 40 | this.downloadUserAgent = downloadUserAgent; 41 | this.serverConfig = serverConfig; 42 | this.artifactStorePath = artifactStorePath; 43 | this.extractDirectory = extractDirectory; 44 | } 45 | 46 | /** 47 | *defaultNatsServerConfig.
48 | * 49 | * @return a {@link np.com.madanpokharel.embed.nats.EmbeddedNatsConfig} object. 50 | */ 51 | public static EmbeddedNatsConfig defaultNatsServerConfig() { 52 | return new EmbeddedNatsConfig.Builder() 53 | .withNatsServerConfig( 54 | new NatsServerConfig.Builder() 55 | .withServerType(ServerType.NATS) 56 | .withRandomPort() 57 | .build() 58 | ) 59 | .build(); 60 | } 61 | 62 | /** 63 | *defaultNatsStreamingConfig.
64 | * 65 | * @return a {@link np.com.madanpokharel.embed.nats.EmbeddedNatsConfig} object. 66 | */ 67 | public static EmbeddedNatsConfig defaultNatsStreamingConfig() { 68 | return new EmbeddedNatsConfig.Builder() 69 | .withNatsServerConfig( 70 | new NatsServerConfig.Builder() 71 | .withServerType(ServerType.NATS_STREAMING) 72 | .withRandomPort() 73 | .build() 74 | ) 75 | .build(); 76 | } 77 | 78 | 79 | /** 80 | *Getter for the field serverConfig
.
EmbeddedNatsServer class.
51 | * 52 | * @author Madan Pokharel 53 | * @version $Id: $Id 54 | */ 55 | public final class EmbeddedNatsServer { 56 | private final EmbeddedNatsConfig config; 57 | 58 | private TransitionWalker.ReachedStateConstructor for EmbeddedNatsServer.
62 | * 63 | * @param config a {@link np.com.madanpokharel.embed.nats.EmbeddedNatsConfig} object. 64 | */ 65 | public EmbeddedNatsServer(EmbeddedNatsConfig config) { 66 | Objects.requireNonNull(config, "config cannot be null"); 67 | this.config = config; 68 | } 69 | 70 | 71 | /** 72 | *startServer.
73 | * 74 | * @throws java.lang.Exception if any. 75 | */ 76 | public void startServer() throws Exception { 77 | runningProcess = transitions().walker().initState(StateID.of(NatsProcess.class)); 78 | } 79 | 80 | 81 | /** 82 | *isServerRunning.
83 | * 84 | * @return a boolean. 85 | */ 86 | public boolean isServerRunning() { 87 | return !Objects.isNull(runningProcess) && runningProcess.current().isAlive(); 88 | } 89 | 90 | 91 | /** 92 | *stopServer.
93 | */ 94 | public void stopServer() { 95 | runningProcess.close(); 96 | } 97 | 98 | /** 99 | *getRunningPort.
100 | * 101 | * @return an int. 102 | */ 103 | public int getRunningPort() { 104 | return config.getServerConfig().getPort(); 105 | } 106 | 107 | /** 108 | *getRunningHost.
109 | * 110 | * @return a {@link java.lang.String} object. 111 | */ 112 | public String getRunningHost() { 113 | return config.getServerConfig().getHost(); 114 | } 115 | 116 | /** 117 | *getNatsUrl.
118 | * 119 | * @return a {@link java.lang.String} object. 120 | */ 121 | public String getNatsUrl() { 122 | return "nats://" + getRunningHost() + ":" + getRunningPort(); 123 | } 124 | 125 | /** 126 | *getClusterId.
127 | * 128 | * @return a {@link java.lang.String} object. 129 | */ 130 | public String getClusterId() { 131 | return this.config.getServerConfig().getClusterId(); 132 | } 133 | 134 | /** 135 | *getServerConfig.
136 | * 137 | * @return a {@link np.com.madanpokharel.embed.nats.NatsServerConfig} object. 138 | */ 139 | public NatsServerConfig getServerConfig() { 140 | return this.config.getServerConfig(); 141 | } 142 | 143 | 144 | private Transitions transitions() { 145 | NatsPackageResolver resolver = new NatsPackageResolver(config.getServerConfig().getServerType()); 146 | Transitions transitions = Transitions.from( 147 | InitTempDirectory.withPlatformTempRandomSubDir(), 148 | 149 | Start.to(EmbeddedNatsConfig.class).providedBy(() -> config), 150 | 151 | Derive.given(EmbeddedNatsConfig.class) 152 | .state(PersistentDir.class) 153 | .deriveBy(embeddedNatsConfig -> PersistentDir.inUserHome(embeddedNatsConfig.artifactStorePath).mapToUncheckedException(RuntimeException::new).get()), 154 | 155 | Derive.given(TempDir.class) 156 | .state(ProcessWorkingDir.class) 157 | .with(Directories.deleteOnTearDown( 158 | TempDir.createDirectoryWith("workingDir"), 159 | ProcessWorkingDir::of) 160 | ), 161 | 162 | Derive.given(PersistentDir.class) 163 | .state(DownloadCache.class) 164 | .deriveBy(storeBaseDir -> new LocalDownloadCache(storeBaseDir.value().resolve("archives"))) 165 | .withTransitionLabel("downloadCache"), 166 | 167 | 168 | Derive.given(PersistentDir.class) 169 | .state(ExtractedFileSetStore.class) 170 | .deriveBy(baseDir -> new ContentHashExtractedFileSetStore(baseDir.value().resolve(config.extractDirectory))) 171 | .withTransitionLabel("extractedFileSetStore"), 172 | 173 | Start.to(Name.class).initializedWith(Name.of("nats")).withTransitionLabel("create Name"), 174 | 175 | Start.to(SupportConfig.class).initializedWith( 176 | SupportConfig.builder().name("nats-server") 177 | .supportUrl("https://github.com/madansp/nats-embedded") 178 | .messageOnException((clazz, ex) -> "Open a bug report at https://github.com/madansp/nats-embedded") 179 | .build() 180 | ).withTransitionLabel("create default"), 181 | 182 | Start.to(ProcessConfig.class).initializedWith(ProcessConfig.defaults()).withTransitionLabel("create default"), 183 | Start.to(ProcessEnv.class).initializedWith(ProcessEnv.of(Collections.emptyMap())).withTransitionLabel("create empty env"), 184 | 185 | Start.to(Version.class).initializedWith(config.getServerConfig().version()).withTransitionLabel("set version"), 186 | 187 | Derive.given(Name.class).state(ProcessOutput.class) 188 | .deriveBy(name -> 189 | ProcessOutput.builder() 190 | .output(Processors.silent()) 191 | .error(Processors.namedConsole("[" + name.value() + " output]")) 192 | .commands(Processors.console()) 193 | .build() 194 | ) 195 | .withTransitionLabel("create named console"), 196 | 197 | Start.to(ProgressListener.class) 198 | .providedBy(StandardConsoleProgressListener::new) 199 | .withTransitionLabel("progressListener"), 200 | 201 | Start.to(ProcessArguments.class).initializedWith(ProcessArguments.of(config.getServerConfig().getConfigList())) 202 | .withTransitionLabel("create arguments"), 203 | 204 | Start.to(ServerType.class).providedBy(() -> config.getServerConfig().getServerType()), 205 | 206 | Derive.given(Version.class).state(Distribution.class) 207 | .deriveBy(version -> Distribution.detectFor(CommonOS.list(), version)) 208 | .withTransitionLabel("version + platform"), 209 | 210 | Start.to(Platform.class).providedBy(() -> Platform.detect(CommonOS.list())), 211 | 212 | PackageOfDistribution.with(dist -> Package.builder() 213 | .archiveType(ArchiveType.ZIP) 214 | .fileSet(resolver.getExecutableFileSet(dist)) 215 | .url(config.downloadPath + resolver.getPath(dist)) 216 | .build()), 217 | 218 | DownloadPackage.builder() 219 | .downloadConfig(DownloadConfig.builder().userAgent(config.downloadUserAgent).build()) 220 | .build(), 221 | 222 | ExtractPackage.withDefaults().withExtractedFileSetStore(StateID.of(ExtractedFileSetStore.class)), 223 | 224 | NatsStarter.defaults() 225 | ); 226 | 227 | return transitions; 228 | } 229 | 230 | } 231 | -------------------------------------------------------------------------------- /src/main/java/np/com/madanpokharel/embed/nats/NatsPackageResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Madan Pokharel 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 np.com.madanpokharel.embed.nats; 18 | 19 | import de.flapdoodle.embed.process.config.store.FileSet; 20 | import de.flapdoodle.embed.process.config.store.FileType; 21 | import de.flapdoodle.embed.process.distribution.Distribution; 22 | import de.flapdoodle.os.BitSize; 23 | import de.flapdoodle.os.Platform; 24 | 25 | final class NatsPackageResolver { 26 | private final ServerType serverType; 27 | 28 | /** 29 | *Constructor for NatsPackageResolver.
30 | * 31 | * @param serverType a {@link np.com.madanpokharel.embed.nats.ServerType} object. 32 | */ 33 | NatsPackageResolver(ServerType serverType) { 34 | this.serverType = serverType; 35 | } 36 | 37 | /** 38 | * {@inheritDoc} 39 | * 40 | * @param distribution a {@link de.flapdoodle.embed.process.distribution.Distribution} object 41 | * @return a {@link java.lang.String} object 42 | */ 43 | public String getPath(Distribution distribution) { 44 | String version = distribution.version().asInDownloadPath(); 45 | String arch = getArch(distribution.platform().architecture().bitSize()); 46 | String platform = getPlatform(distribution.platform()); 47 | return version + "/" + serverType.getServerName() + "-" + version + '-' + platform + '-' + arch + ".zip"; 48 | } 49 | 50 | private String getPlatform(Platform platform) { 51 | switch (platform.operatingSystem().type()) { 52 | case OS_X: 53 | return "darwin"; 54 | case Linux: 55 | return "linux"; 56 | case Windows: 57 | return "windows"; 58 | default: 59 | throw new IllegalArgumentException("Unknown platform " + platform); 60 | } 61 | } 62 | 63 | private String getArch(BitSize bitSize) { 64 | String arch; 65 | if (System.getProperty("os.arch").startsWith("arm")) { 66 | switch (bitSize) { 67 | case B32: 68 | arch = "arm7"; 69 | break; 70 | case B64: 71 | arch = "arm64"; 72 | break; 73 | default: 74 | arch = "arm6"; 75 | } 76 | } else { 77 | switch (bitSize) { 78 | case B32: 79 | arch = "386"; 80 | break; 81 | case B64: 82 | arch = "amd64"; 83 | break; 84 | default: 85 | throw new IllegalArgumentException("Unknown bit size " + bitSize); 86 | } 87 | } 88 | 89 | return arch; 90 | } 91 | 92 | FileSet getExecutableFileSet(Distribution distribution) { 93 | String executableName = serverType.getServerName(); 94 | switch (distribution.platform().operatingSystem().type()) { 95 | case Linux: 96 | case OS_X: 97 | break; 98 | case Windows: 99 | executableName += ".exe"; 100 | break; 101 | default: 102 | throw new IllegalArgumentException( 103 | "Unknown platform " + distribution.platform()); 104 | } 105 | return FileSet.builder() 106 | .addEntry(FileType.Executable, executableName) 107 | .build(); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/main/java/np/com/madanpokharel/embed/nats/NatsProcess.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Madan Pokharel 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 | package np.com.madanpokharel.embed.nats; 17 | 18 | import de.flapdoodle.embed.process.config.SupportConfig; 19 | import de.flapdoodle.embed.process.io.StreamProcessor; 20 | import de.flapdoodle.embed.process.runtime.ProcessControl; 21 | import de.flapdoodle.embed.process.types.RunningProcessFactory; 22 | import de.flapdoodle.os.Platform; 23 | 24 | import java.nio.file.Path; 25 | 26 | /** 27 | *NatsProcess class.
28 | * 29 | * @author madanpokharel 30 | * @version $Id: $Id 31 | */ 32 | public class NatsProcess extends RunningNatsProcess { 33 | 34 | /** 35 | *Constructor for NatsProcess.
36 | * 37 | * @param process a {@link de.flapdoodle.embed.process.runtime.ProcessControl} object 38 | * @param pidFile a {@link java.nio.file.Path} object 39 | * @param onStop a {@link java.lang.Runnable} object 40 | * @param supportConfig a {@link de.flapdoodle.embed.process.config.SupportConfig} object 41 | * @param platform a {@link de.flapdoodle.os.Platform} object 42 | * @param commandOutput a {@link de.flapdoodle.embed.process.io.StreamProcessor} object 43 | * @param natsProcessId a int 44 | * @param serverType a {@link np.com.madanpokharel.embed.nats.ServerType} object 45 | */ 46 | public NatsProcess( 47 | ProcessControl process, 48 | Path pidFile, 49 | Runnable onStop, 50 | SupportConfig supportConfig, 51 | Platform platform, 52 | StreamProcessor commandOutput, 53 | int natsProcessId, 54 | ServerType serverType 55 | ) { 56 | super("natsProcess", process, pidFile, onStop, supportConfig, platform, commandOutput, natsProcessId, serverType); 57 | } 58 | 59 | /** 60 | *factory.
61 | * 62 | * @param supportConfig a {@link de.flapdoodle.embed.process.config.SupportConfig} object 63 | * @param platform a {@link de.flapdoodle.os.Platform} object 64 | * @param serverType a {@link np.com.madanpokharel.embed.nats.ServerType} object 65 | * @return a {@link de.flapdoodle.embed.process.types.RunningProcessFactory} object 66 | */ 67 | public static RunningProcessFactoryNatsServerConfig class.
31 | * 32 | * @author Madan Pokharel 33 | * @version $Id: $Id 34 | */ 35 | public final class NatsServerConfig { 36 | 37 | private final Version version; 38 | private final int port; 39 | private final String host; 40 | private final String clusterId; 41 | private final ServerType serverType; 42 | private final MapConstructor for NatsServerConfig.
47 | * 48 | * @param version a {@link de.flapdoodle.embed.process.distribution.Version} object. 49 | * @param port an int. 50 | * @param host a {@link java.lang.String} object. 51 | * @param clusterId a {@link java.lang.String} object. 52 | * @param serverType a {@link np.com.madanpokharel.embed.nats.ServerType} object. 53 | * @param configParams a {@link java.util.Map} object. 54 | */ 55 | public NatsServerConfig(Version version, 56 | int port, 57 | String host, 58 | String clusterId, 59 | ServerType serverType, 60 | MapGetter for the field port
.
getConfigList.
90 | * 91 | * @return a {@link java.util.List} object. 92 | */ 93 | ListGetter for the field host
.
Getter for the field clusterId
.
Getter for the field serverType
.
Abstract NatsServerStarter class.
36 | * 37 | * @author madanpokharel 38 | * @version $Id: $Id 39 | */ 40 | public abstract class NatsServerStarterprocessExecutable.
43 | * 44 | * @return a {@link de.flapdoodle.reverse.StateID} object 45 | */ 46 | public StateIDprocessWorkingDir.
52 | * 53 | * @return a {@link de.flapdoodle.reverse.StateID} object 54 | */ 55 | public StateIDprocessConfig.
61 | * 62 | * @return a {@link de.flapdoodle.reverse.StateID} object 63 | */ 64 | public StateIDprocessEnv.
70 | * 71 | * @return a {@link de.flapdoodle.reverse.StateID} object 72 | */ 73 | public StateIDarguments.
79 | * 80 | * @return a {@link de.flapdoodle.reverse.StateID} object 81 | */ 82 | public StateIDprocessOutput.
88 | * 89 | * @return a {@link de.flapdoodle.reverse.StateID} object 90 | */ 91 | public StateIDsupportConfig.
97 | * 98 | * @return a {@link de.flapdoodle.reverse.StateID} object 99 | */ 100 | public StateIDplatform.
106 | * 107 | * @return a {@link de.flapdoodle.reverse.StateID} object 108 | */ 109 | public StateIDserverType.
115 | * 116 | * @return a {@link de.flapdoodle.reverse.StateID} object 117 | */ 118 | public StateIDfactory.
141 | * 142 | * @param supportConfig a {@link de.flapdoodle.embed.process.config.SupportConfig} object 143 | * @param platform a {@link de.flapdoodle.os.Platform} object 144 | * @param serverType a {@link np.com.madanpokharel.embed.nats.ServerType} object 145 | * @return a {@link de.flapdoodle.embed.process.types.RunningProcessFactory} object 146 | */ 147 | protected abstract RunningProcessFactoryNatsStarter class.
25 | * 26 | * @author madanpokharel 27 | * @version $Id: $Id 28 | */ 29 | public class NatsStarter extends NatsServerStarterNatsStreamingVersion class.
23 | * 24 | * @author Madan Pokharel 25 | * @version $Id: $Id 26 | */ 27 | public class NatsStreamingVersion implements Version { 28 | /** 29 | * ConstantLATEST
30 | */
31 | public static final NatsStreamingVersion LATEST = new NatsStreamingVersion("v0.25.6");
32 |
33 | /**
34 | * Constant V0_25_6
35 | */
36 | public static final NatsStreamingVersion V0_25_6 = new NatsStreamingVersion("v0.25.6");
37 |
38 | /**
39 | * Constant V0_25_5
40 | */
41 | public static final NatsStreamingVersion V0_25_5 = new NatsStreamingVersion("v0.25.5");
42 |
43 | /**
44 | * Constant V0_25_4
45 | */
46 | public static final NatsStreamingVersion V0_25_4 = new NatsStreamingVersion("v0.25.4");
47 |
48 | /**
49 | * Constant V0_25_3
50 | */
51 | public static final NatsStreamingVersion V0_25_3 = new NatsStreamingVersion("v0.25.3");
52 |
53 | /**
54 | * Constant V0_25_2
55 | */
56 | public static final NatsStreamingVersion V0_25_2 = new NatsStreamingVersion("v0.25.2");
57 |
58 | /**
59 | * Constant V0_24_6
60 | */
61 | public static final NatsStreamingVersion V0_24_6 = new NatsStreamingVersion("v0.24.6");
62 |
63 | /**
64 | * Constant V0_24_5
65 | */
66 | public static final NatsStreamingVersion V0_24_5 = new NatsStreamingVersion("v0.24.5");
67 |
68 | /**
69 | * Constant V0_24_4
70 | */
71 | public static final NatsStreamingVersion V0_24_4 = new NatsStreamingVersion("v0.24.4");
72 |
73 | /**
74 | * Constant V0_24_3
75 | */
76 | public static final NatsStreamingVersion V0_24_3 = new NatsStreamingVersion("v0.24.3");
77 |
78 | /**
79 | * Constant V0_24_2
80 | */
81 | public static final NatsStreamingVersion V0_24_2 = new NatsStreamingVersion("v0.24.2");
82 |
83 | /**
84 | * Constant V0_24_1
85 | */
86 | public static final NatsStreamingVersion V0_24_1 = new NatsStreamingVersion("v0.24.1");
87 |
88 | /**
89 | * Constant V0_24_0
90 | */
91 | public static final NatsStreamingVersion V0_24_0 = new NatsStreamingVersion("v0.24.0");
92 |
93 | /**
94 | * Constant V0_23_2
95 | */
96 | public static final NatsStreamingVersion V0_23_2 = new NatsStreamingVersion("v0.23.2");
97 |
98 | /**
99 | * Constant V0_23_1
100 | */
101 | public static final NatsStreamingVersion V0_23_1 = new NatsStreamingVersion("v0.23.1");
102 |
103 | /**
104 | * Constant V0_23_0
105 | */
106 | public static final NatsStreamingVersion V0_23_0 = new NatsStreamingVersion("v0.23.0");
107 |
108 | /**
109 | * Constant V0_22_1
110 | */
111 | public static final NatsStreamingVersion V0_22_1 = new NatsStreamingVersion("v0.21.1");
112 |
113 | /**
114 | * Constant V0_22_0
115 | */
116 | public static final NatsStreamingVersion V0_22_0 = new NatsStreamingVersion("v0.22.0");
117 |
118 | /**
119 | * Constant V0_21_2
120 | */
121 | public static final NatsStreamingVersion V0_21_2 = new NatsStreamingVersion("v0.21.2");
122 |
123 | /**
124 | * Constant V0_21_1
125 | */
126 | public static final NatsStreamingVersion V0_21_1 = new NatsStreamingVersion("v0.21.1");
127 |
128 | /**
129 | * Constant V0_21_0
130 | */
131 | public static final NatsStreamingVersion V0_21_0 = new NatsStreamingVersion("v0.21.0");
132 |
133 | /**
134 | * Constant V0_20_0
135 | */
136 | public static final NatsStreamingVersion V0_20_0 = new NatsStreamingVersion("v0.20.0");
137 |
138 | /**
139 | * Constant V0_19_0
140 | */
141 | public static final NatsStreamingVersion V0_19_0 = new NatsStreamingVersion("v0.19.0");
142 |
143 | /**
144 | * Constant V0_18_0
145 | */
146 | public static final NatsStreamingVersion V0_18_0 = new NatsStreamingVersion("v0.18.0");
147 |
148 |
149 | /**
150 | * Constant V0_17_0
151 | */
152 | public static final NatsStreamingVersion V0_17_0 = new NatsStreamingVersion("v0.17.0");
153 |
154 | /**
155 | * Constant V0_16_2
156 | */
157 | public static final NatsStreamingVersion V0_16_2 = new NatsStreamingVersion("v0.16.2");
158 | /**
159 | * Constant V0_16_0
160 | */
161 | public static final NatsStreamingVersion V0_16_0 = new NatsStreamingVersion("v0.16.0");
162 | /**
163 | * Constant V0_15_1
164 | */
165 | public static final NatsStreamingVersion V0_15_1 = new NatsStreamingVersion("v0.15.1");
166 | /**
167 | * Constant V0_15_0
168 | */
169 | public static final NatsStreamingVersion V0_15_0 = new NatsStreamingVersion("v0.15.0");
170 | /**
171 | * Constant V0_14_3
172 | */
173 | public static final NatsStreamingVersion V0_14_3 = new NatsStreamingVersion("v0.14.3");
174 |
175 | private final String version;
176 |
177 | /**
178 | * Constructor for NatsStreamingVersion.
179 | * 180 | * @param version a {@link java.lang.String} object. 181 | */ 182 | public NatsStreamingVersion(String version) { 183 | this.version = version; 184 | } 185 | 186 | /** {@inheritDoc} */ 187 | @Override 188 | public String asInDownloadPath() { 189 | return version; 190 | } 191 | 192 | /** {@inheritDoc} */ 193 | @Override 194 | public String toString() { 195 | return version; 196 | } 197 | 198 | } 199 | -------------------------------------------------------------------------------- /src/main/java/np/com/madanpokharel/embed/nats/NatsVersion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Madan Pokharel 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 np.com.madanpokharel.embed.nats; 18 | 19 | import de.flapdoodle.embed.process.distribution.Version; 20 | 21 | /** 22 | *NatsVersion class.
23 | * 24 | * @author Madan Pokharel 25 | * @version $Id: $Id 26 | */ 27 | public class NatsVersion implements Version { 28 | /** 29 | * ConstantLATEST
30 | */
31 | public static final NatsVersion LATEST = new NatsVersion("v2.10.18");
32 |
33 | /**
34 | * Constant V2_10_18
35 | */
36 | public static final NatsVersion V2_10_18 = new NatsVersion("v2.10.18");
37 |
38 | /**
39 | * Constant V2_10_17
40 | */
41 | public static final NatsVersion V2_10_17 = new NatsVersion("v2.10.17");
42 |
43 | /**
44 | * Constant V2_10_16
45 | */
46 | public static final NatsVersion V2_10_16 = new NatsVersion("v2.10.16");
47 |
48 | /**
49 | * Constant V2_10_14
50 | */
51 | public static final NatsVersion V2_10_14 = new NatsVersion("v2.10.14");
52 |
53 | /**
54 | * Constant V2_10_12
55 | */
56 | public static final NatsVersion V2_10_12 = new NatsVersion("v2.10.12");
57 |
58 | /**
59 | * Constant V2_10_11
60 | */
61 | public static final NatsVersion V2_10_11 = new NatsVersion("v2.10.11");
62 |
63 | /**
64 | * Constant V2_10_10
65 | */
66 | public static final NatsVersion V2_10_10 = new NatsVersion("v2.10.10");
67 |
68 | /**
69 | * Constant V2_10_9
70 | */
71 | public static final NatsVersion V2_10_9 = new NatsVersion("v2.10.9");
72 |
73 | /**
74 | * Constant V2_10_8
75 | */
76 | public static final NatsVersion V2_10_8 = new NatsVersion("v2.10.8");
77 |
78 | /**
79 | * Constant V2_10_7
80 | */
81 | public static final NatsVersion V2_10_7 = new NatsVersion("v2.10.7");
82 |
83 | /**
84 | * Constant V2_10_6
85 | */
86 | public static final NatsVersion V2_10_6 = new NatsVersion("v2.10.6");
87 |
88 | /**
89 | * Constant V2_10_5
90 | */
91 | public static final NatsVersion V2_10_5 = new NatsVersion("v2.10.5");
92 |
93 | /**
94 | * Constant V2_10_4
95 | */
96 | public static final NatsVersion V2_10_4 = new NatsVersion("v2.10.4");
97 |
98 | /**
99 | * Constant V2_10_3
100 | */
101 | public static final NatsVersion V2_10_3 = new NatsVersion("v2.10.3");
102 |
103 | /**
104 | * Constant V2_10_2
105 | */
106 | public static final NatsVersion V2_10_2 = new NatsVersion("v2.10.2");
107 |
108 | /**
109 | * Constant V2_10_1
110 | */
111 | public static final NatsVersion V2_10_1 = new NatsVersion("v2.10.1");
112 |
113 | /**
114 | * Constant V2_10_0
115 | */
116 | public static final NatsVersion V2_10_0 = new NatsVersion("v2.10.0");
117 |
118 | /**
119 | * Constant V2_9_25
120 | */
121 | public static final NatsVersion V2_9_25 = new NatsVersion("v2.9.25");
122 |
123 | /**
124 | * Constant V2_9_24
125 | */
126 | public static final NatsVersion V2_9_24 = new NatsVersion("v2.9.24");
127 |
128 | /**
129 | * Constant V2_9_23
130 | */
131 | public static final NatsVersion V2_9_23 = new NatsVersion("v2.9.23");
132 |
133 | /**
134 | * Constant V2_9_22
135 | */
136 | public static final NatsVersion V2_9_22 = new NatsVersion("v2.9.22");
137 |
138 | /**
139 | * Constant V2_9_21
140 | */
141 | public static final NatsVersion V2_9_21 = new NatsVersion("v2.9.21");
142 |
143 | /**
144 | * Constant V2_9_20
145 | */
146 | public static final NatsVersion V2_9_20 = new NatsVersion("v2.9.20");
147 |
148 | /**
149 | * Constant V2_9_19
150 | */
151 | public static final NatsVersion V2_9_19 = new NatsVersion("v2.9.19");
152 |
153 | /**
154 | * Constant V2_9_18
155 | */
156 | public static final NatsVersion V2_9_18 = new NatsVersion("v2.9.18");
157 |
158 | /**
159 | * Constant V2_9_17
160 | */
161 | public static final NatsVersion V2_9_17 = new NatsVersion("v2.9.17");
162 |
163 | /**
164 | * Constant V2_9_16
165 | */
166 | public static final NatsVersion V2_9_16 = new NatsVersion("v2.9.16");
167 |
168 | /**
169 | * Constant V2_9_15
170 | */
171 | public static final NatsVersion V2_9_15 = new NatsVersion("v2.9.15");
172 |
173 | /**
174 | * Constant V2_9_14
175 | */
176 | public static final NatsVersion V2_9_14 = new NatsVersion("v2.9.14");
177 |
178 | /**
179 | * Constant V2_9_12
180 | */
181 | public static final NatsVersion V2_9_12 = new NatsVersion("v2.9.12");
182 |
183 | /**
184 | * Constant V2_9_11
185 | */
186 | public static final NatsVersion V2_9_11 = new NatsVersion("v2.9.11");
187 |
188 | /**
189 | * Constant V2_9_10
190 | */
191 | public static final NatsVersion V2_9_10 = new NatsVersion("v2.9.10");
192 |
193 | /**
194 | * Constant V2_9_9
195 | */
196 | public static final NatsVersion V2_9_9 = new NatsVersion("v2.9.9");
197 |
198 | /**
199 | * Constant V2_9_8
200 | */
201 | public static final NatsVersion V2_9_8 = new NatsVersion("v2.9.8");
202 |
203 | /**
204 | * Constant V2_9_7
205 | */
206 | public static final NatsVersion V2_9_7 = new NatsVersion("v2.9.7");
207 |
208 | /**
209 | * Constant V2_9_6
210 | */
211 | public static final NatsVersion V2_9_6 = new NatsVersion("v2.9.6");
212 |
213 | /**
214 | * Constant V2_9_5
215 | */
216 | public static final NatsVersion V2_9_5 = new NatsVersion("v2.9.5");
217 |
218 | /**
219 | * Constant V2_9_4
220 | */
221 | public static final NatsVersion V2_9_4 = new NatsVersion("v2.9.4");
222 |
223 | /**
224 | * Constant V2_9_3
225 | */
226 | public static final NatsVersion V2_9_3 = new NatsVersion("v2.9.3");
227 |
228 | /**
229 | * Constant V2_9_2
230 | */
231 | public static final NatsVersion V2_9_2 = new NatsVersion("v2.9.2");
232 |
233 | /**
234 | * Constant V2_9_1
235 | */
236 | public static final NatsVersion V2_9_1 = new NatsVersion("v2.9.1");
237 |
238 | /**
239 | * Constant V2_9_0
240 | */
241 | public static final NatsVersion V2_9_0 = new NatsVersion("v2.9.0");
242 |
243 | /**
244 | * Constant V2_8_4
245 | */
246 | public static final NatsVersion V2_8_4 = new NatsVersion("v2.8.4");
247 |
248 | /**
249 | * Constant V2_8_3
250 | */
251 | public static final NatsVersion V2_8_3 = new NatsVersion("v2.8.3");
252 |
253 | /**
254 | * Constant V2_8_2
255 | */
256 | public static final NatsVersion V2_8_2 = new NatsVersion("v2.8.2");
257 |
258 | /**
259 | * Constant V2_8_1
260 | */
261 | public static final NatsVersion V2_8_1 = new NatsVersion("v2.8.1");
262 |
263 | /**
264 | * Constant V2_8_0
265 | */
266 | public static final NatsVersion V2_8_0 = new NatsVersion("v2.8.0");
267 |
268 | /**
269 | * Constant V2_7_4
270 | */
271 | public static final NatsVersion V2_7_4 = new NatsVersion("v2.7.4");
272 |
273 | /**
274 | * Constant V2_7_3
275 | */
276 | public static final NatsVersion V2_7_3 = new NatsVersion("v2.7.3");
277 |
278 | /**
279 | * Constant V2_7_2
280 | */
281 | public static final NatsVersion V2_7_2 = new NatsVersion("v2.7.2");
282 |
283 | /**
284 | * Constant V2_7_1
285 | */
286 | public static final NatsVersion V2_7_1 = new NatsVersion("v2.7.1");
287 |
288 | /**
289 | * Constant V2_7_0
290 | */
291 | public static final NatsVersion V2_7_0 = new NatsVersion("v2.7.0");
292 |
293 | /**
294 | * Constant V2_6_6
295 | */
296 | public static final NatsVersion V2_6_6 = new NatsVersion("v2.6.6");
297 |
298 | /**
299 | * Constant V2_6_5
300 | */
301 | public static final NatsVersion V2_6_5 = new NatsVersion("v2.6.5");
302 |
303 | /**
304 | * Constant V2_6_4
305 | */
306 | public static final NatsVersion V2_6_4 = new NatsVersion("v2.6.4");
307 |
308 | /**
309 | * Constant V2_6_3
310 | */
311 | public static final NatsVersion V2_6_3 = new NatsVersion("v2.6.3");
312 |
313 | /**
314 | * Constant V2_6_2
315 | */
316 | public static final NatsVersion V2_6_2 = new NatsVersion("v2.6.2");
317 |
318 | /**
319 | * Constant V2_6_1
320 | */
321 | public static final NatsVersion V2_6_1 = new NatsVersion("v2.6.1");
322 |
323 | /**
324 | * Constant V2_6_0
325 | */
326 | public static final NatsVersion V2_6_0 = new NatsVersion("v2.6.0");
327 |
328 | /**
329 | * Constant V2_5_0
330 | */
331 | public static final NatsVersion V2_5_0 = new NatsVersion("v2.5.0");
332 |
333 | /**
334 | * Constant V2_4_0
335 | */
336 | public static final NatsVersion V2_4_0 = new NatsVersion("v2.4.0");
337 |
338 | /**
339 | * Constant V2_3_4
340 | */
341 | public static final NatsVersion V2_3_4 = new NatsVersion("v2.3.4");
342 |
343 | /**
344 | * Constant V2_3_3
345 | */
346 | public static final NatsVersion V2_3_3 = new NatsVersion("v2.3.3");
347 |
348 | /**
349 | * Constant V2_3_2
350 | */
351 | public static final NatsVersion V2_3_2 = new NatsVersion("v2.3.2");
352 |
353 | /**
354 | * Constant V2_3_1
355 | */
356 | public static final NatsVersion V2_3_1 = new NatsVersion("v2.3.1");
357 |
358 | /**
359 | * Constant V2_3_0
360 | */
361 | public static final NatsVersion V2_3_0 = new NatsVersion("v2.3.0");
362 |
363 | /**
364 | * Constant V2_2_6
365 | */
366 | public static final NatsVersion V2_2_6 = new NatsVersion("v2.2.6");
367 |
368 | /**
369 | * Constant V2_2_5
370 | */
371 | public static final NatsVersion V2_2_5 = new NatsVersion("v2.2.5");
372 |
373 | /**
374 | * Constant V2_2_4
375 | */
376 | public static final NatsVersion V2_2_4 = new NatsVersion("v2.2.4");
377 |
378 | /**
379 | * Constant V2_2_3
380 | */
381 | public static final NatsVersion V2_2_3 = new NatsVersion("v2.2.3");
382 |
383 | /**
384 | * Constant V2_2_2
385 | */
386 | public static final NatsVersion V2_2_2 = new NatsVersion("v2.2.2");
387 |
388 | /**
389 | * Constant V2_2_1
390 | */
391 | public static final NatsVersion V2_2_1 = new NatsVersion("v2.2.1");
392 |
393 | /**
394 | * Constant V2_2_0
395 | */
396 | public static final NatsVersion V2_2_0 = new NatsVersion("v2.2.0");
397 |
398 | /**
399 | * Constant V2_1_9
400 | */
401 | public static final NatsVersion V2_1_9 = new NatsVersion("v2.1.9");
402 |
403 | /**
404 | * Constant V2_1_8
405 | */
406 | public static final NatsVersion V2_1_8 = new NatsVersion("v2.1.8");
407 |
408 | /**
409 | * Constant V2_1_7
410 | */
411 | public static final NatsVersion V2_1_7 = new NatsVersion("v2.1.7");
412 |
413 | /**
414 | * Constant V2_1_6
415 | */
416 | public static final NatsVersion V2_1_6 = new NatsVersion("v2.1.6");
417 |
418 | /**
419 | * Constant V2_1_4
420 | */
421 | public static final NatsVersion V2_1_4 = new NatsVersion("v2.1.4");
422 |
423 | /**
424 | * Constant V2_1_2
425 | */
426 | public static final NatsVersion V2_1_2 = new NatsVersion("v2.1.2");
427 | /**
428 | * Constant V2_1_0
429 | */
430 | public static final NatsVersion V2_1_0 = new NatsVersion("v2.1.0");
431 | /**
432 | * Constant V2_0_4
433 | */
434 | public static final NatsVersion V2_0_4 = new NatsVersion("v2.0.4");
435 | /**
436 | * Constant V2_0_2
437 | */
438 | public static final NatsVersion V2_0_2 = new NatsVersion("v2.0.2");
439 | /**
440 | * Constant V2_0_0
441 | */
442 | public static final NatsVersion V2_0_0 = new NatsVersion("v2.0.0");
443 |
444 |
445 | private final String version;
446 |
447 | /**
448 | * Constructor for NatsVersion.
449 | * 450 | * @param version a {@link java.lang.String} object. 451 | */ 452 | public NatsVersion(String version) { 453 | this.version = version; 454 | } 455 | 456 | /** 457 | * {@inheritDoc} 458 | */ 459 | @Override 460 | public String asInDownloadPath() { 461 | return version; 462 | } 463 | 464 | /** 465 | * {@inheritDoc} 466 | */ 467 | @Override 468 | public String toString() { 469 | return version; 470 | } 471 | } 472 | -------------------------------------------------------------------------------- /src/main/java/np/com/madanpokharel/embed/nats/RunningNatsProcess.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Madan Pokharel 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 | package np.com.madanpokharel.embed.nats; 17 | 18 | import de.flapdoodle.embed.process.config.SupportConfig; 19 | import de.flapdoodle.embed.process.io.*; 20 | import de.flapdoodle.embed.process.runtime.ProcessControl; 21 | import de.flapdoodle.embed.process.runtime.Processes; 22 | import de.flapdoodle.embed.process.types.RunningProcessFactory; 23 | import de.flapdoodle.embed.process.types.RunningProcessImpl; 24 | import de.flapdoodle.os.Platform; 25 | import de.flapdoodle.types.Try; 26 | import org.slf4j.Logger; 27 | import org.slf4j.LoggerFactory; 28 | 29 | import java.nio.file.Path; 30 | import java.util.Arrays; 31 | import java.util.Collections; 32 | import java.util.List; 33 | import java.util.regex.Matcher; 34 | import java.util.regex.Pattern; 35 | 36 | /** 37 | *Abstract RunningNatsProcess class.
38 | * 39 | * @author madanpokharel 40 | * @version $Id: $Id 41 | */ 42 | public abstract class RunningNatsProcess extends RunningProcessImpl { 43 | 44 | private static Logger LOGGER = LoggerFactory.getLogger(RunningNatsProcess.class); 45 | 46 | private final String commandName; 47 | private final SupportConfig supportConfig; 48 | private final Platform platform; 49 | private final StreamProcessor commandOutput; 50 | private final int natsProcessId; 51 | private boolean shutDownCommandAlreadyExecuted = false; 52 | 53 | /** 54 | *Constructor for RunningNatsProcess.
55 | * 56 | * @param commandName a {@link java.lang.String} object 57 | * @param process a {@link de.flapdoodle.embed.process.runtime.ProcessControl} object 58 | * @param pidFile a {@link java.nio.file.Path} object 59 | * @param onStop a {@link java.lang.Runnable} object 60 | * @param supportConfig a {@link de.flapdoodle.embed.process.config.SupportConfig} object 61 | * @param platform a {@link de.flapdoodle.os.Platform} object 62 | * @param commandOutput a {@link de.flapdoodle.embed.process.io.StreamProcessor} object 63 | * @param natsProcessId a int 64 | * @param serverType a {@link np.com.madanpokharel.embed.nats.ServerType} object 65 | */ 66 | protected RunningNatsProcess( 67 | String commandName, 68 | ProcessControl process, 69 | Path pidFile, 70 | Runnable onStop, 71 | SupportConfig supportConfig, 72 | Platform platform, 73 | StreamProcessor commandOutput, 74 | int natsProcessId, 75 | ServerType serverType 76 | ) { 77 | super(process, pidFile, 5000, onStop); 78 | this.commandName = commandName; 79 | this.supportConfig = supportConfig; 80 | this.platform = platform; 81 | this.commandOutput = commandOutput; 82 | this.natsProcessId = natsProcessId; 83 | } 84 | 85 | /** {@inheritDoc} */ 86 | @Override 87 | public int stop() { 88 | try { 89 | stopInternal(); 90 | } finally { 91 | return super.stop(); 92 | } 93 | } 94 | 95 | // @Override 96 | private void stopInternal() { 97 | if (isAlive()) { 98 | if (!sendKillToProcess()) { 99 | LOGGER.warn("could not stop " + commandName + ", try next"); 100 | if (!sendTermToProcess()) { 101 | LOGGER.warn("could not stop " + commandName + ", try next"); 102 | if (!tryKillToProcess()) { 103 | LOGGER.warn("could not stop " + commandName + " the second time, try one last thing"); 104 | } 105 | } 106 | } 107 | 108 | } 109 | } 110 | 111 | private long getProcessId() { 112 | return natsProcessId; 113 | } 114 | 115 | /** 116 | *sendKillToProcess.
117 | * 118 | * @return a boolean 119 | */ 120 | protected boolean sendKillToProcess() { 121 | return getProcessId() > 0 && Processes.killProcess(supportConfig, platform, 122 | StreamToLineProcessor.wrap(commandOutput), getProcessId()); 123 | } 124 | 125 | /** 126 | *sendTermToProcess.
127 | * 128 | * @return a boolean 129 | */ 130 | protected boolean sendTermToProcess() { 131 | return getProcessId() > 0 && Processes.termProcess(supportConfig, platform, 132 | StreamToLineProcessor.wrap(commandOutput), getProcessId()); 133 | } 134 | 135 | /** 136 | *tryKillToProcess.
137 | * 138 | * @return a boolean 139 | */ 140 | protected boolean tryKillToProcess() { 141 | return getProcessId() > 0 && Processes.tryKillProcess(supportConfig, platform, 142 | StreamToLineProcessor.wrap(commandOutput), getProcessId()); 143 | } 144 | 145 | interface InstanceFactoryServerType class.
21 | * 22 | * @author Madan Pokharel 23 | * @version 1.0.0 24 | */ 25 | public enum ServerType { 26 | NATS("nats-server"), 27 | NATS_STREAMING("nats-streaming-server"); 28 | 29 | private final String serverName; 30 | 31 | /** 32 | *Constructor for ServerType.
33 | * 34 | * @param serverName a {@link java.lang.String} object. 35 | */ 36 | ServerType(String serverName) { 37 | this.serverName = serverName; 38 | } 39 | 40 | /** 41 | *Getter for the field serverName
.