Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
org.gradle.parallel=true

group=io.pivotal.cfenv
version=3.1.0-SNAPSHOT
version=3.1.1-SNAPSHOT
onlyShowStandardStreamsOnTestFailure=false

4 changes: 3 additions & 1 deletion java-cfenv-all/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CfEnv> ifInCf);
}
Original file line number Diff line number Diff line change
@@ -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<ApplicationEvent>, 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()));
}
}

}
Original file line number Diff line number Diff line change
@@ -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<CfEnv> ifInCf) {
ifInCf.accept(this.cfEnv);
}
}
Original file line number Diff line number Diff line change
@@ -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);
});
}
}
2 changes: 2 additions & 0 deletions java-cfenv-all/src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.springframework.context.ApplicationListener=\
io.pivotal.cfenv.profile.CloudProfileApplicationListener
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
2 changes: 1 addition & 1 deletion java-cfenv/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}

Expand Down