1 /*
2  * Copyright (C) 2020 Finn Herzfeld
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 package io.finn.signald.storage;
19 
20 import com.fasterxml.jackson.core.JsonGenerator;
21 import com.fasterxml.jackson.core.JsonParser;
22 import com.fasterxml.jackson.databind.*;
23 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
24 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 
31 @JsonSerialize(using = LegacyThreadStore.ThreadStoreSerializer.class)
32 @JsonDeserialize(using = LegacyThreadStore.ThreadStoreDeserializer.class)
33 public class LegacyThreadStore {
34 
35   private Map<String, LegacyThreadInfo> threads = new HashMap<>();
36 
37   private static final ObjectMapper jsonProcessor = new ObjectMapper();
38 
updateThread(LegacyThreadInfo thread)39   public void updateThread(LegacyThreadInfo thread) { threads.put(thread.id, thread); }
40 
getThread(String id)41   public LegacyThreadInfo getThread(String id) { return threads.get(id); }
42 
getThreads()43   public List<LegacyThreadInfo> getThreads() { return new ArrayList<>(threads.values()); }
44 
45   public static class ThreadStoreSerializer extends JsonSerializer<LegacyThreadStore> {
46     @Override
serialize(final LegacyThreadStore value, final JsonGenerator jgen, final SerializerProvider provider)47     public void serialize(final LegacyThreadStore value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException {
48       jgen.writeStartObject();
49       jgen.writeObjectField("threads", value.threads.values());
50       jgen.writeEndObject();
51     }
52   }
53 
54   public static class ThreadStoreDeserializer extends JsonDeserializer<LegacyThreadStore> {
55     @Override
deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)56     public LegacyThreadStore deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
57       LegacyThreadStore store = new LegacyThreadStore();
58       JsonNode node = jsonParser.getCodec().readTree(jsonParser);
59       if (!node.has("threads")) {
60         return store;
61       }
62       for (JsonNode n : node.get("threads")) {
63         LegacyThreadInfo t = jsonProcessor.treeToValue(n, LegacyThreadInfo.class);
64         store.threads.put(t.id, t);
65       }
66 
67       return store;
68     }
69   }
70 }
71