1. Project Clover database Wed Jan 17 2024 23:40:18 CST
  2. Package org.europa.together.application

File FeatureFlagsFF4j.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

14
45
11
1
157
134
18
0.4
4.09
11
1.64

Classes

Class Line # Actions
FeatureFlagsFF4j 32 45 0% 18 1
0.9857142698.6%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    package org.europa.together.application;
2   
3    import java.io.IOException;
4    import java.net.ConnectException;
5    import java.util.HashMap;
6    import java.util.List;
7    import java.util.Map;
8    import org.apache.commons.dbcp2.BasicDataSource;
9    import org.europa.together.business.ConfigurationDAO;
10    import org.europa.together.business.CryptoTools;
11    import org.europa.together.business.FeatureFlags;
12    import org.europa.together.business.Logger;
13    import org.europa.together.business.PropertyReader;
14    import org.europa.together.domain.ConfigurationDO;
15    import org.europa.together.domain.HashAlgorithm;
16    import org.europa.together.domain.LogLevel;
17    import org.europa.together.exceptions.MisconfigurationException;
18    import org.europa.together.utils.Constraints;
19    import org.europa.together.utils.StringUtils;
20    import org.ff4j.FF4j;
21    import org.ff4j.audit.repository.JdbcEventRepository;
22    import org.ff4j.core.Feature;
23    import org.ff4j.property.store.JdbcPropertyStore;
24    import org.ff4j.store.JdbcFeatureStore;
25    import org.springframework.beans.factory.annotation.Autowired;
26    import org.springframework.stereotype.Repository;
27   
28    /**
29    * Implementation of feature toggels.
30    */
31    @Repository
 
32    public class FeatureFlagsFF4j implements FeatureFlags {
33   
34    private static final long serialVersionUID = 13L;
35    private static final Logger LOGGER = new LogbackLogger(FeatureFlagsFF4j.class);
36   
37    @Autowired
38    private ConfigurationDAO configurationDAO;
39    @Autowired
40    private CryptoTools cryptoTools;
41    @Autowired
42    private PropertyReader reader;
43   
44    private FF4j ff4j;
45    private Map<String, String> configuration;
46   
47    /**
48    * Constructor.
49    */
 
50  1 toggle public FeatureFlagsFF4j() {
51  1 LOGGER.log("instance class", LogLevel.INFO);
52  1 configuration = new HashMap<>();
53    }
54   
 
55  8 toggle @Override
56    public FF4j getFeatureStore(final String propertyFile)
57    throws IOException, ConnectException, MisconfigurationException {
58  8 loadConfigurationFromDatabase();
59  7 if (Boolean.parseBoolean(configuration.get("ff.activation"))) {
60  6 reader.appendPropertiesFromClasspath(propertyFile);
61  6 BasicDataSource cpds = new BasicDataSource();
62  6 cpds.setDriverClassName(reader.getPropertyAsString("jdbc.driverClassName"));
63  6 cpds.setUrl(reader.getPropertyAsString("jdbc.url"));
64  6 cpds.setUsername(reader.getPropertyAsString("jdbc.user"));
65  6 cpds.setPassword(reader.getPropertyAsString("jdbc.password"));
66   
67  6 ff4j = new FF4j();
68  6 ff4j.setFeatureStore(new JdbcFeatureStore(cpds));
69  6 ff4j.setPropertiesStore(new JdbcPropertyStore(cpds));
70  6 ff4j.setEventRepository(new JdbcEventRepository(cpds));
71  6 ff4j.audit(Boolean.parseBoolean(configuration.get("ff.audit")));
72  6 ff4j.autoCreate(Boolean.parseBoolean(configuration.get("ff.autocreate")));
73    } else {
74  1 String message = "Feature Flags are deactivated by database configuration";
75  1 throw new MisconfigurationException(message);
76    }
77  6 return ff4j.audit();
78    }
79   
 
80  2 toggle @Override
81    public boolean check(final String featureId) {
82  2 return ff4j.check(featureId);
83    }
84   
 
85  1 toggle @Override
86    public void activateFeature(final String featureId) {
87  1 ff4j.enable(featureId);
88    }
89   
 
90  1 toggle @Override
91    public void deactivateFeature(final String featureId) {
92  1 ff4j.disable(featureId);
93    }
94   
 
95  5 toggle @Override
96    public void addFeature(final Feature feature) {
97  5 ff4j.createFeature(feature);
98    }
99   
 
100  5 toggle @Override
101    public Feature getFeature(final String featureId) {
102  5 return ff4j.getFeature(featureId);
103    }
104   
 
105  2 toggle @Override
106    public void updateFeature(final Feature feature) {
107  2 if (ff4j.exist(feature.getUid())) {
108  1 ff4j.delete(feature.getUid());
109    }
110  2 ff4j.createFeature(feature);
111    }
112   
 
113  1 toggle @Override
114    public void removeFeature(final String featureId) {
115  1 ff4j.delete(featureId);
116    }
117   
 
118  3 toggle @Override
119    public Map<String, Feature> listAllFeatures() {
120  3 return ff4j.getFeatures();
121    }
122   
 
123  8 toggle private void loadConfigurationFromDatabase() throws ConnectException {
124  8 LOGGER.log("Load all configuration sets of: " + CONFIG_SET
125    + " - Version: " + CONFIG_VERSION
126    + " - Module: " + Constraints.MODULE_NAME, LogLevel.DEBUG);
127  8 List<ConfigurationDO> configurationEntries
128    = configurationDAO.getAllConfigurationSetEntries(Constraints.MODULE_NAME,
129    CONFIG_VERSION, CONFIG_SET);
130  8 if (!configurationEntries.isEmpty()) {
131  7 LOGGER.log("Size of config SET: " + configurationEntries.size(), LogLevel.DEBUG);
132  7 for (ConfigurationDO entry : configurationEntries) {
133  21 String value;
134  21 if (StringUtils.isEmpty(entry.getValue())) {
135  7 value = entry.getDefaultValue();
136    } else {
137  14 value = entry.getValue();
138    }
139  21 if (entry.getKey()
140    .equals(cryptoTools.calculateHash("ff.activation",
141    HashAlgorithm.SHA256))) {
142  7 this.configuration.put("ff.activation", value);
143  14 } else if (entry.getKey()
144    .equals(cryptoTools.calculateHash("ff.audit",
145    HashAlgorithm.SHA256))) {
146  7 this.configuration.put("ff.audit", value);
147  7 } else if (entry.getKey()
148    .equals(cryptoTools.calculateHash("ff.autocreate",
149    HashAlgorithm.SHA256))) {
150  7 this.configuration.put("ff.autocreate", value);
151    }
152    }
153    } else {
154  1 throw new ConnectException("Feature Flags can't access the configuration.");
155    }
156    }
157    }