Skip to content

Commit

Permalink
Schema cache directory should be configurable
Browse files Browse the repository at this point in the history
Fixes eclipse-lemminx#222

Signed-off-by: Nikolas Komonen <[email protected]>
  • Loading branch information
NikolasKomonen committed Apr 4, 2019
1 parent 6063992 commit fce3b66
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
import static org.eclipse.lsp4j.jsonrpc.CompletableFutures.computeAsync;
import static org.eclipse.lsp4xml.utils.VersionHelper.getVersion;

import java.io.File;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -44,11 +50,13 @@
import org.eclipse.lsp4xml.settings.AllXMLSettings;
import org.eclipse.lsp4xml.settings.InitializationOptionsSettings;
import org.eclipse.lsp4xml.settings.LogsSettings;
import org.eclipse.lsp4xml.settings.ServerSettings;
import org.eclipse.lsp4xml.settings.XMLGeneralClientSettings;
import org.eclipse.lsp4xml.settings.XMLExperimentalCapabilities;
import org.eclipse.lsp4xml.settings.XMLFormattingOptions;
import org.eclipse.lsp4xml.settings.capabilities.ServerCapabilitiesInitializer;
import org.eclipse.lsp4xml.settings.capabilities.XMLCapabilityManager;
import org.eclipse.lsp4xml.utils.FilesUtils;

