2 comments
Comment actions Permalink

See com.intellij.openapi.updateSettings.impl.pluginsAdvertisement.PluginsAdvertiser.KnownExtensions for a similar setup and solution

0
Comment actions Permalink

I managed to do it based on the code you suggested, though I'm not sure if the method I save the file is correct.

import com.intellij.configurationStore.XmlSerializer;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.util.xmlb.annotations.OptionTag;
import com.intellij.util.xmlb.annotations.XMap;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class ProjectCacheService {

private static final String CACHE_XML = "cache.xml";

public ProjectCacheService(Project project) {
}

public static ProjectCacheService getInstance(@NotNull Project project) {
return ServiceManager.getService(project, ProjectCacheService.class);
}

public static class CachedMap {
@OptionTag
@XMap
public Map<String, CacheStringList> cacheMap = new HashMap();

@SuppressWarnings("unused")
public CachedMap() {
}

public CachedMap(Map<String, List<String>> map) {
Iterator iterator = map.keySet().iterator();

while (iterator.hasNext()) {
String key = (String) iterator.next();
this.cacheMap.put(key, new CacheStringList(map.get(key)));
}
}

public List<String> find(String key) {
CacheStringList cacheStringList = this.cacheMap.get(key);
return cacheStringList != null ? cacheStringList.cacheList : null;
}
}

public static class CacheStringList {
public List<String> cacheList = new ArrayList<>();

@SuppressWarnings("unused")
public CacheStringList() {
}

public CacheStringList(List<String> list) {
this.cacheList.addAll(list);
}
}

public static CachedMap loadExtensions(Project currentProject) {
try {
File file = getCacheFile(currentProject);
if (file.isFile()) {
return XmlSerializer.deserialize(JDOMUtil.load(file), CachedMap.class);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public static void saveMapInCache(Map<String, List<String>> map, Project currentProject) throws IOException {
File file = getCacheFile(currentProject);
if (!file.isFile()) {
FileUtil.ensureCanCreateFile(file);
}
JDOMUtil.write(XmlSerializer.serialize(new CachedMap(map)), file);
}

private static File getCacheFile(Project currentProject) {
return new File(currentProject.getBasePath(), CACHE_XML);
}
}

Thanks!

 

0

Please sign in to leave a comment.