/**
 * Copyright 2013 BlackLocus
 *
 * Licensed 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.blacklocus.qs.worker.aws;

import com.blacklocus.qs.worker.QSWorkerIdService;
import com.blacklocus.qs.worker.simple.HostNameQSWorkerIdService;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author Jason Dunkelberger (dirkraft)
 */
public class AmazonEC2WorkerIdService implements QSWorkerIdService {

    private static final Logger LOG = LoggerFactory.getLogger(AmazonEC2WorkerIdService.class);


    public static final String META_PFX = "http://169.254.169.254/latest/meta-data/";
    public static final String META_INSTANCE_ID = "instance-id";

    public static final String PROP_SKIP_EC2_META = "com.blacklocus.qs.aws.disableEC2Meta";


    private final QSWorkerIdService fallback = new HostNameQSWorkerIdService();

    @Override
    public String getWorkerId() {
        return getEC2Meta(META_INSTANCE_ID, false);
    }

    public String getEC2Meta(String metaKey, boolean suppressError) {
        String id = null;

        if (!Boolean.valueOf(System.getProperty(PROP_SKIP_EC2_META, "false"))) {
            String inputLine;
            BufferedReader in = null;
            try {
                URL ec2metadata = new URL(META_PFX + metaKey);
                URLConnection connection = ec2metadata.openConnection();
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                while ((inputLine = in.readLine()) != null) {
                    id = inputLine;
                }
            } catch (Exception e) {
                if (!suppressError) {
                    LOG.warn("Problem retrieving EC2 metadata: " + metaKey + "\n" +
                            "\tIs this an EC2 machine? If not, set " + PROP_SKIP_EC2_META + " to true to skip this lookup.", e);
                }
            } finally {
                IOUtils.closeQuietly(in);
            }
        }

        if (StringUtils.isBlank(id)) {
            id = fallback.getWorkerId();
        }

        return id;
    }

}

@@ -626,16 +626,60 @@
 r.aws;%0A%0A
+import com.amazonaws.util.EC2MetadataUtils;%0A
 import c
@@ -791,46 +791,8 @@
 ce;%0A
-import org.apache.commons.io.IOUtils;%0A
 impo
@@ -836,550 +836,126 @@
 ls;%0A
-import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport java.io.BufferedReader;%0Aimport java.io.InputStreamReader;%0Aimport java.net.URL;%0Aimport java.net.URLConnection;%0A%0A/**%0A * @author Jason Dunkelberger (dirkraft)%0A */%0Apublic class AmazonEC2WorkerIdService implements QSWorkerIdService %7B%0A%0A    private static final Logger LOG = LoggerFactory.getLogger(AmazonEC2WorkerIdService.class);%0A%0A%0A    public static final String META_PFX = %22http://169.254.169.254/latest/meta-data/%22;%0A    public static final String META_INSTANCE_ID = %22instance-id%22;
+%0A/**%0A * @author Jason Dunkelberger (dirkraft)%0A */%0Apublic class AmazonEC2WorkerIdService implements QSWorkerIdService %7B
 %0A%0A  
@@ -1045,17 +1045,16 @@
 Meta%22;%0A%0A
-%0A
     priv
@@ -1186,1052 +1186,158 @@
     
-return getEC2Meta(META_INSTANCE_ID, false);%0A    %7D%0A%0A    public String getEC2Meta(String metaKey, boolean suppressError) %7B%0A        String id = null;%0A%0A        if (!Boolean.valueOf(System.getProperty(PROP_SKIP_EC2_META, %22false%22))) %7B%0A            String inputLine;%0A            BufferedReader in = null;%0A            try %7B%0A                URL ec2metadata = new URL(META_PFX + metaKey);%0A                URLConnection connection = ec2metadata.openConnection();%0A                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));%0A                while ((inputLine = in.readLine()) != null) %7B%0A                    id = inputLine;%0A                %7D%0A            %7D catch (Exception e) %7B%0A                if (!suppressError) %7B%0A                    LOG.warn(%22Problem retrieving EC2 metadata: %22 + metaKey + %22%5Cn%22 +%0A                            %22%5CtIs this an EC2 machine? If not, set %22 + PROP_SKIP_EC2_META + %22 to true to skip this lookup.%22, e);%0A                %7D%0A            %7D finally %7B%0A                IOUtils.closeQuietly(in);%0A            %7D
+String id = null;%0A%0A        if (!Boolean.valueOf(System.getProperty(PROP_SKIP_EC2_META, %22false%22))) %7B%0A            id = EC2MetadataUtils.getInstanceId();
 %0A   

make ec2 worker id service use aws sdk MetadataUtils