content: /*
   * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
   */
  package com.amazon.gamelift.agent.manager;
  
  import com.amazon.gamelift.agent.websocket.SdkWebsocketEndpointProvider;
  import com.amazon.gamelift.agent.module.ConfigModule;
  import com.google.common.annotations.VisibleForTesting;
  import com.google.common.base.Strings;
  import com.google.common.collect.ImmutableMap;
  
  import javax.inject.Inject;
  import javax.inject.Named;
  import java.util.Iterator;
  import java.util.Map;
  
  public class ProcessEnvironmentManager {
      @VisibleForTesting static final String ENV_VAR_WEBSOCKET_URL = "GAMELIFT_SDK_WEBSOCKET_URL";
      @VisibleForTesting static final String ENV_VAR_COMPUTE_AUTH_TOKEN = "GAMELIFT_SDK_AUTH_TOKEN";
      @VisibleForTesting static final String ENV_VAR_FLEET_ID = "GAMELIFT_SDK_FLEET_ID";
      @VisibleForTesting static final String ENV_VAR_HOST_ID = "GAMELIFT_SDK_HOST_ID";
      @VisibleForTesting static final String ENV_VAR_PROCESS_ID = "GAMELIFT_SDK_PROCESS_ID";
  
      private final String fleetId;
      private final String computeName;
      private final ComputeAuthTokenManager computeAuthTokenManager;
      private final SdkWebsocketEndpointProvider sdkWebsocketEndpointProvider;
  
      /**
       * Constructor for ProcessEnvironmentManager
       * @param fleetId
       * @param computeName
       * @param computeAuthTokenManager
       * @param sdkWebsocketEndpointProvider
       */
      @Inject
      public ProcessEnvironmentManager(
              @Named(ConfigModule.FLEET_ID) final String fleetId,
              @Named(ConfigModule.COMPUTE_NAME) final String computeName,
              final ComputeAuthTokenManager computeAuthTokenManager,
              final SdkWebsocketEndpointProvider sdkWebsocketEndpointProvider) {
          this.fleetId = fleetId;
          this.computeName = computeName;
          this.computeAuthTokenManager = computeAuthTokenManager;
          this.sdkWebsocketEndpointProvider = sdkWebsocketEndpointProvider;
      }
  
      /**
       * Creates a map of all necessary Environment Variables to run a GameProcess. These
       * variables include information which is read by the Amazon GameLift SDK in order to communicate
       * over the SDK Websocket.
       *
       * @param processUUID of the process, generated by the GameLift agent and used to communicate with GameLift services
       * @returns map containing all the necessary environment variables for the process with the given UUID
       */
      public Map<String, String> getProcessEnvironmentVariables(final String processUUID) {
          return new ImmutableMap.Builder<String, String>()
                  .put(ENV_VAR_WEBSOCKET_URL, Strings.nullToEmpty(sdkWebsocketEndpointProvider.getSdkWebsocketEndpoint()))
                  .put(ENV_VAR_COMPUTE_AUTH_TOKEN, Strings.nullToEmpty(computeAuthTokenManager.getComputeAuthToken()))
                  .put(ENV_VAR_FLEET_ID, Strings.nullToEmpty(fleetId))
                  .put(ENV_VAR_HOST_ID, Strings.nullToEmpty(computeName))
                  .put(ENV_VAR_PROCESS_ID, Strings.nullToEmpty(processUUID))
                  .build();
      }
  
      /**
       * Helper method to print out specific environment variables.
       */
      public String getPrintableEnvironmentVariables(final String processUUID) {
          final StringBuilder sb = new StringBuilder();
          final Iterator<Map.Entry<String, String>> iterator = getProcessEnvironmentVariables(processUUID)
                  .entrySet().iterator();
          while (iterator.hasNext()) {
              final Map.Entry<String, String> entry = iterator.next();
              if (!entry.getKey().contains(ENV_VAR_COMPUTE_AUTH_TOKEN)) {
                  sb.append(entry.getKey()).append('=').append('"').append(entry.getValue()).append('"');
                  if (iterator.hasNext()) {
                      sb.append(", ");
                  }
              }
          }
          return sb.toString();
      }
  }
  