|
5 | 5 | import co.featureflags.commons.model.FlagState; |
6 | 6 | import co.featureflags.server.exterior.FFCClient; |
7 | 7 | import co.featureflags.wrapper.model.Message; |
| 8 | +import org.apache.commons.lang3.StringUtils; |
8 | 9 | import org.slf4j.Logger; |
9 | 10 | import org.slf4j.LoggerFactory; |
| 11 | +import org.springframework.beans.factory.InitializingBean; |
10 | 12 | import org.springframework.beans.factory.annotation.Autowired; |
11 | 13 | import org.springframework.beans.factory.annotation.Value; |
12 | 14 | import org.springframework.stereotype.Service; |
13 | 15 |
|
14 | 16 | import java.io.BufferedWriter; |
15 | 17 | import java.io.FileWriter; |
| 18 | +import java.io.IOException; |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.nio.file.Paths; |
16 | 22 |
|
17 | 23 | @Service |
18 | | -public class FFCSDKServiceImp implements FFCSDKService { |
| 24 | +public class FFCSDKServiceImp implements FFCSDKService, InitializingBean { |
19 | 25 |
|
20 | 26 | private final static Logger LOG = LoggerFactory.getLogger(FFCSDKServiceImp.class); |
21 | 27 |
|
22 | 28 | private final static String DEFAULT_VALUE = "NOT FOUND"; |
23 | 29 |
|
24 | | - @Value("${ffc.offline}") |
| 30 | + @Value("${ffc.spring.offline}") |
25 | 31 | private boolean offline; |
26 | 32 |
|
27 | 33 | @Value("${ffc.dataFile}") |
@@ -63,4 +69,26 @@ public Message initializeFromExternalJson(String json) { |
63 | 69 | return Message.ERROR(unexpected.getMessage()); |
64 | 70 | } |
65 | 71 | } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void afterPropertiesSet() throws Exception { |
| 75 | + if (offline) { |
| 76 | + String json = null; |
| 77 | + Path path = Paths.get(dataFile); |
| 78 | + if (Files.isRegularFile(path)) { |
| 79 | + try { |
| 80 | + json = Files.lines(path) |
| 81 | + .reduce((s, line) -> s.concat(line)) |
| 82 | + .orElse(null); |
| 83 | + if (StringUtils.isNotEmpty(json)) { |
| 84 | + client.initializeFromExternalJson(json); |
| 85 | + LOG.info("initializing from a file works well"); |
| 86 | + } |
| 87 | + } catch (IOException unexpected) { |
| 88 | + LOG.warn("initializing from a file didn't work well: {}", unexpected.getMessage()); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
66 | 94 | } |
0 commit comments