Coverage Report - com.ctrip.apollo.util.ConfigUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigUtil
38%
7/18
0%
0/6
1.111
 
 1  
 package com.ctrip.apollo.util;
 2  
 
 3  
 import com.google.common.base.Preconditions;
 4  
 
 5  
 import com.ctrip.apollo.core.ConfigConsts;
 6  
 import com.ctrip.apollo.core.MetaDomainConsts;
 7  
 import com.ctrip.apollo.core.enums.Env;
 8  
 import com.ctrip.apollo.core.enums.EnvUtils;
 9  
 import com.ctrip.framework.foundation.Foundation;
 10  
 
 11  
 import org.unidal.lookup.annotation.Named;
 12  
 
 13  
 import java.util.concurrent.TimeUnit;
 14  
 
 15  
 /**
 16  
  * @author Jason Song(song_s@ctrip.com)
 17  
  */
 18  
 @Named(type = ConfigUtil.class)
 19  17
 public class ConfigUtil {
 20  
   //TODO read from config?
 21  
   private static final int refreshInterval = 5;
 22  1
   private static final TimeUnit refreshIntervalTimeUnit = TimeUnit.MINUTES;
 23  
   private static final int connectTimeout = 5000; //5 seconds
 24  
   private static final int readTimeout = 10000; //10 seconds
 25  
 
 26  
   /**
 27  
    * Get the app id for the current application.
 28  
    * @return the app id
 29  
    * @throws IllegalStateException if app id is not set
 30  
    */
 31  
   public String getAppId() {
 32  0
     String appId = Foundation.app().getAppId();
 33  0
     Preconditions.checkState(appId != null, "app.id is not set");
 34  0
     return appId;
 35  
   }
 36  
 
 37  
   /**
 38  
    * Get the data center info for the current application.
 39  
    * @return the current data center, null if there is no such info.
 40  
    */
 41  
   public String getDataCenter() {
 42  0
     return Foundation.server().getDataCenter();
 43  
   }
 44  
 
 45  
   /**
 46  
    * Get the cluster name for the current application.
 47  
    * @return the cluster name, or "default" if not specified
 48  
    */
 49  
   public String getCluster() {
 50  0
     String cluster = System.getProperty("apollo.cluster");
 51  0
     if (cluster == null) {
 52  0
       cluster = ConfigConsts.CLUSTER_NAME_DEFAULT;
 53  
     }
 54  0
     return cluster;
 55  
   }
 56  
 
 57  
   /**
 58  
    * Get the current environment.
 59  
    * @return the env
 60  
    * @throws IllegalStateException if env is set
 61  
    */
 62  
   public Env getApolloEnv() {
 63  0
     Env env = EnvUtils.transformEnv(Foundation.server().getEnvType());
 64  0
     Preconditions.checkState(env != null, "env is not set");
 65  0
     return env;
 66  
   }
 67  
 
 68  
   public String getMetaServerDomainName() {
 69  32
     return MetaDomainConsts.getDomain(getApolloEnv());
 70  
   }
 71  
 
 72  
   public int getConnectTimeout() {
 73  103
     return connectTimeout;
 74  
   }
 75  
 
 76  
   public int getReadTimeout() {
 77  73
     return readTimeout;
 78  
   }
 79  
 
 80  
   public int getRefreshInterval() {
 81  12
     return refreshInterval;
 82  
   }
 83  
 
 84  
   public TimeUnit getRefreshTimeUnit() {
 85  8
     return refreshIntervalTimeUnit;
 86  
   }
 87  
 }