diff --git a/java/compiler/impl/src/org/jetbrains/builtInWebServer/ArtifactWebServerRootsProvider.java b/java/compiler/impl/src/org/jetbrains/builtInWebServer/ArtifactWebServerRootsProvider.java index df3788c84f60..4733ca3747ec 100644 --- a/java/compiler/impl/src/org/jetbrains/builtInWebServer/ArtifactWebServerRootsProvider.java +++ b/java/compiler/impl/src/org/jetbrains/builtInWebServer/ArtifactWebServerRootsProvider.java @@ -1,7 +1,6 @@ package org.jetbrains.builtInWebServer; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Pair; import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.packaging.artifacts.Artifact; @@ -13,13 +12,13 @@ import org.jetbrains.annotations.Nullable; final class ArtifactWebServerRootsProvider extends PrefixlessWebServerRootsProvider { @Nullable @Override - public Pair> resolve(@NotNull String path, @NotNull Project project, @NotNull PairFunction resolver) { + public PathInfo resolve(@NotNull String path, @NotNull Project project, @NotNull PairFunction resolver) { for (Artifact artifact : ArtifactManager.getInstance(project).getArtifacts()) { VirtualFile root = artifact.getOutputFile(); if (root != null) { VirtualFile file = root.findFileByRelativePath(path); if (file != null) { - return Pair.create(file, new Pair(root, null)); + return new PathInfo(file, root); } } } @@ -28,11 +27,11 @@ final class ArtifactWebServerRootsProvider extends PrefixlessWebServerRootsProvi @Nullable @Override - public Pair getRoot(@NotNull VirtualFile file, @NotNull Project project) { + public PathInfo getRoot(@NotNull VirtualFile file, @NotNull Project project) { for (Artifact artifact : ArtifactManager.getInstance(project).getArtifacts()) { VirtualFile root = artifact.getOutputFile(); if (root != null && VfsUtilCore.isAncestor(root, file, true)) { - return Pair.create(root, null); + return new PathInfo(file, root); } } return null; diff --git a/xml/impl/src/org/jetbrains/builtInWebServer/DefaultWebServerRootsProvider.java b/xml/impl/src/org/jetbrains/builtInWebServer/DefaultWebServerRootsProvider.java index d03cb8173ff3..b5621295957d 100644 --- a/xml/impl/src/org/jetbrains/builtInWebServer/DefaultWebServerRootsProvider.java +++ b/xml/impl/src/org/jetbrains/builtInWebServer/DefaultWebServerRootsProvider.java @@ -1,16 +1,28 @@ +/* + * Copyright 2000-2014 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.jetbrains.builtInWebServer; import com.intellij.openapi.application.AccessToken; import com.intellij.openapi.application.ReadAction; -import com.intellij.openapi.application.Result; import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.roots.ProjectFileIndex; import com.intellij.openapi.roots.ProjectRootManager; -import com.intellij.openapi.util.Couple; -import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.PairFunction; @@ -21,7 +33,7 @@ import org.jetbrains.annotations.Nullable; final class DefaultWebServerRootsProvider extends WebServerRootsProvider { @Nullable @Override - public Pair> resolve(@NotNull String path, @NotNull Project project) { + public PathInfo resolve(@NotNull String path, @NotNull Project project) { PairFunction resolver; if (PlatformUtils.isIntelliJ()) { int index = path.indexOf('/'); @@ -41,12 +53,12 @@ final class DefaultWebServerRootsProvider extends WebServerRootsProvider { resolver = WebServerPathToFileManager.getInstance(project).getResolver(path); ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module); - Couple result = resolve(path, moduleRootManager.getSourceRoots(), resolver); + PathInfo result = resolve(path, moduleRootManager.getSourceRoots(), resolver, moduleName); if (result == null) { - result = resolve(path, moduleRootManager.getContentRoots(), resolver); + result = resolve(path, moduleRootManager.getContentRoots(), resolver, moduleName); } if (result != null) { - return Pair.create(result.first, Pair.create(result.second, module.getName())); + return result; } } } @@ -62,7 +74,7 @@ final class DefaultWebServerRootsProvider extends WebServerRootsProvider { } resolver = WebServerPathToFileManager.getInstance(project).getResolver(path); - Pair> result = findByRelativePath(project, path, modules, true, resolver); + PathInfo result = findByRelativePath(project, path, modules, true, resolver); if (result == null) { // let's find in content roots return findByRelativePath(project, path, modules, false, resolver); @@ -74,24 +86,26 @@ final class DefaultWebServerRootsProvider extends WebServerRootsProvider { @Nullable @Override - public Pair getRoot(@NotNull final VirtualFile file, @NotNull final Project project) { - return new ReadAction>() { - protected void run(@NotNull final Result> result) { - VirtualFile root = null; - ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex(); - if (fileIndex.isInSourceContent(file)) { - root = fileIndex.getSourceRootForFile(file); - } - else if (fileIndex.isInContent(file)) { - root = fileIndex.getContentRootForFile(file); - } - else if (fileIndex.isInLibraryClasses(file)) { - root = fileIndex.getClassRootForFile(file); - } - assert root != null : file.getPresentableUrl(); - result.setResult(Pair.create(root, getModuleNameQualifier(project, fileIndex.getModuleForFile(file)))); + public PathInfo getRoot(@NotNull VirtualFile file, @NotNull Project project) { + AccessToken token = ReadAction.start(); + try { + VirtualFile root = null; + ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex(); + if (fileIndex.isInSourceContent(file)) { + root = fileIndex.getSourceRootForFile(file); } - }.execute().getResultObject(); + else if (fileIndex.isInContent(file)) { + root = fileIndex.getContentRootForFile(file); + } + else if (fileIndex.isInLibraryClasses(file)) { + root = fileIndex.getClassRootForFile(file); + } + assert root != null : file.getPresentableUrl(); + return new PathInfo(file, root, getModuleNameQualifier(project, fileIndex.getModuleForFile(file))); + } + finally { + token.finish(); + } } @Nullable @@ -105,28 +119,29 @@ final class DefaultWebServerRootsProvider extends WebServerRootsProvider { } @Nullable - private static Couple resolve(@NotNull String path, @NotNull VirtualFile[] roots, @NotNull PairFunction resolver) { + private static PathInfo resolve(@NotNull String path, @NotNull VirtualFile[] roots, @NotNull PairFunction resolver, @Nullable String moduleName) { for (VirtualFile root : roots) { VirtualFile file = resolver.fun(path, root); if (file != null) { - return Couple.of(file, root); + return new PathInfo(file, root, moduleName); } } return null; } @Nullable - private static Pair> findByRelativePath(@NotNull Project project, - @NotNull String path, - @NotNull Module[] modules, - boolean inSourceRoot, - @NotNull PairFunction resolver) { + private static PathInfo findByRelativePath(@NotNull Project project, + @NotNull String path, + @NotNull Module[] modules, + boolean inSourceRoot, + @NotNull PairFunction resolver) { for (Module module : modules) { if (!module.isDisposed()) { ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module); - Couple result = resolve(path, inSourceRoot ? moduleRootManager.getSourceRoots() : moduleRootManager.getContentRoots(), resolver); + PathInfo result = resolve(path, inSourceRoot ? moduleRootManager.getSourceRoots() : moduleRootManager.getContentRoots(), resolver, null); if (result != null) { - return Pair.create(result.first, Pair.create(result.second, getModuleNameQualifier(project, module))); + result.moduleName = getModuleNameQualifier(project, module); + return result; } } } diff --git a/xml/impl/src/org/jetbrains/builtInWebServer/PathInfo.java b/xml/impl/src/org/jetbrains/builtInWebServer/PathInfo.java new file mode 100644 index 000000000000..b55b16be84b2 --- /dev/null +++ b/xml/impl/src/org/jetbrains/builtInWebServer/PathInfo.java @@ -0,0 +1,47 @@ +package org.jetbrains.builtInWebServer; + +import com.intellij.openapi.vfs.VfsUtilCore; +import com.intellij.openapi.vfs.VirtualFile; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class PathInfo { + private final VirtualFile child; + private final VirtualFile root; + String moduleName; + + private String computedPath; + + public PathInfo(@NotNull VirtualFile child, @NotNull VirtualFile root, @Nullable String moduleName) { + this.child = child; + this.root = root; + this.moduleName = moduleName; + } + + public PathInfo(@NotNull VirtualFile child, @NotNull VirtualFile root) { + this(child, root, null); + } + + @NotNull + public VirtualFile getChild() { + return child; + } + + @NotNull + public VirtualFile getRoot() { + return root; + } + + @Nullable + public String getModuleName() { + return moduleName; + } + + @NotNull + public String getPath() { + if (computedPath == null) { + computedPath = (moduleName == null ? "" : moduleName + '/') + VfsUtilCore.getRelativePath(child, root, '/'); + } + return computedPath; + } +} \ No newline at end of file diff --git a/xml/impl/src/org/jetbrains/builtInWebServer/PrefixlessWebServerRootsProvider.java b/xml/impl/src/org/jetbrains/builtInWebServer/PrefixlessWebServerRootsProvider.java index 95792194e425..145fdedb350e 100644 --- a/xml/impl/src/org/jetbrains/builtInWebServer/PrefixlessWebServerRootsProvider.java +++ b/xml/impl/src/org/jetbrains/builtInWebServer/PrefixlessWebServerRootsProvider.java @@ -1,7 +1,6 @@ package org.jetbrains.builtInWebServer; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Pair; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.PairFunction; import org.jetbrains.annotations.NotNull; @@ -10,10 +9,10 @@ import org.jetbrains.annotations.Nullable; public abstract class PrefixlessWebServerRootsProvider extends WebServerRootsProvider { @Nullable @Override - public final Pair> resolve(@NotNull String path, @NotNull Project project) { + public final PathInfo resolve(@NotNull String path, @NotNull Project project) { return resolve(path, project, WebServerPathToFileManager.getInstance(project).getResolver(path)); } @Nullable - public abstract Pair> resolve(@NotNull String path, @NotNull Project project, @NotNull PairFunction resolver); + public abstract PathInfo resolve(@NotNull String path, @NotNull Project project, @NotNull PairFunction resolver); } \ No newline at end of file diff --git a/xml/impl/src/org/jetbrains/builtInWebServer/WebServerPathToFileManager.java b/xml/impl/src/org/jetbrains/builtInWebServer/WebServerPathToFileManager.java index 5e63581b460e..987fdcd3a139 100644 --- a/xml/impl/src/org/jetbrains/builtInWebServer/WebServerPathToFileManager.java +++ b/xml/impl/src/org/jetbrains/builtInWebServer/WebServerPathToFileManager.java @@ -8,8 +8,6 @@ import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ModuleRootAdapter; import com.intellij.openapi.roots.ModuleRootEvent; -import com.intellij.openapi.util.Pair; -import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFileManager; import com.intellij.openapi.vfs.newvfs.BulkFileListener; @@ -46,7 +44,7 @@ public class WebServerPathToFileManager { final Cache pathToFileCache = CacheBuilder.newBuilder().maximumSize(512).expireAfterAccess(10, TimeUnit.MINUTES).build(); // time to expire should be greater than pathToFileCache - private final Cache> fileToRoot = CacheBuilder.newBuilder().maximumSize(512).expireAfterAccess(11, TimeUnit.MINUTES).build(); + private final Cache fileToRoot = CacheBuilder.newBuilder().maximumSize(512).expireAfterAccess(11, TimeUnit.MINUTES).build(); public static WebServerPathToFileManager getInstance(@NotNull Project project) { return ServiceManager.getService(project, WebServerPathToFileManager.class); @@ -106,18 +104,13 @@ public class WebServerPathToFileManager { @Nullable public String getPath(@NotNull VirtualFile file) { - Pair root = getRoot(file); - if (root == null) { - return null; - } - else { - return (root.second == null ? "" : root.second + '/') + VfsUtilCore.getRelativePath(file, root.first, '/'); - } + PathInfo pathInfo = getRoot(file); + return pathInfo == null ? null : pathInfo.getPath(); } @Nullable - public Pair getRoot(@NotNull VirtualFile child) { - Pair result = fileToRoot.getIfPresent(child); + public PathInfo getRoot(@NotNull VirtualFile child) { + PathInfo result = fileToRoot.getIfPresent(child); if (result == null) { for (WebServerRootsProvider rootsProvider : WebServerRootsProvider.EP_NAME.getExtensions()) { result = rootsProvider.getRoot(child, project); @@ -133,10 +126,10 @@ public class WebServerPathToFileManager { @Nullable VirtualFile findByRelativePath(@NotNull Project project, @NotNull String path) { for (WebServerRootsProvider rootsProvider : WebServerRootsProvider.EP_NAME.getExtensions()) { - Pair> result = rootsProvider.resolve(path, project); + PathInfo result = rootsProvider.resolve(path, project); if (result != null) { - fileToRoot.put(result.first, result.second); - return result.first; + fileToRoot.put(result.getChild(), result); + return result.getChild(); } } return null; diff --git a/xml/impl/src/org/jetbrains/builtInWebServer/WebServerRootsProvider.java b/xml/impl/src/org/jetbrains/builtInWebServer/WebServerRootsProvider.java index 46861fc39b77..bb8972591ac5 100644 --- a/xml/impl/src/org/jetbrains/builtInWebServer/WebServerRootsProvider.java +++ b/xml/impl/src/org/jetbrains/builtInWebServer/WebServerRootsProvider.java @@ -2,7 +2,6 @@ package org.jetbrains.builtInWebServer; import com.intellij.openapi.extensions.ExtensionPointName; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Pair; import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -11,13 +10,10 @@ public abstract class WebServerRootsProvider { static final ExtensionPointName EP_NAME = ExtensionPointName.create("org.jetbrains.webServerRootsProvider"); @Nullable - /** - * @return pair of child and root or null if cannot resolve - */ - public abstract Pair> resolve(@NotNull String path, @NotNull Project project); + public abstract PathInfo resolve(@NotNull String path, @NotNull Project project); @Nullable - public abstract Pair getRoot(@NotNull VirtualFile file, @NotNull Project project); + public abstract PathInfo getRoot(@NotNull VirtualFile file, @NotNull Project project); public boolean isClearCacheOnFileContentChanged(@NotNull VirtualFile file) { return false; diff --git a/xml/impl/src/org/jetbrains/io/fastCgi/FastCgiRequest.java b/xml/impl/src/org/jetbrains/io/fastCgi/FastCgiRequest.java index 1e0eaad2a7da..e92d20eebffc 100644 --- a/xml/impl/src/org/jetbrains/io/fastCgi/FastCgiRequest.java +++ b/xml/impl/src/org/jetbrains/io/fastCgi/FastCgiRequest.java @@ -1,7 +1,6 @@ package org.jetbrains.io.fastCgi; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Pair; import com.intellij.openapi.vfs.VirtualFile; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufAllocator; @@ -12,6 +11,7 @@ import io.netty.handler.codec.http.HttpHeaders; import io.netty.util.CharsetUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.builtInWebServer.PathInfo; import org.jetbrains.builtInWebServer.WebServerPathToFileManager; import org.jetbrains.io.Responses; @@ -41,9 +41,9 @@ public class FastCgiRequest { } public void writeFileHeaders(@NotNull VirtualFile file, @NotNull Project project, @NotNull CharSequence canonicalRequestPath) { - Pair root = WebServerPathToFileManager.getInstance(project).getRoot(file); + PathInfo root = WebServerPathToFileManager.getInstance(project).getRoot(file); FastCgiService.LOG.assertTrue(root != null); - addHeader("DOCUMENT_ROOT", root.first.getPath()); + addHeader("DOCUMENT_ROOT", root.getRoot().getPath()); addHeader("SCRIPT_FILENAME", file.getPath()); addHeader("SCRIPT_NAME", canonicalRequestPath); }