6
6
import java .util .Arrays ;
7
7
import java .util .Collections ;
8
8
import java .util .List ;
9
+ import java .util .Map ;
10
+ import java .util .Set ;
11
+ import java .util .concurrent .ConcurrentHashMap ;
12
+ import java .util .concurrent .ConcurrentSkipListSet ;
9
13
10
14
import org .eclipse .core .resources .IProject ;
11
15
import org .eclipse .core .resources .ProjectScope ;
12
- import org .eclipse .core .runtime .preferences .IEclipsePreferences ;
13
16
import org .eclipse .core .runtime .preferences .IScopeContext ;
14
- import org .eclipse .core .runtime .preferences .InstanceScope ;
15
-
16
- import com .google .gson .Gson ;
17
17
18
18
import io .snyk .eclipse .plugin .Activator ;
19
19
import io .snyk .eclipse .plugin .utils .ResourceUtils ;
20
20
import io .snyk .eclipse .plugin .utils .SnykLogger ;
21
21
import io .snyk .languageserver .protocolextension .messageObjects .FolderConfig ;
22
22
23
23
public class FolderConfigs {
24
+ public static Set <Path > LanguageServerConfigReceived = new ConcurrentSkipListSet <>();
25
+
24
26
protected static FolderConfigs instance ;
25
- private static final IEclipsePreferences instancePreferences = InstanceScope . INSTANCE . getNode ( Activator . PLUGIN_ID );
26
- private static final Gson gson = new Gson ();
27
+
28
+ private static Map < Path , FolderConfig > inMemoryConfigs = new ConcurrentHashMap <> ();
27
29
28
30
private FolderConfigs () {
29
31
}
@@ -36,22 +38,15 @@ public static synchronized FolderConfigs getInstance() {
36
38
}
37
39
38
40
public void addFolderConfig (FolderConfig folderConfig ) {
39
- var folderPath = Paths .get (folderConfig .getFolderPath ());
40
- persist (folderPath , folderConfig );
41
+ storeInMemory (Paths .get (folderConfig .getFolderPath ()), folderConfig );
41
42
}
42
43
43
44
public List <String > getLocalBranches (Path projectPath ) {
44
45
return getFolderConfig (projectPath ).getLocalBranches ();
45
46
}
46
47
47
- private void persist (Path path , FolderConfig folderConfig ) {
48
- String updatedJson = gson .toJson (folderConfig );
49
- instancePreferences .put (path .normalize ().toString (), updatedJson );
50
- try {
51
- instancePreferences .flush ();
52
- } catch (Exception e ) {
53
- SnykLogger .logError (e );
54
- }
48
+ private void storeInMemory (Path path , FolderConfig folderConfig ) {
49
+ inMemoryConfigs .put (path .normalize (), folderConfig );
55
50
}
56
51
57
52
public String getBaseBranch (Path projectPath ) {
@@ -77,10 +72,10 @@ public List<FolderConfig> getAll() {
77
72
var additionalParamsList = Arrays .asList (additionalParams .split (" " ));
78
73
var folderConfig = getFolderConfig (path );
79
74
folderConfig .setAdditionalParameters (additionalParamsList );
80
- persist (path , folderConfig );
75
+ storeInMemory (path , folderConfig );
81
76
folderConfigs .add (folderConfig );
82
77
}
83
-
78
+
84
79
return Collections .unmodifiableList (folderConfigs );
85
80
}
86
81
@@ -90,18 +85,21 @@ public List<FolderConfig> getAll() {
90
85
* @return a folder config (always)
91
86
*/
92
87
public FolderConfig getFolderConfig (Path folderPath ) {
93
- Path path = folderPath .normalize ();
94
- String json = instancePreferences .get (path .toString (), null );
95
- if (json == null ) {
96
- SnykLogger .logInfo ("No valid configuration for path: " + folderPath .toString ());
97
- FolderConfig folderConfig = new FolderConfig (path .toString ());
98
- persist (path , folderConfig );
99
- return folderConfig ;
88
+ var path = folderPath .normalize ();
89
+
90
+ FolderConfig folderConfig = inMemoryConfigs .get (path );
91
+ if (folderConfig == null ) {
92
+ SnykLogger .logInfo ("Did not find FolderConfig for path" + path + ", creating new one." );
93
+ folderConfig = new FolderConfig (path .toString ());
100
94
}
101
- return gson . fromJson ( json , FolderConfig . class ) ;
95
+ return folderConfig ;
102
96
}
103
97
104
98
public static void setInstance (FolderConfigs folderConfigs ) {
105
99
instance = folderConfigs ;
106
100
}
101
+
102
+ public void setLanguageServerConfigReceived (Path path ) {
103
+ LanguageServerConfigReceived .add (path );
104
+ }
107
105
}
0 commit comments