View Javadoc

1   package com.ning.metrics.goodwill.binder.modules;
2   
3   import com.google.inject.Inject;
4   import com.google.inject.Provider;
5   import com.ning.metrics.goodwill.binder.config.GoodwillConfig;
6   import com.ning.metrics.goodwill.dao.DAOBoneCPAccess;
7   import com.ning.metrics.goodwill.store.CSVFileStore;
8   import com.ning.metrics.goodwill.store.GoodwillStore;
9   import com.ning.metrics.goodwill.store.MySQLStore;
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  
13  import java.io.IOException;
14  
15  public class GoodwillStoreProvider implements Provider<GoodwillStore>
16  {
17      private static final Logger log = LoggerFactory.getLogger(GoodwillServicesModule.class);
18  
19      private final GoodwillConfig config;
20  
21      @Inject
22      public GoodwillStoreProvider(final GoodwillConfig config)
23      {
24          this.config = config;
25      }
26  
27      @Override
28      public GoodwillStore get()
29      {
30          final String storeType = config.getStoreType();
31          if (storeType.equals("mysql")) {
32              log.info("Enabling MySQL store");
33              try {
34                  return new MySQLStore(config, new DAOBoneCPAccess(config));
35              }
36              catch (IOException e) {
37                  log.error("Unable to connect to MySQL", e);
38              }
39          }
40          else if (storeType.equals("csv")) {
41              log.info("Enabling CSV store");
42              try {
43                  return new CSVFileStore(config);
44              }
45              catch (IOException e) {
46                  log.error("Unable to create the CSV file store", e);
47              }
48          }
49          else {
50              throw new IllegalStateException("Unknown store type " + storeType);
51          }
52  
53          return null;
54      }
55  }