Efficiently detect datacenter based on server hostname
There are a few things which I think will simplify your code a lot, and it all centers around your Enum.

Firstly, there is no need in your enum to have the BY_CODE_MAP system. Remove it. You can very efficiently (and with the identical results) use the method:

    public static String forCode(int code) {
        return (code &gt;= 0 &amp;&amp; code &lt; values().length) ? values()[code].name() : null;
    }


The values() array in an enum will always be indexed in the same order as the ORDINAL of each Enum.

The second thing, is that you should only be calculating the Datacenter location once for each JVM. Having it as a method which is called potentially multiple times, each time interrogating the java.net.* layer for hostnames, is inefficient.

I suggest you add a static initializer method to your Enum that 'knows'.

Consider the following changes to your enum, and replace the logic in your code:

private String getCurrentDatacenter(){
    DatacenterEnum us = DatacenterEnum.getCurrentDatacenter();
    return us == null ? null : us.name();
}


Then, your Enum will look like (I have commented out the code I think is redundant):

import java.net.InetAddress;
import java.net.UnknownHostException;


public enum DatacenterEnum {
    DC1, DC2, DC3;

//    private static final Map&lt;Integer, DatacenterEnum&gt; BY_CODE_MAP = new LinkedHashMap&lt;Integer, DatacenterEnum&gt;();

//    static {
//        for (DatacenterEnum rae : DatacenterEnum.values()) {
//            BY_CODE_MAP.put(rae.ordinal(), rae);
//        }
//    }

    public static String forCode(int code) {
        return (code &gt;= 0 &amp;&amp; code &lt; values().length) ? values()[code].name() : null;
//        return BY_CODE_MAP.get(code).name();
    }

    private static final DatacenterEnum ourlocation = compareLocation();

    private static DatacenterEnum compareLocation() {
        String ourhost = getHostName();
        for (DatacenterEnum dc : values()) {
            String namepart = "." + dc.name().toLowerCase() + ".";
            if (ourhost.indexOf(namepart) &gt;= 0) {
                return dc;
            }
        }
        return null;
    }

    private static final String getHostName() {
        try {
            return InetAddress.getLocalHost().getCanonicalHostName().toLowerCase();
        } catch (UnknownHostException e) {      
            // s_logger.debug(LogLevel.ERROR+"\t"+e.getStackTrace().toString());
            return "localhost";
        }
    }

    public static DatacenterEnum getCurrentDatacenter() {
        return ourlocation;
    }

    public static void main(String[] args) {
        System.out.println(DatacenterEnum.getCurrentDatacenter());
    }

}


If you ever have additional datacenters, you can simply add a new name to the enum and the rest will work. Also, you can add values that are not the same naming convention... for example, if you add the ENUM "EU1" for your first european datacenter, it will match aaa.eu1.xxx.yyy