/**
* XML language server.
Expand Down Expand Up @@ -135,6 +143,20 @@ public void updateSettings(Object initializationOptionsSettings) {
if (newCompletions != null) {
xmlTextDocumentService.updateCompletionSettings(newCompletions);
}

ServerSettings serverSettings = xmlClientSettings.getServer();
if(serverSettings != null) {
String workDir = serverSettings.getWorkDir();
workDir = FilesUtils.validatePath(workDir);
if(workDir != null && !workDir.isEmpty()) {
if(workDir.equals(FilesUtils.getCachePathSetting()) == false) {
FilesUtils.setCachePathSetting(workDir);
}
}
else if(FilesUtils.getCachePathSetting() != null){
FilesUtils.setCachePathSetting(null);
}
}

// Experimental capabilities
XMLExperimentalCapabilities experimental = xmlClientSettings.getExperimental();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*******************************************************************************
* Copyright (c) 2019 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/

package org.eclipse.lsp4xml.settings;

import org.eclipse.lsp4xml.utils.JSONUtility;

/**
* ServerSettings
*/
public class ServerSettings {

private String workDir;


/**
* @return the workDir
*/
public String getWorkDir() {
return workDir;
}

/**
* @param workDir the workDir to set
*/
public void setWorkDir(String workDir) {
this.workDir = workDir;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class XMLGeneralClientSettings {

private CompletionSettings completion;

private ServerSettings server;

public void setLogs(LogsSettings logs) {
this.logs = logs;
}
Expand Down Expand Up @@ -78,6 +80,20 @@ public CompletionSettings getCompletion() {
return completion;
}

/**
* @return the server
*/
public ServerSettings getServer() {
return server;
}

/**
* @param server the server to set
*/
public void setServer(ServerSettings server) {
this.server = server;
}

public static XMLGeneralClientSettings getGeneralXMLSettings(Object initializationOptionsSettings) {
return JSONUtility.toModel(initializationOptionsSettings, XMLGeneralClientSettings.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.regex.Pattern;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
Expand All @@ -29,6 +30,16 @@
public class FilesUtils {

public static final String LSP4XML_WORKDIR_KEY = "lsp4xml.workdir";
private static String cachePathSetting = null;

public static String getCachePathSetting() {
return cachePathSetting;
}

public static void setCachePathSetting(String cachePathSetting) {
FilesUtils.cachePathSetting = cachePathSetting;
resetDeployPath();
}

private FilesUtils() {
}
Expand All @@ -44,11 +55,27 @@ public static void resetDeployPath() {
DEPLOYED_BASE_PATH = Suppliers.memoize(() -> getDeployedBasePath());
}

/**
* Given a file path as a string, will normalize it
* and return the normalized string if valid, or null if not.
*/
public static String validatePath(String pathString) {
if(pathString != null && !pathString.isEmpty()) {
pathString = pathString.replaceFirst("^~", System.getProperty("user.home"));
Path p = Paths.get(pathString);
return p.normalize().toString();
}
return null;
}

private static Path getDeployedBasePath() {
String dir = System.getProperty(LSP4XML_WORKDIR_KEY);
if (dir != null) {
return Paths.get(dir);
}
if(cachePathSetting != null && !cachePathSetting.isEmpty()) {
return Paths.get(cachePathSetting);
}
dir = System.getProperty("user.home");
if (dir == null) {
dir = System.getProperty("user.dir");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@

import org.eclipse.lsp4j.FormattingOptions;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4xml.XMLLanguageServer;
import org.eclipse.lsp4xml.extensions.contentmodel.settings.ContentModelSettings;
import org.eclipse.lsp4xml.utils.FilesUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

Expand All @@ -24,43 +31,60 @@
*/
public class SettingsTest {

private static String testFolder = "TestXMLCacheFolder1122333";

@After
public void cleanup() {
String path = "~/" + testFolder;
path = path.replaceFirst("^~", System.getProperty("user.home"));
File f = new File(path);
if(f.exists()) {
f.delete();
}
}

private final String json =
"{\r\n" + //
" \"settings\": {\r\n" + //
// Content model settings
" \"xml\": {\r\n" +
" \"fileAssociations\": [\r\n" + //
" {\r\n" + //
" \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\spring-beans-3.0.xsd\",\r\n" + //
" \"pattern\": \"**/test*.xml\"\r\n" + //
" },\r\n" + //
" {\r\n" + //
" \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\projectDescription.xsd\",\r\n" + //
" \"pattern\": \"projectDescription.xml\"\r\n" + //
" }\r\n" + //
" ],\r\n" + //
" \"catalogs\": [\r\n" + //
" \"src\\\\test\\\\resources\\\\catalogs\\\\catalog.xml\"\r\n" + //
" ],\r\n" + //
" \"validation\": {\r\n" + //
" \"enabled\": true,\r\n" + //
" \"schema\": false\r\n" + //
" },\r\n" + //
// Client (commons) settings
" \"format\": {\r\n" + //
" \"tabSize\": 10,\r\n" + //
" \"insertSpaces\": false,\r\n" + //
" \"splitAttributes\": true,\r\n" + //
" \"joinCDATALines\": true,\r\n" + //
" \"formatComments\": true,\r\n" + //
" \"joinCommentLines\": true,\r\n" + //
" \"quotations\": " + XMLFormattingOptions.DOUBLE_QUOTES_VALUE + "\r\n" + //
" },\r\n" +
" \"server\": {\r\n" + //
" \"workDir\": \"~/" + testFolder + "/Nested\"\r\n" + //
" }\r\n" +
" }\r\n" +
" }\r\n" +
"}";

@Test
public void initializationOptionsSettings() {
String json =
"{\r\n" + //
" \"settings\": {\r\n" + //
// Content model settings
" \"xml\": {\r\n" +
" \"fileAssociations\": [\r\n" + //
" {\r\n" + //
" \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\spring-beans-3.0.xsd\",\r\n" + //
" \"pattern\": \"**/test*.xml\"\r\n" + //
" },\r\n" + //
" {\r\n" + //
" \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\projectDescription.xsd\",\r\n" + //
" \"pattern\": \"projectDescription.xml\"\r\n" + //
" }\r\n" + //
" ],\r\n" + //
" \"catalogs\": [\r\n" + //
" \"src\\\\test\\\\resources\\\\catalogs\\\\catalog.xml\"\r\n" + //
" ],\r\n" + //
" \"validation\": {\r\n" + //
" \"enabled\": true,\r\n" + //
" \"schema\": false\r\n" + //
" },\r\n" + //
// Client (commons) settings
" \"format\": {\r\n" + //
" \"tabSize\": 10,\r\n" + //
" \"insertSpaces\": false,\r\n" + //
" \"splitAttributes\": true,\r\n" + //
" \"joinCDATALines\": true,\r\n" + //
" \"formatComments\": true,\r\n" + //
" \"joinCommentLines\": true,\r\n" + //
" \"quotations\": " + XMLFormattingOptions.DOUBLE_QUOTES_VALUE + "\r\n" + //
" }\r\n" +
" }\r\n" +
" }\r\n" +
"}";

// Emulate InitializeParams#getInitializationOptions() object created as
// JSONObject when XMLLanguageServer#initialize(InitializeParams params) is
// called
Expand All @@ -71,6 +95,8 @@ public void initializationOptionsSettings() {
initializationOptionsSettings = AllXMLSettings.getAllXMLSettings(initializationOptionsSettings);
XMLGeneralClientSettings settings = XMLGeneralClientSettings.getGeneralXMLSettings(initializationOptionsSettings);
Assert.assertNotNull(settings);
//Server
Assert.assertEquals("~/" + testFolder + "/Nested", settings.getServer().getWorkDir());

// Test content model extension settings
ContentModelSettings cmSettings = ContentModelSettings.getContentModelXMLSettings(initializationOptionsSettings);
Expand Down Expand Up @@ -138,4 +164,21 @@ public void formatSettings() {

Assert.assertTrue(xmlFormattingOptions.getQuotations().equals(XMLFormattingOptions.SINGLE_QUOTES_VALUE));
}

@Test
public void cachePathSettings() {
// Emulate InitializeParams#getInitializationOptions() object created as
// JSONObject when XMLLanguageServer#initialize(InitializeParams params) is
// called
InitializeParams params = createInitializeParams(json);
Object initializationOptionsSettings = InitializationOptionsSettings.getSettings(params);
XMLLanguageServer languageServer = new XMLLanguageServer();

languageServer.updateSettings(initializationOptionsSettings);

Assert.assertEquals(System.getProperty("user.home") + "/" + testFolder + "/Nested", FilesUtils.getCachePathSetting());

//Reset static cache path
FilesUtils.setCachePathSetting(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.nio.file.Path;
import java.util.concurrent.TimeUnit;

import com.google.common.cache.Cache;
Expand Down Expand Up @@ -95,7 +96,6 @@ public void testAvailableCache() throws Exception {
} catch (CacheResourceDownloadingException ignored) {
}
TimeUnit.MILLISECONDS.sleep(200);

assertNotNull(cacheResourcesManager.getResource(uri));

server.stop();
Expand All @@ -108,5 +108,4 @@ public void testAvailableCache() throws Exception {
private Cache<String, Boolean> testingCache() {
return CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.SECONDS).maximumSize(1).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*******************************************************************************
* Copyright (c) 2019 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/


package org.eclipse.lsp4xml.utils;

import static org.junit.Assert.assertEquals;

import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Test;

/**
* FilesUtilsTest
*/
public class FilesUtilsTest {

@Test
public void testFilesCachePathPreference() throws Exception {
System.clearProperty(FilesUtils.LSP4XML_WORKDIR_KEY);
String newBasePathString = System.getProperty("user.home");
String newSubPathString = "New/Sub/Path";
Path newSubPath = Paths.get(newSubPathString);
FilesUtils.setCachePathSetting(newBasePathString);
Path finalPath = FilesUtils.getDeployedPath(newSubPath);
assertEquals(newBasePathString + "/" + newSubPathString, finalPath.toString());
}
}

0 comments on commit fce3b66

Please sign in to comment.