Skip to content

Commit

Permalink
Merge pull request #1 from transferwise/fix_build
Browse files Browse the repository at this point in the history
New proper initial version.
  • Loading branch information
onukristo authored Mar 27, 2024
2 parents f7c018a + 2ef4e9c commit b5c4bb9
Show file tree
Hide file tree
Showing 32 changed files with 575 additions and 304 deletions.
6 changes: 2 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.0.1] - 2024-03-21
## [0.0.2] - 2024-03-27

### Added

* A bean to ask the current environment from.
Initial version.
42 changes: 34 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
# Wise Environment.

![Apache 2](https://img.shields.io/hexpm/l/plug.svg)
![Java 11](https://img.shields.io/badge/Java-11-blue.svg)
![Java 17](https://img.shields.io/badge/Java-17-blue.svg)
![Maven Central](https://badgen.net/maven/v/maven-central/com.transferwise.common/wise-environment)
[![Owners](https://img.shields.io/badge/team-AppEng-blueviolet.svg?logo=wise)](https://transferwise.atlassian.net/wiki/spaces/EKB/pages/2520812116/Application+Engineering+Team) [![Slack](https://img.shields.io/badge/slack-sre--guild-blue.svg?logo=slack)](https://app.slack.com/client/T026FB76G/CLR1U8SNS)
> Use the `@application-engineering-on-call` handle on Slack for help.
---

Provides information to other libraries in which environment they are running in.

Allows to set default properties via environment variables and system properties.
Allows to set default properties for specific environments.

Those have to prefixed respectively with
* `WISE_DEFAULTS_`
* `wise.defaults.`
Typical use case is for various Wise libraries to set environment specific default properties in their `EnvironmentPostProcessor` implementations.

E.g.
* `WISE_DEFAULTS_WISE_ENVIRONMENT_TEST_VALUE1`
* `wise.defaults.wise.environment.test.value4`
```java
WiseEnvironment.setDefaultProperty("my-library", WiseEnvironment.PRODUCTION, "tw-reliable-jdbc.sslMode", SslMode.VERIFY_FULL);
WiseEnvironment.setDefaultProperty("my-library", WiseEnvironment.STAGING, "tw-reliable-jdbc.sslMode", SslMode.PREFERRED);
WiseEnvironment.setDefaultProperty("my-library", WiseEnvironment.CUSTOM_ENVIRONMENT, "tw-reliable-jdbc.sslMode", SslMode.VERIFY_CA);
```

See `WiseEnvironment` class for other optional use cases. E.g. asking which environments are currently active.

The environments themselves can be hierarchical. In that sense, that if you set a default property to `STAGING`, it would also apply to
`CUSTOM_ENVIRONMENT`, unless a different value is specifically set for `CUSTOM_ENVIRONMENT`.

Also, a convenience DSL is available for setting properties. E.g.

```java
WiseEnvironment.setDefaultProperties(dsl -> dsl
.source("tw-reliable-jdbc")

.environment(WiseEnvironment.WISE)
.set(TW_OBS_BASE_EXTREMUM_CONFIG_PATH, gaugeNames)
.set("spring.flyway.validate-migration-naming", "true")

.keyPrefix("tw-reliable-jdbc.")
.environment(WiseEnvironment.PRODUCTION)
.set("sslMode", SslMode.VERIFY_FULL)
.set("requiredSslModeLevel", SslMode.VERIFY_CA)
.set("requireMinPoolSizePct", 100d)

.environment(WiseEnvironment.STAGING)
.set("sslMode", SslMode.VERIFY_FULL)
);
```

## License
Copyright 2024 TransferWise Ltd.
Expand Down
7 changes: 0 additions & 7 deletions build.common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ apply plugin: "java-library"
apply plugin: "checkstyle"
apply plugin: "idea"
apply plugin: "com.github.spotbugs"
apply plugin: "groovy"

apply from: "$rootProject.rootDir/build.libraries.gradle"

Expand Down Expand Up @@ -91,12 +90,6 @@ compileTestJava {
test {
useJUnitPlatform()

environment("WISE_DEFAULTS_WISE_ENVIRONMENT_TEST_VALUE1", "foo")
environment("WISE_DEFAULTS_WISE_ENVIRONMENT_TEST_VALUE2", "foo")
environment("WISE_ENVIRONMENT_TEST_VALUE3", "foo")

systemProperty("wise.defaults.wise.environment.test.value4", "foo")

testLogging {
events TestLogEvent.STARTED, TestLogEvent.FAILED, TestLogEvent.SKIPPED, TestLogEvent.PASSED,
TestLogEvent.STANDARD_ERROR
Expand Down
3 changes: 2 additions & 1 deletion build.libraries.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ ext {
springBootDependencies: "org.springframework.boot:spring-boot-dependencies:${springBootVersion}",

// versions managed by spring-boot-dependencies platform
commonsLang3 : 'org.apache.commons:commons-lang3',
lombok : 'org.projectlombok:lombok',
springBootStarter : "org.springframework.boot:spring-boot-starter",
springBootStarterTest : "org.springframework.boot:spring-boot-starter-test"
springBootStarterTest : "org.springframework.boot:spring-boot-starter-test",
]
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.0.1
version=0.0.2
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
14 changes: 7 additions & 7 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -202,11 +202,11 @@ fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
20 changes: 10 additions & 10 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
5 changes: 3 additions & 2 deletions wise-environment-starter/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
ext.projectArtifactName = "wise-environment"
ext.projectArtifactName = "wise-environment-starter"

apply from: "$rootProject.rootDir/build.common.gradle"
apply from: "$rootProject.rootDir/build.library.gradle"

dependencies {
compileOnly libraries.springBootStarter
implementation libraries.commonsLang3
implementation libraries.springBootStarter

testImplementation libraries.springBootStarterTest
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.wise.common.environment;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CachingWiseEnvironmentProvider implements WiseEnvironmentProvider {

private final List<WiseEnvironment> activeEnvironments;

private Map<WiseEnvironment, Boolean> isActiveCache = new ConcurrentHashMap<>();

public CachingWiseEnvironmentProvider(List<WiseEnvironment> activeEnvironments) {
this.activeEnvironments = activeEnvironments;
}

@Override
public List<WiseEnvironment> getActiveEnvironments() {
return activeEnvironments;
}

@Override
public boolean isEnvironmentActive(WiseEnvironment environment) {
return isActiveCache.computeIfAbsent(environment, k -> {
var activeEnvironments = getActiveEnvironments();

for (var activeEnvironment : activeEnvironments) {
while (activeEnvironment != null) {
if (activeEnvironment == environment) {
return true;
}
activeEnvironment = activeEnvironment.parent();
}
}

return false;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.wise.common.environment;

public class DefaultPropertiesSetterDsl implements PropertiesSetterDsl {

private String source;
private WiseEnvironment wiseEnvironment;

private String keyPrefix;

private Dsl0 dsl0 = new Dsl0Impl();
private Dsl1 dsl1 = new Dsl1Impl();
private Dsl2 dsl2 = new Dsl2Impl();

@Override
public Dsl0 source(String source) {
this.source = source;
return dsl0;
}

class Dsl0Impl implements Dsl0 {

@Override
public Dsl2 environment(WiseEnvironment wiseEnvironment) {
DefaultPropertiesSetterDsl.this.wiseEnvironment = wiseEnvironment;
return dsl2;
}

@Override
public Dsl1 keyPrefix(String keyPrefix) {
DefaultPropertiesSetterDsl.this.keyPrefix = keyPrefix;
return dsl1;
}
}

class Dsl1Impl implements Dsl1 {

@Override
public Dsl2 environment(WiseEnvironment wiseEnvironment) {
DefaultPropertiesSetterDsl.this.wiseEnvironment = wiseEnvironment;
return dsl2;
}
}

class Dsl2Impl implements Dsl2 {

@Override
public Dsl2 environment(WiseEnvironment wiseEnvironment) {
DefaultPropertiesSetterDsl.this.wiseEnvironment = wiseEnvironment;
return this;
}

@Override
public Dsl2 keyPrefix(String keyPrefix) {
DefaultPropertiesSetterDsl.this.keyPrefix = keyPrefix;
return this;
}

@Override
public Dsl2 set(String name, Object value) {
WiseEnvironment.setDefaultProperty(source, wiseEnvironment, keyPrefix == null ? name : keyPrefix + name, value);
return this;
}
}
}

This file was deleted.

Loading

0 comments on commit b5c4bb9

Please sign in to comment.