Skip to content

Commit b161c4e

Browse files
Add Cloud profile (#227)
* Bump to next iteration version * Fix issue with missing version in dependency * that could provoke issues with projects including cf-env with the error `'dependencies.dependency.version' for org.springframework.boot:spring-boot-starter-test:jar is missing. in io.pivotal.cfenv:java-cfenv:3.1.0` ` * Add cloud profile to java-cfenv-all * so that when you add it to an app, it will add the `cloud` profile to it, similar to Spring Auto Reconfiguration
1 parent 471305d commit b161c4e

10 files changed

Lines changed: 246 additions & 3 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
org.gradle.parallel=true
22

33
group=io.pivotal.cfenv
4-
version=3.1.0-SNAPSHOT
4+
version=3.1.1-SNAPSHOT
55
onlyShowStandardStreamsOnTestFailure=false
66

java-cfenv-all/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ plugins {
1414
apply plugin: 'com.github.johnrengelman.shadow'
1515
apply plugin: 'java-library'
1616

17-
description = 'Java CF Env Boot All, contains all java-cfenv modules in a convenient uberjar'
17+
description = 'java-cfenv-all, contains all java-cfenv modules in a convenient uberjar - to be used with CF Java Buildpack'
1818

1919
dependencies {
20+
api 'org.springframework.boot:spring-boot'
2021
api project(':java-cfenv-boot-pivotal-scs')
2122
api project(':java-cfenv-boot-pivotal-sso')
23+
testImplementation 'org.springframework.boot:spring-boot-starter-test'
2224
}
2325

2426
import com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2011-2023 the original author or authors.
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+
* https://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 io.pivotal.cfenv.profile;
17+
18+
import java.util.function.Consumer;
19+
20+
import io.pivotal.cfenv.core.CfEnv;
21+
22+
/**
23+
* Originally contributed by Dylan Roberts in
24+
* https://github.com/cloudfoundry/java-buildpack-auto-reconfiguration/pull/76/files
25+
*/
26+
public interface CfEnvHolder {
27+
void withCfEnv(Consumer<CfEnv> ifInCf);
28+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2011-2023 the original author or authors.
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+
* https://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 io.pivotal.cfenv.profile;
18+
19+
import org.apache.commons.logging.Log;
20+
import org.apache.commons.logging.LogFactory;
21+
22+
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
23+
import org.springframework.boot.context.event.ApplicationPreparedEvent;
24+
import org.springframework.boot.logging.DeferredLog;
25+
import org.springframework.context.ApplicationEvent;
26+
import org.springframework.context.ApplicationListener;
27+
import org.springframework.core.Ordered;
28+
29+
/**
30+
* Originally contributed by Dylan Roberts in
31+
* https://github.com/cloudfoundry/java-buildpack-auto-reconfiguration/pull/76/files
32+
*/
33+
public final class CloudProfileApplicationListener implements ApplicationListener<ApplicationEvent>, Ordered {
34+
35+
private Log logger = new DeferredLog();
36+
public static final String CLOUD_PROFILE = "cloud";
37+
private final CfEnvHolder cfEnvHolder;
38+
39+
public CloudProfileApplicationListener() {
40+
this.cfEnvHolder = new DefaultCfEnvHolder();
41+
}
42+
43+
@Override
44+
public int getOrder() {
45+
return Ordered.HIGHEST_PRECEDENCE + 3; // Before Boot properties file load to enable support for `application-cloud.properties`
46+
}
47+
48+
@Override
49+
public void onApplicationEvent(ApplicationEvent event) {
50+
if (event instanceof ApplicationEnvironmentPreparedEvent e) {
51+
ProfileUtils.activateProfile(CLOUD_PROFILE, e.getEnvironment(), this.cfEnvHolder);
52+
logger.info(String.format("'%s' profile activated", CLOUD_PROFILE));
53+
} else if (event instanceof ApplicationPreparedEvent) {
54+
DeferredLog.replay(logger, LogFactory.getLog(getClass()));
55+
}
56+
}
57+
58+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2011-2023 the original author or authors.
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+
* https://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 io.pivotal.cfenv.profile;
18+
19+
import java.util.function.Consumer;
20+
21+
import io.pivotal.cfenv.core.CfEnv;
22+
import io.pivotal.cfenv.core.CfEnvSingleton;
23+
24+
/**
25+
* Originally contributed by Dylan Roberts in
26+
* https://github.com/cloudfoundry/java-buildpack-auto-reconfiguration/pull/76/files
27+
*/
28+
public class DefaultCfEnvHolder implements CfEnvHolder {
29+
30+
private final CfEnv cfEnv;
31+
32+
public DefaultCfEnvHolder() {
33+
this.cfEnv = CfEnvSingleton.getCfEnvInstance();
34+
}
35+
36+
@Override
37+
public final void withCfEnv(Consumer<CfEnv> ifInCf) {
38+
ifInCf.accept(this.cfEnv);
39+
}
40+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2011-2023 the original author or authors.
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+
* https://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 io.pivotal.cfenv.profile;
18+
19+
import org.springframework.core.env.ConfigurableEnvironment;
20+
21+
/**
22+
* Originally contributed by Dylan Roberts in
23+
* https://github.com/cloudfoundry/java-buildpack-auto-reconfiguration/pull/76/files
24+
*/
25+
final class ProfileUtils {
26+
27+
private ProfileUtils() {
28+
}
29+
30+
static void activateProfile(String profile, ConfigurableEnvironment environment, CfEnvHolder cfEnvHolder) {
31+
cfEnvHolder.withCfEnv(
32+
cfEnv -> {
33+
environment.addActiveProfile(profile);
34+
});
35+
}
36+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.context.ApplicationListener=\
2+
io.pivotal.cfenv.profile.CloudProfileApplicationListener
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2011-2023 the original author or authors.
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+
* https://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 io.pivotal.cfenv.profile;
18+
19+
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.Test;
21+
import org.mockito.Mockito;
22+
23+
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
24+
import org.springframework.core.env.ConfigurableEnvironment;
25+
26+
class CloudProfileApplicationListenerTest {
27+
28+
@Test
29+
void getOrderTest() {
30+
CloudProfileApplicationListener cloudProfileApplicationListener = new CloudProfileApplicationListener();
31+
Assertions.assertEquals(-2147483645, cloudProfileApplicationListener.getOrder());
32+
}
33+
34+
@Test
35+
void onApplicationEventTest_application_env_prepared_event() {
36+
CloudProfileApplicationListener cloudProfileApplicationListener = new CloudProfileApplicationListener();
37+
38+
ApplicationEnvironmentPreparedEvent aepe = Mockito.mock(ApplicationEnvironmentPreparedEvent.class);
39+
Mockito.when(aepe.getEnvironment()).thenReturn(Mockito.mock(ConfigurableEnvironment.class));
40+
41+
cloudProfileApplicationListener.onApplicationEvent(aepe);
42+
43+
Mockito.verify(aepe, Mockito.times(1)).getEnvironment();
44+
}
45+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2011-2023 the original author or authors.
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+
* https://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 io.pivotal.cfenv.profile;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.mockito.Mockito;
21+
22+
import org.springframework.core.env.ConfigurableEnvironment;
23+
24+
class ProfileUtilsTest {
25+
26+
@Test
27+
void activateProfileTest() {
28+
ConfigurableEnvironment environment = Mockito.mock(ConfigurableEnvironment.class);
29+
ProfileUtils.activateProfile("my-profile", environment, new DefaultCfEnvHolder());
30+
Mockito.verify(environment, Mockito.times(1)).addActiveProfile("my-profile");
31+
}
32+
}

java-cfenv/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ dependencies {
1717
}
1818

1919
testFixturesApi platform("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
20-
testFixturesImplementation "org.springframework.boot:spring-boot-starter-test"
20+
testFixturesImplementation "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
2121
testFixturesImplementation "org.jmockit:jmockit:${jmockitVersion}"
2222
}
2323

0 commit comments

Comments
 (0)