diff --git a/gradle.properties b/gradle.properties index 67c75766..a2a1ad22 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ org.gradle.parallel=true group=io.pivotal.cfenv -version=3.1.0-SNAPSHOT +version=3.1.1-SNAPSHOT onlyShowStandardStreamsOnTestFailure=false diff --git a/java-cfenv-all/build.gradle b/java-cfenv-all/build.gradle index 006ac79b..fbb6a847 100644 --- a/java-cfenv-all/build.gradle +++ b/java-cfenv-all/build.gradle @@ -14,11 +14,13 @@ plugins { apply plugin: 'com.github.johnrengelman.shadow' apply plugin: 'java-library' -description = 'Java CF Env Boot All, contains all java-cfenv modules in a convenient uberjar' +description = 'java-cfenv-all, contains all java-cfenv modules in a convenient uberjar - to be used with CF Java Buildpack' dependencies { + api 'org.springframework.boot:spring-boot' api project(':java-cfenv-boot-pivotal-scs') api project(':java-cfenv-boot-pivotal-sso') + testImplementation 'org.springframework.boot:spring-boot-starter-test' } import com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer diff --git a/java-cfenv-all/src/main/java/io/pivotal/cfenv/profile/CfEnvHolder.java b/java-cfenv-all/src/main/java/io/pivotal/cfenv/profile/CfEnvHolder.java new file mode 100644 index 00000000..d947665f --- /dev/null +++ b/java-cfenv-all/src/main/java/io/pivotal/cfenv/profile/CfEnvHolder.java @@ -0,0 +1,28 @@ +/* + * Copyright 2011-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.pivotal.cfenv.profile; + +import java.util.function.Consumer; + +import io.pivotal.cfenv.core.CfEnv; + +/** + * Originally contributed by Dylan Roberts in + * https://github.com/cloudfoundry/java-buildpack-auto-reconfiguration/pull/76/files + */ +public interface CfEnvHolder { + void withCfEnv(Consumer ifInCf); +} diff --git a/java-cfenv-all/src/main/java/io/pivotal/cfenv/profile/CloudProfileApplicationListener.java b/java-cfenv-all/src/main/java/io/pivotal/cfenv/profile/CloudProfileApplicationListener.java new file mode 100644 index 00000000..ec4b61d0 --- /dev/null +++ b/java-cfenv-all/src/main/java/io/pivotal/cfenv/profile/CloudProfileApplicationListener.java @@ -0,0 +1,58 @@ +/* + * Copyright 2011-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.pivotal.cfenv.profile; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; +import org.springframework.boot.context.event.ApplicationPreparedEvent; +import org.springframework.boot.logging.DeferredLog; +import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.core.Ordered; + +/** + * Originally contributed by Dylan Roberts in + * https://github.com/cloudfoundry/java-buildpack-auto-reconfiguration/pull/76/files + */ +public final class CloudProfileApplicationListener implements ApplicationListener, Ordered { + + private Log logger = new DeferredLog(); + public static final String CLOUD_PROFILE = "cloud"; + private final CfEnvHolder cfEnvHolder; + + public CloudProfileApplicationListener() { + this.cfEnvHolder = new DefaultCfEnvHolder(); + } + + @Override + public int getOrder() { + return Ordered.HIGHEST_PRECEDENCE + 3; // Before Boot properties file load to enable support for `application-cloud.properties` + } + + @Override + public void onApplicationEvent(ApplicationEvent event) { + if (event instanceof ApplicationEnvironmentPreparedEvent e) { + ProfileUtils.activateProfile(CLOUD_PROFILE, e.getEnvironment(), this.cfEnvHolder); + logger.info(String.format("'%s' profile activated", CLOUD_PROFILE)); + } else if (event instanceof ApplicationPreparedEvent) { + DeferredLog.replay(logger, LogFactory.getLog(getClass())); + } + } + +} diff --git a/java-cfenv-all/src/main/java/io/pivotal/cfenv/profile/DefaultCfEnvHolder.java b/java-cfenv-all/src/main/java/io/pivotal/cfenv/profile/DefaultCfEnvHolder.java new file mode 100644 index 00000000..e3a1efd6 --- /dev/null +++ b/java-cfenv-all/src/main/java/io/pivotal/cfenv/profile/DefaultCfEnvHolder.java @@ -0,0 +1,40 @@ +/* + * Copyright 2011-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.pivotal.cfenv.profile; + +import java.util.function.Consumer; + +import io.pivotal.cfenv.core.CfEnv; +import io.pivotal.cfenv.core.CfEnvSingleton; + +/** + * Originally contributed by Dylan Roberts in + * https://github.com/cloudfoundry/java-buildpack-auto-reconfiguration/pull/76/files + */ +public class DefaultCfEnvHolder implements CfEnvHolder { + + private final CfEnv cfEnv; + + public DefaultCfEnvHolder() { + this.cfEnv = CfEnvSingleton.getCfEnvInstance(); + } + + @Override + public final void withCfEnv(Consumer ifInCf) { + ifInCf.accept(this.cfEnv); + } +} diff --git a/java-cfenv-all/src/main/java/io/pivotal/cfenv/profile/ProfileUtils.java b/java-cfenv-all/src/main/java/io/pivotal/cfenv/profile/ProfileUtils.java new file mode 100644 index 00000000..e57bdd68 --- /dev/null +++ b/java-cfenv-all/src/main/java/io/pivotal/cfenv/profile/ProfileUtils.java @@ -0,0 +1,36 @@ +/* + * Copyright 2011-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.pivotal.cfenv.profile; + +import org.springframework.core.env.ConfigurableEnvironment; + +/** + * Originally contributed by Dylan Roberts in + * https://github.com/cloudfoundry/java-buildpack-auto-reconfiguration/pull/76/files + */ +final class ProfileUtils { + + private ProfileUtils() { + } + + static void activateProfile(String profile, ConfigurableEnvironment environment, CfEnvHolder cfEnvHolder) { + cfEnvHolder.withCfEnv( + cfEnv -> { + environment.addActiveProfile(profile); + }); + } +} diff --git a/java-cfenv-all/src/main/resources/META-INF/spring.factories b/java-cfenv-all/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..48fae5f3 --- /dev/null +++ b/java-cfenv-all/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.context.ApplicationListener=\ +io.pivotal.cfenv.profile.CloudProfileApplicationListener diff --git a/java-cfenv-all/src/test/java/io/pivotal/cfenv/profile/CloudProfileApplicationListenerTest.java b/java-cfenv-all/src/test/java/io/pivotal/cfenv/profile/CloudProfileApplicationListenerTest.java new file mode 100644 index 00000000..5cebba38 --- /dev/null +++ b/java-cfenv-all/src/test/java/io/pivotal/cfenv/profile/CloudProfileApplicationListenerTest.java @@ -0,0 +1,45 @@ +/* + * Copyright 2011-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.pivotal.cfenv.profile; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; +import org.springframework.core.env.ConfigurableEnvironment; + +class CloudProfileApplicationListenerTest { + + @Test + void getOrderTest() { + CloudProfileApplicationListener cloudProfileApplicationListener = new CloudProfileApplicationListener(); + Assertions.assertEquals(-2147483645, cloudProfileApplicationListener.getOrder()); + } + + @Test + void onApplicationEventTest_application_env_prepared_event() { + CloudProfileApplicationListener cloudProfileApplicationListener = new CloudProfileApplicationListener(); + + ApplicationEnvironmentPreparedEvent aepe = Mockito.mock(ApplicationEnvironmentPreparedEvent.class); + Mockito.when(aepe.getEnvironment()).thenReturn(Mockito.mock(ConfigurableEnvironment.class)); + + cloudProfileApplicationListener.onApplicationEvent(aepe); + + Mockito.verify(aepe, Mockito.times(1)).getEnvironment(); + } +} diff --git a/java-cfenv-all/src/test/java/io/pivotal/cfenv/profile/ProfileUtilsTest.java b/java-cfenv-all/src/test/java/io/pivotal/cfenv/profile/ProfileUtilsTest.java new file mode 100644 index 00000000..7624fdfa --- /dev/null +++ b/java-cfenv-all/src/test/java/io/pivotal/cfenv/profile/ProfileUtilsTest.java @@ -0,0 +1,32 @@ +/* + * Copyright 2011-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.pivotal.cfenv.profile; + +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import org.springframework.core.env.ConfigurableEnvironment; + +class ProfileUtilsTest { + + @Test + void activateProfileTest() { + ConfigurableEnvironment environment = Mockito.mock(ConfigurableEnvironment.class); + ProfileUtils.activateProfile("my-profile", environment, new DefaultCfEnvHolder()); + Mockito.verify(environment, Mockito.times(1)).addActiveProfile("my-profile"); + } +} diff --git a/java-cfenv/build.gradle b/java-cfenv/build.gradle index d97a2386..017a449a 100644 --- a/java-cfenv/build.gradle +++ b/java-cfenv/build.gradle @@ -17,7 +17,7 @@ dependencies { } testFixturesApi platform("org.springframework.boot:spring-boot-dependencies:${springBootVersion}") - testFixturesImplementation "org.springframework.boot:spring-boot-starter-test" + testFixturesImplementation "org.springframework.boot:spring-boot-starter-test:${springBootVersion}" testFixturesImplementation "org.jmockit:jmockit:${jmockitVersion}" }