Coverage Report - com.ctrip.apollo.portal.service.AppService
 
Classes in this File Line Coverage Branch Coverage Complexity
AppService
5%
2/38
0%
0/10
3
 
 1  
 package com.ctrip.apollo.portal.service;
 2  
 
 3  
 import java.util.List;
 4  
 
 5  
 import org.slf4j.Logger;
 6  
 import org.slf4j.LoggerFactory;
 7  
 import org.springframework.beans.factory.annotation.Autowired;
 8  
 import org.springframework.http.HttpStatus;
 9  
 import org.springframework.stereotype.Service;
 10  
 import org.springframework.web.client.HttpClientErrorException;
 11  
 import org.springframework.web.client.HttpStatusCodeException;
 12  
 
 13  
 import com.ctrip.apollo.common.utils.ExceptionUtils;
 14  
 import com.ctrip.apollo.core.dto.AppDTO;
 15  
 import com.ctrip.apollo.core.enums.Env;
 16  
 import com.ctrip.apollo.core.exception.BadRequestException;
 17  
 import com.ctrip.apollo.core.exception.ServiceException;
 18  
 import com.ctrip.apollo.portal.PortalSettings;
 19  
 import com.ctrip.apollo.portal.api.AdminServiceAPI;
 20  
 import com.ctrip.apollo.portal.entity.EnvClusterInfo;
 21  
 
 22  
 @Service
 23  1
 public class AppService {
 24  
 
 25  1
   private Logger logger = LoggerFactory.getLogger(AppService.class);
 26  
 
 27  
   @Autowired
 28  
   private PortalSettings portalSettings;
 29  
 
 30  
   @Autowired
 31  
   private AdminServiceAPI.AppAPI appAPI;
 32  
 
 33  
   @Autowired ClusterService clusterService;
 34  
 
 35  
   public List<AppDTO> findAll(Env env) {
 36  0
     return appAPI.findApps(env);
 37  
   }
 38  
 
 39  
   public AppDTO load(String appId) {
 40  
     //轮询环境直到能找到此app的信息
 41  0
     AppDTO app = null;
 42  0
     boolean isCallAdminServiceError = false;
 43  0
     for (Env env : portalSettings.getEnvs()) {
 44  
       try {
 45  0
         app = appAPI.loadApp(env, appId);
 46  0
         break;
 47  0
       } catch (HttpClientErrorException e) {
 48  
         //not exist maybe because create app fail.
 49  0
         if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
 50  0
           logger.warn("app:{} in {} not exist", appId, env);
 51  
         } else {
 52  0
           isCallAdminServiceError = true;
 53  0
           logger.error("load app info({}) from env:{} error.", appId, env);
 54  
         }
 55  
       }
 56  0
     }
 57  0
     if (app == null) {
 58  0
       if (isCallAdminServiceError){
 59  0
         throw new ServiceException("call admin service error");
 60  
       }else {
 61  0
         throw new BadRequestException(String.format("invalid app id %s", appId));
 62  
       }
 63  
     }
 64  
 
 65  0
     return app;
 66  
   }
 67  
 
 68  
   public AppDTO load(Env env, String appId){
 69  0
     return appAPI.loadApp(env, appId);
 70  
   }
 71  
 
 72  
   public void createAppInAllEnvs(AppDTO app) {
 73  0
     List<Env> envs = portalSettings.getEnvs();
 74  0
     for (Env env : envs) {
 75  
       try {
 76  0
         appAPI.createApp(env, app);
 77  0
       } catch (HttpStatusCodeException e) {
 78  0
         logger.error(ExceptionUtils.toString(e));
 79  0
         throw e;
 80  0
       }
 81  0
     }
 82  0
   }
 83  
 
 84  
   public void createApp(Env env, AppDTO app) {
 85  
     try {
 86  0
       appAPI.createApp(env, app);
 87  0
     } catch (HttpStatusCodeException e) {
 88  0
       logger.error(ExceptionUtils.toString(e));
 89  0
       throw e;
 90  0
     }
 91  0
   }
 92  
 
 93  
   public EnvClusterInfo createEnvNavNode(Env env, String appId){
 94  0
     EnvClusterInfo node = new EnvClusterInfo(env);
 95  0
     node.setClusters(clusterService.findClusters(env, appId));
 96  0
     return node;
 97  
   }
 98  
 
 99  
 }