/*
 * Copyright (c) 2015 Spotify AB.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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
 *
 *   http://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 com.spotify.heroic.metric.bigtable.credentials;

import static com.google.common.base.Preconditions.checkNotNull;

import java.nio.file.Files;
import java.nio.file.Path;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.cloud.bigtable.config.CredentialOptions;
import com.spotify.heroic.metric.bigtable.CredentialsBuilder;

import lombok.ToString;

@ToString(of = { "path" })
public class JsonCredentialsBuilder implements CredentialsBuilder {
    private final Path path;

    @JsonCreator
    public JsonCredentialsBuilder(@JsonProperty("path") Path path) {
        this.path = checkNotNull(path, "path");
    }

    @Override
    public CredentialOptions build() throws Exception {
        // XXX: You have to leave the input stream open for BigtableSession to use it.
        //      This does 'leak' an input stream, but it's only once, so we'll live with it for now.
        //      Reported here: https://github.com/GoogleCloudPlatform/cloud-bigtable-client/issues/534
        return CredentialOptions.jsonCredentials(Files.newInputStream(path));
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        private Path path;

        public Builder path(Path path) {
            checkNotNull(path, "path");

            if (!Files.isReadable(path)) {
                throw new IllegalArgumentException("Path must be readable: " + path.toAbsolutePath());
            }

            this.path = path;
            return this;
        }

        public JsonCredentialsBuilder build() {
            return new JsonCredentialsBuilder(path);
        }
    }
}
@@ -1016,16 +1016,71 @@
 le.Path;
+%0Aimport java.nio.file.Paths;%0Aimport java.util.Optional;
 %0A%0Aimport
@@ -1418,24 +1418,93 @@
 lsBuilder %7B%0A
+    public static final String DEFAULT_PATH = %22./credentials.json%22;%0A%0A
     private 
@@ -2233,25 +2233,54 @@
 private 
+Optional%3C
 Path
+%3E
  path
+ = Optional.empty()
 ;%0A%0A     
@@ -2354,16 +2354,208 @@
 %22path%22);
+%0A            this.path = Optional.of(path);%0A            return this;%0A        %7D%0A%0A        public JsonCredentialsBuilder build() %7B%0A            final Path p = path.orElse(Paths.get(DEFAULT_PATH));
 %0A%0A      
@@ -2583,19 +2583,16 @@
 adable(p
-ath
 )) %7B%0A   
@@ -2668,19 +2668,16 @@
 e: %22 + p
-ath
 .toAbsol
@@ -2707,122 +2707,8 @@
  %7D%0A%0A
-            this.path = path;%0A            return this;%0A        %7D%0A%0A        public JsonCredentialsBuilder build() %7B%0A
     
@@ -2750,19 +2750,16 @@
 uilder(p
-ath
 );%0A     

fix json credentials