protected Node fromRepository(
      CmrRepository repository, ArtifactContext context, boolean addLeaf) {
    log.debug(" Trying repository " + repository.getDisplayString());
    Node node = repository.findParent(context);
    if (node != null) {
      if (addLeaf) {
        Node parent = node;
        context.toNode(parent);
        NodeUtils.keepRepository(parent, repository);
        try {
          String[] names = repository.getArtifactNames(context);
          for (String name : names) {
            node = parent.getChild(name);
            if (node != null) {
              break;
            }
          }
        } finally {
          ArtifactContext.removeNode(parent);
        }
      }

      if (node != null) {
        NodeUtils.keepRepository(node, repository);
        log.debug("  -> Found at " + NodeUtils.getFullPath(node));
      }
    }
    return node;
  }
  @Override
  protected void putFolder(ArtifactContext context, File folder) throws RepositoryException {
    Node parent = getOrCreateParent(context);
    log.debug("Adding folder " + context + " to cache " + cache.getDisplayString());
    log.debug(" -> " + NodeUtils.getFullPath(parent));

    // fast-path for Herd
    if (!canHandleFolders()) {
      uploadZipped(parent, context, folder);
      return;
    }

    final String[] names = cache.getArtifactNames(context);
    if (names.length != 1) {
      throw new RepositoryException("ArtifactContext should have a single suffix");
    }
    final String label = names[0];
    if (parent instanceof OpenNode) {
      final OpenNode on = (OpenNode) parent;
      final OpenNode curent = on.createNode(label);
      try {
        for (File f : folder.listFiles()) // ignore folder, it should match new root
        putFiles(curent, f, context);
      } catch (RepositoryException e) {
        throw e;
      } catch (Exception e) {
        removeArtifact(context);
        throw new RepositoryException(e);
      }
    } else {
      throw new RepositoryException(
          "Cannot put folder [" + folder + "] to non-open node: " + context);
    }
    log.debug(" -> [done]");
  }
 public void putArtifact(ArtifactContext context, InputStream content) throws RepositoryException {
   try {
     final Node parent = getOrCreateParent(context);
     log.debug("Adding artifact " + context + " to cache " + cache.getDisplayString());
     log.debug(" -> " + NodeUtils.getFullPath(parent));
     final String[] names = cache.getArtifactNames(context);
     if (names.length != 1) {
       throw new RepositoryException("ArtifactContext should have a single suffix");
     }
     final String label = names[0];
     try {
       if (parent instanceof OpenNode) {
         final OpenNode on = (OpenNode) parent;
         if (on.addContent(label, content, context) == null)
           addContent(context, parent, label, content);
       } else {
         addContent(context, parent, label, content);
       }
     } catch (IOException e) {
       throw new RepositoryException(e);
     }
     log.debug(" -> [done]");
   } finally {
     IOUtils.safeClose(content);
   }
 }
 public List<String> getRepositoriesDisplayString() {
   final List<String> displayStrings = new ArrayList<>();
   for (CmrRepository root : getRepositories()) {
     displayStrings.add(root.getDisplayString());
   }
   return displayStrings;
 }
 @Override
 public ModuleVersionResult completeVersions(ModuleVersionQuery query) {
   ModuleVersionResult result = new ModuleVersionResult(query.getName());
   for (CmrRepository root : getRepositories()) {
     if (query.getNamespace() == null || query.getNamespace().equals(root.getNamespace())) {
       root.completeVersions(query, result);
     }
   }
   return result;
 }
 @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;
 }
 protected Node getOrCreateParent(ArtifactContext context) {
   Node parent = getFromCacheNode(context, false);
   if (parent == null) {
     parent = cache.createParent(context);
   }
   return parent;
 }
 public void removeArtifact(ArtifactContext context) throws RepositoryException {
   Node parent = getFromCacheNode(context, false);
   log.debug("Remove artifact " + context + " to repository " + cache.getDisplayString());
   if (parent != null) {
     final String[] labels = cache.getArtifactNames(context);
     for (String label : labels) {
       try {
         removeNode(parent, label);
       } catch (IOException e) {
         throw new RepositoryException(e);
       }
     }
     log.debug(" -> [done]");
   } else {
     log.debug(" -> No such artifact: " + context);
   }
 }
 @Override
 public synchronized List<CmrRepository> getRepositories() {
   if (allRoots == null) {
     allRoots = new ArrayList<>();
     boolean cacheAdded = false;
     for (CmrRepository root : roots) {
       if (!addCacheAsRoot && cache != null && !cacheAdded && root.getRoot().isRemote()) {
         allRoots.add(cache);
         cacheAdded = true;
       }
       allRoots.add(root);
     }
     if (!addCacheAsRoot && cache != null && !cacheAdded) {
       allRoots.add(cache);
     }
   }
   return allRoots;
 }
  /** Cache is only used for remote repos; see issue #47. */
  private Node fromRepositories(
      Iterable<CmrRepository> repositories, ArtifactContext context, boolean addLeaf) {
    log.debug("Looking for " + context);

    for (CmrRepository repository : repositories) {
      log.debug(" Looking in " + repository);
      if (!repository.supportsNamespace(context.getNamespace())) {
        log.debug(" -> does not support namespace " + context.getNamespace());
        continue;
      }
      Node child = fromRepository(repository, context, addLeaf);
      if (child != null) {
        log.debug(" -> Found");
        return child;
      }

      log.debug("  -> Not Found");
    }

    log.debug(" -> Artifact " + context + " not found in any repository");
    return null;
  }
 // Check if the source file and destination node point to the same file
 @Override
 public boolean isSameFile(ArtifactContext context, File srcFile) throws RepositoryException {
   boolean same = false;
   if (!cache.getRoot().isRemote()) {
     Node dstParent = getOrCreateParent(context);
     Node newChild = dstParent.getChild(srcFile.getName());
     if (newChild != null) {
       try {
         File existing = newChild.getContent(File.class);
         same = FileUtil.sameFile(srcFile, existing);
       } catch (IOException e) {
         throw new RepositoryException(e);
       }
     }
   }
   return same;
 }
 @Override
 public void refresh(boolean recurse) {
   for (CmrRepository root : getRepositories()) {
     root.refresh(recurse);
   }
 }
  @Override
  public ModuleSearchResult searchModules(ModuleQuery query) {
    if (!query.isPaging()) {
      // that's pretty simple
      ModuleSearchResult result = new ModuleSearchResult();
      for (CmrRepository root : getRepositories()) {
        if (query.getNamespace() == null || query.getNamespace().equals(root.getNamespace())) {
          root.searchModules(query, result);
        }
      }
      return result;
    } else {
      // we need to merge manually
      List<CmrRepository> repos = getRepositories();
      ModuleSearchResult[] results = new ModuleSearchResult[repos.size()];
      // keep an overall module name ordering
      SortedSet<String> names = new TreeSet<>();
      int i = 0;
      long[] pagingInfo = query.getPagingInfo();
      if (pagingInfo != null) {
        // check its length
        if (pagingInfo.length != repos.size())
          throw new IllegalArgumentException(
              "Paging info is not the same size as roots, it must have come from a different RepositoryManager");
      }
      Long start = query.getStart();
      for (CmrRepository root : repos) {
        if (query.getNamespace() == null || query.getNamespace().equals(root.getNamespace())) {
          ModuleSearchResult result = new ModuleSearchResult();
          // adapt the start index if required
          if (pagingInfo != null) query.setStart(pagingInfo[i]);
          root.searchModules(query, result);
          results[i++] = result;
          names.addAll(result.getModuleNames());
        }
      }
      // restore the query start
      query.setStart(start);
      // now merge results
      ModuleSearchResult result = new ModuleSearchResult();
      long[] resultPagingInfo = new long[repos.size()];
      // initialise it if we need to
      if (pagingInfo != null)
        System.arraycopy(pagingInfo, 0, resultPagingInfo, 0, resultPagingInfo.length);

      result.setNextPagingInfo(resultPagingInfo);
      i = 0;
      for (String module : names) {
        // stop if we exceeded the count
        if (query.getCount() != null && i++ == query.getCount()) break;
        // collect every module result for that name from the results
        int repo = 0;
        for (ModuleSearchResult resultPart : results) {
          ModuleDetails details = resultPart.getResult(module);
          // did we find anything in that repo?
          if (details == null) {
            repo++;
            continue;
          } else {
            // count one result for this repo
            resultPagingInfo[repo++]++;
          }
          // merge it
          result.addResult(module, details);
        }
      }
      // see if there are any records left in next pages
      int repo = 0;
      for (ModuleSearchResult resultPart : results) {
        // if we had more results in the first place then we must have another page
        if (resultPart.getHasMoreResults()) {
          result.setHasMoreResults(true);
          break;
        }
        // see how many results we added from this repo
        long resultsAddedForThisRepo;
        if (pagingInfo != null) resultsAddedForThisRepo = resultPagingInfo[repo] - pagingInfo[repo];
        else resultsAddedForThisRepo = resultPagingInfo[repo];
        // did we have more results than we put in?
        if (resultPart.getCount() > resultsAddedForThisRepo) {
          result.setHasMoreResults(true);
          break;
        }
        repo++;
      }
      // record where we started (i is one greater than the number of modules added)
      if (query.getStart() != null) result.setStart(query.getStart());
      else result.setStart(0);
      // all done
      return result;
    }
  }
 protected ArtifactResult toArtifactResult(Node node) {
   final CmrRepository adapter = NodeUtils.getRepository(node);
   return adapter.getArtifactResult(this, node);
 }
 private boolean canHandleFolders(CmrRepository repo) {
   ContentStore cs = repo.getRoot().getService(ContentStore.class);
   return cs != null && cs.canHandleFolders();
 }
  protected OpenNode getCache() {
    if (cache == null) return null;

    return cache.getRoot();
  }
 private void setupOverrides(CmrRepository repo) {
   repo.getRoot().addService(Overrides.class, overrides);
 }
