1 package com.atlassian.plugin.manager.store;
2
3 import com.atlassian.plugin.manager.PluginEnabledState;
4 import com.atlassian.plugin.manager.PluginPersistentStateModifier;
5 import com.atlassian.plugin.manager.PluginPersistentStateStore;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8
9 import java.util.HashMap;
10 import java.util.Map;
11
12 import static com.atlassian.plugin.manager.PluginPersistentState.Util.RESTART_STATE_SEPARATOR;
13
14
15
16
17
18
19 public class PluginPersistentStateStoreMigrator {
20 private static final Logger log = LoggerFactory.getLogger(PluginPersistentStateStoreMigrator.class);
21
22 private PluginPersistentStateStoreMigrator() {}
23
24
25
26
27
28
29
30
31
32
33 public static void removeDirectives(final PluginPersistentStateStore store) {
34 new PluginPersistentStateModifier(store).apply(builder -> {
35 Map<String, PluginEnabledState> state = builder.toState().getStatesMap();
36 Map<String, PluginEnabledState> newState = new HashMap<>(state.size());
37 for (Map.Entry<String, PluginEnabledState> entry : state.entrySet()) {
38 String key = entry.getKey();
39 String newKey = removeDirectivesFromKey(key);
40 if (newKey == null) {
41 continue;
42 }
43
44 builder.removeState(key);
45 if (state.containsKey(newKey)) {
46 log.warn("{} contains both {} and {}", store, key, newKey);
47 }
48 newState.put(newKey, entry.getValue());
49 }
50 builder.addPluginEnabledState(newState);
51 });
52 }
53
54
55
56
57
58
59
60
61
62
63 public static String removeDirectivesFromKey(String key) {
64 if (key.contains(RESTART_STATE_SEPARATOR)) {
65 return null;
66 }
67
68 int versionBeg = key.indexOf('-');
69 if (versionBeg == -1) {
70 versionBeg = key.length();
71 }
72
73 int directiveBeg = key.indexOf(';');
74 if (directiveBeg > -1 && directiveBeg < versionBeg) {
75 return key.substring(0, directiveBeg) + key.substring(versionBeg);
76 }
77 return null;
78 }
79 }