tryLoadRootElement — do not attempt to load file if file not found

This commit is contained in:
Vladimir Krivosheev
2017-05-30 09:38:21 +02:00
parent 0a69dfd5f4
commit 8f73ccc2b4
4 changed files with 50 additions and 42 deletions
@@ -21,11 +21,13 @@ import com.intellij.openapi.util.SystemInfo;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.TimingLog;
import org.jetbrains.jps.model.JpsElement;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
/**
@@ -40,6 +42,10 @@ public abstract class JpsLoaderBase {
myMacroExpander = macroExpander;
}
/**
* Returns null if file doesn't exist
*/
@Nullable
protected Element loadRootElement(@NotNull Path file) {
return loadRootElement(file, myMacroExpander);
}
@@ -51,14 +57,7 @@ public abstract class JpsLoaderBase {
String fileName = serializer.getConfigFileName();
Path configFile = dir.resolve(fileName != null ? fileName : defaultFileName);
Runnable timingLog = TimingLog.startActivity("loading: " + configFile.getFileName() + ":" + serializer.getComponentName());
Element componentTag;
if (Files.exists(configFile)) {
componentTag = JDomSerializationUtil.findComponent(loadRootElement(configFile), serializer.getComponentName());
}
else {
componentTag = null;
}
Element componentTag = JDomSerializationUtil.findComponent(loadRootElement(configFile), serializer.getComponentName());
if (componentTag != null) {
serializer.loadExtension(element, componentTag);
}
@@ -68,35 +67,45 @@ public abstract class JpsLoaderBase {
timingLog.run();
}
protected static Element loadRootElement(@NotNull Path file, final JpsMacroExpander macroExpander) {
try {
final Element element = tryLoadRootElement(file);
/**
* Returns null if file doesn't exist
*/
@Nullable
protected static Element loadRootElement(@NotNull Path file, @NotNull JpsMacroExpander macroExpander) {
final Element element = tryLoadRootElement(file);
if (element != null) {
macroExpander.substitute(element, SystemInfo.isFileSystemCaseSensitive);
return element;
}
catch (JDOMException e) {
throw new CannotLoadJpsModelException(file.toFile(), "Cannot parse xml file " + file.toAbsolutePath() + ": " + e.getMessage(), e);
}
catch (IOException e) {
throw new CannotLoadJpsModelException(file.toFile(), "Cannot read file " + file.toAbsolutePath() + ": " + e.getMessage(), e);
}
return element;
}
private static Element tryLoadRootElement(@NotNull Path file) throws IOException, JDOMException {
for (int i = 0; i < MAX_ATTEMPTS - 1; i++) {
@Nullable
private static Element tryLoadRootElement(@NotNull Path file) {
int i = 0;
while (true) {
try {
return JDOMUtil.load(Files.newBufferedReader(file));
}
catch (Exception e) {
catch (NoSuchFileException e) {
return null;
}
catch (IOException | JDOMException e) {
if (++i == MAX_ATTEMPTS) {
//noinspection InstanceofCatchParameter
throw new CannotLoadJpsModelException(file.toFile(), "Cannot " + (e instanceof IOException ? "read" : "parse") + " file " + file.toAbsolutePath() + ": " + e.getMessage(), e);
}
LOG.info("Loading attempt #" + i + " failed", e);
}
//most likely configuration file is being written by IDE so we'll wait a little
try {
//noinspection BusyWait
Thread.sleep(300);
}
catch (InterruptedException ignored) { }
catch (InterruptedException ignored) {
return null;
}
}
return JDOMUtil.load(Files.newBufferedReader(file));
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ import com.intellij.openapi.components.ExpandMacroToPathMap;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
@@ -59,7 +60,7 @@ public class JpsMacroExpander {
}
}
public void substitute(Element element, boolean caseSensitive) {
public void substitute(@NotNull Element element, boolean caseSensitive) {
myExpandMacroMap.substitute(element, caseSensitive);
}
@@ -142,11 +142,7 @@ public class JpsProjectLoader extends JpsLoaderBase {
for (Path configurationFile : listXmlFiles(dir.resolve("runConfigurations"))) {
JpsRunConfigurationSerializer.loadRunConfigurations(myProject, loadRootElement(configurationFile));
}
Path workspaceFile = dir.resolve("workspace.xml");
if (Files.exists(workspaceFile)) {
Element runManager = JDomSerializationUtil.findComponent(loadRootElement(workspaceFile), "RunManager");
JpsRunConfigurationSerializer.loadRunConfigurations(myProject, runManager);
}
JpsRunConfigurationSerializer.loadRunConfigurations(myProject, JDomSerializationUtil.findComponent(loadRootElement(dir.resolve("workspace.xml")), "RunManager"));
runConfTimingLog.run();
}
}
@@ -178,7 +174,7 @@ public class JpsProjectLoader extends JpsLoaderBase {
String projectName = FileUtil.getNameWithoutExtension(iprFile.getFileName().toString());
myProject.setName(projectName);
Path iwsFile = iprFile.getParent().resolve(projectName + ".iws");
Element iwsRoot = Files.exists(iwsFile) ? loadRootElement(iwsFile) : null;
Element iwsRoot = loadRootElement(iwsFile);
JpsSdkType<?> projectSdkType = loadProjectRoot(iprRoot);
for (JpsModelSerializerExtension extension : JpsModelSerializerExtension.getExtensions()) {
@@ -207,7 +203,7 @@ public class JpsProjectLoader extends JpsLoaderBase {
}
@Nullable
private JpsSdkType<?> loadProjectRoot(Element root) {
private JpsSdkType<?> loadProjectRoot(@Nullable Element root) {
JpsSdkType<?> sdkType = null;
Element rootManagerElement = JDomSerializationUtil.findComponent(root, "ProjectRootManager");
if (rootManagerElement != null) {
@@ -225,7 +221,7 @@ public class JpsProjectLoader extends JpsLoaderBase {
JpsLibraryTableSerializer.loadLibraries(libraryTableElement, myProject.getLibraryCollection());
}
private void loadModules(Element root, final @Nullable JpsSdkType<?> projectSdkType) {
private void loadModules(@Nullable Element root, final @Nullable JpsSdkType<?> projectSdkType) {
Runnable timingLog = TimingLog.startActivity("loading modules");
Element componentRoot = JDomSerializationUtil.findComponent(root, "ProjectModuleManager");
if (componentRoot == null) return;
@@ -263,14 +259,10 @@ public class JpsProjectLoader extends JpsLoaderBase {
futureModuleFilesContents.add(ourThreadPool.submit(() -> {
final JpsMacroExpander expander = createModuleMacroExpander(pathVariables, file);
Element data = null;
if (Files.exists(file)) {
data = loadRootElement(file, expander);
}
Element data = loadRootElement(file, expander);
Path externalPath = externalModuleDir == null ? null : externalModuleDir.resolve(FileUtilRt.getNameWithoutExtension(file.getFileName().toString()) + ".xml");
if (externalPath != null && Files.exists(externalPath)) {
Element externalData = loadRootElement(externalPath, expander);
Element externalData = externalPath == null ? null : loadRootElement(externalPath, expander);
if (externalData != null) {
if (data == null) {
data = externalData;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@ import org.jetbrains.jps.model.JpsElementFactory;
import org.jetbrains.jps.model.JpsProject;
import org.jetbrains.jps.model.serialization.JpsModelSerializerExtension;
import java.util.List;
import java.util.Map;
/**
@@ -35,6 +36,11 @@ public class JpsRunConfigurationSerializer {
private static final Logger LOG = Logger.getInstance(JpsRunConfigurationSerializer.class);
public static void loadRunConfigurations(@NotNull JpsProject project, @Nullable Element runManagerTag) {
List<Element> elements = JDOMUtil.getChildren(runManagerTag, "configuration");
if (elements.isEmpty()) {
return;
}
Map<String, JpsRunConfigurationPropertiesSerializer<?>> serializers = new HashMap<>();
for (JpsModelSerializerExtension extension : JpsModelSerializerExtension.getExtensions()) {
for (JpsRunConfigurationPropertiesSerializer<?> serializer : extension.getRunConfigurationPropertiesSerializers()) {
@@ -42,7 +48,7 @@ public class JpsRunConfigurationSerializer {
}
}
for (Element configurationTag : JDOMUtil.getChildren(runManagerTag, "configuration")) {
for (Element configurationTag : elements) {
if (Boolean.parseBoolean(configurationTag.getAttributeValue("default"))) {
continue;
}