Skip to content

Commit

Permalink
[Gitar] Updating Java files
Browse files Browse the repository at this point in the history
  • Loading branch information
Gitar committed Dec 30, 2024
1 parent db1e420 commit 01357f6
Showing 1 changed file with 55 additions and 44 deletions.
99 changes: 55 additions & 44 deletions src/main/java/Hello.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import java.io.IOException;

import com.launchdarkly.sdk.*;
import com.launchdarkly.sdk.server.*;
import java.io.IOException;

public class Hello {

Expand All @@ -10,80 +9,88 @@ public class Hello {

// Set FEATURE_FLAG_KEY to the feature flag key you want to evaluate.
static String FEATURE_FLAG_KEY = "sample-feature";

private static void showMessage(String s) {
System.out.println("*** " + s);
System.out.println();
}

private static void showBanner() {
showMessage("\n ██ \n" +
" ██ \n" +
" ████████ \n" +
" ███████ \n" +
"██ LAUNCHDARKLY █\n" +
" ███████ \n" +
" ████████ \n" +
" ██ \n" +
" ██ \n");
showMessage(
"\n ██ \n"
+ " ██ \n"
+ " ████████ \n"
+ " ███████ \n"
+ "██ LAUNCHDARKLY █\n"
+ " ███████ \n"
+ " ████████ \n"
+ " ██ \n"
+ " ██ \n");
}

public static void main(String... args) throws Exception {
boolean CIMode = System.getenv("CI") != null;

String envSDKKey = System.getenv("LAUNCHDARKLY_SDK_KEY");
if(envSDKKey != null) {
if (envSDKKey != null) {
SDK_KEY = envSDKKey;
}

String envFlagKey = System.getenv("LAUNCHDARKLY_FLAG_KEY");
if(envFlagKey != null) {
if (envFlagKey != null) {
FEATURE_FLAG_KEY = envFlagKey;
}

LDConfig config = new LDConfig.Builder().build();

if (SDK_KEY == null || SDK_KEY.equals("")) {
showMessage("Please set the LAUNCHDARKLY_SDK_KEY environment variable or edit Hello.java to set SDK_KEY to your LaunchDarkly SDK key first.");
showMessage(
"Please set the LAUNCHDARKLY_SDK_KEY environment variable or edit Hello.java to set"
+ " SDK_KEY to your LaunchDarkly SDK key first.");
System.exit(1);
}

final LDClient client = new LDClient(SDK_KEY, config);
if (client.isInitialized()) {
showMessage("SDK successfully initialized!");
} else {
showMessage("SDK failed to initialize. Please check your internet connection and SDK credential for any typo.");
showMessage(
"SDK failed to initialize. Please check your internet connection and SDK credential for"
+ " any typo.");
System.exit(1);
}

// Set up the evaluation context. This context should appear on your
// LaunchDarkly contexts dashboard soon after you run the demo.
final LDContext context = LDContext.builder("example-user-key")
.name("Sandy")
.build();
final LDContext context = LDContext.builder("example-user-key").name("Sandy").build();
showMessage("The '" + FEATURE_FLAG_KEY + "' feature flag evaluates to " + true + ".");

// Evaluate the feature flag for this context.
boolean flagValue = client.boolVariation(FEATURE_FLAG_KEY, context, false);
showMessage("The '" + FEATURE_FLAG_KEY + "' feature flag evaluates to " + flagValue + ".");
showBanner();

if (flagValue) {
showBanner();
}

//If this is building for CI, we don't need to keep running the Hello App continously.
if(CIMode) {
// If this is building for CI, we don't need to keep running the Hello App continously.
if (CIMode) {
System.exit(0);
}

// We set up a flag change listener so you can see flag changes as you change
// the flag rules.
client.getFlagTracker().addFlagValueChangeListener(FEATURE_FLAG_KEY, context, event -> {
showMessage("The '" + FEATURE_FLAG_KEY + "' feature flag evaluates to " + event.getNewValue() + ".");

if (event.getNewValue().booleanValue()) {
showBanner();
}
});
client
.getFlagTracker()
.addFlagValueChangeListener(
FEATURE_FLAG_KEY,
context,
event -> {
showMessage(
"The '"
+ FEATURE_FLAG_KEY
+ "' feature flag evaluates to "
+ event.getNewValue()
+ ".");

if (event.getNewValue().booleanValue()) {
showBanner();
}
});
showMessage("Listening for feature flag changes.");

// Here we ensure that when the application terminates, the SDK shuts down
Expand All @@ -92,15 +99,19 @@ public static void main(String... args) throws Exception {
// statistics may not appear on your dashboard. In a normal long-running
// application, the SDK would continue running and events would be delivered
// automatically in the background.
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
try {
client.close();
} catch (IOException e) {
// ignore
}
}
}, "ldclient-cleanup-thread"));
Runtime.getRuntime()
.addShutdownHook(
new Thread(
new Runnable() {
public void run() {
try {
client.close();
} catch (IOException e) {
// ignore
}
}
},
"ldclient-cleanup-thread"));

// Keeps example application alive.
Object mon = new Object();
Expand Down

0 comments on commit 01357f6

Please sign in to comment.