/**
   * Returns GP ServiceClient instance used for this for this maven session.
   *
   * <p>This implementation resolves service credentials in the following order:
   *
   * <ol>
   *   <li>A json file specified by user defined property 'gp.credentials.json'. This is typically
   *       specified by a command line argument, e.g.<br>
   *       <code>-D gp.credentials.json=/home/yoshito/gpcreds.json</code>
   *   <li>A json file specified by &lt;credentialsJson&gt; in pom.xml
   *   <li>A set of fields specified by &lt;credentials&gt; in pom.xml
   * </ol>
   *
   * @return An instance of ServiceClient.
   * @throws MojoFailureException on a failure.
   */
  protected ServiceClient getServiceClient() throws MojoFailureException {
    if (gpClient == null) {
      synchronized (this) {
        Credentials creds = credentials;
        if (credentialsJson != null) {
          getLog().info("Reading GP service credentials from " + credentialsJson.getAbsolutePath());
          try (InputStreamReader reader =
              new InputStreamReader(new FileInputStream(credentialsJson), StandardCharsets.UTF_8)) {
            Gson gson = new Gson();
            creds = gson.fromJson(reader, Credentials.class);
          } catch (IOException e) {
            throw new MojoFailureException(
                "Error while reading the specified JSON credential file.", e);
          } catch (JsonSyntaxException e) {
            throw new MojoFailureException("Bad JSON credential format.", e);
          }
        }

        if (creds == null) {
          throw new MojoFailureException(
              "Globalization Pipeline service credentials are not specified.");
        }

        if (!creds.isValid()) {
          throw new MojoFailureException("Bad credentials: " + creds);
        }

        getLog().debug("Using GP service credentials " + creds);

        gpClient =
            ServiceClient.getInstance(
                ServiceAccount.getInstance(
                    creds.getUrl(), creds.getInstanceId(),
                    creds.getUserId(), creds.getPassword()));
      }
    }
    return gpClient;
  }