Skip to content

Commit

Permalink
Load Profile instances using the ServiceLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
thegridman committed Feb 7, 2024
1 parent 9de5565 commit d171323
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.oracle.bedrock.Option;
import com.oracle.bedrock.OptionsByType;

import java.util.ServiceLoader;

/**
* Helpers for {@link Profile}s.
* <p>
Expand All @@ -38,16 +40,17 @@
*/
public class Profiles
{
public static final String ORACLE_TOOLS_PROFILE = "bedrock.profile.";

/**
* Auto-detect, instantiate and configure the set of {@link Profile}s.
*
* @return an {@link Iterable}
*/
@SuppressWarnings("unchecked")
public static OptionsByType getProfiles()
{
final String ORACLE_TOOLS_PROFILE = "bedrock.profile.";

OptionsByType profiles = OptionsByType.empty();
OptionsByType profiles = OptionsByType.empty();

for (String name : System.getProperties().stringPropertyNames())
{
Expand All @@ -60,7 +63,7 @@ public static OptionsByType getProfiles()
String profileValue = System.getProperty(name);

// when the profile name contains a "." we don't process this system property
if (profileName.indexOf(".") < 0)
if (!profileName.contains("."))
{
String profileClassName = System.getProperty(name + ".classname");
if (profileClassName == null)
Expand Down Expand Up @@ -94,6 +97,15 @@ public static OptionsByType getProfiles()
}
}
}

ServiceLoader<Profile> loader = ServiceLoader.load(Profile.class);
for (Profile profile : loader)
{
if (profile instanceof Option)
{
profiles.add((Option) profile);
}
}
}

return profiles;
Expand Down
2 changes: 2 additions & 0 deletions bedrock-runtime/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
requires java.logging;
requires java.management;
requires java.rmi;

uses com.oracle.bedrock.runtime.Profile;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

package com.oracle.bedrock.runtime;

import com.oracle.bedrock.Option;
import com.oracle.bedrock.OptionsByType;
import org.junit.AfterClass;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
Expand All @@ -43,6 +45,12 @@
*/
public class ProfilesTest
{
@AfterClass
public static void cleanup()
{
System.getProperties().remove("bedrock.profile.example");
}

/**
* Ensure that we can load and instantiate Profiles.
*/
Expand All @@ -55,14 +63,58 @@ public void shouldReturnCreateProfiles()
OptionsByType optionsByType = Profiles.getProfiles();

assertThat(optionsByType, is(not(nullValue())));
assertThat(optionsByType.asArray().length, is(1));
assertThat(optionsByType.asArray().length, is(2));

ExampleProfile profile = optionsByType.get(ExampleProfile.class);

assertThat(profile, is(not(nullValue())));

assertThat(profile.getParameters(), is("hello"));

System.getProperties().remove("bedrock.profile.example");
CustomProfile customProfile = optionsByType.get(CustomProfile.class);
assertThat(customProfile, is(not(nullValue())));
}

/**
* A custom profile which will be loaded via the ServiceLoader.
*/
public static class CustomProfile
implements Profile, Option
{
@Override
public void onLaunching(Platform platform, MetaClass metaClass, OptionsByType optionsByType)
{
}

@Override
public void onLaunched(Platform platform, Application application, OptionsByType optionsByType)
{
}

@Override
public void onClosing(Platform platform, Application application, OptionsByType optionsByType)
{
}
}

/**
* A custom profile which will be loaded via the ServiceLoader, but is not an Option.
*/
public static class NonOptionCustomProfile
implements Profile
{
@Override
public void onLaunching(Platform platform, MetaClass metaClass, OptionsByType optionsByType)
{
}

@Override
public void onLaunched(Platform platform, Application application, OptionsByType optionsByType)
{
}

@Override
public void onClosing(Platform platform, Application application, OptionsByType optionsByType)
{
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

com.oracle.bedrock.runtime.ProfilesTest$CustomProfile
com.oracle.bedrock.runtime.ProfilesTest$NonOptionCustomProfile
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<maven.deploy.plugin.version>3.0.0-M2</maven.deploy.plugin.version>
<maven.doxia.markdown.plugin.version>2.0.0-M2</maven.doxia.markdown.plugin.version>
<maven.enforcer.plugin.version>3.0.0</maven.enforcer.plugin.version>
<maven.failsafe.plugin.version>3.0.0-M5</maven.failsafe.plugin.version>
<maven.failsafe.plugin.version>3.1.2</maven.failsafe.plugin.version>
<maven.flatten.plugin.version>1.2.7</maven.flatten.plugin.version>
<maven.gpg.plugin.version>3.0.1</maven.gpg.plugin.version>
<maven.install.plugin.version>3.0.0-M1</maven.install.plugin.version>
Expand All @@ -113,7 +113,7 @@
<maven.shade.plugin.version>3.3.0</maven.shade.plugin.version>
<maven.site.plugin.version>3.11.0</maven.site.plugin.version>
<maven.source.plugin.version>3.2.1</maven.source.plugin.version>
<maven.surefire.plugin.version>3.0.0-M5</maven.surefire.plugin.version>
<maven.surefire.plugin.version>3.1.2</maven.surefire.plugin.version>
<maven.dependency-check.plugin.version>8.2.1</maven.dependency-check.plugin.version>

<!-- dependency versions -->
Expand Down

0 comments on commit d171323

Please sign in to comment.