// dealing with imported module other than root and directly importing root
  private static Collection<Module> getImportedModules(
      Map<ModuleId, Module> allModules,
      Set<Module> baseModules,
      TreeMultimap<String, Module> nameToModulesAll) {

    List<Module> relatedModules = Lists.newLinkedList();

    for (Module module : baseModules) {
      for (ModuleImport moduleImport : module.getImports()) {

        Date revisionDate =
            moduleImport.getRevision() == null
                ? nameToModulesAll.get(moduleImport.getModuleName()).first().getRevision()
                : moduleImport.getRevision();

        ModuleId key = new ModuleId(moduleImport.getModuleName(), revisionDate);
        Module importedModule = allModules.get(key);

        Preconditions.checkArgument(
            importedModule != null,
            "Invalid schema, cannot find imported module: %s from module: %s, %s, modules:%s",
            key,
            module.getQNameModule(),
            module.getName());
        relatedModules.add(importedModule);

        // calling imports recursive
        relatedModules.addAll(
            getImportedModules(
                allModules, Collections.singleton(importedModule), nameToModulesAll));
      }
    }

    return relatedModules;
  }
  // check for any dependency regarding given string
  private boolean checkModuleDependency(Module module, Collection<ModuleId> rootModules) {

    for (ModuleId rootModule : rootModules) {

      if (rootModule.equals(new ModuleId(module.getName(), module.getRevision()))) {
        return true;
      }

      // handling/checking imports regarding root modules
      for (ModuleImport moduleImport : module.getImports()) {

        if (moduleImport.getModuleName().equals(rootModule.getName())) {

          if (moduleImport.getRevision() != null
              && !moduleImport.getRevision().equals(rootModule.getRev())) {
            return false;
          }

          return true;
        }
      }

      // submodules handling
      for (Module moduleSub : module.getSubmodules()) {
        return checkModuleDependency(moduleSub, rootModules);
      }
    }

    return false;
  }
Exemple #3
0
 public static ModuleImport findImport(final Set<ModuleImport> imports, final String prefix) {
   ModuleImport result = null;
   for (ModuleImport moduleImport : imports) {
     if (moduleImport.getPrefix().equals(prefix)) {
       result = moduleImport;
       break;
     }
   }
   return result;
 }
Exemple #4
0
  /** Extract module:revision from module builders */
  private static void processDependencies(
      final Map<String, Map<Date, ModuleNodeImpl>> moduleGraph,
      final Iterable<ModuleOrModuleBuilder> mmbs) {
    Map<URI, ModuleOrModuleBuilder> allNS = new HashMap<>();

    // Create edges in graph
    for (ModuleOrModuleBuilder mmb : mmbs) {
      Map<String, Date> imported = Maps.newHashMap();

      String fromName;
      Date fromRevision;
      Collection<ModuleImport> imports;
      URI ns;

      if (mmb.isModule()) {
        Module module = mmb.getModule();
        fromName = module.getName();
        fromRevision = module.getRevision();
        imports = module.getImports();
        ns = module.getNamespace();
      } else {
        ModuleBuilder moduleBuilder = mmb.getModuleBuilder();
        fromName = moduleBuilder.getName();
        fromRevision = moduleBuilder.getRevision();
        imports = moduleBuilder.getImports().values();
        ns = moduleBuilder.getNamespace();
      }

      // check for existence of module with same namespace
      if (allNS.containsKey(ns)) {
        ModuleOrModuleBuilder mod = allNS.get(ns);
        String name = null;
        Date revision = null;
        if (mod.isModule()) {
          name = mod.getModule().getName();
          revision = mod.getModule().getRevision();
        } else if (mod.isModuleBuilder()) {
          name = mod.getModuleBuilder().getName();
          revision = mod.getModuleBuilder().getRevision();
        }
        if (!(fromName.equals(name))) {
          LOGGER.warn(
              "Error while sorting module [{}, {}]: module with same namespace ({}) already loaded: [{}, {}]",
              fromName,
              fromRevision,
              ns,
              name,
              revision);
        }
      } else {
        allNS.put(ns, mmb);
      }

      // no need to check if other Type of object, check is performed in
      // process modules

      if (fromRevision == null) {
        fromRevision = DEFAULT_REVISION;
      }

      for (ModuleImport imprt : imports) {
        String toName = imprt.getModuleName();
        Date toRevision = imprt.getRevision() == null ? DEFAULT_REVISION : imprt.getRevision();

        ModuleNodeImpl from = moduleGraph.get(fromName).get(fromRevision);

        ModuleNodeImpl to =
            getModuleByNameAndRevision(moduleGraph, fromName, fromRevision, toName, toRevision);

        /*
         * Check imports: If module is imported twice with different
         * revisions then throw exception
         */
        if (imported.get(toName) != null
            && !imported.get(toName).equals(toRevision)
            && !imported.get(toName).equals(DEFAULT_REVISION)
            && !toRevision.equals(DEFAULT_REVISION)) {
          ex(
              String.format(
                  "Module:%s imported twice with different revisions:%s, %s",
                  toName, formatRevDate(imported.get(toName)), formatRevDate(toRevision)));
        }

        imported.put(toName, toRevision);

        from.addEdge(to);
      }
    }
  }