Ejemplo n.º 18
0
  private ArtifactResult fetchDependencies(
      RepositoryManager manager,
      CmrRepository repository,
      String groupId,
      String artifactId,
      String version,
      boolean fetchSingleArtifact,
      String repositoryDisplayString) {

    String classifier = null;
    Overrides overrides = repository.getRoot().getService(Overrides.class);
    ArtifactOverrides ao = null;
    log.debug("Overrides: " + overrides);
    ArtifactContext context = getArtifactContext(groupId, artifactId, version, null, null);
    if (overrides != null) {
      ao = overrides.getArtifactOverrides(context);
      log.debug(" [" + context + "] => " + ao);
    }
    // entire replacement
    ArtifactContext replacementContext = null;
    if (ao != null && ao.getReplace() != null) {
      replacementContext = ao.getReplace().getArtifactContext();
    } else if (overrides != null) {
      replacementContext = overrides.replace(context);
    }
    if (replacementContext != null) {
      log.debug(
          String.format("[Maven-Overrides] Replacing %s with %s.", context, replacementContext));
      // replace fetched dependency
      String[] nameToGroupArtifactIds = nameToGroupArtifactIds(replacementContext.getName());
      if (nameToGroupArtifactIds != null) {
        groupId = nameToGroupArtifactIds[0];
        artifactId = nameToGroupArtifactIds[1];
        version = replacementContext.getVersion();
        // new AO
        context = getArtifactContext(groupId, artifactId, version, null, null);
        ao = overrides.getArtifactOverrides(context);
      }
    }
    // version replacement
    if (overrides != null && overrides.isVersionOverridden(context)) {
      version = overrides.getVersionOverride(context);
      context.setVersion(version);
    }
    // classifier replacement
    if (ao != null && ao.hasClassifier()) {
      classifier = ao.getClassifier();
      log.debug("Using classifier " + classifier);
    }

    final String name = toCanonicalForm(groupId, artifactId);
    final String coordinates = toCanonicalForm(name, version);
    try {
      DependencyDescriptor info =
          impl.getDependencies(groupId, artifactId, version, classifier, null, fetchSingleArtifact);
      if (info == null) {
        log.debug("No artifact found: " + coordinates);
        return null;
      }

      final SingleArtifactResult result;
      if (fetchSingleArtifact) {
        result =
            new SingleArtifactResult(
                repository, name, version, info.getFile(), repositoryDisplayString);
      } else {
        final List<ArtifactResult> dependencies = new ArrayList<>();

        for (DependencyDescriptor dep : info.getDependencies()) {
          String dGroupId = dep.getGroupId();
          String dArtifactId = dep.getArtifactId();
          String dVersion = dep.getVersion();
          boolean export = false;
          boolean optional = dep.isOptional();
          boolean isCeylon = false;
          ArtifactContext dContext = null;
          if (overrides != null)
            dContext = getArtifactContext(dGroupId, dArtifactId, dVersion, null, null);

          if (overrides != null) {
            if (overrides.isRemoved(dContext) || (ao != null && ao.isRemoved(dContext))) {
              log.debug(String.format("[Maven-Overrides] Removing %s from %s.", dep, context));
              continue; // skip dependency
            }
            if (ao != null && ao.isAddedOrUpdated(dContext)) {
              log.debug(String.format("[Maven-Overrides] Replacing %s from %s.", dep, context));
              continue; // skip dependency
            }
            ArtifactContext replace = overrides.replace(dContext);
            if (replace != null) {
              dContext = replace;
              String[] groupArtifactIds = nameToGroupArtifactIds(replace.getName());
              if (groupArtifactIds == null) {
                isCeylon = true;
              } else {
                dGroupId = groupArtifactIds[0];
                dArtifactId = groupArtifactIds[1];
              }
              dVersion = replace.getVersion();
            }
            if (ao != null) {
              if (ao.isShareOverridden(dContext)) export = ao.isShared(dContext);
              if (ao.isOptionalOverridden(dContext)) optional = ao.isOptional(dContext);
            }
          }

          // do we have a version update?
          if (overrides != null && overrides.isVersionOverridden(dContext)) {
            dVersion = overrides.getVersionOverride(dContext);
          }

          ArtifactResult dr;
          if (isCeylon)
            dr =
                createArtifactResult(
                    manager,
                    dContext.getName(),
                    dVersion,
                    export,
                    optional,
                    repositoryDisplayString);
          else
            dr =
                createArtifactResult(
                    manager,
                    repository,
                    dGroupId,
                    dArtifactId,
                    dVersion,
                    export,
                    optional,
                    repositoryDisplayString);
          dependencies.add(dr);
        }

        if (ao != null) {
          for (DependencyOverride addon : ao.getAdd()) {
            ArtifactContext dContext = addon.getArtifactContext();
            String dVersion = overrides.getVersionOverride(dContext);
            dependencies.add(
                createArtifactResult(
                    manager,
                    repository,
                    dContext,
                    dVersion,
                    addon.isShared(),
                    addon.isOptional(),
                    repositoryDisplayString));
            log.debug(
                String.format(
                    "[Maven-Overrides] Added %s to %s.", addon.getArtifactContext(), context));
          }
        }

        result =
            new AetherArtifactResult(
                repository, name, version, info.getFile(), dependencies, repositoryDisplayString);
      }

      if (ao != null && ao.getFilter() != null) {
        result.setFilter(PathFilterParser.parse(ao.getFilter()));
      }

      return result;
    } catch (IOException e) {
      throw new IllegalStateException(e);
    } catch (AetherException e) {
      log.debug("Could not resolve artifact [" + coordinates + "] : " + e);
      return null;
    }
  }