1 package io.getunleash.repository;
2
3 import com.google.gson.JsonParseException;
4 import io.getunleash.UnleashException;
5 import io.getunleash.event.EventDispatcher;
6 import io.getunleash.event.UnleashEvent;
7 import io.getunleash.event.UnleashSubscriber;
8 import io.getunleash.util.UnleashConfig;
9 import java.io.*;
10 import java.util.Collections;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 public class FeatureBackupHandlerFile implements BackupHandler<FeatureCollection> {
15     private static final Logger LOG = LoggerFactory.getLogger(FeatureBackupHandlerFile.class);
16
17     private final String backupFile;
18     private final EventDispatcher eventDispatcher;
19
20     public FeatureBackupHandlerFile(UnleashConfig config) {
21         this.backupFile = config.getBackupFile();
22         this.eventDispatcher = new EventDispatcher(config);
23     }
24
25     @Override
26     public FeatureCollection read() {
27         LOG.info("Unleash will try to load feature toggle states from temporary backup");
28         try (FileReader reader = new FileReader(backupFile)) {
29             BufferedReader br = new BufferedReader(reader);
30             FeatureCollection featureCollection = JsonFeatureParser.fromJson(br);
31             eventDispatcher.dispatch(new FeatureBackupRead(featureCollection));
32             return featureCollection;
33         } catch (FileNotFoundException e) {
34             LOG.info(
35                     " Unleash could not find the backup-file '"
36                             + backupFile
37                             + "'. \n"
38                             + "This is expected behavior the first time unleash runs in a new environment.");
39         } catch (IOException | IllegalStateException | JsonParseException e) {
40             eventDispatcher.dispatch(
41                     new UnleashException("Failed to read backup file: " + backupFile, e));
42         }
43         return new FeatureCollection(
44                 new ToggleCollection(Collections.emptyList()),
45                 new SegmentCollection(Collections.emptyList()));
46     }
47
48     @Override
49     public void write(FeatureCollection featureCollection) {
50         try (FileWriter writer = new FileWriter(backupFile)) {
51             writer.write(JsonFeatureParser.toJsonString(featureCollection));
52             eventDispatcher.dispatch(new FeatureBackupWritten(featureCollection));
53         } catch (IOException e) {
54             eventDispatcher.dispatch(
55                     new UnleashException(
56                             "Unleash was unable to backup feature toggles to file: " + backupFile,
57                             e));
58         }
59     }
60
61     private static class FeatureBackupRead implements UnleashEvent {
62
63         private final FeatureCollection featureCollection;
64
65         private FeatureBackupRead(FeatureCollection featureCollection) {
66             this.featureCollection = featureCollection;
67         }
68
69         @Override
70         public void publishTo(UnleashSubscriber unleashSubscriber) {
71             unleashSubscriber.featuresBackupRestored(featureCollection);
72         }
73     }
74
75     private static class FeatureBackupWritten implements UnleashEvent {
76
77         private final FeatureCollection featureCollection;
78
79         private FeatureBackupWritten(FeatureCollection featureCollection) {
80             this.featureCollection = featureCollection;
81         }
82
83         @Override
84         public void publishTo(UnleashSubscriber unleashSubscriber) {
85             unleashSubscriber.featuresBackedUp(featureCollection);
86         }
87     }
88 }
89