Coverage Report - com.ctrip.framework.apollo.ds.ApolloDataSourceProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
ApolloDataSourceProvider
0%
0/18
0%
0/4
3
 
 1  
 package com.ctrip.framework.apollo.ds;
 2  
 
 3  
 import org.codehaus.plexus.logging.LogEnabled;
 4  
 import org.codehaus.plexus.logging.Logger;
 5  
 import org.unidal.dal.jdbc.datasource.DataSourceProvider;
 6  
 import org.unidal.dal.jdbc.datasource.model.entity.DataSourcesDef;
 7  
 import org.unidal.dal.jdbc.datasource.model.transform.DefaultSaxParser;
 8  
 import org.unidal.lookup.annotation.Named;
 9  
 
 10  
 import com.ctrip.framework.apollo.ConfigFile;
 11  
 import com.ctrip.framework.apollo.ConfigService;
 12  
 import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
 13  
 import com.ctrip.framework.foundation.Foundation;
 14  
 
 15  
 /**
 16  
  * Data source provider based on Apollo configuration service.
 17  
  * <p>
 18  
  *
 19  
  * Use following component definition to replace default
 20  
  * <code>DataSourceProvider</code>:
 21  
  * <p>
 22  
  * <code><pre>
 23  
  *   public List<Component> defineComponents() {
 24  
  *      List<Component> all = new ArrayList<>();
 25  
  * 
 26  
  *      all.add(A(ApolloDataSourceProvider.class));
 27  
  * 
 28  
  *      return all;
 29  
  *   }
 30  
  * </pre></code>
 31  
  * 
 32  
  * <b>WARNING:</b> all defined <code>DataSourceProvider</code> components will
 33  
  * be taken affect. DO NOT define unused <code>DataSourceProvider</code>
 34  
  * component.
 35  
  */
 36  
 @Named(type = DataSourceProvider.class, value = "apollo")
 37  0
 public class ApolloDataSourceProvider implements DataSourceProvider, LogEnabled {
 38  
    private Logger m_logger;
 39  
 
 40  
    private DataSourcesDef m_def;
 41  
 
 42  
    @Override
 43  
    public DataSourcesDef defineDatasources() {
 44  0
       if (m_def == null) {
 45  0
          ConfigFile file = ConfigService.getConfigFile("datasources", ConfigFileFormat.XML);
 46  0
          String appId = Foundation.app().getAppId();
 47  0
          String envType = Foundation.server().getEnvType();
 48  
 
 49  0
          if (file.hasContent()) {
 50  0
             String content = file.getContent();
 51  
 
 52  0
             m_logger.info(String.format("Found datasources.xml from Apollo(env=%s, app.id=%s)!", envType, appId));
 53  
 
 54  
             try {
 55  0
                m_def = DefaultSaxParser.parse(content);
 56  0
             } catch (Exception e) {
 57  0
                throw new IllegalStateException(String.format("Error when parsing datasources.xml from Apollo(env=%s, app.id=%s)!", envType, appId), e);
 58  0
             }
 59  0
          } else {
 60  0
             m_logger.warn(String.format("Can't get datasources.xml from Apollo(env=%s, app.id=%s)!", envType, appId));
 61  0
             m_def = new DataSourcesDef();
 62  
          }
 63  
       }
 64  
 
 65  0
       return m_def;
 66  
    }
 67  
 
 68  
    @Override
 69  
    public void enableLogging(Logger logger) {
 70  0
       m_logger = logger;
 71  0
    }
 72  
 }