/**
 * 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 org.apache.storm.nimbus;

import org.apache.storm.Config;
import org.apache.storm.utils.ObjectReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;

public class NimbusInfo implements Serializable {
    private static final long serialVersionUID = 2161446155116099333L;
    private static final Logger LOG = LoggerFactory.getLogger(NimbusInfo.class);
    private static final String DELIM = ":";

    private String host;
    private int port;
    private boolean isLeader;

    public NimbusInfo(String host, int port, boolean isLeader) {
        this.host = host;
        this.port = port;
        this.isLeader = isLeader;
    }

    public static NimbusInfo parse(String nimbusInfo) {
        String[] hostAndPort = nimbusInfo.split(DELIM);
        if(hostAndPort != null && hostAndPort.length == 2) {
            return new NimbusInfo(hostAndPort[0], Integer.parseInt(hostAndPort[1]), false);
        } else {
            throw new RuntimeException("nimbusInfo should have format of host:port, invalid string " + nimbusInfo);
        }
    }

    public static NimbusInfo fromConf(Map<String, Object> conf) {
        try {
            String host = InetAddress.getLocalHost().getCanonicalHostName();
            if (conf.containsKey(Config.STORM_LOCAL_HOSTNAME)) {
                host = (String) conf.get(Config.STORM_LOCAL_HOSTNAME);
                LOG.info("Overriding nimbus host to storm.local.hostname -> {}", host);
            } else {
                LOG.info("Nimbus figures out its name to {}", host);
            }

            int port = ObjectReader.getInt(conf.get(Config.NIMBUS_THRIFT_PORT), 6627);
            return new NimbusInfo(host, port, false);

        } catch (UnknownHostException e) {
            throw new RuntimeException("Something wrong with network/dns config, host cant figure out its name", e);
        }
    }

    public String toHostPortString() {
        return String.format("%s%s%s",host,DELIM,port);
    }

    public boolean isLeader() {
        return isLeader;
    }

    public int getPort() {
        return port;
    }

    public String getHost() {
        return host;
    }

    public void setLeader(boolean isLeader) {
        this.isLeader = isLeader;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof NimbusInfo)) return false;

        NimbusInfo that = (NimbusInfo) o;

        if (isLeader != that.isLeader) return false;
        if (port != that.port) return false;
        return host.equals(that.host);
    }

    @Override
    public int hashCode() {
        int result = host.hashCode();
        result = 31 * result + port;
        result = 31 * result + (isLeader ? 1 : 0);
        return result;
    }

    @Override
    public String toString() {
        return "NimbusInfo{" +
                "host='" + host + '\'' +
                ", port=" + port +
                ", isLeader=" + isLeader +
                '}';
    }
}

@@ -830,16 +830,199 @@
 imbus;%0A%0A
+import java.io.Serializable;%0Aimport java.net.InetAddress;%0Aimport java.net.UnknownHostException;%0Aimport java.util.Map;%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A%0A
 import o
@@ -1151,127 +1151,8 @@
 y;%0A%0A
-import java.io.Serializable;%0Aimport java.net.InetAddress;%0Aimport java.net.UnknownHostException;%0Aimport java.util.Map;%0A%0A
 publ
@@ -1352,53 +1352,8 @@
 ss);
-%0A    private static final String DELIM = %22:%22;
 %0A%0A  
@@ -1505,372 +1505,487 @@
     
-this.host = host;%0A        this.port = port;%0A        this.isLeader = isLeader;%0A    %7D%0A%0A    public static NimbusInfo parse(String nimbusInfo) %7B%0A        String%5B%5D hostAndPort = nimbusInfo.split(DELIM);%0A        if(hostAndPort != null && hostAndPort.length == 2) %7B%0A            return new NimbusInfo(hostAndPort%5B0%5D, Integer.parseInt(hostAndPort%5B1%5D), false);%0A        %7D else
+if (host == null) throw new NullPointerException(%22Host cannot be null%22);%0A        if (port %3C= 0) throw new IllegalArgumentException(%22Port must be positive%22);%0A        this.host = host;%0A        this.port = port;%0A        this.isLeader = isLeader;%0A    %7D%0A%0A    private static final Pattern HOST_PORT_PATTERN = Pattern.compile(%22%5E(.*):(%5B0-9%5D+)$%22);%0A    public static NimbusInfo parse(String nimbusInfo) %7B%0A        Matcher m = HOST_PORT_PATTERN.matcher(nimbusInfo);%0A        if (!m.matches())
  %7B%0A 
@@ -2101,32 +2101,111 @@
 nfo);%0A        %7D%0A
+        return new NimbusInfo(m.group(1), Integer.valueOf(m.group(2)), false);%0A
     %7D%0A%0A    publi
@@ -3071,18 +3071,17 @@
 rmat(%22%25s
-%25s
+:
 %25s%22,host
@@ -3085,14 +3085,8 @@
 ost,
-DELIM,
 port

Fix parsing of host:port to deal with IPv6 addresses