From 3662bcf4797175a3e239b07ed7ff4922f2c69397 Mon Sep 17 00:00:00 2001 From: Tako Schotanus Date: Sun, 10 Jul 2016 14:30:10 +0200 Subject: [PATCH] Now validate import namespaces against the currently configured repositories (see also #6367) --- .../ceylon/cmr/api/RepositoryManager.java | 5 +++ .../impl/AbstractNodeRepositoryManager.java | 12 ++++++ .../cmr/impl/OutputRepositoryManager.java | 5 +++ .../typechecker/analyzer/ModuleValidator.java | 39 ++++++++++++------- 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/cmr/api/src/main/java/com/redhat/ceylon/cmr/api/RepositoryManager.java b/cmr/api/src/main/java/com/redhat/ceylon/cmr/api/RepositoryManager.java index 5db32886f3e..705c5d7d4e1 100644 --- a/cmr/api/src/main/java/com/redhat/ceylon/cmr/api/RepositoryManager.java +++ b/cmr/api/src/main/java/com/redhat/ceylon/cmr/api/RepositoryManager.java @@ -239,4 +239,9 @@ public interface RepositoryManager { void refresh(boolean recurse); Overrides getOverrides(); + + /** + * Determines if the given namespace is recognized by any of the configured repositories + */ + boolean isValidNamespace(String namespace); } diff --git a/cmr/impl/src/main/java/com/redhat/ceylon/cmr/impl/AbstractNodeRepositoryManager.java b/cmr/impl/src/main/java/com/redhat/ceylon/cmr/impl/AbstractNodeRepositoryManager.java index daa7791da48..a3f5dad00e2 100644 --- a/cmr/impl/src/main/java/com/redhat/ceylon/cmr/impl/AbstractNodeRepositoryManager.java +++ b/cmr/impl/src/main/java/com/redhat/ceylon/cmr/impl/AbstractNodeRepositoryManager.java @@ -599,4 +599,16 @@ public void refresh(boolean recurse) { root.refresh(recurse); } } + + @Override + public boolean isValidNamespace(String namespace) { + for (CmrRepository root : getRepositories()) { + if (namespace == null && DefaultRepository.NAMESPACE.equals(root.getNamespace())) { + return true; + } else if (namespace != null && namespace.equals(root.getNamespace())) { + return true; + } + } + return false; + } } diff --git a/cmr/impl/src/main/java/com/redhat/ceylon/cmr/impl/OutputRepositoryManager.java b/cmr/impl/src/main/java/com/redhat/ceylon/cmr/impl/OutputRepositoryManager.java index 73587a0ce86..c76d019e06e 100644 --- a/cmr/impl/src/main/java/com/redhat/ceylon/cmr/impl/OutputRepositoryManager.java +++ b/cmr/impl/src/main/java/com/redhat/ceylon/cmr/impl/OutputRepositoryManager.java @@ -133,4 +133,9 @@ public void refresh(boolean recurse) { public ArtifactContext getArtifactOverride(ArtifactContext context) throws RepositoryException { return context; } + + @Override + public boolean isValidNamespace(String namespace) { + return namespace == null || DefaultRepository.NAMESPACE.equals(namespace); + } } diff --git a/typechecker/src/com/redhat/ceylon/compiler/typechecker/analyzer/ModuleValidator.java b/typechecker/src/com/redhat/ceylon/compiler/typechecker/analyzer/ModuleValidator.java index 85343d4ffec..fc7af4759ef 100644 --- a/typechecker/src/com/redhat/ceylon/compiler/typechecker/analyzer/ModuleValidator.java +++ b/typechecker/src/com/redhat/ceylon/compiler/typechecker/analyzer/ModuleValidator.java @@ -254,21 +254,30 @@ private void resolveModuleIfRequired(Module module, boolean forCompiledModule, M } else { //try and load the module from the repository RepositoryManager repositoryManager = context.getRepositoryManager(); - Exception exceptionOnGetArtifact = null; - Iterable searchedArtifactExtensions = moduleManager.getSearchedArtifactExtensions(); - ArtifactContext artifactContext = new ArtifactContext(moduleImport.getNamespace(), module.getNameAsString(), module.getVersion(), getArtifactSuffixes(searchedArtifactExtensions)); - listener.retrievingModuleArtifact(module, artifactContext); - try { - artifact = repositoryManager.getArtifactResult(artifactContext); - } catch (Exception e) { - exceptionOnGetArtifact = catchIfPossible(e); - } - if (artifact == null) { - //not there => error - ModuleHelper.buildErrorOnMissingArtifact(artifactContext, module, moduleImport, dependencyTree, exceptionOnGetArtifact, moduleManagerUtil); - listener.retrievingModuleArtifactFailed(module, artifactContext); - }else{ - listener.retrievingModuleArtifactSuccess(module, artifact); + if (repositoryManager.isValidNamespace(moduleImport.getNamespace())) { + Exception exceptionOnGetArtifact = null; + Iterable searchedArtifactExtensions = moduleManager.getSearchedArtifactExtensions(); + ArtifactContext artifactContext = new ArtifactContext(moduleImport.getNamespace(), module.getNameAsString(), module.getVersion(), getArtifactSuffixes(searchedArtifactExtensions)); + listener.retrievingModuleArtifact(module, artifactContext); + try { + artifact = repositoryManager.getArtifactResult(artifactContext); + } catch (Exception e) { + exceptionOnGetArtifact = catchIfPossible(e); + } + if (artifact == null) { + //not there => error + ModuleHelper.buildErrorOnMissingArtifact(artifactContext, module, moduleImport, dependencyTree, exceptionOnGetArtifact, moduleManagerUtil); + listener.retrievingModuleArtifactFailed(module, artifactContext); + }else{ + listener.retrievingModuleArtifactSuccess(module, artifact); + } + } else { + String msg = "unknown import namespace: '" + moduleImport.getNamespace() + + "', make sure the proper repository has been enabled"; + if (!"maven".equals(moduleImport.getNamespace())) { + msg += " (if this is a Maven import make sure to add a 'maven:' prefix)"; + } + moduleManagerUtil.attachErrorToDependencyDeclaration(moduleImport, dependencyTree, msg); } alreadySearchedArtifacts.put(module, artifact); firstTime = true;