Coverage Report - com.ctrip.framework.apollo.util.ConfigUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigUtil
59%
42/71
44%
8/18
1.632
 
 1  
 package com.ctrip.framework.apollo.util;
 2  
 
 3  
 import com.google.common.base.Preconditions;
 4  
 import com.google.common.base.Strings;
 5  
 
 6  
 import com.ctrip.framework.apollo.core.ConfigConsts;
 7  
 import com.ctrip.framework.apollo.core.MetaDomainConsts;
 8  
 import com.ctrip.framework.apollo.core.enums.Env;
 9  
 import com.ctrip.framework.apollo.core.enums.EnvUtils;
 10  
 import com.ctrip.framework.foundation.Foundation;
 11  
 
 12  
 import org.slf4j.Logger;
 13  
 import org.slf4j.LoggerFactory;
 14  
 import org.unidal.lookup.annotation.Named;
 15  
 import org.unidal.net.Networks;
 16  
 
 17  
 import java.util.concurrent.TimeUnit;
 18  
 
 19  
 /**
 20  
  * @author Jason Song(song_s@ctrip.com)
 21  
  */
 22  
 @Named(type = ConfigUtil.class)
 23  
 public class ConfigUtil {
 24  1
   private static final Logger logger = LoggerFactory.getLogger(ConfigUtil.class);
 25  19
   private int refreshInterval = 5;
 26  19
   private TimeUnit refreshIntervalTimeUnit = TimeUnit.MINUTES;
 27  19
   private int connectTimeout = 5000; //5 seconds
 28  19
   private int readTimeout = 10000; //10 seconds
 29  
   private String cluster;
 30  19
   private int loadConfigQPS = 2; //2 times per second
 31  19
   private int longPollQPS = 2; //2 times per second
 32  
 
 33  19
   public ConfigUtil() {
 34  19
     initRefreshInterval();
 35  19
     initConnectTimeout();
 36  19
     initReadTimeout();
 37  19
     initCluster();
 38  19
     initQPS();
 39  19
   }
 40  
 
 41  
   /**
 42  
    * Get the app id for the current application.
 43  
    *
 44  
    * @return the app id
 45  
    * @throws IllegalStateException if app id is not set
 46  
    */
 47  
   public String getAppId() {
 48  0
     String appId = Foundation.app().getAppId();
 49  0
     Preconditions.checkState(appId != null, "app.id is not set");
 50  0
     return appId;
 51  
   }
 52  
 
 53  
   /**
 54  
    * Get the data center info for the current application.
 55  
    *
 56  
    * @return the current data center, null if there is no such info.
 57  
    */
 58  
   public String getDataCenter() {
 59  5
     return Foundation.server().getDataCenter();
 60  
   }
 61  
 
 62  
   private void initCluster() {
 63  
     //Load data center from system property
 64  19
     cluster = System.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY);
 65  
 
 66  
     //Use data center as cluster
 67  19
     if (Strings.isNullOrEmpty(cluster)) {
 68  19
       cluster = getDataCenter();
 69  
     }
 70  
 
 71  
     //Use default cluster
 72  19
     if (Strings.isNullOrEmpty(cluster)) {
 73  11
       cluster = ConfigConsts.CLUSTER_NAME_DEFAULT;
 74  
     }
 75  19
   }
 76  
 
 77  
   /**
 78  
    * Get the cluster name for the current application.
 79  
    *
 80  
    * @return the cluster name, or "default" if not specified
 81  
    */
 82  
   public String getCluster() {
 83  0
     return cluster;
 84  
   }
 85  
 
 86  
   /**
 87  
    * Get the current environment.
 88  
    *
 89  
    * @return the env
 90  
    * @throws IllegalStateException if env is set
 91  
    */
 92  
   public Env getApolloEnv() {
 93  0
     Env env = EnvUtils.transformEnv(Foundation.server().getEnvType());
 94  0
     Preconditions.checkState(env != null, "env is not set");
 95  0
     return env;
 96  
   }
 97  
 
 98  
   public String getLocalIp() {
 99  817
     return Networks.forIp().getLocalHostAddress();
 100  
   }
 101  
 
 102  
   public String getMetaServerDomainName() {
 103  32
     return MetaDomainConsts.getDomain(getApolloEnv());
 104  
   }
 105  
 
 106  
   private void initConnectTimeout() {
 107  19
     String customizedConnectTimeout = System.getProperty("apollo.connectTimeout");
 108  19
     if (!Strings.isNullOrEmpty(customizedConnectTimeout)) {
 109  
       try {
 110  0
         connectTimeout = Integer.parseInt(customizedConnectTimeout);
 111  0
       } catch (Throwable ex) {
 112  0
         logger.error("Config for apollo.connectTimeout is invalid: {}", customizedConnectTimeout);
 113  0
       }
 114  
     }
 115  19
   }
 116  
 
 117  
   public int getConnectTimeout() {
 118  101
     return connectTimeout;
 119  
   }
 120  
 
 121  
   private void initReadTimeout() {
 122  19
     String customizedReadTimeout = System.getProperty("apollo.readTimeout");
 123  19
     if (!Strings.isNullOrEmpty(customizedReadTimeout)) {
 124  
       try {
 125  0
         readTimeout = Integer.parseInt(customizedReadTimeout);
 126  0
       } catch (Throwable ex) {
 127  0
         logger.error("Config for apollo.readTimeout is invalid: {}", customizedReadTimeout);
 128  0
       }
 129  
     }
 130  19
   }
 131  
 
 132  
   public int getReadTimeout() {
 133  71
     return readTimeout;
 134  
   }
 135  
 
 136  
   private void initRefreshInterval() {
 137  19
     String customizedRefreshInterval = System.getProperty("apollo.refreshInterval");
 138  19
     if (!Strings.isNullOrEmpty(customizedRefreshInterval)) {
 139  
       try {
 140  0
         refreshInterval = Integer.parseInt(customizedRefreshInterval);
 141  0
       } catch (Throwable ex) {
 142  0
         logger.error("Config for apollo.refreshInterval is invalid: {}", customizedRefreshInterval);
 143  0
       }
 144  
     }
 145  19
   }
 146  
 
 147  
   public int getRefreshInterval() {
 148  18
     return refreshInterval;
 149  
   }
 150  
 
 151  
   public TimeUnit getRefreshIntervalTimeUnit() {
 152  12
     return refreshIntervalTimeUnit;
 153  
   }
 154  
 
 155  
   private void initQPS() {
 156  19
     String customizedLoadConfigQPS = System.getProperty("apollo.loadConfigQPS");
 157  19
     if (!Strings.isNullOrEmpty(customizedLoadConfigQPS)) {
 158  
       try {
 159  0
         loadConfigQPS = Integer.parseInt(customizedLoadConfigQPS);
 160  0
       } catch (Throwable ex) {
 161  0
         logger.error("Config for apollo.loadConfigQPS is invalid: {}", customizedLoadConfigQPS);
 162  0
       }
 163  
     }
 164  
 
 165  19
     String customizedLongPollQPS = System.getProperty("apollo.longPollQPS");
 166  19
     if (!Strings.isNullOrEmpty(customizedLongPollQPS)) {
 167  
       try {
 168  0
         longPollQPS = Integer.parseInt(customizedLongPollQPS);
 169  0
       } catch (Throwable ex) {
 170  0
         logger.error("Config for apollo.longPollQPS is invalid: {}", customizedLongPollQPS);
 171  0
       }
 172  
     }
 173  19
   }
 174  
 
 175  
   public int getLoadConfigQPS() {
 176  0
     return loadConfigQPS;
 177  
   }
 178  
 
 179  
   public int getLongPollQPS() {
 180  0
     return longPollQPS;
 181  
   }
 182  
 
 183  
   public String getDefaultLocalCacheDir() {
 184  
     //TODO call Framework Foundation to get the default local cache dir
 185  6
     return String.format("/opt/data/%s", getAppId());
 186  
   }
 187  
 }