|
| 1 | +package org.eclipse.osc.modules.file.storage; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.Properties; |
| 8 | +import java.util.Set; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.apache.karaf.minho.boot.service.ConfigService; |
| 11 | +import org.apache.karaf.minho.boot.service.ServiceRegistry; |
| 12 | +import org.apache.karaf.minho.boot.spi.Service; |
| 13 | +import org.eclipse.osc.orchestrator.OrchestratorStorage; |
| 14 | + |
| 15 | +@Slf4j |
| 16 | +public class FileOrchestratorStorage implements OrchestratorStorage, Service { |
| 17 | + |
| 18 | + public static final String DEFAULT_FILENAME = "orchestrator.properties"; |
| 19 | + |
| 20 | + private final Properties properties = new Properties(); |
| 21 | + private File file = new File(DEFAULT_FILENAME); |
| 22 | + |
| 23 | + @Override |
| 24 | + public String name() { |
| 25 | + return "file-orchestrator-storage"; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public void onRegister(ServiceRegistry serviceRegistry) { |
| 30 | + log.info("Registering file orchestrator storage service ..."); |
| 31 | + ConfigService configService = serviceRegistry.get(ConfigService.class); |
| 32 | + file = new File(configService.getProperty("orchestrator.store.filename", DEFAULT_FILENAME)); |
| 33 | + if (file.exists()) { |
| 34 | + try { |
| 35 | + try (var stream = new FileInputStream(file)) { |
| 36 | + properties.load(stream); |
| 37 | + } |
| 38 | + } catch (IOException ex) { |
| 39 | + throw new IllegalStateException("Read file failed " + file, ex); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public int priority() { |
| 46 | + return 900; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public synchronized void store(String sid) { |
| 51 | + properties.put(sid, sid); |
| 52 | + save(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void store(String sid, String pluginName, String key, String value) { |
| 57 | + String propertyKey = sid + "__" + pluginName + "__" + key; |
| 58 | + properties.put(propertyKey, value); |
| 59 | + save(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public String getKey(String sid, String pluginName, String key) { |
| 64 | + String propertyKey = sid + "__" + pluginName + "__" + key; |
| 65 | + return properties.getProperty(propertyKey, ""); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean exists(String sid) { |
| 70 | + return properties.containsKey(sid); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public Set<String> services() { |
| 75 | + return properties.stringPropertyNames(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public synchronized void remove(String sid) { |
| 80 | + properties.remove(sid); |
| 81 | + save(); |
| 82 | + } |
| 83 | + |
| 84 | + private void save() { |
| 85 | + try (var stream = new FileOutputStream(file)) { |
| 86 | + properties.store(stream, null); |
| 87 | + } catch (IOException ex) { |
| 88 | + log.warn("Can't save orchestrator state", ex); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments